patch control subcommands

This commit is contained in:
2021-10-05 08:57:42 +03:00
parent 3e0058620b
commit 5cfffbcd46
9 changed files with 516 additions and 70 deletions

View File

@ -0,0 +1,123 @@
import argparse
from pathlib import Path
from pytest_mock import MockerFixture
from ahriman.application.application import Application
from ahriman.application.handlers import Patch
from ahriman.core.configuration import Configuration
from ahriman.models.action import Action
from ahriman.models.package import Package
def _default_args(args: argparse.Namespace) -> argparse.Namespace:
"""
default arguments for these test cases
:param args: command line arguments fixture
:return: generated arguments for these test cases
"""
args.package = "ahriman"
args.remove = False
args.track = ["*.diff", "*.patch"]
return args
def test_run(args: argparse.Namespace, configuration: Configuration, mocker: MockerFixture) -> None:
"""
must run command
"""
args = _default_args(args)
args.action = Action.Update
mocker.patch("pathlib.Path.mkdir")
application_mock = mocker.patch("ahriman.application.handlers.patch.Patch.patch_set_create")
Patch.run(args, "x86_64", configuration, True)
application_mock.assert_called_once()
def test_run_list(args: argparse.Namespace, configuration: Configuration, mocker: MockerFixture) -> None:
"""
must run command with list flag
"""
args = _default_args(args)
args.action = Action.List
mocker.patch("pathlib.Path.mkdir")
application_mock = mocker.patch("ahriman.application.handlers.patch.Patch.patch_set_list")
Patch.run(args, "x86_64", configuration, True)
application_mock.assert_called_once()
def test_run_remove(args: argparse.Namespace, configuration: Configuration, mocker: MockerFixture) -> None:
"""
must run command with remove flag
"""
args = _default_args(args)
args.action = Action.Remove
mocker.patch("pathlib.Path.mkdir")
application_mock = mocker.patch("ahriman.application.handlers.patch.Patch.patch_set_remove")
Patch.run(args, "x86_64", configuration, True)
application_mock.assert_called_once()
def test_patch_set_list(application: Application, mocker: MockerFixture) -> None:
"""
must list available patches for the command
"""
mocker.patch("pathlib.Path.is_dir", return_value=True)
glob_mock = mocker.patch("pathlib.Path.glob", return_value=[Path("local")])
print_mock = mocker.patch("ahriman.application.handlers.patch.Patch._print")
Patch.patch_set_list(application, "ahriman")
glob_mock.assert_called_with("*.patch")
print_mock.assert_called()
def test_patch_set_list_no_dir(application: Application, mocker: MockerFixture) -> None:
"""
must not fail if no patches directory found
"""
mocker.patch("pathlib.Path.is_dir", return_value=False)
glob_mock = mocker.patch("pathlib.Path.glob")
print_mock = mocker.patch("ahriman.application.handlers.patch.Patch._print")
Patch.patch_set_list(application, "ahriman")
glob_mock.assert_not_called()
print_mock.assert_not_called()
def test_patch_set_create(application: Application, package_ahriman: Package, mocker: MockerFixture) -> None:
"""
must create patch set for the package
"""
mocker.patch("ahriman.models.package.Package.load", return_value=package_ahriman)
create_mock = mocker.patch("ahriman.core.build_tools.sources.Sources.patch_create")
patch_dir = application.repository.paths.patches_for(package_ahriman.base)
Patch.patch_set_create(application, Path("path"), ["*.patch"])
create_mock.assert_called_with(Path("path"), patch_dir / "00-main.patch", "*.patch")
def test_patch_set_create_clear(application: Application, package_ahriman: Package, mocker: MockerFixture) -> None:
"""
must clear patches directory before new set creation
"""
mocker.patch("pathlib.Path.is_dir", return_value=True)
mocker.patch("ahriman.models.package.Package.load", return_value=package_ahriman)
mocker.patch("ahriman.core.build_tools.sources.Sources.patch_create")
remove_mock = mocker.patch("shutil.rmtree")
Patch.patch_set_create(application, Path("path"), ["*.patch"])
remove_mock.assert_called()
def test_patch_set_remove(application: Application, package_ahriman: Package, mocker: MockerFixture) -> None:
"""
must remove patch set for the package
"""
remove_mock = mocker.patch("shutil.rmtree")
patch_dir = application.repository.paths.patches_for(package_ahriman.base)
Patch.patch_set_remove(application, package_ahriman.base)
remove_mock.assert_called_with(patch_dir, ignore_errors=True)

View File

@ -4,6 +4,7 @@ from pathlib import Path
from pytest_mock import MockerFixture
from ahriman.application.handlers import Handler
from ahriman.models.action import Action
from ahriman.models.build_status import BuildStatusEnum
from ahriman.models.sign_settings import SignSettings
from ahriman.models.user_access import UserAccess
@ -126,12 +127,77 @@ def test_subparsers_key_import(parser: argparse.ArgumentParser) -> None:
def test_subparsers_key_import_architecture(parser: argparse.ArgumentParser) -> None:
"""
check command must correctly parse architecture list
key-import command must correctly parse architecture list
"""
args = parser.parse_args(["-a", "x86_64", "key-import", "key"])
assert args.architecture == [""]
def test_subparsers_patch_add(parser: argparse.ArgumentParser) -> None:
"""
patch-add command must imply action, architecture list, lock and no-report
"""
args = parser.parse_args(["patch-add", "ahriman"])
assert args.action == Action.Update
assert args.architecture == [""]
assert args.lock is None
assert args.no_report
def test_subparsers_patch_add_architecture(parser: argparse.ArgumentParser) -> None:
"""
patch-add command must correctly parse architecture list
"""
args = parser.parse_args(["-a", "x86_64", "patch-add", "ahriman"])
assert args.architecture == [""]
def test_subparsers_patch_add_track(parser: argparse.ArgumentParser) -> None:
"""
patch-add command must correctly parse track files patterns
"""
args = parser.parse_args(["patch-add", "-t", "*.py", "ahriman"])
assert args.track == ["*.diff", "*.patch", "*.py"]
def test_subparsers_patch_list(parser: argparse.ArgumentParser) -> None:
"""
patch-list command must imply action, architecture list, lock and no-report
"""
args = parser.parse_args(["patch-list", "ahriman"])
assert args.action == Action.List
assert args.architecture == [""]
assert args.lock is None
assert args.no_report
def test_subparsers_patch_list_architecture(parser: argparse.ArgumentParser) -> None:
"""
patch-list command must correctly parse architecture list
"""
args = parser.parse_args(["-a", "x86_64", "patch-list", "ahriman"])
assert args.architecture == [""]
def test_subparsers_patch_remove(parser: argparse.ArgumentParser) -> None:
"""
patch-remove command must imply action, architecture list, lock and no-report
"""
args = parser.parse_args(["patch-remove", "ahriman"])
assert args.action == Action.Remove
assert args.architecture == [""]
assert args.lock is None
assert args.no_report
def test_subparsers_patch_remove_architecture(parser: argparse.ArgumentParser) -> None:
"""
patch-remove command must correctly parse architecture list
"""
args = parser.parse_args(["-a", "x86_64", "patch-remove", "ahriman"])
assert args.architecture == [""]
def test_subparsers_rebuild_architecture(parser: argparse.ArgumentParser) -> None:
"""
rebuild command must correctly parse architecture list

View File

@ -7,6 +7,34 @@ from unittest import mock
from ahriman.core.build_tools.sources import Sources
def test_add(mocker: MockerFixture) -> None:
"""
must add files to git
"""
glob_mock = mocker.patch("pathlib.Path.glob", return_value=[Path("local/1"), Path("local/2")])
check_output_mock = mocker.patch("ahriman.core.build_tools.sources.Sources._check_output")
local = Path("local")
Sources.add(local, "pattern1", "pattern2")
glob_mock.assert_has_calls([mock.call("pattern1"), mock.call("pattern2")])
check_output_mock.assert_called_with(
"git", "add", "--intent-to-add", "1", "2", "1", "2",
exception=None, cwd=local, logger=pytest.helpers.anyvar(int))
def test_diff(mocker: MockerFixture) -> None:
"""
must calculate diff
"""
write_mock = mocker.patch("pathlib.Path.write_text")
check_output_mock = mocker.patch("ahriman.core.build_tools.sources.Sources._check_output")
local = Path("local")
Sources.diff(local, Path("patch"))
write_mock.assert_called_once()
check_output_mock.assert_called_with("git", "diff", exception=None, cwd=local, logger=pytest.helpers.anyvar(int))
def test_fetch_existing(mocker: MockerFixture) -> None:
"""
must fetch new package via clone command
@ -17,15 +45,10 @@ def test_fetch_existing(mocker: MockerFixture) -> None:
local = Path("local")
Sources.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", "checkout", "--force", "master",
exception=pytest.helpers.anyvar(int),
cwd=local, logger=pytest.helpers.anyvar(int)),
mock.call("git", "fetch", "origin", "master", exception=None, cwd=local, logger=pytest.helpers.anyvar(int)),
mock.call("git", "checkout", "--force", "master", exception=None, 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))
exception=None, cwd=local, logger=pytest.helpers.anyvar(int))
])
@ -39,15 +62,10 @@ def test_fetch_new(mocker: MockerFixture) -> None:
local = Path("local")
Sources.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", "checkout", "--force", "master",
exception=pytest.helpers.anyvar(int),
cwd=local, logger=pytest.helpers.anyvar(int)),
mock.call("git", "clone", "remote", str(local), exception=None, logger=pytest.helpers.anyvar(int)),
mock.call("git", "checkout", "--force", "master", exception=None, 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))
exception=None, cwd=local, logger=pytest.helpers.anyvar(int))
])
@ -56,14 +74,14 @@ def test_load(mocker: MockerFixture) -> None:
must load packages sources correctly
"""
fetch_mock = mocker.patch("ahriman.core.build_tools.sources.Sources.fetch")
patch_mock = mocker.patch("ahriman.core.build_tools.sources.Sources.patch")
patch_mock = mocker.patch("ahriman.core.build_tools.sources.Sources.patch_apply")
Sources.load(Path("local"), "remote", Path("patches"))
fetch_mock.assert_called_with(Path("local"), "remote")
patch_mock.assert_called_with(Path("local"), Path("patches"))
def test_patches(mocker: MockerFixture) -> None:
def test_patch_apply(mocker: MockerFixture) -> None:
"""
must apply patches if any
"""
@ -72,30 +90,28 @@ def test_patches(mocker: MockerFixture) -> None:
check_output_mock = mocker.patch("ahriman.core.build_tools.sources.Sources._check_output")
local = Path("local")
Sources.patch(local, Path("patches"))
Sources.patch_apply(local, Path("patches"))
glob_mock.assert_called_once()
check_output_mock.assert_has_calls([
mock.call("git", "apply", "--ignore-space-change", "--ignore-whitespace", "01.patch",
exception=pytest.helpers.anyvar(int),
cwd=local, logger=pytest.helpers.anyvar(int)),
exception=None, cwd=local, logger=pytest.helpers.anyvar(int)),
mock.call("git", "apply", "--ignore-space-change", "--ignore-whitespace", "02.patch",
exception=pytest.helpers.anyvar(int),
cwd=local, logger=pytest.helpers.anyvar(int)),
exception=None, cwd=local, logger=pytest.helpers.anyvar(int)),
])
def test_patches_no_dir(mocker: MockerFixture) -> None:
def test_patch_apply_no_dir(mocker: MockerFixture) -> None:
"""
must not fail if no patches directory exists
"""
mocker.patch("pathlib.Path.is_dir", return_value=False)
glob_mock = mocker.patch("pathlib.Path.glob")
Sources.patch(Path("local"), Path("patches"))
Sources.patch_apply(Path("local"), Path("patches"))
glob_mock.assert_not_called()
def test_patches_no_patches(mocker: MockerFixture) -> None:
def test_patch_apply_no_patches(mocker: MockerFixture) -> None:
"""
must not fail if no patches exist
"""
@ -103,5 +119,17 @@ def test_patches_no_patches(mocker: MockerFixture) -> None:
mocker.patch("pathlib.Path.glob", return_value=[])
check_output_mock = mocker.patch("ahriman.core.build_tools.sources.Sources._check_output")
Sources.patch(Path("local"), Path("patches"))
Sources.patch_apply(Path("local"), Path("patches"))
check_output_mock.assert_not_called()
def test_patch_create(mocker: MockerFixture) -> None:
"""
must create patch set for the package
"""
add_mock = mocker.patch("ahriman.core.build_tools.sources.Sources.add")
diff_mock = mocker.patch("ahriman.core.build_tools.sources.Sources.diff")
Sources.patch_create(Path("local"), Path("patch"), "glob")
add_mock.assert_called_with(Path("local"), "glob")
diff_mock.assert_called_with(Path("local"), Path("patch"))