|
1 | 1 | import pytest |
| 2 | +from cyclonedx.model.bom import Bom |
2 | 3 | from cyclonedx.model.component import Component |
3 | 4 | from cyclonedx.model.license import DisjunctiveLicense, LicenseExpression |
| 5 | +from cyclonedx.schema import SchemaVersion |
4 | 6 | from cyclonedx_py._internal.utils.cdx import licenses_fixup |
5 | 7 |
|
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 | + |
11 | 24 | licenses_fixup(comp) |
| 25 | + |
12 | 26 | assert comp.licenses[0].value == "MIT" |
13 | 27 | assert comp.evidence is None |
14 | 28 |
|
15 | | -def test_multiple_named_no_change(): |
| 29 | + |
| 30 | +def test_legacy_multiple_named_no_change(): |
16 | 31 | 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")) |
20 | 34 | ) |
| 35 | + attach_schema(comp, SchemaVersion.V1_6) |
| 36 | + |
21 | 37 | 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"} |
24 | 40 | assert comp.evidence is None |
25 | 41 |
|
26 | | -def test_expression_plus_named_moves_named_to_evidence(): |
| 42 | + |
| 43 | +def test_legacy_expression_plus_named_moves_to_evidence(): |
27 | 44 | 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")) |
31 | 47 | ) |
| 48 | + attach_schema(comp, SchemaVersion.V1_6) |
| 49 | + |
32 | 50 | licenses_fixup(comp) |
33 | | - # Check expression stays |
| 51 | + |
34 | 52 | assert comp.licenses[0].value == "MIT" |
35 | | - # Check named moved to evidence |
36 | 53 | 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 | + |
39 | 66 |
|
40 | | -def test_empty_licenses_no_change(): |
| 67 | +# ------------------------------- |
| 68 | +# Modern behavior (>= 1.7) |
| 69 | +# ------------------------------- |
| 70 | + |
| 71 | +def test_modern_no_fixup_mixed_is_untouched(): |
41 | 72 | comp = Component( |
42 | | - name="test-component", |
43 | | - licenses=() |
| 73 | + name="c", |
| 74 | + licenses=(LicenseExpression("MIT"), DisjunctiveLicense(name="Apache-2.0")) |
44 | 75 | ) |
| 76 | + attach_schema(comp, SchemaVersion.V1_7) |
| 77 | + |
45 | 78 | 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" |
47 | 108 | assert comp.evidence is None |
0 commit comments