Verified Commit aaf40e02 authored by David Runge's avatar David Runge
Browse files

Extend tests for CLI and add integration tests

repo_management/cli.py:
Change `db2json` and `json2db` to try/except
`errors.RepoManagementError` and `argpase.ArgumentTypeError` and exit
with a non-zero return code in that case.

tests/test_cli.py:
Change the tests for `db2json` and `json2db` to cover potentially
raising `errors.RepoManagementError` and `argpase.ArgumentTypeError` and
checking whether exiting with a non-zero return code in that case.
Add parametrized integration test `test_transform_databases()` that
transforms existing pacman .files sync databases to JSON files and back
to .files repository databases again.
Add parametrized integration
`test_transform_databases_and_use_with_pacman()` to transform existing
pacman .files sync databases to JSON files and create .files and .db
repository databases from them. Afterwards pacman uses the created
repository databases to search for packages with the "linux" keyword in
them and list the files owned by the "linux" package using the created
.files sync databases.
parent e47f74bd
from repo_management import argparse, defaults, operations from argparse import ArgumentTypeError
from sys import exit
from repo_management import argparse, defaults, errors, operations
def db2json() -> None: def db2json() -> None:
...@@ -8,11 +11,15 @@ def db2json() -> None: ...@@ -8,11 +11,15 @@ def db2json() -> None:
repository database file. repository database file.
""" """
args = argparse.ArgParseFactory.db2json().parse_args() try:
operations.dump_db_to_json_files( args = argparse.ArgParseFactory.db2json().parse_args()
input_path=args.db_file, operations.dump_db_to_json_files(
output_path=args.output_dir, input_path=args.db_file,
) output_path=args.output_dir,
)
except (errors.RepoManagementError, ArgumentTypeError) as e:
print(e)
exit(1)
def json2db() -> None: def json2db() -> None:
...@@ -22,9 +29,13 @@ def json2db() -> None: ...@@ -22,9 +29,13 @@ def json2db() -> None:
in a directory. in a directory.
""" """
args = argparse.ArgParseFactory.json2db().parse_args() try:
operations.create_db_from_json_files( args = argparse.ArgParseFactory.json2db().parse_args()
input_path=args.input_dir, operations.create_db_from_json_files(
output_path=args.db_file, input_path=args.input_dir,
db_type=defaults.RepoDbType.FILES if args.files else defaults.RepoDbType.DEFAULT, output_path=args.db_file,
) db_type=defaults.RepoDbType.FILES if args.files else defaults.RepoDbType.DEFAULT,
)
except (errors.RepoManagementError, ArgumentTypeError) as e:
print(e)
exit(1)
from argparse import Namespace import shutil
import tempfile
from argparse import ArgumentTypeError, Namespace
from pathlib import Path
from typing import Dict, Iterator
from mock import Mock, patch from mock import Mock, patch
from pytest import mark from pytest import fixture, mark
from repo_management import cli, defaults from repo_management import cli, commands, defaults, errors
@fixture(scope="function")
def empty_dir() -> Iterator[Path]:
directory = tempfile.mkdtemp()
yield Path(directory)
shutil.rmtree(directory)
@fixture(scope="function")
def empty_file() -> Iterator[Path]:
[foo, file_name] = tempfile.mkstemp()
yield Path(file_name)
@mark.parametrize(
"fail_argparse, fail_dump",
[
(False, False),
(True, False),
(False, True),
],
)
@patch("repo_management.operations.dump_db_to_json_files") @patch("repo_management.operations.dump_db_to_json_files")
@patch("repo_management.argparse.ArgParseFactory") @patch("repo_management.argparse.ArgParseFactory")
def test_db2json(argparsefactory_mock: Mock, dump_db_to_json_files_mock: Mock) -> None: @patch("repo_management.cli.exit")
def test_db2json(
exit_mock: Mock,
argparsefactory_mock: Mock,
dump_db_to_json_files_mock: Mock,
fail_argparse: bool,
fail_dump: bool,
) -> None:
namespace = Namespace(db_file="db_file", output_dir="output_dir") namespace = Namespace(db_file="db_file", output_dir="output_dir")
argparsefactory_mock.db2json.return_value = Mock(parse_args=Mock(return_value=namespace)) if fail_argparse:
argparsefactory_mock.db2json.side_effect = ArgumentTypeError
else:
argparsefactory_mock.db2json.return_value = Mock(parse_args=Mock(return_value=namespace))
if fail_dump:
dump_db_to_json_files_mock.side_effect = errors.RepoManagementError
cli.db2json() cli.db2json()
dump_db_to_json_files_mock.assert_called_once_with(input_path=namespace.db_file, output_path=namespace.output_dir) if fail_argparse or fail_dump:
exit_mock.assert_called_once_with(1)
else:
dump_db_to_json_files_mock.assert_called_once_with(
input_path=namespace.db_file,
output_path=namespace.output_dir,
)
@mark.parametrize("files, db_type", [(True, defaults.RepoDbType.FILES), (False, defaults.RepoDbType.DEFAULT)]) @mark.parametrize(
"files, db_type, fail_argparse, fail_create",
[
(True, defaults.RepoDbType.FILES, False, False),
(False, defaults.RepoDbType.DEFAULT, False, False),
(True, defaults.RepoDbType.FILES, True, False),
(False, defaults.RepoDbType.DEFAULT, True, False),
(True, defaults.RepoDbType.FILES, False, True),
(False, defaults.RepoDbType.DEFAULT, False, True),
],
)
@patch("repo_management.operations.create_db_from_json_files") @patch("repo_management.operations.create_db_from_json_files")
@patch("repo_management.argparse.ArgParseFactory") @patch("repo_management.argparse.ArgParseFactory")
@patch("repo_management.cli.exit")
def test_json2db( def test_json2db(
argparsefactory_mock: Mock, create_db_from_json_files: Mock, files: bool, db_type: defaults.RepoDbType exit_mock: Mock,
argparsefactory_mock: Mock,
create_db_from_json_files_mock: Mock,
files: bool,
db_type: defaults.RepoDbType,
fail_argparse: bool,
fail_create: bool,
) -> None: ) -> None:
namespace = Namespace(db_file="db_file", input_dir="input_dir", files=files) namespace = Namespace(db_file="db_file", input_dir="input_dir", files=files)
argparsefactory_mock.json2db.return_value = Mock(parse_args=Mock(return_value=namespace)) if fail_argparse:
argparsefactory_mock.json2db.side_effect = ArgumentTypeError
else:
argparsefactory_mock.json2db.return_value = Mock(parse_args=Mock(return_value=namespace))
if fail_create:
create_db_from_json_files_mock.side_effect = errors.RepoManagementError
cli.json2db() cli.json2db()
create_db_from_json_files.assert_called_once_with( if fail_argparse:
input_path=namespace.input_dir, exit_mock.assert_called_once_with(1)
output_path=namespace.db_file, if fail_create:
db_type=db_type, create_db_from_json_files_mock.assert_called_once_with(
input_path=namespace.input_dir,
output_path=namespace.db_file,
db_type=db_type,
)
exit_mock.assert_called_once_with(1)
if not fail_argparse and not fail_create:
create_db_from_json_files_mock.assert_called_once_with(
input_path=namespace.input_dir,
output_path=namespace.db_file,
db_type=db_type,
)
@mark.integration
@mark.parametrize(
"db",
[
("core"),
("extra"),
("community"),
],
)
def test_transform_databases(db: str, empty_dir: Path, empty_file: Path) -> None:
commands.run_command(
cmd=["db2json", f"/var/lib/pacman/sync/{db}.files", empty_dir.as_posix()],
debug=True,
check=True,
)
commands.run_command(
cmd=["json2db", "-f", empty_dir.as_posix(), empty_file.as_posix()],
debug=True,
check=True,
)
@mark.integration
def test_transform_databases_and_use_with_pacman(empty_dir: Path) -> None:
dbs = ["core", "extra", "community"]
json_paths: Dict[str, Path] = {}
pacman_path = Path(empty_dir / Path("pacman"))
pacman_path.mkdir(parents=True)
db_path = Path(empty_dir / Path("pacman/db_path"))
db_path.mkdir(parents=True)
sync_path = Path(empty_dir / Path("pacman/db_path/sync"))
sync_path.mkdir(parents=True)
cache_path = Path(empty_dir / Path("pacman/cache_path"))
cache_path.mkdir(parents=True)
for db in dbs:
json_path = Path(empty_dir / Path(db))
json_path.mkdir()
json_paths[db] = json_path
for (name, json_path) in json_paths.items():
commands.run_command(
cmd=["db2json", f"/var/lib/pacman/sync/{name}.files", json_path.as_posix()],
debug=True,
check=True,
)
commands.run_command(
cmd=["json2db", json_path.as_posix(), f"{sync_path.as_posix()}/{name}.db"],
debug=True,
check=True,
)
commands.run_command(
cmd=["json2db", "-f", json_path.as_posix(), f"{sync_path.as_posix()}/{name}.files"],
debug=True,
check=True,
)
commands.run_command(
cmd=[
"pacman",
"--cache",
cache_path.as_posix(),
"--logfile",
f"{pacman_path.as_posix()}/pacman.log",
"--dbpath",
db_path.as_posix(),
"-Ss",
"linux",
],
debug=True,
check=True,
)
commands.run_command(
cmd=[
"pacman",
"--cache",
cache_path.as_posix(),
"--logfile",
f"{pacman_path.as_posix()}/pacman.log",
"--dbpath",
db_path.as_posix(),
"-Fl",
"linux",
],
debug=True,
check=True,
) )
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment