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

Function check_jsonlines_scan

src/scancode/cli_test_utils.py:341–383  ·  view source on GitHub ↗

Check the scan result_file JSON Lines results against the expected_file expected JSON results, which is a list of mappings, one per line. If regen is True the expected_file WILL BE overwritten with the results. This is convenient for updating tests expectations. But use with caution

(
    expected_file,
    result_file,
    regen=False,
    remove_file_date=False,
    check_headers=False,
    remove_uuid=True,
)

Source from the content-addressed store, hash-verified

339
340
341def check_jsonlines_scan(
342 expected_file,
343 result_file,
344 regen=False,
345 remove_file_date=False,
346 check_headers=False,
347 remove_uuid=True,
348):
349 """
350 Check the scan result_file JSON Lines results against the expected_file
351 expected JSON results, which is a list of mappings, one per line. If regen
352 is True the expected_file WILL BE overwritten with the results. This is
353 convenient for updating tests expectations. But use with caution.
354
355 If `remove_file_date` is True, the file.date attribute is removed.
356 """
357 with io.open(result_file, encoding='utf-8') as res:
358 results = [json.loads(line) for line in res]
359
360 if remove_uuid:
361 for result in results:
362 result = remove_uuid_from_scan(result)
363 streamline_jsonlines_scan(results, remove_file_date)
364
365 if regen:
366 with open(expected_file, 'w') as reg:
367 json.dump(results, reg, indent=2, separators=(',', ': '))
368
369 with io.open(expected_file, encoding='utf-8') as res:
370 expected = json.load(res)
371 if remove_uuid:
372 for result in results:
373 result = remove_uuid_from_scan(result)
374
375 streamline_jsonlines_scan(expected, remove_file_date)
376
377 if not check_headers:
378 results[0].pop('headers', None)
379 expected[0].pop('headers', None)
380
381 expected = json.dumps(expected, indent=2, separators=(',', ': '))
382 results = json.dumps(results, indent=2, separators=(',', ': '))
383 assert results == expected
384
385
386def streamline_jsonlines_scan(scan_result, remove_file_date=False):

Calls 8

remove_uuid_from_scanFunction · 0.85
loadsMethod · 0.80
openMethod · 0.45
dumpMethod · 0.45
loadMethod · 0.45
popMethod · 0.45
dumpsMethod · 0.45

Tested by

no test coverage detected