|
| 1 | +# |
| 2 | +# Copyright (c) 2017 nexB Inc. and others. All rights reserved. |
| 3 | +# http://nexb.com and https://github.com/nexB/scancode-toolkit/ |
| 4 | +# The ScanCode software is licensed under the Apache License version 2.0. |
| 5 | +# Data generated with ScanCode require an acknowledgment. |
| 6 | +# ScanCode is a trademark of nexB Inc. |
| 7 | +# |
| 8 | +# You may not use this software except in compliance with the License. |
| 9 | +# You may obtain a copy of the License at: http://apache.org/licenses/LICENSE-2.0 |
| 10 | +# Unless required by applicable law or agreed to in writing, software distributed |
| 11 | +# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 12 | +# CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 13 | +# specific language governing permissions and limitations under the License. |
| 14 | +# |
| 15 | +# When you publish or redistribute any data created with ScanCode or any ScanCode |
| 16 | +# derivative work, you must accompany this data with the following acknowledgment: |
| 17 | +# |
| 18 | +# Generated with ScanCode and provided on an "AS IS" BASIS, WITHOUT WARRANTIES |
| 19 | +# OR CONDITIONS OF ANY KIND, either express or implied. No content created from |
| 20 | +# ScanCode should be considered or used as legal advice. Consult an Attorney |
| 21 | +# for any legal advice. |
| 22 | +# ScanCode is a free software code scanning tool from nexB Inc. and others. |
| 23 | +# Visit https://github.com/nexB/scancode-toolkit/ for support and download. |
| 24 | + |
| 25 | +from __future__ import absolute_import |
| 26 | +from __future__ import print_function |
| 27 | +from __future__ import division |
| 28 | +from __future__ import unicode_literals |
| 29 | + |
| 30 | +import codecs |
| 31 | +from collections import OrderedDict |
| 32 | +import json |
| 33 | +import os |
| 34 | + |
| 35 | +from commoncode.testcase import FileDrivenTesting |
| 36 | +from scancode.cli_test_utils import run_scan_click |
| 37 | + |
| 38 | + |
| 39 | +test_env = FileDrivenTesting() |
| 40 | +test_env.test_data_dir = os.path.join(os.path.dirname(__file__), 'data') |
| 41 | + |
| 42 | + |
| 43 | +def remove_variable_data(scan_result): |
| 44 | + """ |
| 45 | + Remove variable fields from scan, such as date, version to ensure that the |
| 46 | + test data is stable. |
| 47 | + """ |
| 48 | + for line in scan_result: |
| 49 | + header = line.get('header') |
| 50 | + if header: |
| 51 | + del header['scancode_version'] |
| 52 | + for scanned_file in line.get('files', []): |
| 53 | + if 'date' in scanned_file: |
| 54 | + del scanned_file['date'] |
| 55 | + |
| 56 | + |
| 57 | +def check_jsonlines_scan(expected_file, result_file, regen=False): |
| 58 | + """ |
| 59 | + Check the scan result_file JSON Lines results against the expected_file |
| 60 | + expected JSON results, which is a list of mappings, one per line. If regen |
| 61 | + is True the expected_file WILL BE overwritten with the results. This is |
| 62 | + convenient for updating tests expectations. But use with caution. |
| 63 | + """ |
| 64 | + result = _load_jsonlines_result(result_file) |
| 65 | + remove_variable_data(result) |
| 66 | + |
| 67 | + if regen: |
| 68 | + with open(expected_file, 'wb') as reg: |
| 69 | + json.dump(result, reg) |
| 70 | + |
| 71 | + expected = _load_json_result(expected_file) |
| 72 | + remove_variable_data(expected) |
| 73 | + |
| 74 | + assert expected == result |
| 75 | + |
| 76 | + |
| 77 | +def _load_jsonlines_result(result_file): |
| 78 | + """ |
| 79 | + Load the result file as utf-8 JSON Lines |
| 80 | + """ |
| 81 | + with codecs.open(result_file, encoding='utf-8') as res: |
| 82 | + return [json.loads(line, object_pairs_hook=OrderedDict) for line in res] |
| 83 | + |
| 84 | + |
| 85 | +def _load_json_result(result_file): |
| 86 | + """ |
| 87 | + Load the result file as utf-8 JSON |
| 88 | + """ |
| 89 | + with codecs.open(result_file, encoding='utf-8') as res: |
| 90 | + return json.load(res, object_pairs_hook=OrderedDict) |
| 91 | + |
| 92 | + |
| 93 | +def test_jsonlines(): |
| 94 | + test_dir = test_env.get_test_loc('json/simple') |
| 95 | + result_file = test_env.get_temp_file('jsonline') |
| 96 | + |
| 97 | + result = run_scan_click(['-i', '--format', 'jsonlines', test_dir, result_file]) |
| 98 | + assert result.exit_code == 0 |
| 99 | + assert 'Scanning done' in result.output |
| 100 | + |
| 101 | + expected = test_env.get_test_loc('json/simple-expected.jsonlines') |
| 102 | + check_jsonlines_scan(test_env.get_test_loc(expected), result_file, regen=False) |
0 commit comments