mirror of
https://github.com/arcan1s/ahriman.git
synced 2025-06-28 06:41:43 +00:00
Add ability to show more info in search and status subcommands
This feature also introduces the followiing changes * aur-search command now works as expected with multiterms * printer classes for managing of data print * --sort-by argument for aur-search subcommand instead of using package name * --quiet argument now has also --no-quite option * if --quite is supplied, the log level will be set to warn instead of critical to be able to see error messages * pretty_datetime function now also supports datetime objects * BuildStatus is now pure dataclass
This commit is contained in:
47
tests/ahriman/application/formatters/conftest.py
Normal file
47
tests/ahriman/application/formatters/conftest.py
Normal file
@ -0,0 +1,47 @@
|
||||
import aur
|
||||
import pytest
|
||||
|
||||
from ahriman.application.formatters.aur_printer import AurPrinter
|
||||
from ahriman.application.formatters.configuration_printer import ConfigurationPrinter
|
||||
from ahriman.application.formatters.package_printer import PackagePrinter
|
||||
from ahriman.application.formatters.status_printer import StatusPrinter
|
||||
from ahriman.models.build_status import BuildStatus
|
||||
from ahriman.models.package import Package
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def aur_package_ahriman_printer(aur_package_ahriman: aur.Package) -> AurPrinter:
|
||||
"""
|
||||
fixture for AUR package printer
|
||||
:param aur_package_ahriman: AUR package fixture
|
||||
:return: AUR package printer test instance
|
||||
"""
|
||||
return AurPrinter(aur_package_ahriman)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def configuration_printer() -> ConfigurationPrinter:
|
||||
"""
|
||||
fixture for configuration printer
|
||||
:return: configuration printer test instance
|
||||
"""
|
||||
return ConfigurationPrinter("section", {"key_one": "value_one", "key_two": "value_two"})
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def package_ahriman_printer(package_ahriman: Package) -> PackagePrinter:
|
||||
"""
|
||||
fixture for package printer
|
||||
:param package_ahriman: package fixture
|
||||
:return: package printer test instance
|
||||
"""
|
||||
return PackagePrinter(package_ahriman, BuildStatus())
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def status_printer(package_ahriman: Package) -> StatusPrinter:
|
||||
"""
|
||||
fixture for build status printer
|
||||
:return: build status printer test instance
|
||||
"""
|
||||
return StatusPrinter(BuildStatus())
|
15
tests/ahriman/application/formatters/test_aur_printer.py
Normal file
15
tests/ahriman/application/formatters/test_aur_printer.py
Normal file
@ -0,0 +1,15 @@
|
||||
from ahriman.application.formatters.aur_printer import AurPrinter
|
||||
|
||||
|
||||
def test_properties(aur_package_ahriman_printer: AurPrinter) -> None:
|
||||
"""
|
||||
must return non empty properties list
|
||||
"""
|
||||
assert aur_package_ahriman_printer.properties()
|
||||
|
||||
|
||||
def test_title(aur_package_ahriman_printer: AurPrinter) -> None:
|
||||
"""
|
||||
must return non empty title
|
||||
"""
|
||||
assert aur_package_ahriman_printer.title() is not None
|
@ -0,0 +1,22 @@
|
||||
from ahriman.application.formatters.configuration_printer import ConfigurationPrinter
|
||||
|
||||
|
||||
def test_properties(configuration_printer: ConfigurationPrinter) -> None:
|
||||
"""
|
||||
must return non empty properties list
|
||||
"""
|
||||
assert configuration_printer.properties()
|
||||
|
||||
|
||||
def test_properties_required(configuration_printer: ConfigurationPrinter) -> None:
|
||||
"""
|
||||
must return all properties as required
|
||||
"""
|
||||
assert all(prop.is_required for prop in configuration_printer.properties())
|
||||
|
||||
|
||||
def test_title(configuration_printer: ConfigurationPrinter) -> None:
|
||||
"""
|
||||
must return non empty title
|
||||
"""
|
||||
assert configuration_printer.title() == "[section]"
|
15
tests/ahriman/application/formatters/test_package_printer.py
Normal file
15
tests/ahriman/application/formatters/test_package_printer.py
Normal file
@ -0,0 +1,15 @@
|
||||
from ahriman.application.formatters.package_printer import PackagePrinter
|
||||
|
||||
|
||||
def test_properties(package_ahriman_printer: PackagePrinter) -> None:
|
||||
"""
|
||||
must return non empty properties list
|
||||
"""
|
||||
assert package_ahriman_printer.properties()
|
||||
|
||||
|
||||
def test_title(package_ahriman_printer: PackagePrinter) -> None:
|
||||
"""
|
||||
must return non empty title
|
||||
"""
|
||||
assert package_ahriman_printer.title() is not None
|
45
tests/ahriman/application/formatters/test_printer.py
Normal file
45
tests/ahriman/application/formatters/test_printer.py
Normal file
@ -0,0 +1,45 @@
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
from ahriman.application.formatters.package_printer import PackagePrinter
|
||||
from ahriman.application.formatters.printer import Printer
|
||||
|
||||
|
||||
def test_print(package_ahriman_printer: PackagePrinter) -> None:
|
||||
"""
|
||||
must print content
|
||||
"""
|
||||
log_mock = MagicMock()
|
||||
package_ahriman_printer.print(verbose=False, log_fn=log_mock)
|
||||
log_mock.assert_called()
|
||||
|
||||
|
||||
def test_print_empty() -> None:
|
||||
"""
|
||||
must not print empty object
|
||||
"""
|
||||
log_mock = MagicMock()
|
||||
Printer().print(verbose=True, log_fn=log_mock)
|
||||
log_mock.assert_not_called()
|
||||
|
||||
|
||||
def test_print_verbose(package_ahriman_printer: PackagePrinter) -> None:
|
||||
"""
|
||||
must print content with increased verbosity
|
||||
"""
|
||||
log_mock = MagicMock()
|
||||
package_ahriman_printer.print(verbose=True, log_fn=log_mock)
|
||||
log_mock.assert_called()
|
||||
|
||||
|
||||
def test_properties() -> None:
|
||||
"""
|
||||
must return empty properties list
|
||||
"""
|
||||
assert Printer().properties() == []
|
||||
|
||||
|
||||
def test_title() -> None:
|
||||
"""
|
||||
must return empty title
|
||||
"""
|
||||
assert Printer().title() is None
|
15
tests/ahriman/application/formatters/test_status_printer.py
Normal file
15
tests/ahriman/application/formatters/test_status_printer.py
Normal file
@ -0,0 +1,15 @@
|
||||
from ahriman.application.formatters.status_printer import StatusPrinter
|
||||
|
||||
|
||||
def test_properties(status_printer: StatusPrinter) -> None:
|
||||
"""
|
||||
must return empty properties list
|
||||
"""
|
||||
assert not status_printer.properties()
|
||||
|
||||
|
||||
def test_title(status_printer: StatusPrinter) -> None:
|
||||
"""
|
||||
must return non empty title
|
||||
"""
|
||||
assert status_printer.title() is not None
|
@ -11,7 +11,7 @@ def test_run(args: argparse.Namespace, configuration: Configuration, mocker: Moc
|
||||
must run command
|
||||
"""
|
||||
mocker.patch("ahriman.models.repository_paths.RepositoryPaths.tree_create")
|
||||
print_mock = mocker.patch("ahriman.application.handlers.dump.Dump._print")
|
||||
print_mock = mocker.patch("ahriman.application.formatters.printer.Printer.print")
|
||||
application_mock = mocker.patch("ahriman.core.configuration.Configuration.dump",
|
||||
return_value=configuration.dump())
|
||||
|
||||
|
@ -14,6 +14,7 @@ def _default_args(args: argparse.Namespace) -> argparse.Namespace:
|
||||
:return: generated arguments for these test cases
|
||||
"""
|
||||
args.dry_run = False
|
||||
args.info = False
|
||||
return args
|
||||
|
||||
|
||||
@ -42,19 +43,29 @@ def test_run_dry_run(args: argparse.Namespace, configuration: Configuration, pac
|
||||
application_mock = mocker.patch("ahriman.application.application.Application.unknown",
|
||||
return_value=[package_ahriman])
|
||||
remove_mock = mocker.patch("ahriman.application.application.Application.remove")
|
||||
log_fn_mock = mocker.patch("ahriman.application.handlers.remove_unknown.RemoveUnknown.log_fn")
|
||||
print_mock = mocker.patch("ahriman.application.formatters.printer.Printer.print")
|
||||
|
||||
RemoveUnknown.run(args, "x86_64", configuration, True)
|
||||
application_mock.assert_called_once()
|
||||
remove_mock.assert_not_called()
|
||||
log_fn_mock.assert_called_once_with(package_ahriman)
|
||||
print_mock.assert_called_once_with(False)
|
||||
|
||||
|
||||
def test_log_fn(package_ahriman: Package, mocker: MockerFixture) -> None:
|
||||
def test_run_dry_run_verbose(args: argparse.Namespace, configuration: Configuration, package_ahriman: Package,
|
||||
mocker: MockerFixture) -> None:
|
||||
"""
|
||||
log function must call print built-in
|
||||
must run simplified command with increased verbosity
|
||||
"""
|
||||
print_mock = mocker.patch("builtins.print")
|
||||
args = _default_args(args)
|
||||
args.dry_run = True
|
||||
args.info = True
|
||||
mocker.patch("ahriman.models.repository_paths.RepositoryPaths.tree_create")
|
||||
application_mock = mocker.patch("ahriman.application.application.Application.unknown",
|
||||
return_value=[package_ahriman])
|
||||
remove_mock = mocker.patch("ahriman.application.application.Application.remove")
|
||||
print_mock = mocker.patch("ahriman.application.formatters.printer.Printer.print")
|
||||
|
||||
RemoveUnknown.log_fn(package_ahriman)
|
||||
print_mock.assert_called() # we don't really care about call details tbh
|
||||
RemoveUnknown.run(args, "x86_64", configuration, True)
|
||||
application_mock.assert_called_once()
|
||||
remove_mock.assert_not_called()
|
||||
print_mock.assert_called_once_with(True)
|
||||
|
@ -1,10 +1,13 @@
|
||||
import argparse
|
||||
import aur
|
||||
import pytest
|
||||
|
||||
from pytest_mock import MockerFixture
|
||||
from unittest import mock
|
||||
|
||||
from ahriman.application.handlers import Search
|
||||
from ahriman.core.configuration import Configuration
|
||||
from ahriman.core.exceptions import InvalidOption
|
||||
|
||||
|
||||
def _default_args(args: argparse.Namespace) -> argparse.Namespace:
|
||||
@ -14,6 +17,8 @@ def _default_args(args: argparse.Namespace) -> argparse.Namespace:
|
||||
:return: generated arguments for these test cases
|
||||
"""
|
||||
args.search = ["ahriman"]
|
||||
args.info = False
|
||||
args.sort_by = "name"
|
||||
return args
|
||||
|
||||
|
||||
@ -24,35 +29,71 @@ def test_run(args: argparse.Namespace, configuration: Configuration, aur_package
|
||||
"""
|
||||
args = _default_args(args)
|
||||
mocker.patch("aur.search", return_value=[aur_package_ahriman])
|
||||
log_mock = mocker.patch("ahriman.application.handlers.search.Search.log_fn")
|
||||
print_mock = mocker.patch("ahriman.application.formatters.printer.Printer.print")
|
||||
|
||||
Search.run(args, "x86_64", configuration, True)
|
||||
log_mock.assert_called_once()
|
||||
print_mock.assert_called_once()
|
||||
|
||||
|
||||
def test_run_multiple_search(args: argparse.Namespace, configuration: Configuration, mocker: MockerFixture) -> None:
|
||||
def test_run_multiple_search(args: argparse.Namespace, configuration: Configuration, aur_package_ahriman: aur.Package,
|
||||
mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must run command with multiple search arguments
|
||||
"""
|
||||
args = _default_args(args)
|
||||
args.search = ["ahriman", "is", "cool"]
|
||||
search_mock = mocker.patch("aur.search")
|
||||
search_mock = mocker.patch("aur.search", return_value=[aur_package_ahriman])
|
||||
|
||||
Search.run(args, "x86_64", configuration, True)
|
||||
search_mock.assert_called_once_with(" ".join(args.search))
|
||||
search_mock.assert_has_calls([mock.call(term) for term in args.search])
|
||||
|
||||
|
||||
def test_log_fn(args: argparse.Namespace, configuration: Configuration, aur_package_ahriman: aur.Package,
|
||||
mocker: MockerFixture) -> None:
|
||||
def test_run_sort(args: argparse.Namespace, configuration: Configuration, aur_package_ahriman: aur.Package,
|
||||
mocker: MockerFixture) -> None:
|
||||
"""
|
||||
log function must call print built-in
|
||||
must run command with sorting
|
||||
"""
|
||||
args = _default_args(args)
|
||||
mocker.patch("aur.search", return_value=[aur_package_ahriman])
|
||||
print_mock = mocker.patch("builtins.print")
|
||||
sort_mock = mocker.patch("ahriman.application.handlers.search.Search.sort")
|
||||
|
||||
Search.run(args, "x86_64", configuration, True)
|
||||
print_mock.assert_called() # we don't really care about call details tbh
|
||||
sort_mock.assert_called_once_with([aur_package_ahriman], "name")
|
||||
|
||||
|
||||
def test_run_sort_by(args: argparse.Namespace, configuration: Configuration, aur_package_ahriman: aur.Package,
|
||||
mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must run command with sorting by specified field
|
||||
"""
|
||||
args = _default_args(args)
|
||||
args.sort_by = "field"
|
||||
mocker.patch("aur.search", return_value=[aur_package_ahriman])
|
||||
sort_mock = mocker.patch("ahriman.application.handlers.search.Search.sort")
|
||||
|
||||
Search.run(args, "x86_64", configuration, True)
|
||||
sort_mock.assert_called_once_with([aur_package_ahriman], "field")
|
||||
|
||||
|
||||
def test_sort(aur_package_ahriman: aur.Package) -> None:
|
||||
"""
|
||||
must sort package list
|
||||
"""
|
||||
another = aur_package_ahriman._replace(name="1", package_base="base")
|
||||
# sort by name
|
||||
assert Search.sort([aur_package_ahriman, another], "name") == [another, aur_package_ahriman]
|
||||
# sort by another field
|
||||
assert Search.sort([aur_package_ahriman, another], "package_base") == [aur_package_ahriman, another]
|
||||
# sort by field with the same values
|
||||
assert Search.sort([aur_package_ahriman, another], "version") == [another, aur_package_ahriman]
|
||||
|
||||
|
||||
def test_sort_exception(aur_package_ahriman: aur.Package) -> None:
|
||||
"""
|
||||
must raise an exception on unknown sorting field
|
||||
"""
|
||||
with pytest.raises(InvalidOption):
|
||||
Search.sort([aur_package_ahriman], "random_field")
|
||||
|
||||
|
||||
def test_disallow_auto_architecture_run() -> None:
|
||||
@ -60,3 +101,10 @@ def test_disallow_auto_architecture_run() -> None:
|
||||
must not allow multi architecture run
|
||||
"""
|
||||
assert not Search.ALLOW_AUTO_ARCHITECTURE_RUN
|
||||
|
||||
|
||||
def test_sort_fields() -> None:
|
||||
"""
|
||||
must store valid field list which are allowed to be used for sorting
|
||||
"""
|
||||
assert all(field in aur.Package._fields for field in Search.SORT_FIELDS)
|
||||
|
@ -1,6 +1,7 @@
|
||||
import argparse
|
||||
|
||||
from pytest_mock import MockerFixture
|
||||
from unittest import mock
|
||||
|
||||
from ahriman.application.handlers import Status
|
||||
from ahriman.core.configuration import Configuration
|
||||
@ -15,6 +16,7 @@ def _default_args(args: argparse.Namespace) -> argparse.Namespace:
|
||||
:return: generated arguments for these test cases
|
||||
"""
|
||||
args.ahriman = True
|
||||
args.info = False
|
||||
args.package = []
|
||||
args.status = None
|
||||
return args
|
||||
@ -31,12 +33,28 @@ def test_run(args: argparse.Namespace, configuration: Configuration, package_ahr
|
||||
packages_mock = mocker.patch("ahriman.core.status.client.Client.get",
|
||||
return_value=[(package_ahriman, BuildStatus(BuildStatusEnum.Success)),
|
||||
(package_python_schedule, BuildStatus(BuildStatusEnum.Failed))])
|
||||
pretty_print_mock = mocker.patch("ahriman.models.package.Package.pretty_print")
|
||||
print_mock = mocker.patch("ahriman.application.formatters.printer.Printer.print")
|
||||
|
||||
Status.run(args, "x86_64", configuration, True)
|
||||
application_mock.assert_called_once()
|
||||
packages_mock.assert_called_once()
|
||||
pretty_print_mock.assert_called()
|
||||
print_mock.assert_has_calls([mock.call(False) for _ in range(3)])
|
||||
|
||||
|
||||
def test_run_verbose(args: argparse.Namespace, configuration: Configuration, package_ahriman: Package,
|
||||
mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must run command
|
||||
"""
|
||||
args = _default_args(args)
|
||||
args.info = True
|
||||
mocker.patch("ahriman.models.repository_paths.RepositoryPaths.tree_create")
|
||||
mocker.patch("ahriman.core.status.client.Client.get",
|
||||
return_value=[(package_ahriman, BuildStatus(BuildStatusEnum.Success))])
|
||||
print_mock = mocker.patch("ahriman.application.formatters.printer.Printer.print")
|
||||
|
||||
Status.run(args, "x86_64", configuration, True)
|
||||
print_mock.assert_has_calls([mock.call(True) for _ in range(2)])
|
||||
|
||||
|
||||
def test_run_with_package_filter(args: argparse.Namespace, configuration: Configuration, package_ahriman: Package,
|
||||
@ -65,10 +83,10 @@ def test_run_by_status(args: argparse.Namespace, configuration: Configuration, p
|
||||
return_value=[(package_ahriman, BuildStatus(BuildStatusEnum.Success)),
|
||||
(package_python_schedule, BuildStatus(BuildStatusEnum.Failed))])
|
||||
mocker.patch("ahriman.models.repository_paths.RepositoryPaths.tree_create")
|
||||
pretty_print_mock = mocker.patch("ahriman.models.package.Package.pretty_print")
|
||||
print_mock = mocker.patch("ahriman.application.formatters.printer.Printer.print")
|
||||
|
||||
Status.run(args, "x86_64", configuration, True)
|
||||
pretty_print_mock.assert_called_once()
|
||||
print_mock.assert_has_calls([mock.call(False) for _ in range(2)])
|
||||
|
||||
|
||||
def test_imply_with_report(args: argparse.Namespace, configuration: Configuration, mocker: MockerFixture) -> None:
|
||||
|
@ -1,4 +1,5 @@
|
||||
import configparser
|
||||
import logging
|
||||
import pytest
|
||||
|
||||
from pathlib import Path
|
||||
@ -194,7 +195,7 @@ def test_load_logging_quiet(configuration: Configuration, mocker: MockerFixture)
|
||||
"""
|
||||
disable_mock = mocker.patch("logging.disable")
|
||||
configuration.load_logging(quiet=True)
|
||||
disable_mock.assert_called_once()
|
||||
disable_mock.assert_called_once_with(logging.WARNING)
|
||||
|
||||
|
||||
def test_merge_sections_missing(configuration: Configuration) -> None:
|
||||
|
@ -1,3 +1,4 @@
|
||||
import datetime
|
||||
import logging
|
||||
import pytest
|
||||
import subprocess
|
||||
@ -6,7 +7,7 @@ from pathlib import Path
|
||||
from pytest_mock import MockerFixture
|
||||
|
||||
from ahriman.core.exceptions import InvalidOption, UnsafeRun
|
||||
from ahriman.core.util import check_output, check_user, package_like, pretty_datetime, pretty_size, walk
|
||||
from ahriman.core.util import check_output, check_user, filter_json, package_like, pretty_datetime, pretty_size, walk
|
||||
from ahriman.models.package import Package
|
||||
|
||||
|
||||
@ -79,6 +80,26 @@ def test_check_user_exception(mocker: MockerFixture) -> None:
|
||||
check_user(cwd)
|
||||
|
||||
|
||||
def test_filter_json(package_ahriman: Package) -> None:
|
||||
"""
|
||||
must filter fields by known list
|
||||
"""
|
||||
expected = package_ahriman.view()
|
||||
probe = package_ahriman.view()
|
||||
probe["unknown_field"] = "value"
|
||||
|
||||
assert expected == filter_json(probe, expected.keys())
|
||||
|
||||
|
||||
def test_filter_json_empty_value(package_ahriman: Package) -> None:
|
||||
"""
|
||||
must return empty values from object
|
||||
"""
|
||||
probe = package_ahriman.view()
|
||||
probe["base"] = None
|
||||
assert "base" not in filter_json(probe, probe.keys())
|
||||
|
||||
|
||||
def test_package_like(package_ahriman: Package) -> None:
|
||||
"""
|
||||
package_like must return true for archives
|
||||
@ -102,6 +123,13 @@ def test_pretty_datetime() -> None:
|
||||
assert pretty_datetime(0) == "1970-01-01 00:00:00"
|
||||
|
||||
|
||||
def test_pretty_datetime_datetime() -> None:
|
||||
"""
|
||||
must generate string from datetime object
|
||||
"""
|
||||
assert pretty_datetime(datetime.datetime(1970, 1, 1, 0, 0, 0)) == "1970-01-01 00:00:00"
|
||||
|
||||
|
||||
def test_pretty_datetime_empty() -> None:
|
||||
"""
|
||||
must generate empty string from None timestamp
|
||||
|
0
tests/ahriman/models/test_property.py
Normal file
0
tests/ahriman/models/test_property.py
Normal file
Reference in New Issue
Block a user