1919from json import load as json_load
2020from unittest import TestCase
2121
22- from ddt import data , ddt , idata , unpack
22+ from ddt import ddt , idata , unpack
2323
2424from cyclonedx import spdx
2525from cyclonedx .schema ._res import SPDX_JSON
2626
2727# rework access
2828with open (SPDX_JSON ) as spdx_schema :
29- KNOWN_SPDX_IDS = json_load (spdx_schema )['enum' ]
29+ KNOWN_SPDX_IDS = set (json_load (spdx_schema )['enum' ])
30+
31+ # for valid test data see the spec: https://spdx.github.io/spdx-spec/v3.0.1/annexes/spdx-license-expressions/
32+ VALID_EXPRESSIONS = {
33+ # region Simple license expressions
34+ 'CDDL-1.0' ,
35+ # 'CDDL-1.0+', # not supported yet - https://github.com/aboutcode-org/license-expression/issues/110
36+ # 'LicenseRef-23', # not supported yet - https://github.com/aboutcode-org/license-expression/issues/109
37+ # 'LicenseRef-MIT-Style-1', # not supported yet - https://github.com/aboutcode-org/license-expression/issues/109
38+ # 'DocumentRef-spdx-tool-1.2:LicenseRef-MIT-Style-2', # not supported yet - https://github.com/aboutcode-org/license-expression/issues/109
39+ # endregion Simple license expressions
40+ # region Composite license expressions
41+ 'LGPL-2.1-only OR MIT' ,
42+ 'MIT or LGPL-2.1-only' ,
43+ '(MIT OR LGPL-2.1-only)' ,
44+ 'LGPL-2.1-only OR MIT OR BSD-3-Clause' ,
45+ 'LGPL-2.1-only AND MIT' ,
46+ 'MIT AND LGPL-2.1-only' ,
47+ 'MIT and LGPL-2.1-only' ,
48+ '(MIT AND LGPL-2.1-only)' ,
49+ 'LGPL-2.1-only AND MIT AND BSD-2-Clause' ,
50+ 'GPL-2.0-or-later WITH Bison-exception-2.2' ,
51+ 'LGPL-2.1-only OR BSD-3-Clause AND MIT' ,
52+ 'MIT AND (LGPL-2.1-or-later OR BSD-3-Clause)' ,
53+ # endregion Composite license expressions
54+ # region examples from CDX spec
55+ 'Apache-2.0 AND (MIT OR GPL-2.0-only)' ,
56+ 'GPL-3.0-only WITH Classpath-exception-2.0' ,
57+ # endregion examples from CDX spec
58+ }
59+
60+ INVALID_EXPRESSIONS = {
61+ 'MIT AND Apache-2.0 OR something-unknown'
62+ 'something invalid' ,
63+ '(c) John Doe' ,
64+ 'Apache License, Version 2.0' ,
65+ }
3066
31- VALID_COMPOUND_EXPRESSIONS = {
32- # for valid test data see the spec: https://spdx.github.io/spdx-spec/v2.3/SPDX-license-expressions/
33- '(MIT AND Apache-2.0)' ,
34- 'BSD-2-Clause OR Apache-2.0' ,
67+ UNKNOWN_SPDX_IDS = {
68+ '' ,
69+ 'something unsupported' , 'something unfixable' ,
70+ 'Apache 2.0' ,
71+ 'LicenseRef-custom-identifier' ,
72+ * (VALID_EXPRESSIONS - KNOWN_SPDX_IDS ),
73+ * INVALID_EXPRESSIONS ,
3574}
3675
3776
@@ -43,12 +82,11 @@ def test_positive(self, supported_value: str) -> None:
4382 actual = spdx .is_supported_id (supported_value )
4483 self .assertTrue (actual )
4584
46- @data (
47- 'something unsupported' ,
48- # somehow case-twisted values
49- 'MiT' ,
50- 'mit' ,
51- )
85+ @idata (chain (UNKNOWN_SPDX_IDS , (
86+ # region somehow case-twisted values
87+ 'MiT' , 'mit' ,
88+ # endregion somehow case-twisted values
89+ )))
5290 def test_negative (self , unsupported_value : str ) -> None :
5391 actual = spdx .is_supported_id (unsupported_value )
5492 self .assertFalse (actual )
@@ -60,37 +98,31 @@ class TestSpdxFixup(TestCase):
6098 @idata (chain (
6199 # original value
62100 ((v , v ) for v in KNOWN_SPDX_IDS ),
63- # somehow case-twisted values
101+ # region somehow case-twisted values
64102 ((v .lower (), v ) for v in KNOWN_SPDX_IDS ),
65103 ((v .upper (), v ) for v in KNOWN_SPDX_IDS )
104+ # endregion somehow case-twisted values
66105 ))
67106 @unpack
68107 def test_positive (self , fixable : str , expected_fixed : str ) -> None :
69108 actual = spdx .fixup_id (fixable )
70109 self .assertEqual (expected_fixed , actual )
71110
72- @data (
73- 'something unfixable' ,
74- )
111+ @idata (UNKNOWN_SPDX_IDS )
75112 def test_negative (self , unfixable : str ) -> None :
76113 actual = spdx .fixup_id (unfixable )
77114 self .assertIsNone (actual )
78115
79116
80117@ddt
81- class TestSpdxIsCompoundExpression (TestCase ):
118+ class TestSpdxIsExpression (TestCase ):
82119
83- @idata (VALID_COMPOUND_EXPRESSIONS )
120+ @idata (VALID_EXPRESSIONS )
84121 def test_positive (self , valid_expression : str ) -> None :
85122 actual = spdx .is_expression (valid_expression )
86123 self .assertTrue (actual )
87124
88- @data (
89- 'MIT AND Apache-2.0 OR something-unknown'
90- 'something invalid' ,
91- '(c) John Doe' ,
92- 'Apache License, Version 2.0'
93- )
125+ @idata (INVALID_EXPRESSIONS )
94126 def test_negative (self , invalid_expression : str ) -> None :
95127 actual = spdx .is_expression (invalid_expression )
96128 self .assertFalse (actual )
0 commit comments