From bf8a93d08078b2c90da9a0dff1df5cfaf7ad0938 Mon Sep 17 00:00:00 2001 From: Evgeniy Alekseev Date: Thu, 25 Mar 2021 02:18:44 +0300 Subject: [PATCH] update tests for pathlib --- tests/ahriman/core/alpm/test_repo.py | 34 +++++-------------- tests/ahriman/core/conftest.py | 8 ++--- tests/ahriman/models/conftest.py | 4 ++- tests/ahriman/models/test_package.py | 1 - tests/ahriman/models/test_repository_paths.py | 19 +++++------ 5 files changed, 24 insertions(+), 42 deletions(-) diff --git a/tests/ahriman/core/alpm/test_repo.py b/tests/ahriman/core/alpm/test_repo.py index 53cce97b..480c082d 100644 --- a/tests/ahriman/core/alpm/test_repo.py +++ b/tests/ahriman/core/alpm/test_repo.py @@ -1,8 +1,7 @@ -import os import pytest +from pathlib import Path from pytest_mock import MockerFixture -from unittest import mock from ahriman.core.alpm.repo import Repo @@ -11,7 +10,7 @@ def test_repo_path(repo: Repo) -> None: """ name must be something like archive name """ - assert repo.repo_path.endswith("db.tar.gz") + assert repo.repo_path.name.endswith("db.tar.gz") def test_repo_add(repo: Repo, mocker: MockerFixture) -> None: @@ -20,7 +19,7 @@ def test_repo_add(repo: Repo, mocker: MockerFixture) -> None: """ check_output_mock = mocker.patch("ahriman.core.alpm.repo.Repo._check_output") - repo.add("path") + repo.add(Path("path")) Repo._check_output.assert_called_once() assert check_output_mock.call_args[0][0] == "repo-add" @@ -29,10 +28,10 @@ def test_repo_remove(repo: Repo, mocker: MockerFixture) -> None: """ must call repo-remove on package addition """ - mocker.patch("os.listdir", return_value=[]) + mocker.patch("pathlib.Path.glob", return_value=[]) check_output_mock = mocker.patch("ahriman.core.alpm.repo.Repo._check_output") - repo.remove("package", "package.pkg.tar.xz") + repo.remove("package", Path("package.pkg.tar.xz")) Repo._check_output.assert_called_once() assert check_output_mock.call_args[0][0] == "repo-remove" @@ -41,25 +40,8 @@ def test_repo_remove_fail_no_file(repo: Repo, mocker: MockerFixture) -> None: """ must fail on missing file """ - mocker.patch("os.listdir", return_value=["package.pkg.tar.xz"]) - mocker.patch("os.remove", side_effect=FileNotFoundError()) + mocker.patch("pathlib.Path.glob", return_value=[Path("package.pkg.tar.xz")]) + mocker.patch("pathlib.Path.unlink", side_effect=FileNotFoundError()) with pytest.raises(FileNotFoundError): - repo.remove("package", "package.pkg.tar.xz") - - -def test_repo_remove_remove_requested(repo: Repo, mocker: MockerFixture) -> None: - """ - must remove only requested files - """ - packages = ["package.pkg.tar.xz", "package.pkg.tar.xz.sig"] - all_packages = packages + ["valid-package.pkg.tar.xz.sig", "package-valid.pkg.tar.xz.sig"] - - mocker.patch("os.listdir", return_value=all_packages) - remove_mock = mocker.patch("os.remove") - mocker.patch("ahriman.core.alpm.repo.Repo._check_output") - - repo.remove("package", "package.pkg.tar.xz") - removed = [call.call_list()[0][0][0] for call in remove_mock.call_args_list] - to_be_removed = [os.path.join(repo.paths.repository, package) for package in packages] - assert set(removed) == set(to_be_removed) + repo.remove("package", Path("package.pkg.tar.xz")) diff --git a/tests/ahriman/core/conftest.py b/tests/ahriman/core/conftest.py index 48a70529..c8259a81 100644 --- a/tests/ahriman/core/conftest.py +++ b/tests/ahriman/core/conftest.py @@ -1,7 +1,7 @@ -from pathlib import Path - import pytest +from pathlib import Path + from ahriman.core.alpm.pacman import Pacman from ahriman.core.alpm.repo import Repo from ahriman.core.configuration import Configuration @@ -11,7 +11,7 @@ from ahriman.models.repository_paths import RepositoryPaths @pytest.fixture def configuration(resource_path_root: Path) -> Configuration: path = resource_path_root / "core" / "ahriman.ini" - return Configuration.from_path(path=str(path), logfile=False) + return Configuration.from_path(path=path, logfile=False) @pytest.fixture @@ -23,5 +23,5 @@ def pacman(configuration: Configuration) -> Pacman: def repo(configuration: Configuration) -> Repo: return Repo( configuration.get("repository", "name"), - RepositoryPaths(configuration.get("repository", "root"), "x86_64"), + RepositoryPaths(Path(configuration.get("repository", "root")), "x86_64"), []) diff --git a/tests/ahriman/models/conftest.py b/tests/ahriman/models/conftest.py index 7946b54b..57ea758b 100644 --- a/tests/ahriman/models/conftest.py +++ b/tests/ahriman/models/conftest.py @@ -1,5 +1,7 @@ import pytest +from pathlib import Path + from ahriman.models.build_status import BuildStatus, BuildStatusEnum from ahriman.models.package import Package from ahriman.models.package_desciption import PackageDescription @@ -76,4 +78,4 @@ def package_description_python2_schedule() -> PackageDescription: def repository_paths() -> RepositoryPaths: return RepositoryPaths( architecture="x86_64", - root="/var/lib/ahriman") + root=Path("/var/lib/ahriman")) diff --git a/tests/ahriman/models/test_package.py b/tests/ahriman/models/test_package.py index f513bb13..0cd3a463 100644 --- a/tests/ahriman/models/test_package.py +++ b/tests/ahriman/models/test_package.py @@ -1,5 +1,4 @@ from pathlib import Path - from pytest_mock import MockerFixture from ahriman.models.package import Package diff --git a/tests/ahriman/models/test_repository_paths.py b/tests/ahriman/models/test_repository_paths.py index e59fc0e6..99730a3a 100644 --- a/tests/ahriman/models/test_repository_paths.py +++ b/tests/ahriman/models/test_repository_paths.py @@ -1,5 +1,4 @@ -import os - +from pathlib import Path from pytest_mock import MockerFixture from unittest import mock @@ -11,15 +10,15 @@ def test_create_tree(repository_paths: RepositoryPaths, mocker: MockerFixture) - must create whole tree """ paths = { - property - for property in dir(repository_paths) - if not property.startswith("_") and property not in ("architecture", "create_tree", "root") + prop + for prop in dir(repository_paths) + if not prop.startswith("_") and prop not in ("architecture", "create_tree", "root") } - mocker.patch("os.makedirs") + mocker.patch("pathlib.Path.mkdir") repository_paths.create_tree() - os.makedirs.assert_has_calls( + Path.mkdir.assert_has_calls( [ - mock.call(getattr(repository_paths, path), mode=0o755, exist_ok=True) - for path in paths - ], any_order=True) + mock.call(mode=0o755, parents=True, exist_ok=True) + for _ in paths + ])