diff --git a/apport/sandboxutils.py b/apport/sandboxutils.py index 840094689..1d420fba0 100644 --- a/apport/sandboxutils.py +++ b/apport/sandboxutils.py @@ -120,6 +120,17 @@ def needed_runtime_packages(report, pkgmap_cache_dir, pkg_versions, verbose=Fals return [(p, pkg_versions.get(p)) for p in pkgs] +def _move_or_add_base_files_first(pkgs: list[tuple[str, None | str]]) -> None: + """Move base-files to the front or add it if missing.""" + base_files_version = None + for i, (pkg, version) in enumerate(pkgs): + if pkg == "base-files": + base_files_version = version + pkgs.pop(i) + break + pkgs[:0] = [("base-files", base_files_version)] + + # pylint: disable-next=too-many-arguments def make_sandbox( report: apport.Report, @@ -222,6 +233,9 @@ def make_sandbox( if origins: apport.logging.log(f"Origins: {origins}") + # Install base-files first to get correct usrmerge + _move_or_add_base_files_first(pkgs) + # unpack packages, if any, using cache and sandbox try: outdated_msg = packaging.install_packages( diff --git a/tests/unit/test_sandboxutils.py b/tests/unit/test_sandboxutils.py index 791f92249..bdb933613 100644 --- a/tests/unit/test_sandboxutils.py +++ b/tests/unit/test_sandboxutils.py @@ -17,7 +17,7 @@ from apport.packaging import PackageInfo from apport.report import Report -from apport.sandboxutils import make_sandbox +from apport.sandboxutils import _move_or_add_base_files_first, make_sandbox class TestSandboxutils(unittest.TestCase): @@ -71,3 +71,36 @@ def test_make_sandbox_with_sandbox_dir(self, packaging_mock: MagicMock) -> None: self.assertEqual(cache, cache_dir) self.assertEqual(outdated_msg, "obsolete\nobsolete\n") self.assertEqual(packaging_mock.install_packages.call_count, 2) + + def test_move_or_add_base_files_first_existing(self) -> None: + """_move_or_add_base_files_first() with base-files in list.""" + pkgs: list[tuple[str, None | str]] = [ + ("chaos-marmosets", "0.1.2-2"), + ("base-files", "13ubuntu9"), + ("libc6", "2.39-0ubuntu8.2"), + ] + _move_or_add_base_files_first(pkgs) + self.assertEqual( + pkgs, + [ + ("base-files", "13ubuntu9"), + ("chaos-marmosets", "0.1.2-2"), + ("libc6", "2.39-0ubuntu8.2"), + ], + ) + + def test_move_or_add_base_files_first_missing(self) -> None: + """_move_or_add_base_files_first() without base-files in list.""" + pkgs: list[tuple[str, None | str]] = [ + ("chaos-marmosets", "0.1.2-2"), + ("libc6", "2.39-0ubuntu8.2"), + ] + _move_or_add_base_files_first(pkgs) + self.assertEqual( + pkgs, + [ + ("base-files", None), + ("chaos-marmosets", "0.1.2-2"), + ("libc6", "2.39-0ubuntu8.2"), + ], + )