build_tools package tests

This commit is contained in:
Evgenii Alekseev 2021-03-25 04:16:50 +03:00
parent f3acc01906
commit fc29689ef9
4 changed files with 100 additions and 34 deletions

View File

@ -1,10 +1,16 @@
import pytest import pytest
from pathlib import Path
from typing import Any, Type, TypeVar from typing import Any, Type, TypeVar
from ahriman.models.package import Package
from ahriman.models.package_desciption import PackageDescription
from ahriman.models.repository_paths import RepositoryPaths
T = TypeVar("T") T = TypeVar("T")
# helpers
# https://stackoverflow.com/a/21611963 # https://stackoverflow.com/a/21611963
@pytest.helpers.register @pytest.helpers.register
def anyvar(cls: Type[T], strict: bool = False) -> T: def anyvar(cls: Type[T], strict: bool = False) -> T:
@ -12,3 +18,30 @@ def anyvar(cls: Type[T], strict: bool = False) -> T:
def __eq__(self, other: Any) -> bool: def __eq__(self, other: Any) -> bool:
return not strict or isinstance(other, cls) return not strict or isinstance(other, cls)
return AnyVar() return AnyVar()
# generic fixtures
@pytest.fixture
def package_ahriman(package_description_ahriman: PackageDescription) -> Package:
packages = {"ahriman": package_description_ahriman}
return Package(
base="ahriman",
version="0.12.1-1",
aur_url="https://aur.archlinux.org",
packages=packages)
@pytest.fixture
def package_description_ahriman() -> PackageDescription:
return PackageDescription(
archive_size=4200,
build_date=42,
filename="ahriman-0.12.1-1-any.pkg.tar.zst",
installed_size=4200000)
@pytest.fixture
def repository_paths() -> RepositoryPaths:
return RepositoryPaths(
architecture="x86_64",
root=Path("/var/lib/ahriman"))

View File

@ -0,0 +1,58 @@
import pytest
import shutil
from pathlib import Path
from pytest_mock import MockerFixture
from unittest import mock
from ahriman.core.build_tools.task import Task
def test_fetch_existing(mocker: MockerFixture) -> None:
"""
must fetch new package via clone command
"""
mocker.patch("pathlib.Path.is_dir", return_value=True)
check_output_mock = mocker.patch("ahriman.core.build_tools.task.Task._check_output")
local = Path("local")
Task.fetch(local, "remote", "master")
check_output_mock.assert_has_calls([
mock.call("git", "fetch", "origin", "master",
exception=pytest.helpers.anyvar(int),
cwd=local, logger=pytest.helpers.anyvar(int)),
mock.call("git", "reset", "--hard", "origin/master",
exception=pytest.helpers.anyvar(int),
cwd=local, logger=pytest.helpers.anyvar(int))
])
def test_fetch_new(mocker: MockerFixture) -> None:
"""
must fetch new package via clone command
"""
mocker.patch("pathlib.Path.is_dir", return_value=False)
check_output_mock = mocker.patch("ahriman.core.build_tools.task.Task._check_output")
local = Path("local")
Task.fetch(local, "remote", "master")
check_output_mock.assert_has_calls([
mock.call("git", "clone", "remote", str(local),
exception=pytest.helpers.anyvar(int),
logger=pytest.helpers.anyvar(int)),
mock.call("git", "reset", "--hard", "origin/master",
exception=pytest.helpers.anyvar(int),
cwd=local, logger=pytest.helpers.anyvar(int))
])
def test_init_with_cache(task_ahriman: Task, mocker: MockerFixture) -> None:
"""
must copy tree instead of fetch
"""
mocker.patch("pathlib.Path.is_dir", return_value=True)
mocker.patch("shutil.copytree")
mocker.patch("ahriman.core.build_tools.task.Task.fetch")
task_ahriman.init(None)
shutil.copytree.assert_called_once()

View File

@ -4,7 +4,9 @@ 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.build_tools.task import Task
from ahriman.core.configuration import Configuration from ahriman.core.configuration import Configuration
from ahriman.models.package import Package
from ahriman.models.repository_paths import RepositoryPaths from ahriman.models.repository_paths import RepositoryPaths
@ -20,8 +22,10 @@ def pacman(configuration: Configuration) -> Pacman:
@pytest.fixture @pytest.fixture
def repo(configuration: Configuration) -> Repo: def repo(configuration: Configuration, repository_paths: RepositoryPaths) -> Repo:
return Repo( return Repo(configuration.get("repository", "name"), repository_paths, [])
configuration.get("repository", "name"),
RepositoryPaths(Path(configuration.get("repository", "root")), "x86_64"),
[]) @pytest.fixture
def task_ahriman(package_ahriman: Package, configuration: Configuration, repository_paths: RepositoryPaths) -> Task:
return Task(package_ahriman, "x86_64", configuration, repository_paths)

View File

@ -1,11 +1,8 @@
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
from ahriman.models.repository_paths import RepositoryPaths
@pytest.fixture @pytest.fixture
@ -13,16 +10,6 @@ def build_status_failed() -> BuildStatus:
return BuildStatus(BuildStatusEnum.Failed, 42) return BuildStatus(BuildStatusEnum.Failed, 42)
@pytest.fixture
def package_ahriman(package_description_ahriman: PackageDescription) -> Package:
packages = {"ahriman": package_description_ahriman}
return Package(
base="ahriman",
version="0.12.1-1",
aur_url="https://aur.archlinux.org",
packages=packages)
@pytest.fixture @pytest.fixture
def package_python_schedule( def package_python_schedule(
package_description_python_schedule: PackageDescription, package_description_python_schedule: PackageDescription,
@ -47,15 +34,6 @@ def package_tpacpi_bat_git() -> Package:
packages={"tpacpi-bat-git": PackageDescription()}) packages={"tpacpi-bat-git": PackageDescription()})
@pytest.fixture
def package_description_ahriman() -> PackageDescription:
return PackageDescription(
archive_size=4200,
build_date=42,
filename="ahriman-0.12.1-1-any.pkg.tar.zst",
installed_size=4200000)
@pytest.fixture @pytest.fixture
def package_description_python_schedule() -> PackageDescription: def package_description_python_schedule() -> PackageDescription:
return PackageDescription( return PackageDescription(
@ -72,10 +50,3 @@ def package_description_python2_schedule() -> PackageDescription:
build_date=422, build_date=422,
filename="python2-schedule-1.0.0-2-any.pkg.tar.zst", filename="python2-schedule-1.0.0-2-any.pkg.tar.zst",
installed_size=4200002) installed_size=4200002)
@pytest.fixture
def repository_paths() -> RepositoryPaths:
return RepositoryPaths(
architecture="x86_64",
root=Path("/var/lib/ahriman"))