Compare commits

..

2 Commits

3 changed files with 127 additions and 15 deletions

View File

@ -43,7 +43,7 @@ class Search(Handler):
SORT_FIELDS = { SORT_FIELDS = {
field.name field.name
for field in fields(AURPackage) for field in fields(AURPackage)
if field.default_factory is not list # type: ignore[comparison-overlap] if field.default_factory is not list
} }
@classmethod @classmethod

View File

@ -40,6 +40,7 @@ class PackageArchive:
Attributes: Attributes:
package(Package): package descriptor package(Package): package descriptor
root(Path): path to root filesystem root(Path): path to root filesystem
pacman(Pacman): alpm wrapper instance
""" """
root: Path root: Path
@ -217,7 +218,7 @@ class PackageArchive:
extract list of the installed packages and their content extract list of the installed packages and their content
Returns: Returns:
dict[str, tuple[list[Path], list[Path]]]; map of package name to list of directories and files contained dict[str, FilesystemPackage]; map of package name to list of directories and files contained
by this package by this package
""" """
result = {} result = {}

View File

@ -1,7 +1,10 @@
from io import BytesIO from io import BytesIO
from pathlib import Path from pathlib import Path
from pytest_mock import MockerFixture from pytest_mock import MockerFixture
from unittest.mock import MagicMock, PropertyMock
from ahriman.core.exceptions import UnknownPackageError
from ahriman.models.filesystem_package import FilesystemPackage
from ahriman.models.package_archive import PackageArchive from ahriman.models.package_archive import PackageArchive
@ -44,27 +47,135 @@ def test_is_elf() -> None:
assert PackageArchive.is_elf(BytesIO(b"\x7fELF\nrandom string")) assert PackageArchive.is_elf(BytesIO(b"\x7fELF\nrandom string"))
def test_depends_on(package_archive_ahriman: PackageArchive, mocker: MockerFixture) -> None: def test_load_pacman_package(package_archive_ahriman: PackageArchive, mocker: MockerFixture) -> None:
""" """
must extract packages and files which are dependencies for the package must correctly load filesystem package from pacman
""" """
mocker.patch("ahriman.models.package_archive.PackageArchive.installed_packages", return_value={ directory = f"{package_archive_ahriman.package.base}-{package_archive_ahriman.package.version}"
package_archive_ahriman.package.base: ([Path("usr") / "dir2"], [Path("file1")]), path = Path("/") / "var" / "lib" / "pacman" / "local" / directory / "files"
"package1": ( package = MagicMock()
[Path("package1") / "dir1", Path("usr") / "dir2"], type(package).depends = PropertyMock(return_value=["depends"])
[Path("package1") / "file1", Path("package1") / "file2"], type(package).opt_depends = PropertyMock(return_value=["opt_depends"])
info_mock = mocker.patch("ahriman.core.alpm.remote.OfficialSyncdb.info", return_value=package)
assert package_archive_ahriman._load_pacman_package(path) == FilesystemPackage(
package_name=package_archive_ahriman.package.base,
depends={"depends"},
opt_depends={"opt_depends"},
)
info_mock.assert_called_once_with(package_archive_ahriman.package.base, pacman=package_archive_ahriman.pacman)
def test_load_pacman_package_exception(package_archive_ahriman: PackageArchive, mocker: MockerFixture) -> None:
"""
must return empty package if no package found
"""
directory = f"{package_archive_ahriman.package.base}-{package_archive_ahriman.package.version}"
path = Path("/") / "var" / "lib" / "pacman" / "local" / directory / "files"
mocker.patch("ahriman.core.alpm.remote.OfficialSyncdb.info",
side_effect=UnknownPackageError(package_archive_ahriman.package.base))
assert package_archive_ahriman._load_pacman_package(path) == FilesystemPackage(
package_name=package_archive_ahriman.package.base,
depends=set(),
opt_depends=set(),
)
def test_raw_dependencies_packages(package_archive_ahriman: PackageArchive, mocker: MockerFixture) -> None:
"""
must correctly extract raw dependencies list
"""
packages = {
package_archive_ahriman.package.base: FilesystemPackage(
package_name=package_archive_ahriman.package.base,
depends=set(),
opt_depends=set(),
directories=[Path("usr") / "dir2"],
files=[Path("file1")],
), ),
"package2": ( "package1": FilesystemPackage(
[Path("usr") / "dir2", Path("package2") / "dir3", Path("package2") / "dir4"], package_name="package1",
[Path("package2") / "file4", Path("package2") / "file3"], depends=set(),
opt_depends=set(),
directories=[Path("package1") / "dir1", Path("usr") / "dir2"],
files=[Path("package1") / "file1", Path("package1") / "file2"],
), ),
}) "package2": FilesystemPackage(
package_name="package2",
depends=set(),
opt_depends=set(),
directories=[Path("usr") / "dir2", Path("package2") / "dir3", Path("package2") / "dir4"],
files=[Path("package2") / "file4", Path("package2") / "file3"],
),
}
mocker.patch("ahriman.models.package_archive.PackageArchive.installed_packages", return_value=packages)
mocker.patch("ahriman.models.package_archive.PackageArchive.depends_on_paths", return_value=( mocker.patch("ahriman.models.package_archive.PackageArchive.depends_on_paths", return_value=(
{"file1", "file3"}, {"file1", "file3"},
{Path("usr") / "dir2", Path("dir3"), Path("package2") / "dir4"}, {Path("usr") / "dir2", Path("dir3"), Path("package2") / "dir4"},
)) ))
result = package_archive_ahriman._raw_dependencies_packages()
assert result == {
Path("package1") / "file1": [packages["package1"]],
Path("package2") / "file3": [packages["package2"]],
Path("package2") / "dir4": [packages["package2"]],
Path("usr") / "dir2": [packages["package1"], packages["package2"]],
}
def test_refine_dependencies(package_archive_ahriman: PackageArchive, mocker: MockerFixture) -> None:
"""
must correctly refine dependencies list
"""
base_package = MagicMock()
type(base_package).depends = PropertyMock(return_value=["base"])
info_mock = mocker.patch("ahriman.core.alpm.remote.OfficialSyncdb.info", return_value=base_package)
path1 = Path("usr") / "lib" / "python3.12"
path2 = path1 / "site-packages"
path3 = Path("etc")
path4 = Path("var") / "lib" / "whatever"
package1 = FilesystemPackage(package_name="package1", depends={"package5"}, opt_depends={"package2"})
package2 = FilesystemPackage(package_name="package2", depends={"package1"}, opt_depends=set())
package3 = FilesystemPackage(package_name="package3", depends=set(), opt_depends={"package1"})
package4 = FilesystemPackage(package_name="base", depends=set(), opt_depends=set())
package5 = FilesystemPackage(package_name="package5", depends={"package1"}, opt_depends=set())
package6 = FilesystemPackage(package_name="package6", depends=set(), opt_depends=set())
assert package_archive_ahriman._refine_dependencies({
path1: [package1, package2, package3, package5, package6],
path2: [package1, package2, package3, package5],
path3: [package1, package4],
path4: [package1],
}) == {
path1: [package6],
path2: [package1, package5],
path4: [package1],
}
info_mock.assert_called_once_with("base", pacman=package_archive_ahriman.pacman)
def test_depends_on(package_archive_ahriman: PackageArchive, mocker: MockerFixture) -> None:
"""
must extract packages and files which are dependencies for the package
"""
raw_mock = mocker.patch("ahriman.models.package_archive.PackageArchive._raw_dependencies_packages",
return_value="1")
refined_mock = mocker.patch("ahriman.models.package_archive.PackageArchive._refine_dependencies", return_value={
Path("package1") / "file1": [FilesystemPackage(package_name="package1", depends=set(), opt_depends=set())],
Path("package2") / "file3": [FilesystemPackage(package_name="package2", depends=set(), opt_depends=set())],
Path("package2") / "dir4": [FilesystemPackage(package_name="package2", depends=set(), opt_depends=set())],
Path("usr") / "dir2": [
FilesystemPackage(package_name="package1", depends=set(), opt_depends=set()),
FilesystemPackage(package_name="package2", depends=set(), opt_depends=set()),
],
})
result = package_archive_ahriman.depends_on() result = package_archive_ahriman.depends_on()
raw_mock.assert_called_once_with()
refined_mock.assert_called_once_with("1")
assert result.paths == { assert result.paths == {
"package1/file1": ["package1"], "package1/file1": ["package1"],
"package2/file3": ["package2"], "package2/file3": ["package2"],
@ -106,7 +217,7 @@ def test_installed_packages(package_archive_ahriman: PackageArchive, mocker: Moc
result = package_archive_ahriman.installed_packages() result = package_archive_ahriman.installed_packages()
assert result assert result
assert Path("usr") in result[package_archive_ahriman.package.base][0] assert Path("usr") in result[package_archive_ahriman.package.base].directories
assert Path("usr/bin/ahriman") in result[package_archive_ahriman.package.base][1] assert Path("usr/bin/ahriman") in result[package_archive_ahriman.package.base].files
walk_mock.assert_called_once_with(package_archive_ahriman.root / "var" / "lib" / "pacman" / "local") walk_mock.assert_called_once_with(package_archive_ahriman.root / "var" / "lib" / "pacman" / "local")
read_mock.assert_called_once_with(encoding="utf8") read_mock.assert_called_once_with(encoding="utf8")