packagers support (#100)

This commit is contained in:
2023-06-05 02:37:19 +03:00
committed by GitHub
parent d495163fdd
commit 4b984afb64
89 changed files with 849 additions and 318 deletions

View File

@ -10,7 +10,7 @@ def test_build(task_ahriman: Task, mocker: MockerFixture) -> None:
must build package
"""
check_output_mock = mocker.patch("ahriman.core.build_tools.task.Task._check_output")
task_ahriman.build(Path("ahriman"))
task_ahriman.build(Path("ahriman"), "packager")
check_output_mock.assert_called()

View File

@ -27,7 +27,7 @@ def test_migrate_data(connection: Connection, configuration: Configuration, mock
def test_migrate_package_depends(connection: Connection, configuration: Configuration, package_ahriman: Package,
mocker: MockerFixture) -> None:
"""
must update make and opt depends list
must update check depends list
"""
mocker.patch("pathlib.Path.is_dir", return_value=True)
mocker.patch("pathlib.Path.iterdir", return_value=[package_ahriman.packages[package_ahriman.base].filepath])
@ -45,7 +45,7 @@ def test_migrate_package_depends(connection: Connection, configuration: Configur
def test_migrate_package_depends_skip(connection: Connection, configuration: Configuration,
mocker: MockerFixture) -> None:
"""
must skip update make and opt depends list if no repository directory found
must skip update check depends list if no repository directory found
"""
mocker.patch("pathlib.Path.is_dir", return_value=False)
migrate_package_check_depends(connection, configuration)

View File

@ -0,0 +1,52 @@
import pytest
from pytest_mock import MockerFixture
from sqlite3 import Connection
from ahriman.core.configuration import Configuration
from ahriman.core.database.migrations.m008_packagers import migrate_data, migrate_package_base_packager, steps
from ahriman.models.package import Package
def test_migration_packagers() -> None:
"""
migration must not be empty
"""
assert steps
def test_migrate_data(connection: Connection, configuration: Configuration, mocker: MockerFixture) -> None:
"""
must perform data migration
"""
depends_mock = mocker.patch("ahriman.core.database.migrations.m008_packagers.migrate_package_base_packager")
migrate_data(connection, configuration)
depends_mock.assert_called_once_with(connection, configuration)
def test_migrate_package_base_packager(connection: Connection, configuration: Configuration, package_ahriman: Package,
mocker: MockerFixture) -> None:
"""
must update packagers
"""
mocker.patch("pathlib.Path.is_dir", return_value=True)
mocker.patch("pathlib.Path.iterdir", return_value=[package_ahriman.packages[package_ahriman.base].filepath])
package_mock = mocker.patch("ahriman.models.package.Package.from_archive", return_value=package_ahriman)
migrate_package_base_packager(connection, configuration)
package_mock.assert_called_once_with(
package_ahriman.packages[package_ahriman.base].filepath, pytest.helpers.anyvar(int), remote=None)
connection.executemany.assert_called_once_with(pytest.helpers.anyvar(str, strict=True), [{
"package_base": package_ahriman.base,
"packager": package_ahriman.packager,
}])
def test_migrate_package_depends_skip(connection: Connection, configuration: Configuration,
mocker: MockerFixture) -> None:
"""
must skip update packagers if no repository directory found
"""
mocker.patch("pathlib.Path.is_dir", return_value=False)
migrate_package_base_packager(connection, configuration)
connection.executemany.assert_not_called()

View File

@ -16,21 +16,22 @@ def test_user_list(database: SQLite, user: User) -> None:
must return all users
"""
database.user_update(user)
database.user_update(User(username=user.password, password=user.username, access=user.access))
second = User(username=user.password, password=user.username, access=user.access, packager_id=None, key=None)
database.user_update(second)
users = database.user_list(None, None)
assert len(users) == 2
assert user in users
assert User(username=user.password, password=user.username, access=user.access) in users
assert second in users
def test_user_list_filter_by_username(database: SQLite) -> None:
"""
must return users filtered by its id
"""
first = User(username="1", password="", access=UserAccess.Read)
second = User(username="2", password="", access=UserAccess.Full)
third = User(username="3", password="", access=UserAccess.Read)
first = User(username="1", password="", access=UserAccess.Read, packager_id=None, key=None)
second = User(username="2", password="", access=UserAccess.Full, packager_id=None, key=None)
third = User(username="3", password="", access=UserAccess.Read, packager_id=None, key=None)
database.user_update(first)
database.user_update(second)
@ -45,9 +46,9 @@ def test_user_list_filter_by_access(database: SQLite) -> None:
"""
must return users filtered by its access
"""
first = User(username="1", password="", access=UserAccess.Read)
second = User(username="2", password="", access=UserAccess.Full)
third = User(username="3", password="", access=UserAccess.Read)
first = User(username="1", password="", access=UserAccess.Read, packager_id=None, key=None)
second = User(username="2", password="", access=UserAccess.Full, packager_id=None, key=None)
third = User(username="3", password="", access=UserAccess.Read, packager_id=None, key=None)
database.user_update(first)
database.user_update(second)
@ -63,9 +64,9 @@ def test_user_list_filter_by_username_access(database: SQLite) -> None:
"""
must return users filtered by its access and username
"""
first = User(username="1", password="", access=UserAccess.Read)
second = User(username="2", password="", access=UserAccess.Full)
third = User(username="3", password="", access=UserAccess.Read)
first = User(username="1", password="", access=UserAccess.Read, packager_id=None, key=None)
second = User(username="2", password="", access=UserAccess.Full, packager_id=None, key=None)
third = User(username="3", password="", access=UserAccess.Read, packager_id=None, key=None)
database.user_update(first)
database.user_update(second)
@ -91,6 +92,7 @@ def test_user_update(database: SQLite, user: User) -> None:
database.user_update(user)
assert database.user_get(user.username) == user
new_user = User(username=user.username, password=user.hash_password("salt").password, access=UserAccess.Full)
new_user = User(username=user.username, password=user.hash_password("salt").password, access=UserAccess.Full,
packager_id=None, key="new key")
database.user_update(new_user)
assert database.user_get(new_user.username) == new_user

View File

@ -32,7 +32,7 @@ def test_package_update(database: SQLite, configuration: Configuration, package_
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")
runner = RemotePush(database, configuration, "gitremote")
assert runner.package_update(package_ahriman, local) == package_ahriman.base
glob_mock.assert_called_once_with(".git*")
@ -56,7 +56,7 @@ def test_packages_update(database: SQLite, configuration: Configuration, result:
"""
update_mock = mocker.patch("ahriman.core.gitremote.remote_push.RemotePush.package_update",
return_value=[package_ahriman.base])
runner = RemotePush(configuration, database, "gitremote")
runner = RemotePush(database, configuration, "gitremote")
local = Path("local")
assert list(runner.packages_update(result, local))
@ -71,7 +71,7 @@ def test_run(database: SQLite, configuration: Configuration, result: Result, pac
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, database, "gitremote")
runner = RemotePush(database, configuration, "gitremote")
runner.run(result)
fetch_mock.assert_called_once_with(pytest.helpers.anyvar(int), runner.remote_source)
@ -85,7 +85,7 @@ def test_run_failed(database: SQLite, configuration: Configuration, result: Resu
must reraise exception on error occurred
"""
mocker.patch("ahriman.core.build_tools.sources.Sources.fetch", side_effect=Exception())
runner = RemotePush(configuration, database, "gitremote")
runner = RemotePush(database, configuration, "gitremote")
with pytest.raises(GitRemoteError):
runner.run(result)

View File

@ -6,6 +6,8 @@ from unittest.mock import call as MockCall
from ahriman.core.repository.executor import Executor
from ahriman.models.package import Package
from ahriman.models.packagers import Packagers
from ahriman.models.user import User
def test_load_archives(executor: Executor) -> None:
@ -33,7 +35,7 @@ def test_process_build(executor: Executor, package_ahriman: Package, mocker: Moc
move_mock = mocker.patch("shutil.move")
status_client_mock = mocker.patch("ahriman.core.status.client.Client.set_building")
executor.process_build([package_ahriman])
executor.process_build([package_ahriman], Packagers("packager"))
# must move files (once)
move_mock.assert_called_once_with(Path(package_ahriman.base), executor.paths.packages / package_ahriman.base)
# must update status
@ -157,7 +159,7 @@ def test_process_remove_unknown(executor: Executor, package_ahriman: Package, mo
status_client_mock.assert_called_once_with(package_ahriman.base)
def test_process_update(executor: Executor, package_ahriman: Package, mocker: MockerFixture) -> None:
def test_process_update(executor: Executor, package_ahriman: Package, user: User, mocker: MockerFixture) -> None:
"""
must run update process
"""
@ -168,14 +170,16 @@ def test_process_update(executor: Executor, package_ahriman: Package, mocker: Mo
sign_package_mock = mocker.patch("ahriman.core.sign.gpg.GPG.process_sign_package", side_effect=lambda fn, _: [fn])
status_client_mock = mocker.patch("ahriman.core.status.client.Client.set_success")
remove_mock = mocker.patch("ahriman.core.repository.executor.Executor.process_remove")
packager_mock = mocker.patch("ahriman.core.repository.executor.Executor.packager", return_value=user)
filepath = next(package.filepath for package in package_ahriman.packages.values())
# must return complete
assert executor.process_update([filepath])
assert executor.process_update([filepath], Packagers("packager"))
packager_mock.assert_called_once_with(Packagers("packager"), "ahriman")
# must move files (once)
move_mock.assert_called_once_with(executor.paths.packages / filepath, executor.paths.repository / filepath)
# must sign package
sign_package_mock.assert_called_once_with(executor.paths.packages / filepath, package_ahriman.base)
sign_package_mock.assert_called_once_with(executor.paths.packages / filepath, user.key)
# must add package
repo_add_mock.assert_called_once_with(executor.paths.repository / filepath)
# must update status

View File

@ -4,6 +4,10 @@ from ahriman.core.configuration import Configuration
from ahriman.core.database import SQLite
from ahriman.core.exceptions import UnsafeRunError
from ahriman.core.repository.repository_properties import RepositoryProperties
from ahriman.models.packagers import Packagers
from ahriman.models.pacman_synchronization import PacmanSynchronization
from ahriman.models.user import User
from ahriman.models.user_access import UserAccess
def test_create_tree_on_load(configuration: Configuration, database: SQLite, mocker: MockerFixture) -> None:
@ -12,7 +16,8 @@ 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, refresh_pacman_database=0)
RepositoryProperties("x86_64", configuration, database, report=False, unsafe=False,
refresh_pacman_database=PacmanSynchronization.Disabled)
tree_create_mock.assert_called_once_with()
@ -23,6 +28,36 @@ 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, refresh_pacman_database=0)
RepositoryProperties("x86_64", configuration, database, report=False, unsafe=False,
refresh_pacman_database=PacmanSynchronization.Disabled)
tree_create_mock.assert_not_called()
def test_packager(repository: RepositoryProperties, mocker: MockerFixture) -> None:
"""
must extract packager
"""
database_mock = mocker.patch("ahriman.core.database.SQLite.user_get")
assert repository.packager(Packagers("username", {}), "base")
database_mock.assert_called_once_with("username")
def test_packager_empty(repository: RepositoryProperties, mocker: MockerFixture) -> None:
"""
must return empty user if username was not set
"""
database_mock = mocker.patch("ahriman.core.database.SQLite.user_get")
user = User(username="", password="", access=UserAccess.Read, packager_id=None, key=None)
assert repository.packager(Packagers(), "base") == user
database_mock.assert_not_called()
def test_packager_empty_result(repository: RepositoryProperties, mocker: MockerFixture) -> None:
"""
must return empty user if it wasn't found in database
"""
database_mock = mocker.patch("ahriman.core.database.SQLite.user_get", return_value=None)
user = User(username="username", password="", access=UserAccess.Read, packager_id=None, key=None)
assert repository.packager(Packagers(user.username), "base") == user
database_mock.assert_called_once_with(user.username)

View File

@ -74,7 +74,7 @@ def test_updates_aur_filter(update_handler: UpdateHandler, package_ahriman: Pack
package_load_mock = mocker.patch("ahriman.models.package.Package.from_aur", return_value=package_ahriman)
assert update_handler.updates_aur([package_ahriman.base], vcs=True) == [package_ahriman]
package_load_mock.assert_called_once_with(package_ahriman.base, update_handler.pacman)
package_load_mock.assert_called_once_with(package_ahriman.base, update_handler.pacman, None)
def test_updates_aur_ignore(update_handler: UpdateHandler, package_ahriman: Package,
@ -120,7 +120,7 @@ def test_updates_local(update_handler: UpdateHandler, package_ahriman: Package,
assert update_handler.updates_local(vcs=True) == [package_ahriman]
fetch_mock.assert_called_once_with(Path(package_ahriman.base), remote=None)
package_load_mock.assert_called_once_with(Path(package_ahriman.base), "x86_64")
package_load_mock.assert_called_once_with(Path(package_ahriman.base), "x86_64", None)
status_client_mock.assert_called_once_with(package_ahriman.base)
package_is_outdated_mock.assert_called_once_with(
package_ahriman, update_handler.paths,

View File

@ -135,21 +135,6 @@ def test_key_import(gpg: GPG, mocker: MockerFixture) -> None:
check_output_mock.assert_called_once_with("gpg", "--import", input_data="key", logger=pytest.helpers.anyvar(int))
def test_keys(gpg: GPG) -> None:
"""
must extract keys
"""
assert gpg.keys() == []
gpg.default_key = "key"
assert gpg.keys() == [gpg.default_key]
gpg.configuration.set_option("sign", "key_a", "key1")
gpg.configuration.set_option("sign", "key_b", "key1")
gpg.configuration.set_option("sign", "key_c", "key2")
assert gpg.keys() == ["key", "key1", "key2"]
def test_process(gpg_with_key: GPG, mocker: MockerFixture) -> None:
"""
must call process method correctly
@ -170,7 +155,7 @@ def test_process_sign_package_1(gpg_with_key: GPG, mocker: MockerFixture) -> Non
gpg_with_key.targets = {SignSettings.Packages}
assert gpg_with_key.process_sign_package(Path("a"), "a") == result
process_mock.assert_called_once_with(Path("a"), "key")
process_mock.assert_called_once_with(Path("a"), "a")
def test_process_sign_package_2(gpg_with_key: GPG, mocker: MockerFixture) -> None:
@ -182,7 +167,19 @@ def test_process_sign_package_2(gpg_with_key: GPG, mocker: MockerFixture) -> Non
gpg_with_key.targets = {SignSettings.Packages, SignSettings.Repository}
assert gpg_with_key.process_sign_package(Path("a"), "a") == result
process_mock.assert_called_once_with(Path("a"), "key")
process_mock.assert_called_once_with(Path("a"), "a")
def test_process_sign_package_3(gpg_with_key: GPG, mocker: MockerFixture) -> None:
"""
must sign package with default key if none passed
"""
result = [Path("a"), Path("a.sig")]
process_mock = mocker.patch("ahriman.core.sign.gpg.GPG.process", return_value=result)
gpg_with_key.targets = {SignSettings.Packages}
assert gpg_with_key.process_sign_package(Path("a"), None) == result
process_mock.assert_called_once_with(Path("a"), gpg_with_key.default_key)
def test_process_sign_package_skip_1(gpg_with_key: GPG, mocker: MockerFixture) -> None:
@ -211,7 +208,7 @@ def test_process_sign_package_skip_3(gpg: GPG, mocker: MockerFixture) -> None:
"""
process_mock = mocker.patch("ahriman.core.sign.gpg.GPG.process")
gpg.targets = {SignSettings.Packages}
gpg.process_sign_package(Path("a"), "a")
gpg.process_sign_package(Path("a"), None)
process_mock.assert_not_called()
@ -221,7 +218,7 @@ def test_process_sign_package_skip_4(gpg: GPG, mocker: MockerFixture) -> None:
"""
process_mock = mocker.patch("ahriman.core.sign.gpg.GPG.process")
gpg.targets = {SignSettings.Packages, SignSettings.Repository}
gpg.process_sign_package(Path("a"), "a")
gpg.process_sign_package(Path("a"), None)
process_mock.assert_not_called()

View File

@ -1,24 +1,26 @@
import pytest
from ahriman.core.configuration import Configuration
from ahriman.core.database import SQLite
from ahriman.core.sign.gpg import GPG
from ahriman.core.support.pkgbuild.keyring_generator import KeyringGenerator
from ahriman.core.support.pkgbuild.pkgbuild_generator import PkgbuildGenerator
@pytest.fixture
def keyring_generator(gpg: GPG, configuration: Configuration) -> KeyringGenerator:
def keyring_generator(database: SQLite, gpg: GPG, configuration: Configuration) -> KeyringGenerator:
"""
fixture for keyring pkgbuild generator
Args:
database(SQLite): database fixture
gpg(GPG): empty GPG fixture
configuration(Configuration): configuration fixture
Returns:
KeyringGenerator: keyring generator test instance
"""
return KeyringGenerator(gpg, configuration, "keyring")
return KeyringGenerator(database, gpg, configuration, "keyring")
@pytest.fixture

View File

@ -5,84 +5,87 @@ from pytest_mock import MockerFixture
from unittest.mock import MagicMock, call as MockCall
from ahriman.core.configuration import Configuration
from ahriman.core.database import SQLite
from ahriman.core.exceptions import PkgbuildGeneratorError
from ahriman.core.sign.gpg import GPG
from ahriman.core.support.pkgbuild.keyring_generator import KeyringGenerator
from ahriman.models.user import User
def test_init_packagers(gpg: GPG, configuration: Configuration, mocker: MockerFixture) -> None:
def test_init_packagers(database: SQLite, gpg: GPG, configuration: Configuration, user: User,
mocker: MockerFixture) -> None:
"""
must extract packagers keys
"""
mocker.patch("ahriman.core.sign.gpg.GPG.keys", return_value=["key"])
mocker.patch("ahriman.core.database.SQLite.user_list", return_value=[user])
assert KeyringGenerator(gpg, configuration, "keyring").packagers == ["key"]
assert KeyringGenerator(database, gpg, configuration, "keyring").packagers == ["key"]
configuration.set_option("keyring", "packagers", "key1")
assert KeyringGenerator(gpg, configuration, "keyring").packagers == ["key1"]
assert KeyringGenerator(database, gpg, configuration, "keyring").packagers == ["key1"]
def test_init_revoked(gpg: GPG, configuration: Configuration) -> None:
def test_init_revoked(database: SQLite, gpg: GPG, configuration: Configuration) -> None:
"""
must extract revoked keys
"""
assert KeyringGenerator(gpg, configuration, "keyring").revoked == []
assert KeyringGenerator(database, gpg, configuration, "keyring").revoked == []
configuration.set_option("keyring", "revoked", "key1")
assert KeyringGenerator(gpg, configuration, "keyring").revoked == ["key1"]
assert KeyringGenerator(database, gpg, configuration, "keyring").revoked == ["key1"]
def test_init_trusted(gpg: GPG, configuration: Configuration) -> None:
def test_init_trusted(database: SQLite, gpg: GPG, configuration: Configuration) -> None:
"""
must extract trusted keys
"""
assert KeyringGenerator(gpg, configuration, "keyring").trusted == []
assert KeyringGenerator(database, gpg, configuration, "keyring").trusted == []
gpg.default_key = "key"
assert KeyringGenerator(gpg, configuration, "keyring").trusted == ["key"]
assert KeyringGenerator(database, gpg, configuration, "keyring").trusted == ["key"]
configuration.set_option("keyring", "trusted", "key1")
assert KeyringGenerator(gpg, configuration, "keyring").trusted == ["key1"]
assert KeyringGenerator(database, gpg, configuration, "keyring").trusted == ["key1"]
def test_license(gpg: GPG, configuration: Configuration) -> None:
def test_license(database: SQLite, gpg: GPG, configuration: Configuration) -> None:
"""
must generate correct licenses list
"""
assert KeyringGenerator(gpg, configuration, "keyring").license == ["Unlicense"]
assert KeyringGenerator(database, gpg, configuration, "keyring").license == ["Unlicense"]
configuration.set_option("keyring", "license", "GPL MPL")
assert KeyringGenerator(gpg, configuration, "keyring").license == ["GPL", "MPL"]
assert KeyringGenerator(database, gpg, configuration, "keyring").license == ["GPL", "MPL"]
def test_pkgdesc(gpg: GPG, configuration: Configuration) -> None:
def test_pkgdesc(database: SQLite, gpg: GPG, configuration: Configuration) -> None:
"""
must generate correct pkgdesc property
"""
assert KeyringGenerator(gpg, configuration, "keyring").pkgdesc == "aur-clone PGP keyring"
assert KeyringGenerator(database, gpg, configuration, "keyring").pkgdesc == "aur-clone PGP keyring"
configuration.set_option("keyring", "description", "description")
assert KeyringGenerator(gpg, configuration, "keyring").pkgdesc == "description"
assert KeyringGenerator(database, gpg, configuration, "keyring").pkgdesc == "description"
def test_pkgname(gpg: GPG, configuration: Configuration) -> None:
def test_pkgname(database: SQLite, gpg: GPG, configuration: Configuration) -> None:
"""
must generate correct pkgname property
"""
assert KeyringGenerator(gpg, configuration, "keyring").pkgname == "aur-clone-keyring"
assert KeyringGenerator(database, gpg, configuration, "keyring").pkgname == "aur-clone-keyring"
configuration.set_option("keyring", "package", "keyring")
assert KeyringGenerator(gpg, configuration, "keyring").pkgname == "keyring"
assert KeyringGenerator(database, gpg, configuration, "keyring").pkgname == "keyring"
def test_url(gpg: GPG, configuration: Configuration) -> None:
def test_url(database: SQLite, gpg: GPG, configuration: Configuration) -> None:
"""
must generate correct url property
"""
assert KeyringGenerator(gpg, configuration, "keyring").url == ""
assert KeyringGenerator(database, gpg, configuration, "keyring").url == ""
configuration.set_option("keyring", "homepage", "homepage")
assert KeyringGenerator(gpg, configuration, "keyring").url == "homepage"
assert KeyringGenerator(database, gpg, configuration, "keyring").url == "homepage"
def test_generate_gpg(keyring_generator: KeyringGenerator, mocker: MockerFixture) -> None:

View File

@ -1,6 +1,8 @@
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.sign.gpg import GPG
from ahriman.core.support import KeyringTrigger
from ahriman.models.context_key import ContextKey
@ -21,10 +23,10 @@ def test_on_start(configuration: Configuration, mocker: MockerFixture) -> None:
"""
must run report for specified targets
"""
gpg_mock = mocker.patch("ahriman.core._Context.get")
context_mock = mocker.patch("ahriman.core._Context.get")
run_mock = mocker.patch("ahriman.core.support.package_creator.PackageCreator.run")
trigger = KeyringTrigger("x86_64", configuration)
trigger.on_start()
gpg_mock.assert_called_once_with(ContextKey("sign", GPG))
context_mock.assert_has_calls([MockCall(ContextKey("sign", GPG)), MockCall(ContextKey("database", SQLite))])
run_mock.assert_called_once_with()

View File

@ -35,6 +35,6 @@ def test_run(package_creator: PackageCreator, database: SQLite, mocker: MockerFi
write_mock.assert_called_once_with(local_path)
init_mock.assert_called_once_with(local_path)
package_mock.assert_called_once_with(local_path, "x86_64")
package_mock.assert_called_once_with(local_path, "x86_64", None)
database_mock.assert_called_once_with(ContextKey("database", SQLite))
insert_mock.assert_called_once_with(package, pytest.helpers.anyvar(int))

View File

@ -42,7 +42,7 @@ def test_spawn_process(spawner: Spawn, mocker: MockerFixture) -> None:
"""
start_mock = mocker.patch("multiprocessing.Process.start")
spawner._spawn_process("add", "ahriman", now="", maybe="?")
spawner._spawn_process("add", "ahriman", now="", maybe="?", none=None)
start_mock.assert_called_once_with()
spawner.args_parser.parse_args.assert_called_once_with(
spawner.command_arguments + [
@ -74,8 +74,8 @@ def test_packages_add(spawner: Spawn, mocker: MockerFixture) -> None:
must call package addition
"""
spawn_mock = mocker.patch("ahriman.core.spawn.Spawn._spawn_process")
spawner.packages_add(["ahriman", "linux"], now=False)
spawn_mock.assert_called_once_with("package-add", "ahriman", "linux", source="aur")
spawner.packages_add(["ahriman", "linux"], None, now=False)
spawn_mock.assert_called_once_with("package-add", "ahriman", "linux", source="aur", username=None)
def test_packages_add_with_build(spawner: Spawn, mocker: MockerFixture) -> None:
@ -83,8 +83,17 @@ def test_packages_add_with_build(spawner: Spawn, mocker: MockerFixture) -> None:
must call package addition with update
"""
spawn_mock = mocker.patch("ahriman.core.spawn.Spawn._spawn_process")
spawner.packages_add(["ahriman", "linux"], now=True)
spawn_mock.assert_called_once_with("package-add", "ahriman", "linux", source="aur", now="")
spawner.packages_add(["ahriman", "linux"], None, now=True)
spawn_mock.assert_called_once_with("package-add", "ahriman", "linux", source="aur", username=None, now="")
def test_packages_add_with_username(spawner: Spawn, mocker: MockerFixture) -> None:
"""
must call package addition with username
"""
spawn_mock = mocker.patch("ahriman.core.spawn.Spawn._spawn_process")
spawner.packages_add(["ahriman", "linux"], "username", now=False)
spawn_mock.assert_called_once_with("package-add", "ahriman", "linux", source="aur", username="username")
def test_packages_rebuild(spawner: Spawn, mocker: MockerFixture) -> None:
@ -92,8 +101,8 @@ def test_packages_rebuild(spawner: Spawn, mocker: MockerFixture) -> None:
must call package rebuild
"""
spawn_mock = mocker.patch("ahriman.core.spawn.Spawn._spawn_process")
spawner.packages_rebuild("python")
spawn_mock.assert_called_once_with("repo-rebuild", **{"depends-on": "python"})
spawner.packages_rebuild("python", "packager")
spawn_mock.assert_called_once_with("repo-rebuild", **{"depends-on": "python", "username": "packager"})
def test_packages_remove(spawner: Spawn, mocker: MockerFixture) -> None:
@ -110,8 +119,8 @@ def test_packages_update(spawner: Spawn, mocker: MockerFixture) -> None:
must call repo update
"""
spawn_mock = mocker.patch("ahriman.core.spawn.Spawn._spawn_process")
spawner.packages_update()
spawn_mock.assert_called_once_with("repo-update")
spawner.packages_update("packager")
spawn_mock.assert_called_once_with("repo-update", username="packager")
def test_run(spawner: Spawn, mocker: MockerFixture) -> None:

View File

@ -11,9 +11,9 @@ from typing import Any
from unittest.mock import MagicMock
from ahriman.core.exceptions import BuildError, OptionError, UnsafeRunError
from ahriman.core.util import check_output, check_user, enum_values, exception_response_text, filter_json, \
full_version, package_like, partition, pretty_datetime, pretty_size, safe_filename, srcinfo_property, \
srcinfo_property_list, trim_package, utcnow, walk
from ahriman.core.util import check_output, check_user, dataclass_view, enum_values, exception_response_text,\
extract_user, filter_json, full_version, package_like, partition, pretty_datetime, pretty_size, safe_filename, \
srcinfo_property, srcinfo_property_list, trim_package, utcnow, walk
from ahriman.models.package import Package
from ahriman.models.package_source import PackageSource
from ahriman.models.repository_paths import RepositoryPaths
@ -91,6 +91,16 @@ def test_check_output_with_user(passwd: Any, mocker: MockerFixture) -> None:
getpwuid_mock.assert_called_once_with(user)
def test_check_output_with_user_and_environment(passwd: Any, mocker: MockerFixture) -> None:
"""
must run set environment if both environment and user are set
"""
mocker.patch("ahriman.core.util.getpwuid", return_value=passwd)
user = os.getuid()
assert check_output("python", "-c", """import os; print(os.getenv("HOME"), os.getenv("VAR"))""",
environment={"VAR": "VALUE"}, user=user) == f"{passwd.pw_dir} VALUE"
def test_check_output_failure(mocker: MockerFixture) -> None:
"""
must process exception correctly
@ -155,6 +165,23 @@ def test_check_user_unsafe(mocker: MockerFixture) -> None:
check_user(paths, unsafe=True)
def test_dataclass_view(package_ahriman: Package) -> None:
"""
must serialize dataclasses
"""
assert Package.from_json(dataclass_view(package_ahriman)) == package_ahriman
def test_dataclass_view_without_none(package_ahriman: Package) -> None:
"""
must serialize dataclasses with None fields removed
"""
package_ahriman.packager = None
result = dataclass_view(package_ahriman)
assert "packager" not in result
assert Package.from_json(result) == package_ahriman
def test_exception_response_text() -> None:
"""
must parse HTTP response to string
@ -174,6 +201,23 @@ def test_exception_response_text_empty() -> None:
assert exception_response_text(exception) == ""
def test_extract_user() -> None:
"""
must extract user from system environment
"""
os.environ["USER"] = "user"
assert extract_user() == "user"
os.environ["SUDO_USER"] = "sudo"
assert extract_user() == "sudo"
os.environ["DOAS_USER"] = "doas"
assert extract_user() == "sudo"
del os.environ["SUDO_USER"]
assert extract_user() == "doas"
def test_filter_json(package_ahriman: Package) -> None:
"""
must filter fields by known list