Skip to content

Commit

Permalink
add unit test for part_has_slices function
Browse files Browse the repository at this point in the history
  • Loading branch information
linostar committed Feb 6, 2024
1 parent 0d1cb7c commit c71971d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
3 changes: 2 additions & 1 deletion craft_parts/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__ = [
Expand All @@ -46,4 +46,5 @@
"expand_environment",
"validate_part",
"part_has_overlay",
"part_has_slices",
]
17 changes: 9 additions & 8 deletions craft_parts/parts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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.
Expand Down
12 changes: 12 additions & 0 deletions tests/unit/test_parts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit c71971d

Please sign in to comment.