Repository navigation
Expand file tree
/
Copy pathtest_summarizer.py
More file actions
284 lines (255 loc) · 10.7 KB
/
Copy pathtest_summarizer.py
File metadata and controls
284 lines (255 loc) · 10.7 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
277
278
279
280
281
282
283
284
#
# 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.
#
from copy import copy
from os import path
import pytest
from commoncode.testcase import FileDrivenTesting
from scancode.cli_test_utils import check_json_scan
from scancode.cli_test_utils import run_scan_click
from scancode_config import REGEN_TEST_FIXTURES
from summarycode.summarizer import remove_from_tallies
from summarycode.summarizer import get_primary_language
from summarycode.summarizer import get_holders_from_copyright
pytestmark = pytest.mark.scanslow
class TestScanSummary(FileDrivenTesting):
test_data_dir = path.join(path.dirname(__file__), 'data')
def test_end2end_summary_and_classify_works_with_empty_dir_and_empty_values(self):
test_dir = self.extract_test_tar('summary/end-2-end/bug-1141.tar.gz')
result_file = self.get_temp_file('json')
expected_file = self.get_test_loc('summary/end-2-end/bug-1141.expected.json')
run_scan_click([
'-clip',
'--summary',
'--classify',
'--json-pp', result_file, test_dir
])
check_json_scan(expected_file, result_file, remove_uuid=True, remove_file_date=True, regen=REGEN_TEST_FIXTURES)
def test_summary_license_ambiguity_unambiguous(self):
test_dir = self.get_test_loc('summary/license_ambiguity/unambiguous')
result_file = self.get_temp_file('json')
expected_file = self.get_test_loc('summary/license_ambiguity/unambiguous.expected.json')
run_scan_click([
'-clip',
'--summary',
'--classify',
'--json-pp', result_file, test_dir
])
check_json_scan(expected_file, result_file, remove_uuid=True, remove_file_date=True, regen=REGEN_TEST_FIXTURES)
def test_summary_license_ambiguity_ambiguous(self):
test_dir = self.get_test_loc('summary/license_ambiguity/ambiguous')
result_file = self.get_temp_file('json')
expected_file = self.get_test_loc('summary/license_ambiguity/ambiguous.expected.json')
run_scan_click([
'-clip',
'--summary',
'--classify',
'--json-pp', result_file, test_dir
])
check_json_scan(expected_file, result_file, remove_uuid=True, remove_file_date=True, regen=REGEN_TEST_FIXTURES)
def test_summary_single_file_with_origin_info(self):
test_dir = self.get_test_loc('summary/single_file/codebase')
result_file = self.get_temp_file('json')
expected_file = self.get_test_loc('summary/single_file/single_file.expected.json')
run_scan_click([
'-clip',
'--summary',
'--classify',
'--json-pp', result_file, test_dir
])
check_json_scan(expected_file, result_file, remove_uuid=True, remove_file_date=True, regen=REGEN_TEST_FIXTURES)
def test_summary_conflicting_license_categories(self):
test_dir = self.get_test_loc('summary/conflicting_license_categories/codebase')
result_file = self.get_temp_file('json')
expected_file = self.get_test_loc('summary/conflicting_license_categories/conflicting_license_categories.expected.json')
run_scan_click([
'-clip',
'--summary',
'--classify',
'--json-pp', result_file, test_dir
])
check_json_scan(expected_file, result_file, remove_uuid=True, remove_file_date=True, regen=REGEN_TEST_FIXTURES)
def test_summary_with_package_data(self):
test_dir = self.get_test_loc('summary/with_package_data/codebase')
result_file = self.get_temp_file('json')
expected_file = self.get_test_loc('summary/with_package_data/with_package_data.expected.json')
run_scan_click([
'-clip',
'--summary',
'--classify',
'--json-pp', result_file, test_dir
])
check_json_scan(expected_file, result_file, remove_uuid=True, remove_file_date=True, regen=REGEN_TEST_FIXTURES)
def test_summary_without_package_data(self):
test_dir = self.get_test_loc('summary/without_package_data/codebase')
result_file = self.get_temp_file('json')
expected_file = self.get_test_loc('summary/without_package_data/without_package_data.expected.json')
run_scan_click([
'-clip',
'--summary',
'--classify',
'--json-pp', result_file, test_dir
])
check_json_scan(expected_file, result_file, remove_uuid=True, remove_file_date=True, regen=REGEN_TEST_FIXTURES)
def test_summary_multiple_package_data(self):
test_dir = self.get_test_loc('summary/multiple_package_data/codebase')
result_file = self.get_temp_file('json')
expected_file = self.get_test_loc('summary/multiple_package_data/multiple_package_data.expected.json')
run_scan_click([
'-clip',
'--summary',
'--classify',
'--json-pp', result_file, test_dir
])
check_json_scan(expected_file, result_file, remove_uuid=True, remove_file_date=True, regen=REGEN_TEST_FIXTURES)
def test_summary_root_package_with_embedded_packages(self):
test_dir = self.get_test_loc('summary/embedded_packages/bunkerweb')
result_file = self.get_temp_file('json')
expected_file = self.get_test_loc('summary/embedded_packages/bunkerweb.expected.json')
run_scan_click([
'-clip',
'--summary',
'--classify',
'--json-pp', result_file, test_dir
])
check_json_scan(expected_file, result_file, remove_uuid=True, remove_file_date=True, regen=REGEN_TEST_FIXTURES)
def test_summary_use_holder_from_package_resource(self):
test_dir = self.get_test_loc('summary/use_holder_from_package_resource/codebase')
result_file = self.get_temp_file('json')
expected_file = self.get_test_loc('summary/use_holder_from_package_resource/use_holder_from_package_resource.expected.json')
run_scan_click([
'-clip',
'--summary',
'--classify',
'--json-pp', result_file, test_dir
])
check_json_scan(expected_file, result_file, remove_uuid=True, remove_file_date=True, regen=REGEN_TEST_FIXTURES)
def test_summary_clear_holder(self):
test_dir = self.get_test_loc('summary/holders/clear_holder')
result_file = self.get_temp_file('json')
expected_file = self.get_test_loc('summary/holders/clear_holder.expected.json')
run_scan_click([
'-clip',
'--summary',
'--classify',
'--json-pp', result_file, test_dir
])
check_json_scan(expected_file, result_file, remove_uuid=True, remove_file_date=True, regen=REGEN_TEST_FIXTURES)
def test_summary_combined_holders(self):
test_dir = self.get_test_loc('summary/holders/combined_holders')
result_file = self.get_temp_file('json')
expected_file = self.get_test_loc('summary/holders/combined_holders.expected.json')
run_scan_click([
'-clip',
'--summary',
'--classify',
'--json-pp', result_file, test_dir
])
check_json_scan(expected_file, result_file, remove_uuid=True, remove_file_date=True, regen=REGEN_TEST_FIXTURES)
def test_summary_without_copyright_or_holders(self):
test_dir = self.get_test_loc('summary/summary_without_holder/pip-22.0.4/')
result_file = self.get_temp_file('json')
expected_file = self.get_test_loc('summary/summary_without_holder/summary-without-holder-pypi.expected.json')
run_scan_click([
'-lp',
'--summary',
'--classify',
'--json-pp', result_file, test_dir
])
check_json_scan(expected_file, result_file, remove_uuid=True, remove_file_date=True, regen=REGEN_TEST_FIXTURES)
def test_remove_from_tallies(self):
tallies = [
{
'value': 'apache-2.0',
'count': 5,
},
{
'value': 'mit',
'count': 2,
},
{
'value': 'gpl-3.0',
'count': 1,
}
]
test_entry_1 = {
'value': 'apache-2.0',
'count': 5,
}
expected_1 = [
{
'value': 'mit',
'count': 2,
},
{
'value': 'gpl-3.0',
'count': 1,
}
]
result_1 = remove_from_tallies(test_entry_1, copy(tallies))
assert result_1 == expected_1
test_entry_2 = [
{
'value': 'mit',
'count': 2,
},
{
'value': 'gpl-3.0',
'count': 1,
}
]
expected_2 = [
{
'value': 'apache-2.0',
'count': 5,
},
]
result_2 = remove_from_tallies(test_entry_2, copy(tallies))
assert result_2 == expected_2
test_entry_3 = 'apache-2.0'
expected_3 = [
{
'value': 'mit',
'count': 2,
},
{
'value': 'gpl-3.0',
'count': 1,
}
]
result_3 = remove_from_tallies(test_entry_3, copy(tallies))
assert result_3 == expected_3
def test_get_primary_language(self):
language_tallies = [
{
'value': 'Python',
'count': 5,
},
{
'value': 'Java',
'count': 2,
},
{
'value': 'C++',
'count': 1,
}
]
expected_1 = 'Python'
result_1 = get_primary_language(language_tallies)
assert result_1 == expected_1
def test_get_holders_from_copyright(self):
test_copyright_string = 'Copyright (c) 2017, The University of Chicago. All rights reserved.'
result = list(get_holders_from_copyright(test_copyright_string))
assert result == ['The University of Chicago']
test_copyright_lines = [
'Copyright (c) 2017, The University of Chicago. All rights reserved.',
'Copyright (c) MIT',
'Copyright (c) Apache Software Foundation',
]
result = list(get_holders_from_copyright(test_copyright_lines))
assert result == ['The University of Chicago', 'MIT', 'Apache Software Foundation']