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 727e012
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
20 changes: 10 additions & 10 deletions craft_parts/parts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down 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,15 +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 727e012

Please sign in to comment.