From bb0c5293c22fc7cf5725cac91c239d83967bed18 Mon Sep 17 00:00:00 2001 From: Aris Tritas Date: Thu, 28 Mar 2024 16:16:49 +0100 Subject: [PATCH] Update legacy py.path package and fixtures to pathlib --- tests/system/conftest.py | 23 +++++++++---------- tests/system/test_config_reload.py | 6 ++--- tests/unit/common/test_storage.py | 17 +++++++------- tests/unit/common/test_utils.py | 17 +++++++------- tests/unit/conftest.py | 11 +++++---- tests/unit/coordinator/conftest.py | 15 ++++++------- tests/unit/node/conftest.py | 31 +++++++++++++------------- tests/unit/node/test_node_cassandra.py | 10 ++++----- 8 files changed, 62 insertions(+), 68 deletions(-) diff --git a/tests/system/conftest.py b/tests/system/conftest.py index 070e0399..9edd0111 100644 --- a/tests/system/conftest.py +++ b/tests/system/conftest.py @@ -17,7 +17,6 @@ import json import logging import os.path -import py import pytest import subprocess @@ -98,11 +97,11 @@ def create_astacus_config_dict( def create_astacus_config( *, - tmpdir, + tmp_path: Path, node: TestNode, plugin_config: Mapping[str, Any] = MappingProxyType(DEFAULT_PLUGIN_CONFIG), ) -> Path: - a = Path(tmpdir / "node" / node.name) + a = tmp_path / "node" / node.name node.path = a root_path = a / "root" root_path.mkdir(parents=True, exist_ok=True) @@ -113,7 +112,7 @@ def create_astacus_config( db_path.mkdir(exist_ok=True) node.db_path = db_path a_conf = create_astacus_config_dict( - tmpdir=Path(tmpdir), root_path=root_path, link_path=link_path, node=node, plugin_config=plugin_config + tmpdir=tmp_path, root_path=root_path, link_path=link_path, node=node, plugin_config=plugin_config ) a_conf_path = a / "astacus.conf" a_conf_path.write_text(json.dumps(a_conf)) @@ -140,9 +139,9 @@ def fixture_rootdir(pytestconfig: Config) -> str: @asynccontextmanager -async def _astacus(*, tmpdir: py.path.local, index: int) -> AsyncIterator[TestNode]: +async def _astacus(*, tmp_path: Path, index: int) -> AsyncIterator[TestNode]: node = ASTACUS_NODES[index] - a_conf_path = create_astacus_config(tmpdir=tmpdir, node=node) + a_conf_path = create_astacus_config(tmp_path=tmp_path, node=node) astacus_source_root = os.path.join(os.path.dirname(__file__), "..", "..") cmd = format_astacus_command("server", "-c", str(a_conf_path)) async with background_process(*cmd, env={"PYTHONPATH": astacus_source_root}) as process: @@ -152,20 +151,20 @@ async def _astacus(*, tmpdir: py.path.local, index: int) -> AsyncIterator[TestNo @pytest.fixture(name="astacus1") -async def fixture_astacus1(tmpdir: py.path.local) -> AsyncIterator[TestNode]: - async with _astacus(tmpdir=tmpdir, index=0) as a: +async def fixture_astacus1(tmp_path: Path) -> AsyncIterator[TestNode]: + async with _astacus(tmp_path=tmp_path, index=0) as a: yield a @pytest.fixture(name="astacus2") -async def fixture_astacus2(tmpdir: py.path.local) -> AsyncIterator[TestNode]: - async with _astacus(tmpdir=tmpdir, index=1) as a: +async def fixture_astacus2(tmp_path: Path) -> AsyncIterator[TestNode]: + async with _astacus(tmp_path=tmp_path, index=1) as a: yield a @pytest.fixture(name="astacus3") -async def fixture_astacus3(tmpdir: py.path.local) -> AsyncIterator[TestNode]: - async with _astacus(tmpdir=tmpdir, index=2) as a: +async def fixture_astacus3(tmp_path: Path) -> AsyncIterator[TestNode]: + async with _astacus(tmp_path=tmp_path, index=2) as a: yield a diff --git a/tests/system/test_config_reload.py b/tests/system/test_config_reload.py index c3ce883a..4995b315 100644 --- a/tests/system/test_config_reload.py +++ b/tests/system/test_config_reload.py @@ -4,16 +4,16 @@ Hot-reloading of astacus configuration """ - +from pathlib import Path from tests.system.conftest import astacus_ls, astacus_run, create_astacus_config, TestNode import pytest @pytest.mark.order("last") -def test_reload_config(tmpdir, rootdir: str, astacus1: TestNode, astacus2: TestNode, astacus3: TestNode) -> None: +def test_reload_config(tmp_path: Path, rootdir: str, astacus1: TestNode, astacus2: TestNode, astacus3: TestNode) -> None: # Update the root_globs config of the first node - create_astacus_config(tmpdir=tmpdir, node=astacus1, plugin_config={"root_globs": ["*.foo"]}) + create_astacus_config(tmp_path=tmp_path, node=astacus1, plugin_config={"root_globs": ["*.foo"]}) # Check that astacus can detect that reloading config is needed assert astacus_run(rootdir, astacus1, "check-reload", "--status", check=False).returncode == 1 check_without_status = astacus_run(rootdir, astacus1, "check-reload", check=True, capture_output=True) diff --git a/tests/unit/common/test_storage.py b/tests/unit/common/test_storage.py index 4443574b..7840b467 100644 --- a/tests/unit/common/test_storage.py +++ b/tests/unit/common/test_storage.py @@ -20,7 +20,6 @@ from unittest.mock import Mock, patch import json -import py import pytest TEST_HEXDIGEST = "deadbeef" @@ -30,16 +29,16 @@ TEST_JSON_DATA: Json = {"foo": 7, "array": [1, 2, 3], "true": True} -def create_storage(*, tmpdir: py.path.local, engine: str, **kw): +def create_storage(*, tmpdir: Path, engine: str, **kw): if engine == "rohmu": config = create_rohmu_config(tmpdir, **kw) return RohmuStorage(config=config) if engine == "file": - path = Path(tmpdir / "test-storage-file") + path = tmpdir / "test-storage-file" return FileStorage(path) if engine == "cache": # FileStorage cache, and then rohmu filestorage underneath - cache_storage = FileStorage(Path(tmpdir / "test-storage-file")) + cache_storage = FileStorage(tmpdir / "test-storage-file") config = create_rohmu_config(tmpdir, **kw) backend_storage = RohmuStorage(config=config) return CachingJsonStorage(backend_storage=backend_storage, cache_storage=cache_storage) @@ -85,22 +84,22 @@ def _test_jsonstorage(storage: JsonStorage) -> None: ("rohmu", {"compression": False, "encryption": False}, pytest.raises(exceptions.CompressionOrEncryptionRequired)), ], ) -def test_storage(tmpdir: py.path.local, engine: str, kw: dict[str, bool], ex: AbstractContextManager | None) -> None: +def test_storage(tmp_path: Path, engine: str, kw: dict[str, bool], ex: AbstractContextManager | None) -> None: if ex is None: ex = does_not_raise() with ex: - storage = create_storage(tmpdir=tmpdir, engine=engine, **kw) + storage = create_storage(tmpdir=tmp_path, engine=engine, **kw) if isinstance(storage, FileStorage): _test_hexdigeststorage(storage) if isinstance(storage, JsonStorage): _test_jsonstorage(storage) -def test_caching_storage(tmpdir: py.path.local, mocker: MockerFixture) -> None: - storage = create_storage(tmpdir=tmpdir, engine="cache") +def test_caching_storage(tmp_path: Path, mocker: MockerFixture) -> None: + storage = create_storage(tmpdir=tmp_path, engine="cache") storage.upload_json(TEST_JSON, TEST_JSON_DATA) - storage = create_storage(tmpdir=tmpdir, engine="cache") + storage = create_storage(tmpdir=tmp_path, engine="cache") assert storage.list_jsons() == [TEST_JSON] # We shouldn't wind up in download method of rohmu at all. diff --git a/tests/unit/common/test_utils.py b/tests/unit/common/test_utils.py index 4af1fa2a..96144ccc 100644 --- a/tests/unit/common/test_utils.py +++ b/tests/unit/common/test_utils.py @@ -14,7 +14,6 @@ import asyncio import logging -import py import pytest import tempfile import time @@ -149,26 +148,26 @@ def test_sizelimitedfile() -> None: assert lf.read() == b"bar" -def test_open_path_with_atomic_rename(tmpdir: py.path.local) -> None: +def test_open_path_with_atomic_rename(tmp_path: Path) -> None: # default is bytes - f1_path = f"{tmpdir}/f1" + f1_path = tmp_path / "f1" with utils.open_path_with_atomic_rename(f1_path) as f1: f1.write(b"test1") - assert Path(f1_path).read_text() == "test1" + assert f1_path.read_text() == "test1" # text mode requires passing mode flag but should work - f2_path = f"{tmpdir}/f2" + f2_path = tmp_path / "f2" with utils.open_path_with_atomic_rename(f2_path, mode="w") as f2: f2.write("test2") - assert Path(f2_path).read_text() == "test2" + assert f2_path.read_text() == "test2" # rewriting should be fine too with utils.open_path_with_atomic_rename(f2_path, mode="w") as f2: f2.write("test2-new") - assert Path(f2_path).read_text() == "test2-new" + assert f2_path.read_text() == "test2-new" # erroneous cases should not produce file at all - f3_path = f"{tmpdir}/f3" + f3_path = tmp_path / "f3" class TestException(RuntimeError): pass @@ -177,7 +176,7 @@ class TestException(RuntimeError): with utils.open_path_with_atomic_rename(f3_path): raise TestException() # This version of MyPy works poorly with pytest.raises - assert not Path(f3_path).exists() # type: ignore[unreachable] + assert not f3_path.exists() # type: ignore[unreachable] def test_parse_umask() -> None: diff --git a/tests/unit/conftest.py b/tests/unit/conftest.py index b38f9898..4aa93feb 100644 --- a/tests/unit/conftest.py +++ b/tests/unit/conftest.py @@ -9,7 +9,6 @@ from pytest_mock import MockerFixture import logging -import py import pytest import tempfile @@ -17,8 +16,8 @@ class CassandraTestConfig: - def __init__(self, *, mocker: MockerFixture, tmpdir: py.path.local): - self.root = Path(tmpdir) / "root" + def __init__(self, *, mocker: MockerFixture, tmp_path: Path): + self.root = tmp_path / "root" # Create fake snapshot files that we want to see being moved/removed/.. keyspace_path = self.root / "data" / "dummyks" @@ -39,7 +38,7 @@ def __init__(self, *, mocker: MockerFixture, tmpdir: py.path.local): self.other_snapshot_path = keyspace_path / "dummytable-123" / "snapshots" / f"not{SNAPSHOT_NAME}" self.other_snapshot_path.mkdir(parents=True) - self.cassandra_conf = Path(tmpdir / "cassandra.yaml") + self.cassandra_conf = tmp_path / "cassandra.yaml" self.cassandra_conf.write_text( """ listen_address: 127.0.0.1 @@ -63,5 +62,5 @@ def __init__(self, *, mocker: MockerFixture, tmpdir: py.path.local): @pytest.fixture(name="cassandra_test_config") -def fixture_cassandra_test_config(mocker: MockerFixture, tmpdir: py.path.local) -> CassandraTestConfig: - return CassandraTestConfig(mocker=mocker, tmpdir=tmpdir) +def fixture_cassandra_test_config(mocker: MockerFixture, tmp_path: Path) -> CassandraTestConfig: + return CassandraTestConfig(mocker=mocker, tmp_path=tmp_path) diff --git a/tests/unit/coordinator/conftest.py b/tests/unit/coordinator/conftest.py index 129f63a6..ff901418 100644 --- a/tests/unit/coordinator/conftest.py +++ b/tests/unit/coordinator/conftest.py @@ -15,20 +15,19 @@ from tests.utils import create_rohmu_config import asyncio -import py import pytest _original_asyncio_sleep = asyncio.sleep @pytest.fixture(name="storage") -def fixture_storage(tmpdir: py.path.local) -> RohmuStorage: - return RohmuStorage(config=create_rohmu_config(tmpdir)) +def fixture_storage(tmp_path: Path) -> RohmuStorage: + return RohmuStorage(config=create_rohmu_config(tmp_path)) @pytest.fixture(name="mstorage") -def fixture_mstorage(tmpdir: py.path.local) -> MultiRohmuStorage: - return MultiRohmuStorage(config=create_rohmu_config(tmpdir)) +def fixture_mstorage(tmp_path: Path) -> MultiRohmuStorage: + return MultiRohmuStorage(config=create_rohmu_config(tmp_path)) @pytest.fixture(name="populated_mstorage") @@ -70,14 +69,14 @@ def fixture_sleepless(mocker: MockerFixture) -> None: @pytest.fixture(name="app") -def fixture_app(mocker: MockerFixture, sleepless: None, storage: RohmuStorage, tmpdir: py.path.local) -> FastAPI: +def fixture_app(mocker: MockerFixture, sleepless: None, storage: RohmuStorage, tmp_path: Path) -> FastAPI: app = FastAPI() app.include_router(router, tags=["coordinator"]) app.state.coordinator_config = CoordinatorConfig( - object_storage=create_rohmu_config(tmpdir), + object_storage=create_rohmu_config(tmp_path), plugin=Plugin.files, plugin_config={"root_globs": ["*"]}, - object_storage_cache=Path(f"{tmpdir}/cache/is/somewhere"), + object_storage_cache=tmp_path / "cache/is/somewhere", ) app.state.coordinator_config.nodes = COORDINATOR_NODES[:] mocker.patch.object(LockedCoordinatorOp, "get_locker", return_value="x") diff --git a/tests/unit/node/conftest.py b/tests/unit/node/conftest.py index 33da3b93..adbb9180 100644 --- a/tests/unit/node/conftest.py +++ b/tests/unit/node/conftest.py @@ -16,19 +16,18 @@ from fastapi.testclient import TestClient from pathlib import Path -import py import pytest @pytest.fixture(name="app") -def fixture_app(tmpdir: py.path.local) -> FastAPI: +def fixture_app(tmp_path: Path) -> FastAPI: app = FastAPI() app.include_router(node_router, prefix="/node", tags=["node"]) - root = Path(tmpdir) / "root" - db_path = Path(tmpdir) / "db_path" - backup_root = Path(tmpdir) / "backup-root" + root = tmp_path / "root" + db_path = tmp_path / "db_path" + backup_root = tmp_path / "backup-root" backup_root.mkdir() - tmp_path = Path(tmpdir) / "backup-tmp" + tmp_path = tmp_path / "backup-tmp" root.mkdir() (root / "foo").write_text("foobar") (root / "foo2").write_text("foobar") @@ -66,34 +65,34 @@ def fixture_uploader(storage): @pytest.fixture(name="storage") -def fixture_storage(tmpdir: py.path.local) -> FileStorage: - storage_path = Path(tmpdir) / "storage" +def fixture_storage(tmp_path: Path) -> FileStorage: + storage_path = tmp_path / "storage" storage_path.mkdir() return FileStorage(storage_path) @pytest.fixture(name="root") -def fixture_root(tmpdir: py.path.local) -> Path: - return Path(tmpdir) +def fixture_root(tmp_path: Path) -> Path: + return tmp_path @pytest.fixture(name="src") -def fixture_src(tmpdir: py.path.local) -> Path: - src = Path(tmpdir) / "src" +def fixture_src(tmp_path: Path) -> Path: + src = tmp_path / "src" src.mkdir() return src @pytest.fixture(name="dst") -def fixture_dst(tmpdir: py.path.local) -> Path: - dst = Path(tmpdir) / "dst" +def fixture_dst(tmp_path: Path) -> Path: + dst = tmp_path / "dst" dst.mkdir() return dst @pytest.fixture(name="db") -def fixture_db(tmpdir: py.path.local) -> Path: - db = Path(tmpdir) / "db" +def fixture_db(tmp_path: Path) -> Path: + db = tmp_path / "db" return db diff --git a/tests/unit/node/test_node_cassandra.py b/tests/unit/node/test_node_cassandra.py index 025ccb98..f5ec4ddb 100644 --- a/tests/unit/node/test_node_cassandra.py +++ b/tests/unit/node/test_node_cassandra.py @@ -11,13 +11,13 @@ from collections.abc import Callable, Sequence from fastapi import FastAPI from fastapi.testclient import TestClient +from pathlib import Path from pytest_mock import MockerFixture from requests import Response from tests.unit.conftest import CassandraTestConfig from types import ModuleType import msgspec -import py import pytest import subprocess @@ -30,8 +30,8 @@ def fixture_astacus_node_cassandra() -> ModuleType: class CassandraTestEnv(CassandraTestConfig): cassandra_node_config: CassandraNodeConfig - def __init__(self, *, app: FastAPI, client: TestClient, mocker: MockerFixture, tmpdir: py.path.local) -> None: - super().__init__(mocker=mocker, tmpdir=tmpdir) + def __init__(self, *, app: FastAPI, client: TestClient, mocker: MockerFixture, tmp_path: Path) -> None: + super().__init__(mocker=mocker, tmp_path=tmp_path) self.app = app self.client = client @@ -60,8 +60,8 @@ def setup_cassandra_node_config(self) -> None: @pytest.fixture(name="ctenv") -def fixture_ctenv(app: FastAPI, client: TestClient, mocker: MockerFixture, tmpdir: py.path.local) -> CassandraTestEnv: - return CassandraTestEnv(app=app, client=client, mocker=mocker, tmpdir=tmpdir) +def fixture_ctenv(app: FastAPI, client: TestClient, mocker: MockerFixture, tmp_path: Path) -> CassandraTestEnv: + return CassandraTestEnv(app=app, client=client, mocker=mocker, tmp_path=tmp_path) @pytest.mark.parametrize(