Skip to content

Commit 077adc0

Browse files
committed
chore: add empty list check in the get_common_purl_from_artifact_purls function
Signed-off-by: Nathan Nguyen <[email protected]>
1 parent 96da6a9 commit 077adc0

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

src/macaron/vsa/vsa.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,9 @@ def get_common_purl_from_artifact_purls(purl_strs: Iterable[str]) -> str | None:
156156
except ValueError:
157157
return None
158158

159+
if len(purls) == 0:
160+
return None
161+
159162
purl_type = purls[0].type
160163
namespace = purls[0].namespace
161164
name = purls[0].name

tests/vsa/test_vsa.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import pytest
88

9-
from macaron.vsa.vsa import get_components_passing_policy
9+
from macaron.vsa.vsa import get_common_purl_from_artifact_purls, get_components_passing_policy
1010

1111

1212
@pytest.mark.parametrize(
@@ -192,3 +192,39 @@ def test_invalid_subject_verification_result(
192192
) -> None:
193193
"""Test the ``get_components_passing_policy`` in cases where the result should be ``None``."""
194194
assert get_components_passing_policy(policy_result) is None
195+
196+
197+
@pytest.mark.parametrize(
198+
("purl_strs", "expected_purl"),
199+
[
200+
pytest.param(
201+
[
202+
"pkg:maven/com.fasterxml.jackson/[email protected]?type=jar",
203+
"pkg:maven/com.fasterxml.jackson/[email protected]?type=javadoc",
204+
"pkg:maven/com.fasterxml.jackson/[email protected]?type=java-source",
205+
"pkg:maven/com.fasterxml.jackson/[email protected]?type=pom",
206+
],
207+
"pkg:maven/com.fasterxml.jackson/[email protected]",
208+
id="Common PURL exists",
209+
),
210+
pytest.param(
211+
[
212+
"pkg:maven/com.fasterxml.jackson/[email protected]?type=jar",
213+
"pkg:maven/com.fasterxml.jackson/[email protected]?type=jar",
214+
],
215+
None,
216+
id="Common PURL does not exist",
217+
),
218+
pytest.param(
219+
[],
220+
None,
221+
id="Common PURL does not exist",
222+
),
223+
],
224+
)
225+
def test_get_common_purl_from_artifact_purl(
226+
purl_strs: list[str],
227+
expected_purl: str | None,
228+
) -> None:
229+
"""Test the ``get_common_purl_from_artifact_purls`` function."""
230+
assert get_common_purl_from_artifact_purls(purl_strs) == expected_purl

0 commit comments

Comments
 (0)