Skip to content

Commit 40455cb

Browse files
authored
Update test_utils.py
Signed-off-by: rohankaf <[email protected]>
1 parent 6a23934 commit 40455cb

File tree

1 file changed

+84
-23
lines changed

1 file changed

+84
-23
lines changed

tests/test_utils.py

Lines changed: 84 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,108 @@
11
import pytest
2+
from cyclonedx.model.bom import Bom
23
from cyclonedx.model.component import Component
34
from cyclonedx.model.license import DisjunctiveLicense, LicenseExpression
5+
from cyclonedx.schema import SchemaVersion
46
from cyclonedx_py._internal.utils.cdx import licenses_fixup
57

6-
def test_single_expression_no_change():
7-
comp = Component(
8-
name="test-component",
9-
licenses=(LicenseExpression("MIT"),)
10-
)
8+
9+
def attach_schema(comp: Component, version: SchemaVersion):
10+
"""Attach a BOM with a specific schema version to a component."""
11+
bom = Bom()
12+
bom.metadata.schema_version = version
13+
comp._bom = bom
14+
15+
16+
# -------------------------------
17+
# Legacy behavior (< 1.7)
18+
# -------------------------------
19+
20+
def test_legacy_single_expression_no_change():
21+
comp = Component(name="c", licenses=(LicenseExpression("MIT"),))
22+
attach_schema(comp, SchemaVersion.V1_6)
23+
1124
licenses_fixup(comp)
25+
1226
assert comp.licenses[0].value == "MIT"
1327
assert comp.evidence is None
1428

15-
def test_multiple_named_no_change():
29+
30+
def test_legacy_multiple_named_no_change():
1631
comp = Component(
17-
name="test-component",
18-
licenses=(DisjunctiveLicense(name="MIT"),
19-
DisjunctiveLicense(name="Apache-2.0"))
32+
name="c",
33+
licenses=(DisjunctiveLicense(name="MIT"), DisjunctiveLicense(name="Apache-2.0"))
2034
)
35+
attach_schema(comp, SchemaVersion.V1_6)
36+
2137
licenses_fixup(comp)
22-
names = {l.name for l in comp.licenses}
23-
assert names == {"MIT", "Apache-2.0"}
38+
39+
assert {l.name for l in comp.licenses} == {"MIT", "Apache-2.0"}
2440
assert comp.evidence is None
2541

26-
def test_expression_plus_named_moves_named_to_evidence():
42+
43+
def test_legacy_expression_plus_named_moves_to_evidence():
2744
comp = Component(
28-
name="test-component",
29-
licenses=(LicenseExpression("MIT"),
30-
DisjunctiveLicense(name="Apache-2.0"))
45+
name="c",
46+
licenses=(LicenseExpression("MIT"), DisjunctiveLicense(name="Apache-2.0"))
3147
)
48+
attach_schema(comp, SchemaVersion.V1_6)
49+
3250
licenses_fixup(comp)
33-
# Check expression stays
51+
3452
assert comp.licenses[0].value == "MIT"
35-
# Check named moved to evidence
3653
assert comp.evidence is not None
37-
moved = {l.name for l in comp.evidence.licenses}
38-
assert moved == {"Apache-2.0"}
54+
assert {l.name for l in comp.evidence.licenses} == {"Apache-2.0"}
55+
56+
57+
def test_legacy_empty_licenses_no_change():
58+
comp = Component(name="c", licenses=())
59+
attach_schema(comp, SchemaVersion.V1_6)
60+
61+
licenses_fixup(comp)
62+
63+
assert tuple(comp.licenses) == ()
64+
assert comp.evidence is None
65+
3966

40-
def test_empty_licenses_no_change():
67+
# -------------------------------
68+
# Modern behavior (>= 1.7)
69+
# -------------------------------
70+
71+
def test_modern_no_fixup_mixed_is_untouched():
4172
comp = Component(
42-
name="test-component",
43-
licenses=()
73+
name="c",
74+
licenses=(LicenseExpression("MIT"), DisjunctiveLicense(name="Apache-2.0"))
4475
)
76+
attach_schema(comp, SchemaVersion.V1_7)
77+
4578
licenses_fixup(comp)
46-
assert tuple(comp.licenses) == ()
79+
80+
# Mixed licenses must not be modified
81+
assert len(comp.licenses) == 2
82+
assert comp.evidence is None
83+
84+
85+
def test_modern_named_only_untouched():
86+
comp = Component(
87+
name="c",
88+
licenses=(DisjunctiveLicense(name="MIT"), DisjunctiveLicense(name="Apache-2.0")),
89+
)
90+
attach_schema(comp, SchemaVersion.V1_7)
91+
92+
licenses_fixup(comp)
93+
94+
assert {l.name for l in comp.licenses} == {"MIT", "Apache-2.0"}
95+
assert comp.evidence is None
96+
97+
98+
def test_modern_expression_only_untouched():
99+
comp = Component(
100+
name="c",
101+
licenses=(LicenseExpression("MIT"),),
102+
)
103+
attach_schema(comp, SchemaVersion.V1_7)
104+
105+
licenses_fixup(comp)
106+
107+
assert comp.licenses[0].value == "MIT"
47108
assert comp.evidence is None

0 commit comments

Comments
 (0)