-
-
Notifications
You must be signed in to change notification settings - Fork 720
Expand file tree
/
Copy pathtest_output_templated.py
More file actions
276 lines (229 loc) · 10 KB
/
test_output_templated.py
File metadata and controls
276 lines (229 loc) · 10 KB
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
# -*- 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
from commoncode import fileutils
from commoncode.resource import VirtualCodebase
from commoncode.testcase import FileDrivenTesting
from formattedcode.output_html import HtmlOutput
from scancode.cli_test_utils import run_scan_click
from scancode_config import __version__
from scancode_config import REGEN_TEST_FIXTURES
test_env = FileDrivenTesting()
test_env.test_data_dir = os.path.join(os.path.dirname(__file__), 'data')
@pytest.mark.scanslow
def test_has_licenses_in_html_format_output():
test_file = test_env.get_test_loc('templated/simple-license.txt')
result_file = test_env.get_temp_file('html')
run_scan_click(['--license', test_file, '--html', result_file])
results = open(result_file).read()
assert 'gpl-2.0' in results
assert __version__ in results
@pytest.mark.scanslow
def test_has_license_references_in_html_format_output():
test_file = test_env.get_test_loc('templated/simple-license.txt')
result_file = test_env.get_temp_file('html')
run_scan_click(['--license', '--license-references', test_file, '--html', result_file])
results = open(result_file).read()
assert 'http://www.gnu.org/licenses/gpl-2.0.html' in results
assert __version__ in results
@pytest.mark.scanslow
def test_paths_are_posix_paths_in_html_app_format_output():
test_dir = test_env.get_test_loc('templated/simple')
result_file = test_env.get_temp_file(extension='html', file_name='test_html')
run_scan_click(['--copyright', test_dir, '--html-app', result_file])
# the data we want to test is in the data.js file
data_file = os.path.join(fileutils.parent_directory(result_file), 'test_html_files', 'data.js')
with io.open(data_file, encoding='utf-8') as res:
results = res.read()
assert '/copyright_acme_c-c.c' in results
results = open(result_file).read()
assert __version__ in results
@pytest.mark.scanslow
def test_paths_are_posix_in_html_format_output():
test_dir = test_env.get_test_loc('templated/simple')
result_file = test_env.get_temp_file('html')
run_scan_click(['--copyright', test_dir, '--html', result_file])
results = open(result_file).read()
assert '/copyright_acme_c-c.c' in results
assert __version__ in results
@pytest.mark.scanslow
def test_html_output_includes_license_text():
test_file = test_env.get_test_loc('templated/simple-license.txt')
result_file = test_env.get_temp_file('html')
run_scan_click(['--license', '--license-text', test_file, '--html', result_file])
results = open(result_file).read()
assert 'matched text' in results.lower()
assert '<pre>' in results # Check for formatted text display
assert __version__ in results
@pytest.mark.scanslow
def test_html_output_includes_license_text_diagnostics():
test_file = test_env.get_test_loc('templated/simple-license.txt')
result_file = test_env.get_temp_file('html')
run_scan_click(['--license', '--license-text', '--license-text-diagnostics',
test_file, '--html', result_file])
results = open(result_file).read()
assert 'diagnostics' in results.lower()
assert __version__ in results
@pytest.mark.scanslow
def test_html_output_includes_license_clarity_score():
test_dir = test_env.get_test_loc('templated/simple')
result_file = test_env.get_temp_file('html')
run_scan_click(['--license', '--classify', '--license-clarity-score',
test_dir, '--html', result_file])
results = open(result_file).read()
assert 'License Clarity Score' in results
assert 'score' in results.lower()
assert __version__ in results
@pytest.mark.scanslow
def test_html_output_includes_summary():
test_dir = test_env.get_test_loc('templated/simple')
result_file = test_env.get_temp_file('html')
run_scan_click(['--license', '--classify', '--summary',
test_dir, '--html', result_file])
results = open(result_file).read()
assert 'Scan Summary' in results
assert __version__ in results
@pytest.mark.scanslow
def test_scanned_path_is_present_in_html_app_output():
test_dir = test_env.get_test_loc('templated/html_app')
result_file = test_env.get_temp_file('test.html')
run_scan_click(['--copyright', '--html-app', result_file, test_dir])
results = open(result_file).read()
assert '<title>ScanCode scan results for: %(test_dir)s</title>' % locals() in results
assert '<div class="row" id = "scan-result-header">' % locals() in results
assert '<strong>scan results for:</strong>' % locals() in results
assert '<p>%(test_dir)s</p>' % locals() in results
assert __version__ in results
@pytest.mark.scanslow
def test_scan_html_output_does_not_truncate_copyright_html():
test_dir = test_env.get_test_loc('templated/tree/scan/')
result_file = test_env.get_temp_file('test.html')
args = ['-clip', '--strip-root', '--verbose', test_dir, '--html', result_file, '--verbose']
run_scan_click(args)
results = open(result_file).read()
assert __version__ in results
expected_template = r'''
<tr>
<td>%s</td>
<td>1</td>
<td>1</td>
<td>copyright</td>
<td>Copyright \(c\) 2000 ACME, Inc\.</td>
<td></td>
</tr>
'''
expected_files = r'''
copy1.c
copy2.c
subdir/copy1.c
subdir/copy4.c
subdir/copy2.c
subdir/copy3.c
copy3.c
'''.split()
# for this test we build regexes from a template so we can abstract
# whitespaces
for scanned_file in expected_files:
exp = expected_template % (scanned_file,)
exp = r'\s*'.join(exp.split())
check = re.findall(exp, results, re.MULTILINE) # NOQA
assert check
def test_custom_format_with_custom_filename_fails_for_directory():
test_dir = test_env.get_temp_dir('html')
result_file = test_env.get_temp_file('html')
args = ['--info', '--custom-template', test_dir, '--custom-output', result_file, test_dir]
result = run_scan_click(args, expected_rc=2)
assert 'Invalid value for "--custom-template":' in normalize_quotes(result.output)
def normalize_quotes(s):
return s.replace("'", '"')
def assert_results_equal_expected_file(result_file, expected_file, regen=REGEN_TEST_FIXTURES):
with open(result_file) as rf:
results = rf.read()
if regen:
with open(expected_file, 'w') as of:
of.write(results)
expected = results
else:
with open(expected_file) as ef:
expected = ef.read()
assert results == expected
@pytest.mark.scanslow
def test_scan_custom_html_output_for_a_directory():
test_dir = test_env.get_test_loc('templated/tree/scan/')
custom_template = test_env.get_test_loc('templated/sample-template.html')
expected_file = test_env.get_test_loc('templated/tree/expected.html')
result_file = test_env.get_temp_file('html')
args = ['-clip', '--strip-root', '--custom-template', custom_template, '--custom-output', result_file, test_dir]
run_scan_click(args)
assert_results_equal_expected_file(result_file, expected_file, regen=REGEN_TEST_FIXTURES)
@pytest.mark.scanslow
def test_custom_format_with_custom_filename():
test_dir = test_env.get_test_loc('templated/simple')
custom_template = test_env.get_test_loc('templated/sample-template.html')
expected_file = test_env.get_test_loc('templated/simple-expected.html')
result_file = test_env.get_temp_file('html')
args = ['-clip', '--custom-template', custom_template, '--custom-output', result_file, test_dir]
run_scan_click(args)
assert_results_equal_expected_file(result_file, expected_file, regen=REGEN_TEST_FIXTURES)
@pytest.mark.scanslow
def test_HtmlOutput_process_codebase_does_not_fail_with_non_ascii_scanned_paths_and_file_opened_in_text_mode_with_utf():
test_scan = '''{
"scancode_notice": "Generated with ScanCode...",
"scancode_version": "2.9.7.post137.2e29fe3.dirty.20181120225811",
"scancode_options": {
"input": "han/",
"--json-pp": "-"
},
"scan_start": "2018-11-23T123252.191917",
"files_count": 1,
"files": [
{
"path": "han",
"type": "directory",
"scan_errors": []
},
{
"path": "han/\u636e.svg",
"type": "file",
"scan_errors": []
}
]
}'''
codebase = VirtualCodebase(test_scan)
result_file = test_env.get_temp_file('html')
ho = HtmlOutput()
with io.open(result_file, 'w', encoding='utf-8') as html:
ho.process_codebase(codebase, html)
results = io.open(result_file, encoding='utf-8').read()
assert '<td>han/据.svg</td>' in results
def test_html_output_can_handle_non_ascii_paths():
test_file = test_env.get_test_loc('templated/unicode.json')
result_file = test_env.get_temp_file(extension='html', file_name='test_html')
run_scan_click(['--from-json', test_file, '--html', result_file])
with io.open(result_file, encoding='utf-8') as res:
results = res.read()
assert '<td>han/据.svg</td>' in results
@pytest.mark.scanslow
def test_custom_html_output_can_handle_non_ascii_paths():
test_file = test_env.get_test_loc('templated/unicode.json')
result_file = test_env.get_temp_file(extension='html', file_name='test_html')
custom_template = test_env.get_test_loc('templated/sample-template.html')
args = [
'--from-json', test_file,
'--custom-template', custom_template,
'--custom-output', result_file
]
run_scan_click(args)
with io.open(result_file, encoding='utf-8') as res:
results = res.read()
assert '<td>han/据.svg</td>' in results