mirror of
https://github.com/arcan1s/ahriman.git
synced 2025-07-29 13:49:57 +00:00
write patches via gitremote push trigger (#79)
* write patches via gitremote push trigger * implement context variables intead of custom database class
This commit is contained in:
@ -5,48 +5,65 @@ from pytest_mock import MockerFixture
|
||||
from unittest.mock import call as MockCall
|
||||
|
||||
from ahriman.core.configuration import Configuration
|
||||
from ahriman.core.database import SQLite
|
||||
from ahriman.core.exceptions import GitRemoteError
|
||||
from ahriman.core.gitremote.remote_push import RemotePush
|
||||
from ahriman.models.package import Package
|
||||
from ahriman.models.pkgbuild_patch import PkgbuildPatch
|
||||
from ahriman.models.result import Result
|
||||
|
||||
|
||||
def test_package_update(package_ahriman: Package, mocker: MockerFixture) -> None:
|
||||
def test_package_update(database: SQLite, configuration: Configuration, package_ahriman: Package,
|
||||
mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must update single package
|
||||
"""
|
||||
patch1 = PkgbuildPatch(None, "patch")
|
||||
patch2 = PkgbuildPatch("key", "value")
|
||||
|
||||
rmtree_mock = mocker.patch("shutil.rmtree")
|
||||
fetch_mock = mocker.patch("ahriman.core.build_tools.sources.Sources.fetch")
|
||||
patches_mock = mocker.patch("ahriman.core.database.SQLite.patches_get", return_value=[patch1, patch2])
|
||||
patches_write_mock = mocker.patch("ahriman.models.pkgbuild_patch.PkgbuildPatch.write")
|
||||
runner = RemotePush(configuration, database, "gitremote")
|
||||
|
||||
local = Path("local")
|
||||
RemotePush.package_update(package_ahriman, local)
|
||||
assert runner.package_update(package_ahriman, local) == package_ahriman.base
|
||||
rmtree_mock.assert_has_calls([
|
||||
MockCall(local / package_ahriman.base, ignore_errors=True),
|
||||
MockCall(local / package_ahriman.base / ".git", ignore_errors=True),
|
||||
])
|
||||
fetch_mock.assert_called_once_with(pytest.helpers.anyvar(int), package_ahriman.remote)
|
||||
patches_mock.assert_called_once_with(package_ahriman.base)
|
||||
patches_write_mock.assert_has_calls([
|
||||
MockCall(local / package_ahriman.base / f"ahriman-{package_ahriman.base}.patch"),
|
||||
MockCall(local / package_ahriman.base / f"ahriman-{patch2.key}.patch"),
|
||||
])
|
||||
|
||||
|
||||
def test_packages_update(result: Result, package_ahriman: Package, mocker: MockerFixture) -> None:
|
||||
def test_packages_update(database: SQLite, configuration: Configuration, result: Result, package_ahriman: Package,
|
||||
mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must generate packages update
|
||||
"""
|
||||
update_mock = mocker.patch("ahriman.core.gitremote.remote_push.RemotePush.package_update",
|
||||
return_value=[package_ahriman.base])
|
||||
runner = RemotePush(configuration, database, "gitremote")
|
||||
|
||||
local = Path("local")
|
||||
assert list(RemotePush.packages_update(result, local))
|
||||
assert list(runner.packages_update(result, local))
|
||||
update_mock.assert_called_once_with(package_ahriman, local)
|
||||
|
||||
|
||||
def test_run(configuration: Configuration, result: Result, package_ahriman: Package, mocker: MockerFixture) -> None:
|
||||
def test_run(database: SQLite, configuration: Configuration, result: Result, package_ahriman: Package,
|
||||
mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must push changes on result
|
||||
"""
|
||||
mocker.patch("ahriman.core.gitremote.remote_push.RemotePush.packages_update", return_value=[package_ahriman.base])
|
||||
fetch_mock = mocker.patch("ahriman.core.build_tools.sources.Sources.fetch")
|
||||
push_mock = mocker.patch("ahriman.core.build_tools.sources.Sources.push")
|
||||
runner = RemotePush(configuration, "gitremote")
|
||||
runner = RemotePush(configuration, database, "gitremote")
|
||||
|
||||
runner.run(result)
|
||||
fetch_mock.assert_called_once_with(pytest.helpers.anyvar(int), runner.remote_source)
|
||||
@ -55,12 +72,12 @@ def test_run(configuration: Configuration, result: Result, package_ahriman: Pack
|
||||
)
|
||||
|
||||
|
||||
def test_run_failed(configuration: Configuration, result: Result, mocker: MockerFixture) -> None:
|
||||
def test_run_failed(database: SQLite, configuration: Configuration, result: Result, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must reraise exception on error occurred
|
||||
"""
|
||||
mocker.patch("ahriman.core.build_tools.sources.Sources.fetch", side_effect=Exception())
|
||||
runner = RemotePush(configuration, "gitremote")
|
||||
runner = RemotePush(configuration, database, "gitremote")
|
||||
|
||||
with pytest.raises(GitRemoteError):
|
||||
runner.run(result)
|
||||
|
@ -1,18 +1,22 @@
|
||||
from pytest_mock import MockerFixture
|
||||
|
||||
from ahriman.core.configuration import Configuration
|
||||
from ahriman.core.database import SQLite
|
||||
from ahriman.core.gitremote import RemotePushTrigger
|
||||
from ahriman.models.context_key import ContextKey
|
||||
from ahriman.models.package import Package
|
||||
from ahriman.models.result import Result
|
||||
|
||||
|
||||
def test_on_result(configuration: Configuration, result: Result, package_ahriman: Package,
|
||||
mocker: MockerFixture) -> None:
|
||||
database: SQLite, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must push changes on result
|
||||
"""
|
||||
database_mock = mocker.patch("ahriman.core._Context.get", return_value=database)
|
||||
run_mock = mocker.patch("ahriman.core.gitremote.remote_push.RemotePush.run")
|
||||
trigger = RemotePushTrigger("x86_64", configuration)
|
||||
|
||||
trigger.on_result(result, [package_ahriman])
|
||||
database_mock.assert_called_once_with(ContextKey("database", SQLite))
|
||||
run_mock.assert_called_once_with(result)
|
||||
|
@ -4,7 +4,6 @@ from pytest_mock import MockerFixture
|
||||
|
||||
from ahriman.core.configuration import Configuration
|
||||
from ahriman.core.database import SQLite
|
||||
from ahriman.core.repository import Repository
|
||||
from ahriman.core.repository.cleaner import Cleaner
|
||||
from ahriman.core.repository.executor import Executor
|
||||
from ahriman.core.repository.update_handler import UpdateHandler
|
||||
@ -24,7 +23,7 @@ def cleaner(configuration: Configuration, database: SQLite, mocker: MockerFixtur
|
||||
Cleaner: cleaner test instance
|
||||
"""
|
||||
mocker.patch("ahriman.models.repository_paths.RepositoryPaths.tree_create")
|
||||
return Cleaner("x86_64", configuration, database, report=False, unsafe=False)
|
||||
return Cleaner("x86_64", configuration, database, report=False, unsafe=False, refresh_pacman_database=0)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@ -45,24 +44,7 @@ def executor(configuration: Configuration, database: SQLite, mocker: MockerFixtu
|
||||
mocker.patch("ahriman.core.repository.cleaner.Cleaner.clear_packages")
|
||||
mocker.patch("ahriman.core.repository.cleaner.Cleaner.clear_queue")
|
||||
mocker.patch("ahriman.models.repository_paths.RepositoryPaths.tree_create")
|
||||
return Executor("x86_64", configuration, database, report=False, unsafe=False)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def repository(configuration: Configuration, database: SQLite, mocker: MockerFixture) -> Repository:
|
||||
"""
|
||||
fixture for repository
|
||||
|
||||
Args:
|
||||
configuration(Configuration): configuration fixture
|
||||
database(SQLite): database fixture
|
||||
mocker(MockerFixture): mocker object
|
||||
|
||||
Returns:
|
||||
Repository: repository test instance
|
||||
"""
|
||||
mocker.patch("ahriman.models.repository_paths.RepositoryPaths.tree_create")
|
||||
return Repository("x86_64", configuration, database, report=False, unsafe=False)
|
||||
return Executor("x86_64", configuration, database, report=False, unsafe=False, refresh_pacman_database=0)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@ -83,4 +65,4 @@ def update_handler(configuration: Configuration, database: SQLite, mocker: Mocke
|
||||
mocker.patch("ahriman.core.repository.cleaner.Cleaner.clear_packages")
|
||||
mocker.patch("ahriman.core.repository.cleaner.Cleaner.clear_queue")
|
||||
mocker.patch("ahriman.models.repository_paths.RepositoryPaths.tree_create")
|
||||
return UpdateHandler("x86_64", configuration, database, report=False, unsafe=False)
|
||||
return UpdateHandler("x86_64", configuration, database, report=False, unsafe=False, refresh_pacman_database=0)
|
||||
|
@ -2,11 +2,42 @@ import pytest
|
||||
|
||||
from pathlib import Path
|
||||
from pytest_mock import MockerFixture
|
||||
from unittest.mock import call as MockCall
|
||||
|
||||
from ahriman.core.alpm.pacman import Pacman
|
||||
from ahriman.core.configuration import Configuration
|
||||
from ahriman.core.database import SQLite
|
||||
from ahriman.core.repository import Repository
|
||||
from ahriman.core.sign.gpg import GPG
|
||||
from ahriman.models.context_key import ContextKey
|
||||
from ahriman.models.package import Package
|
||||
|
||||
|
||||
def test_load(configuration: Configuration, database: SQLite, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must correctly load instance
|
||||
"""
|
||||
context_mock = mocker.patch("ahriman.core.repository.Repository._set_context")
|
||||
Repository.load("x86_64", configuration, database, report=False, unsafe=False)
|
||||
context_mock.assert_called_once_with()
|
||||
|
||||
|
||||
def test_set_context(configuration: Configuration, database: SQLite, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must set context variables
|
||||
"""
|
||||
set_mock = mocker.patch("ahriman.core._Context.set")
|
||||
|
||||
instance = Repository.load("x86_64", configuration, database, report=False, unsafe=False)
|
||||
set_mock.assert_has_calls([
|
||||
MockCall(ContextKey("database", SQLite), instance.database),
|
||||
MockCall(ContextKey("configuration", Configuration), instance.configuration),
|
||||
MockCall(ContextKey("pacman", Pacman), instance.pacman),
|
||||
MockCall(ContextKey("sign", GPG), instance.sign),
|
||||
MockCall(ContextKey("repository", Repository), instance),
|
||||
])
|
||||
|
||||
|
||||
def test_load_archives(package_ahriman: Package, package_python_schedule: Package,
|
||||
repository: Repository, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
|
@ -12,7 +12,7 @@ def test_create_tree_on_load(configuration: Configuration, database: SQLite, moc
|
||||
"""
|
||||
mocker.patch("ahriman.core.repository.repository_properties.check_user")
|
||||
tree_create_mock = mocker.patch("ahriman.models.repository_paths.RepositoryPaths.tree_create")
|
||||
RepositoryProperties("x86_64", configuration, database, report=False, unsafe=False)
|
||||
RepositoryProperties("x86_64", configuration, database, report=False, unsafe=False, refresh_pacman_database=0)
|
||||
|
||||
tree_create_mock.assert_called_once_with()
|
||||
|
||||
@ -23,6 +23,6 @@ def test_create_tree_on_load_unsafe(configuration: Configuration, database: SQLi
|
||||
"""
|
||||
mocker.patch("ahriman.core.repository.repository_properties.check_user", side_effect=UnsafeRunError(0, 1))
|
||||
tree_create_mock = mocker.patch("ahriman.models.repository_paths.RepositoryPaths.tree_create")
|
||||
RepositoryProperties("x86_64", configuration, database, report=False, unsafe=False)
|
||||
RepositoryProperties("x86_64", configuration, database, report=False, unsafe=False, refresh_pacman_database=0)
|
||||
|
||||
tree_create_mock.assert_not_called()
|
||||
|
@ -6,7 +6,6 @@ from ahriman.core.configuration import Configuration
|
||||
from ahriman.core.database import SQLite
|
||||
from ahriman.core.exceptions import UnknownPackageError
|
||||
from ahriman.core.status.watcher import Watcher
|
||||
from ahriman.core.status.web_client import WebClient
|
||||
from ahriman.models.build_status import BuildStatus, BuildStatusEnum
|
||||
from ahriman.models.log_record_id import LogRecordId
|
||||
from ahriman.models.package import Package
|
||||
@ -17,10 +16,10 @@ def test_force_no_report(configuration: Configuration, database: SQLite, mocker:
|
||||
must force dummy report client
|
||||
"""
|
||||
configuration.set_option("web", "port", "8080")
|
||||
mocker.patch("ahriman.models.repository_paths.RepositoryPaths.tree_create")
|
||||
load_mock = mocker.patch("ahriman.core.repository.Repository.load")
|
||||
|
||||
watcher = Watcher("x86_64", configuration, database)
|
||||
assert not isinstance(watcher.repository.reporter, WebClient)
|
||||
load_mock.assert_called_once_with("x86_64", configuration, database, report=False, unsafe=False)
|
||||
|
||||
|
||||
def test_get(watcher: Watcher, package_ahriman: Package) -> None:
|
||||
|
92
tests/ahriman/core/test_context_init.py
Normal file
92
tests/ahriman/core/test_context_init.py
Normal file
@ -0,0 +1,92 @@
|
||||
import pytest
|
||||
|
||||
from ahriman.core import _Context
|
||||
from ahriman.models.context_key import ContextKey
|
||||
|
||||
|
||||
def test_get_set() -> None:
|
||||
"""
|
||||
must set and get variable
|
||||
"""
|
||||
key, value = ContextKey("key", int), 42
|
||||
ctx = _Context()
|
||||
|
||||
ctx.set(key, value)
|
||||
assert ctx.get(key) == value
|
||||
|
||||
|
||||
def test_get_key_exception() -> None:
|
||||
"""
|
||||
must raise KeyError in case if key was not found
|
||||
"""
|
||||
ctx = _Context()
|
||||
with pytest.raises(KeyError):
|
||||
ctx.get(ContextKey("key", int))
|
||||
|
||||
|
||||
def test_get_value_exception() -> None:
|
||||
"""
|
||||
must raise ValueError in case if key type differs from existing value
|
||||
"""
|
||||
key, value = ContextKey("key", int), 42
|
||||
ctx = _Context()
|
||||
ctx.set(key, value)
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
ctx.get(ContextKey("key", str))
|
||||
|
||||
|
||||
def test_set_key_exception() -> None:
|
||||
"""
|
||||
must raise KeyError in case if key already exists
|
||||
"""
|
||||
key, value = ContextKey("key", int), 42
|
||||
ctx = _Context()
|
||||
ctx.set(key, value)
|
||||
|
||||
with pytest.raises(KeyError):
|
||||
ctx.set(key, value)
|
||||
|
||||
|
||||
def test_set_value_exception() -> None:
|
||||
"""
|
||||
must raise ValueError in case if key type differs from new value
|
||||
"""
|
||||
ctx = _Context()
|
||||
with pytest.raises(ValueError):
|
||||
ctx.set(ContextKey("key", str), 42)
|
||||
|
||||
|
||||
def test_contains() -> None:
|
||||
"""
|
||||
must correctly check if element is in list
|
||||
"""
|
||||
key, value = ContextKey("key", int), 42
|
||||
ctx = _Context()
|
||||
ctx.set(key, value)
|
||||
|
||||
assert key not in ctx
|
||||
assert key.key in ctx
|
||||
assert "random-key" not in ctx
|
||||
|
||||
|
||||
def test_iter() -> None:
|
||||
"""
|
||||
must return keys iterator
|
||||
"""
|
||||
key, value = ContextKey("key", int), 42
|
||||
ctx = _Context()
|
||||
ctx.set(key, value)
|
||||
|
||||
assert set(iter(ctx)) == set(iter({key.key: value}))
|
||||
|
||||
|
||||
def test_len() -> None:
|
||||
"""
|
||||
must correctly define collection length
|
||||
"""
|
||||
ctx = _Context()
|
||||
ctx.set(ContextKey("str", str), "str")
|
||||
ctx.set(ContextKey("int", int), 42)
|
||||
|
||||
assert len(ctx) == 2
|
Reference in New Issue
Block a user