From 71d1b19680a5865cf64caac4b07ece28054e7d03 Mon Sep 17 00:00:00 2001 From: Anas Husseini Date: Tue, 6 Feb 2024 11:42:37 +0200 Subject: [PATCH] add unit test for part_has_slices function --- craft_parts/parts.py | 19 ++++++++++--------- tests/unit/test_parts.py | 12 ++++++++++++ 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/craft_parts/parts.py b/craft_parts/parts.py index 339bd566..1090f859 100644 --- a/craft_parts/parts.py +++ b/craft_parts/parts.py @@ -101,7 +101,7 @@ def validate_root(cls, values: Dict[str, Any]) -> Dict[str, Any]: # Detect a mixture of .deb packages and chisel slices. stage_packages = values.get("stage-packages", []) - has_slices = part_has_slices(values) + has_slices = any(name for name in stage_packages if is_slice(name)) has_packages = any(name for name in stage_packages if not is_slice(name)) if has_slices and has_packages: @@ -166,6 +166,15 @@ def has_overlay(self) -> bool: or self.overlay_files != ["*"] ) + @property + def has_slices(self) -> bool: + """Whether the part has slices in its stage-packages. + + :param data: The part data to query. + """ + stage_packages = self.stage_packages or [] + return any(name for name in stage_packages if is_slice(name)) + # pylint: disable=too-many-public-methods class Part: @@ -639,14 +648,6 @@ def part_has_overlay(data: Dict[str, Any]) -> bool: return spec.has_overlay -def part_has_slices(data: Dict[str, Any]) -> bool: - """Whether the part described by ``data`` has slices in its stage-packages. - - :param data: The part data to query. - """ - stage_packages = data.get("stage-packages", []) - return any(name for name in stage_packages if is_slice(name)) - def is_slice(name: str) -> bool: """Whether the stage-package is a slice or a package. diff --git a/tests/unit/test_parts.py b/tests/unit/test_parts.py index 8192bee6..319f7cc3 100644 --- a/tests/unit/test_parts.py +++ b/tests/unit/test_parts.py @@ -339,6 +339,18 @@ def test_part_has_overlay(self, partitions): p = Part("foo", {}, partitions=partitions) assert p.has_overlay is False + @pytest.mark.parametrize( + ("tc_spec", "tc_result"), + [ + ({}, False), + ({"stage-packages": ["libc6_libs"]}, True), + ({"stage-packages": ["python3"]}, False), + ], + ) + def test_part_has_slices(self, partitions, tc_spec, tc_result): + p = Part("foo", tc_spec, partitions=partitions) + assert p.spec.has_slices == tc_result + class TestPartOrdering: """Test part ordering.