update tests for pathlib

This commit is contained in:
Evgenii Alekseev 2021-03-25 02:18:44 +03:00
parent c3b9933c64
commit bf8a93d080
5 changed files with 24 additions and 42 deletions

View File

@ -1,8 +1,7 @@
import os
import pytest import pytest
from pathlib import Path
from pytest_mock import MockerFixture from pytest_mock import MockerFixture
from unittest import mock
from ahriman.core.alpm.repo import Repo 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 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: 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") 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() Repo._check_output.assert_called_once()
assert check_output_mock.call_args[0][0] == "repo-add" 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 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") 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() Repo._check_output.assert_called_once()
assert check_output_mock.call_args[0][0] == "repo-remove" 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 must fail on missing file
""" """
mocker.patch("os.listdir", return_value=["package.pkg.tar.xz"]) mocker.patch("pathlib.Path.glob", return_value=[Path("package.pkg.tar.xz")])
mocker.patch("os.remove", side_effect=FileNotFoundError()) mocker.patch("pathlib.Path.unlink", side_effect=FileNotFoundError())
with pytest.raises(FileNotFoundError): with pytest.raises(FileNotFoundError):
repo.remove("package", "package.pkg.tar.xz") repo.remove("package", Path("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)

View File

@ -1,7 +1,7 @@
from pathlib import Path
import pytest import pytest
from pathlib import Path
from ahriman.core.alpm.pacman import Pacman from ahriman.core.alpm.pacman import Pacman
from ahriman.core.alpm.repo import Repo from ahriman.core.alpm.repo import Repo
from ahriman.core.configuration import Configuration from ahriman.core.configuration import Configuration
@ -11,7 +11,7 @@ from ahriman.models.repository_paths import RepositoryPaths
@pytest.fixture @pytest.fixture
def configuration(resource_path_root: Path) -> Configuration: def configuration(resource_path_root: Path) -> Configuration:
path = resource_path_root / "core" / "ahriman.ini" 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 @pytest.fixture
@ -23,5 +23,5 @@ def pacman(configuration: Configuration) -> Pacman:
def repo(configuration: Configuration) -> Repo: def repo(configuration: Configuration) -> Repo:
return Repo( return Repo(
configuration.get("repository", "name"), configuration.get("repository", "name"),
RepositoryPaths(configuration.get("repository", "root"), "x86_64"), RepositoryPaths(Path(configuration.get("repository", "root")), "x86_64"),
[]) [])

View File

@ -1,5 +1,7 @@
import pytest import pytest
from pathlib import Path
from ahriman.models.build_status import BuildStatus, BuildStatusEnum from ahriman.models.build_status import BuildStatus, BuildStatusEnum
from ahriman.models.package import Package from ahriman.models.package import Package
from ahriman.models.package_desciption import PackageDescription from ahriman.models.package_desciption import PackageDescription
@ -76,4 +78,4 @@ def package_description_python2_schedule() -> PackageDescription:
def repository_paths() -> RepositoryPaths: def repository_paths() -> RepositoryPaths:
return RepositoryPaths( return RepositoryPaths(
architecture="x86_64", architecture="x86_64",
root="/var/lib/ahriman") root=Path("/var/lib/ahriman"))

View File

@ -1,5 +1,4 @@
from pathlib import Path from pathlib import Path
from pytest_mock import MockerFixture from pytest_mock import MockerFixture
from ahriman.models.package import Package from ahriman.models.package import Package

View File

@ -1,5 +1,4 @@
import os from pathlib import Path
from pytest_mock import MockerFixture from pytest_mock import MockerFixture
from unittest import mock from unittest import mock
@ -11,15 +10,15 @@ def test_create_tree(repository_paths: RepositoryPaths, mocker: MockerFixture) -
must create whole tree must create whole tree
""" """
paths = { paths = {
property prop
for property in dir(repository_paths) for prop in dir(repository_paths)
if not property.startswith("_") and property not in ("architecture", "create_tree", "root") 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() 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) mock.call(mode=0o755, parents=True, exist_ok=True)
for path in paths for _ in paths
], any_order=True) ])