Skip to content

Commit 76278e9

Browse files
committed
Add tests, don't shadow name
1 parent c6763d6 commit 76278e9

2 files changed

Lines changed: 21 additions & 4 deletions

File tree

sbom.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import tarfile
2525
import typing
2626
import zipfile
27+
from functools import cache
2728
from pathlib import Path
2829
from typing import Any, LiteralString, NotRequired, TypedDict, cast
2930
from urllib.request import urlopen
@@ -98,12 +99,12 @@ class CreationInfo(TypedDict):
9899
@cache
99100
def spdx_id(value: LiteralString) -> str:
100101
"""Encode a value into characters that are valid in an SPDX ID"""
101-
spdx_id = re.sub(r"[^a-zA-Z0-9.\-]+", "-", value)
102+
value_as_spdx_id = re.sub(r"[^a-zA-Z0-9.\-]+", "-", value)
102103
# To avoid collisions we append a hash suffix.
103104
suffix = hashlib.sha256(value.encode()).hexdigest()[:8]
104-
spdx_id = f"{spdx_id}-{suffix}"
105-
assert _SPDX_IDS_TO_VALUES.setdefault(spdx_id, value) == value
106-
return spdx_id
105+
value_as_spdx_id = f"{value_as_spdx_id}-{suffix}"
106+
assert _SPDX_IDS_TO_VALUES.setdefault(value_as_spdx_id, value) == value
107+
return value_as_spdx_id
107108

108109

109110
def calculate_package_verification_codes(sbom: SBOM) -> None:

tests/test_sbom.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,22 @@
1111
import sbom
1212

1313

14+
@pytest.mark.parametrize(
15+
["value", "expected"],
16+
[
17+
("abc", "abc-ba7816bf"),
18+
("def", "def-cb8379ac"),
19+
("SPDXRef-PACKAGE-pip", "SPDXRef-PACKAGE-pip-ced959c1"),
20+
("SPDXRef-PACKAGE-cpython", "SPDXRef-PACKAGE-cpython-79ab18d2"),
21+
("SPDXRef-PACKAGE-urllib3", "SPDXRef-PACKAGE-urllib3-b8ab4751"),
22+
],
23+
)
24+
def test_spdx_id(value: str, expected: str) -> None:
25+
assert sbom.spdx_id(value) == expected
26+
# Check we get the same value next time
27+
assert sbom.spdx_id(value) == expected
28+
29+
1430
@pytest.mark.parametrize(
1531
["package_sha1s", "package_verification_code"],
1632
[

0 commit comments

Comments
 (0)