diff --git a/craft_parts/__init__.py b/craft_parts/__init__.py index d0efe2d9..5b902c60 100644 --- a/craft_parts/__init__.py +++ b/craft_parts/__init__.py @@ -26,7 +26,7 @@ from .features import Features from .infos import PartInfo, ProjectInfo, StepInfo from .lifecycle_manager import LifecycleManager -from .parts import Part, part_has_overlay, validate_part +from .parts import Part, part_has_overlay, part_has_slices, validate_part from .steps import Step __all__ = [ @@ -46,4 +46,5 @@ "expand_environment", "validate_part", "part_has_overlay", + "part_has_slices", ] diff --git a/craft_parts/parts.py b/craft_parts/parts.py index 339bd566..66c6f099 100644 --- a/craft_parts/parts.py +++ b/craft_parts/parts.py @@ -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.