-
-
Notifications
You must be signed in to change notification settings - Fork 534
/
test_output_spdx.py
363 lines (291 loc) · 13.1 KB
/
test_output_spdx.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# -*- coding: utf-8 -*-
#
# Copyright (c) nexB Inc. and others. All rights reserved.
# ScanCode is a trademark of nexB Inc.
# SPDX-License-Identifier: Apache-2.0
# See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
# See https://github.com/nexB/scancode-toolkit for support or download.
# See https://aboutcode.org for more information about nexB OSS projects.
#
import io
import os
import re
import pytest
import xmltodict
from commoncode.testcase import FileDrivenTesting
from scancode.cli_test_utils import run_scan_click
from scancode.cli_test_utils import run_scan_plain
from scancode_config import REGEN_TEST_FIXTURES
test_env = FileDrivenTesting()
test_env.test_data_dir = os.path.join(os.path.dirname(__file__), 'data')
def strip_variable_text(rdf_text):
"""
Return rdf_text stripped from variable parts such as rdf nodeids
"""
replace_nid = re.compile('rdf:nodeID="[^\\"]*"').sub
rdf_text = replace_nid('', rdf_text)
replace_creation = re.compile('<ns1:creationInfo>.*</ns1:creationInfo>', re.DOTALL).sub # NOQA
rdf_text = replace_creation('', rdf_text)
replace_pcc = re.compile('<ns1:packageVerificationCode>.*</ns1:packageVerificationCode>', re.DOTALL).sub # NOQA
rdf_text = replace_pcc('', rdf_text)
return rdf_text
def load_and_clean_rdf(location):
"""
Return plain Python nested data for the SPDX RDF file at location
suitable for comparison. The file content is cleaned from variable
parts such as dates, generated UUIDs and versions
NOTE: we use plain dicts to avoid ordering issues in XML. the SPDX
tool and lxml do not seem to return a consistent ordering that is
needed for tests.
"""
with io.open(location, encoding='utf-8') as co:
content = co.read()
content = strip_variable_text(content)
data = xmltodict.parse(content, dict_constructor=dict)
return sort_nested(data)
def sort_nested(data):
"""
Return a new ordered and sorted mapping or sequence from a `data` mapping or
sequence with any nested sequences or mappings sorted recursively.
"""
seqtypes = list, tuple
maptypes = dict, dict
coltypes = seqtypes + maptypes
if isinstance(data, maptypes):
new_data = []
for k, v in data.items():
if isinstance(v, coltypes):
v = sort_nested(v)
new_data.append((k, v))
return dict(sorted(new_data, key=_sorter))
elif isinstance(data, seqtypes):
new_data = []
for v in data:
if isinstance(v, coltypes):
v = sort_nested(v)
new_data.append(v)
return sorted(new_data, key=_sorter)
def _sorter(data):
"""
Return a tree of tuples (type, items sequence) for each items in a nested
data structure composed of mappings and sequences. Used as a sorting key.
"""
seqtypes = list, tuple
maptypes = dict, dict
coltypes = seqtypes + maptypes
if isinstance(data, maptypes):
new_data = []
for k, v in data.items():
if isinstance(v, coltypes):
v = _sorter(v)
new_data.append((k, v))
return repr(tuple(sorted(new_data)))
elif isinstance(data, seqtypes):
new_data = []
for v in data:
if isinstance(v, coltypes):
v = _sorter(v)
new_data.append(v)
return repr(tuple(sorted(new_data)))
else:
return repr(data)
def check_rdf_scan(expected_file, result_file, regen=REGEN_TEST_FIXTURES):
"""
Check that expected and result_file are equal.
Both are paths to SPDX RDF XML files, UTF-8 encoded.
"""
import json
result = load_and_clean_rdf(result_file)
if regen:
expected = result
with io.open(expected_file, 'w', encoding='utf-8') as o:
json.dump(result, o, indent=2)
else:
with io.open(expected_file, encoding='utf-8') as i:
expected = json.load(i)
expected = load_and_clean_rdf(result_file)
assert json.dumps(result, indent=2) == json.dumps(expected, indent=2)
def load_and_clean_tv(location):
"""
Return a mapping for the SPDX TV file at location suitable for
comparison. The file content is cleaned from variable parts such as
dates, generated UUIDs and versions
"""
with io.open(location, encoding='utf-8') as co:
content = co.read()
lines = []
lines_append = lines.append
dns = 'DocumentNamespace: http://spdx.org/spdxdocs/'
for line in content.splitlines(False):
line = line.strip()
if not line:
continue
if line.startswith('LicenseListVersion'):
continue
if line.startswith(('Creator: ', 'Created: ',)):
continue
if line.startswith(dns):
# only keep the left side of
# DocumentNamespace: http://spdx.org/spdxdocs/unicode-ea31be0f-16de-4eab-9fc7-4e3bafb753d3
line, _, _ = line.partition('-')
lines_append(line)
return '\n'.join(lines)
def check_tv_scan(expected_file, result_file, regen=REGEN_TEST_FIXTURES):
"""
Check that expected and result_file are equal.
Both are paths to plain spdx tv text files, UTF-8 encoded.
"""
result = load_and_clean_tv(result_file)
if regen:
with io.open(expected_file, 'w', encoding='utf-8') as o:
o.write(result)
expected = load_and_clean_tv(expected_file)
assert result == expected
def test_spdx_rdf_basic():
test_file = test_env.get_test_loc('spdx/simple/test.txt')
result_file = test_env.get_temp_file('rdf')
expected_file = test_env.get_test_loc('spdx/simple/expected.rdf')
run_scan_click([test_file, '-clip', '--spdx-rdf', result_file])
check_rdf_scan(expected_file, result_file)
def test_spdx_tv_basic():
test_dir = test_env.get_test_loc('spdx/simple/test.txt')
result_file = test_env.get_temp_file('tv')
expected_file = test_env.get_test_loc('spdx/simple/expected.tv')
run_scan_click([test_dir, '-clip', '--spdx-tv', result_file])
check_tv_scan(expected_file, result_file)
@pytest.mark.scanslow
def test_spdx_rdf_with_known_licenses():
test_dir = test_env.get_test_loc('spdx/license_known/scan')
result_file = test_env.get_temp_file('rdf')
expected_file = test_env.get_test_loc('spdx/license_known/expected.rdf')
run_scan_click([test_dir, '-clip', '--spdx-rdf', result_file])
check_rdf_scan(expected_file, result_file)
@pytest.mark.scanslow
def test_spdx_rdf_with_license_ref():
test_dir = test_env.get_test_loc('spdx/license_ref/scan')
result_file = test_env.get_temp_file('rdf')
expected_file = test_env.get_test_loc('spdx/license_ref/expected.rdf')
run_scan_click([test_dir, '-clip', '--spdx-rdf', result_file])
check_rdf_scan(expected_file, result_file)
@pytest.mark.scanslow
def test_spdx_tv_with_known_licenses():
test_dir = test_env.get_test_loc('spdx/license_known/scan')
result_file = test_env.get_temp_file('tv')
expected_file = test_env.get_test_loc('spdx/license_known/expected.tv')
run_scan_click([test_dir, '-clip', '--spdx-tv', result_file])
check_tv_scan(expected_file, result_file)
@pytest.mark.scanslow
def test_spdx_tv_with_license_ref():
test_dir = test_env.get_test_loc('spdx/license_ref/scan')
result_file = test_env.get_temp_file('tv')
expected_file = test_env.get_test_loc('spdx/license_ref/expected.tv')
run_scan_click([test_dir, '-clip', '--spdx-tv', result_file])
check_tv_scan(expected_file, result_file)
@pytest.mark.scanslow
def test_spdx_rdf_with_known_licenses_with_text():
test_dir = test_env.get_test_loc('spdx/license_known/scan')
result_file = test_env.get_temp_file('rdf')
expected_file = test_env.get_test_loc('spdx/license_known/expected_with_text.rdf')
run_scan_click([ '-clip', '--license-text', test_dir, '--spdx-rdf', result_file])
check_rdf_scan(expected_file, result_file)
@pytest.mark.scanslow
def test_spdx_rdf_with_license_ref_with_text():
test_dir = test_env.get_test_loc('spdx/license_ref/scan')
result_file = test_env.get_temp_file('rdf')
expected_file = test_env.get_test_loc('spdx/license_ref/expected_with_text.rdf')
run_scan_click(['-clip', '--license-text', test_dir, '--spdx-rdf', result_file])
check_rdf_scan(expected_file, result_file)
@pytest.mark.scanslow
def test_spdx_tv_with_known_licenses_with_text():
test_dir = test_env.get_test_loc('spdx/license_known/scan')
result_file = test_env.get_temp_file('tv')
expected_file = test_env.get_test_loc('spdx/license_known/expected_with_text.tv')
run_scan_click(['-clip', '--license-text', test_dir, '--spdx-tv', result_file])
check_tv_scan(expected_file, result_file)
@pytest.mark.scanslow
def test_spdx_tv_with_license_ref_with_text():
test_dir = test_env.get_test_loc('spdx/license_ref/scan')
result_file = test_env.get_temp_file('tv')
expected_file = test_env.get_test_loc('spdx/license_ref/expected_with_text.tv')
run_scan_click(['-clip', '--license-text', test_dir, '--spdx-tv', result_file])
check_tv_scan(expected_file, result_file)
@pytest.mark.scanslow
def test_spdx_tv_tree():
test_dir = test_env.get_test_loc('spdx/tree/scan')
result_file = test_env.get_temp_file('tv')
expected_file = test_env.get_test_loc('spdx/tree/expected.tv')
run_scan_click(['-clip', test_dir, '--spdx-tv', result_file])
check_tv_scan(expected_file, result_file)
@pytest.mark.scanslow
def test_spdx_rdf_tree():
test_dir = test_env.get_test_loc('spdx/tree/scan')
result_file = test_env.get_temp_file('rdf')
expected_file = test_env.get_test_loc('spdx/tree/expected.rdf')
run_scan_click(['-clip', test_dir, '--spdx-rdf', result_file])
check_rdf_scan(expected_file, result_file)
@pytest.mark.scanslow
def test_spdx_tv_with_unicode_license_text_does_not_fail():
test_file = test_env.get_test_loc('spdx/unicode/et131x.h')
result_file = test_env.get_temp_file('tv')
expected_file = test_env.get_test_loc('spdx/unicode/expected.tv')
args = ['--license', '--copyright', '--info', '--strip-root', '--license-text',
test_file, '--spdx-tv', result_file]
run_scan_plain(args)
check_tv_scan(expected_file, result_file)
@pytest.mark.scanslow
def test_spdx_rdf_with_unicode_license_text_does_not_fail():
test_file = test_env.get_test_loc('spdx/unicode/et131x.h')
result_file = test_env.get_temp_file('rdf')
expected_file = test_env.get_test_loc('spdx/unicode/expected.rdf')
args = ['--license', '--copyright', '--info', '--strip-root',
'--license-text', test_file, '--spdx-rdf', result_file]
run_scan_plain(args)
check_rdf_scan(expected_file, result_file)
@pytest.mark.scanslow
def test_spdx_rdf_with_or_later_license_does_not_fail():
test_file = test_env.get_test_loc('spdx/or_later/test.java')
result_file = test_env.get_temp_file('rdf')
expected_file = test_env.get_test_loc('spdx/or_later/expected.rdf')
args = ['--license', '--copyright', '--info', '--strip-root',
'--license-text', test_file, '--spdx-rdf', result_file]
run_scan_plain(args)
check_rdf_scan(expected_file, result_file)
@pytest.mark.scanslow
def test_spdx_tv_with_empty_scan():
test_file = test_env.get_test_loc('spdx/empty/scan')
result_file = test_env.get_temp_file('spdx.tv')
expected_file = test_env.get_test_loc('spdx/empty/expected.tv')
args = ['--license', '--strip-root', '--info', '--only-findings', test_file, '--spdx-tv', result_file]
run_scan_plain(args)
check_tv_scan(expected_file, result_file, regen=REGEN_TEST_FIXTURES)
@pytest.mark.scanslow
def test_spdx_rdf_with_empty_scan():
test_file = test_env.get_test_loc('spdx/empty/scan')
result_file = test_env.get_temp_file('spdx.rdf')
args = ['--license', '--strip-root', '--info', '--only-findings', test_file, '--spdx-rdf', result_file]
run_scan_plain(args)
expected = "<!-- No results for package 'scan'. -->\n"
results = open(result_file).read()
assert results == expected
@pytest.mark.scanslow
def test_output_spdx_rdf_can_handle_non_ascii_paths():
test_file = test_env.get_test_loc('spdx/unicode.json')
result_file = test_env.get_temp_file(extension='spdx', file_name='test_spdx')
run_scan_click(['--from-json', test_file, '--spdx-rdf', result_file])
with io.open(result_file, encoding='utf-8') as res:
results = res.read()
assert 'han/据.svg' in results
def test_output_spdx_tv_can_handle_non_ascii_paths():
test_file = test_env.get_test_loc('spdx/unicode.json')
result_file = test_env.get_temp_file(extension='spdx', file_name='test_spdx')
run_scan_click(['--from-json', test_file, '--spdx-tv', result_file])
with io.open(result_file, encoding='utf-8') as res:
results = res.read()
assert 'han/据.svg' in results
def test_output_spdx_tv_sh1_of_empty_file():
test_dir = test_env.get_test_loc('spdx/empty/scan/somefile')
result_file = test_env.get_temp_file(extension='spdx', file_name='test_spdx')
run_scan_click([test_dir, '-clip', '--spdx-tv', result_file])
with io.open(result_file, encoding='utf-8') as res:
results = res.read()
assert 'FileChecksum: SHA1: da39a3ee5e6b4b0d3255bfef95601890afd80709' in results