MCPcopy Create free account
hub / github.com/aboutcode-org/scancode-toolkit / make_test

Function make_test

src/licensedcode_test_utils.py:189–269  ·  view source on GitHub ↗

Build and return a test function closing on tests arguments for a license_test LicenseTest object.

(license_test, unknown_detection=False, regen=REGEN_TEST_FIXTURES)

Source from the content-addressed store, hash-verified

187
188
189def make_test(license_test, unknown_detection=False, regen=REGEN_TEST_FIXTURES):
190 """
191 Build and return a test function closing on tests arguments for a
192 license_test LicenseTest object.
193 """
194 test_name = license_test.get_test_method_name()
195
196 from licensedcode import cache
197 from licensedcode.tracing import get_texts
198
199 expected_expressions = license_test.license_expressions or []
200
201 test_file = license_test.test_file
202 test_data_file = license_test.data_file
203 expected_failure = license_test.expected_failure
204
205 def closure_test_function(*args, **kwargs):
206 idx = cache.get_index()
207 matches = idx.match(
208 location=test_file,
209 min_score=0,
210 unknown_licenses=unknown_detection,
211 )
212 if not matches:
213 matches = []
214
215 detected_expressions = [match.rule.license_expression for match in matches]
216
217 # use detection as expected and dump test back
218 if regen:
219 if not expected_failure:
220 license_test.license_expressions = detected_expressions
221 license_test.dump()
222 return
223
224 if detected_expressions != expected_expressions:
225 # On failure, we compare against more result data to get additional
226 # failure details, including the test_file and full match details
227 expected = expected_expressions + ['======================', '']
228 results_failure_trace = (
229 detected_expressions[:]
230 +['======================', '']
231 )
232
233 for match in matches:
234 qtext, itext = get_texts(match)
235 rule_file = match.rule.rule_file()
236 results_failure_trace.extend(['',
237 '======= MATCH ====', repr(match),
238 '======= Matched Query Text for:',
239 f'file://{test_file}'
240 ])
241 if test_data_file:
242 results_failure_trace.append(f'file://{test_data_file}')
243
244 results_failure_trace.append('')
245 results_failure_trace.append(qtext)
246 results_failure_trace.extend(['',

Callers 1

build_testsFunction · 0.70

Calls 1

get_test_method_nameMethod · 0.45

Tested by

no test coverage detected