19
19
from json import load as json_load
20
20
from unittest import TestCase
21
21
22
- from ddt import data , ddt , idata , unpack
22
+ from ddt import ddt , idata , unpack
23
23
24
24
from cyclonedx import spdx
25
25
from cyclonedx .schema ._res import SPDX_JSON
26
26
27
27
# rework access
28
28
with 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
+ # region not supported yet #110 - https://github.com/aboutcode-org/license-expression/issues/110
36
+ # 'CDDL-1.0+',
37
+ # endregion region not supported yet #110
38
+ # region not supported yet #109 - https://github.com/aboutcode-org/license-expression/issues/109
39
+ # 'LicenseRef-23',
40
+ # 'LicenseRef-MIT-Style-1',
41
+ # 'DocumentRef-spdx-tool-1.2:LicenseRef-MIT-Style-2',
42
+ # endregion region not supported yet #109
43
+ # endregion Simple license expressions
44
+ # region Composite license expressions
45
+ 'LGPL-2.1-only OR MIT' ,
46
+ 'MIT or LGPL-2.1-only' ,
47
+ '(MIT OR LGPL-2.1-only)' ,
48
+ 'LGPL-2.1-only OR MIT OR BSD-3-Clause' ,
49
+ 'LGPL-2.1-only AND MIT' ,
50
+ 'MIT AND LGPL-2.1-only' ,
51
+ 'MIT and LGPL-2.1-only' ,
52
+ '(MIT AND LGPL-2.1-only)' ,
53
+ 'LGPL-2.1-only AND MIT AND BSD-2-Clause' ,
54
+ 'GPL-2.0-or-later WITH Bison-exception-2.2' ,
55
+ 'LGPL-2.1-only OR BSD-3-Clause AND MIT' ,
56
+ 'MIT AND (LGPL-2.1-or-later OR BSD-3-Clause)' ,
57
+ # endregion Composite license expressions
58
+ # region examples from CDX spec
59
+ 'Apache-2.0 AND (MIT OR GPL-2.0-only)' ,
60
+ 'GPL-3.0-only WITH Classpath-exception-2.0' ,
61
+ # endregion examples from CDX spec
62
+ }
63
+
64
+ INVALID_EXPRESSIONS = {
65
+ 'MIT AND Apache-2.0 OR something-unknown'
66
+ 'something invalid' ,
67
+ '(c) John Doe' ,
68
+ 'Apache License, Version 2.0' ,
69
+ }
30
70
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' ,
71
+ UNKNOWN_SPDX_IDS = {
72
+ '' ,
73
+ 'something unsupported' , 'something unfixable' ,
74
+ 'Apache 2.0' ,
75
+ 'LicenseRef-custom-identifier' ,
76
+ * (VALID_EXPRESSIONS - KNOWN_SPDX_IDS ),
77
+ * INVALID_EXPRESSIONS ,
35
78
}
36
79
37
80
@@ -43,12 +86,11 @@ def test_positive(self, supported_value: str) -> None:
43
86
actual = spdx .is_supported_id (supported_value )
44
87
self .assertTrue (actual )
45
88
46
- @data (
47
- 'something unsupported' ,
48
- # somehow case-twisted values
49
- 'MiT' ,
50
- 'mit' ,
51
- )
89
+ @idata (chain (UNKNOWN_SPDX_IDS , (
90
+ # region somehow case-twisted values
91
+ 'MiT' , 'mit' ,
92
+ # endregion somehow case-twisted values
93
+ )))
52
94
def test_negative (self , unsupported_value : str ) -> None :
53
95
actual = spdx .is_supported_id (unsupported_value )
54
96
self .assertFalse (actual )
@@ -60,37 +102,31 @@ class TestSpdxFixup(TestCase):
60
102
@idata (chain (
61
103
# original value
62
104
((v , v ) for v in KNOWN_SPDX_IDS ),
63
- # somehow case-twisted values
105
+ # region somehow case-twisted values
64
106
((v .lower (), v ) for v in KNOWN_SPDX_IDS ),
65
107
((v .upper (), v ) for v in KNOWN_SPDX_IDS )
108
+ # endregion somehow case-twisted values
66
109
))
67
110
@unpack
68
111
def test_positive (self , fixable : str , expected_fixed : str ) -> None :
69
112
actual = spdx .fixup_id (fixable )
70
113
self .assertEqual (expected_fixed , actual )
71
114
72
- @data (
73
- 'something unfixable' ,
74
- )
115
+ @idata (UNKNOWN_SPDX_IDS )
75
116
def test_negative (self , unfixable : str ) -> None :
76
117
actual = spdx .fixup_id (unfixable )
77
118
self .assertIsNone (actual )
78
119
79
120
80
121
@ddt
81
- class TestSpdxIsCompoundExpression (TestCase ):
122
+ class TestSpdxIsExpression (TestCase ):
82
123
83
- @idata (VALID_COMPOUND_EXPRESSIONS )
124
+ @idata (VALID_EXPRESSIONS )
84
125
def test_positive (self , valid_expression : str ) -> None :
85
126
actual = spdx .is_expression (valid_expression )
86
127
self .assertTrue (actual )
87
128
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
- )
129
+ @idata (INVALID_EXPRESSIONS )
94
130
def test_negative (self , invalid_expression : str ) -> None :
95
131
actual = spdx .is_expression (invalid_expression )
96
132
self .assertFalse (actual )
0 commit comments