|
| 1 | +# |
| 2 | +# Copyright (c) nexB Inc. and others. All rights reserved. |
| 3 | +# ScanCode is a trademark of nexB Inc. |
| 4 | +# SPDX-License-Identifier: Apache-2.0 |
| 5 | +# See http://www.apache.org/licenses/LICENSE-2.0 for the license text. |
| 6 | +# See https://github.com/nexB/scancode-toolkit for support or download. |
| 7 | +# See https://aboutcode.org for more information about nexB OSS projects. |
| 8 | +# |
| 9 | + |
| 10 | +import json |
| 11 | +from os.path import dirname |
| 12 | +from os.path import join |
| 13 | + |
| 14 | +from commoncode.hash import binary_chunks |
| 15 | +from commoncode.testcase import FileDrivenTesting |
| 16 | +from scancode import results_cache |
| 17 | + |
| 18 | + |
| 19 | +class TestResultsCache(FileDrivenTesting): |
| 20 | + test_data_dir = join(dirname(__file__), 'data') |
| 21 | + test_results_cache = join(test_data_dir, 'results_cache/results') |
| 22 | + test_results_cache_index = "b0e3dd13b9b5980bb1ca7aab89e5490cb136952374489a755947ac004309b035" |
| 23 | + |
| 24 | + def test_hasher_from_chunks(self): |
| 25 | + test_file_loc = self.get_test_loc('results_cache/package.json') |
| 26 | + chunks = binary_chunks(location=test_file_loc) |
| 27 | + hasher = results_cache.hasher_from_chunks(chunks=chunks) |
| 28 | + expected = "cb6f5a82e473620da4d1aecf82dd4d4fa9ada393a7679b28a42cac86f0a83c92" |
| 29 | + assert hasher.hexdigest() == expected |
| 30 | + |
| 31 | + def test_compute_results_cache_index(self): |
| 32 | + test_file_loc = self.get_test_loc('results_cache/package.json') |
| 33 | + results_cache_index = results_cache.compute_results_cache_index(location=test_file_loc, filename='package.json') |
| 34 | + assert results_cache_index == self.test_results_cache_index |
| 35 | + |
| 36 | + def test_get_results_cache_directory_location(self): |
| 37 | + results_cache_directory_location = results_cache.get_results_cache_directory_location( |
| 38 | + results_cache_index=self.test_results_cache_index, |
| 39 | + results_cache_dir=self.test_results_cache, |
| 40 | + ) |
| 41 | + expected_results_cache_directory_location = join( |
| 42 | + self.test_results_cache, 'b0/e3/dd13b9b5980bb1ca7aab89e5490cb136952374489a755947ac004309b035' |
| 43 | + ) |
| 44 | + assert results_cache_directory_location == expected_results_cache_directory_location |
| 45 | + |
| 46 | + def test_get_results_cache_file_location(self): |
| 47 | + for plugin_name in ['copyrights', 'emails', 'info', 'licenses', 'packages', 'urls']: |
| 48 | + results_cache_file_location = results_cache.get_results_cache_file_location( |
| 49 | + results_cache_index=self.test_results_cache_index, |
| 50 | + plugin_name=plugin_name, |
| 51 | + results_cache_dir=self.test_results_cache, |
| 52 | + ) |
| 53 | + expected_results_cache_file_location = join( |
| 54 | + self.test_results_cache, 'b0/e3/dd13b9b5980bb1ca7aab89e5490cb136952374489a755947ac004309b035', plugin_name |
| 55 | + ) |
| 56 | + assert results_cache_file_location == expected_results_cache_file_location |
| 57 | + |
| 58 | + def test_get_results_cache_data(self): |
| 59 | + for plugin_name in ['copyrights', 'emails', 'info', 'licenses', 'packages', 'urls']: |
| 60 | + results_cache_data = results_cache.get_results_cache_data( |
| 61 | + results_cache_index=self.test_results_cache_index, |
| 62 | + plugin_name=plugin_name, |
| 63 | + results_cache_dir=self.test_results_cache, |
| 64 | + ) |
| 65 | + expected_results_cache_file_location = join( |
| 66 | + self.test_results_cache, 'b0/e3/dd13b9b5980bb1ca7aab89e5490cb136952374489a755947ac004309b035', plugin_name |
| 67 | + ) |
| 68 | + with open(expected_results_cache_file_location) as f: |
| 69 | + expected_results_cache_data = json.load(f) |
| 70 | + assert results_cache_data == expected_results_cache_data |
| 71 | + |
| 72 | + def test_update_results_cache_data(self): |
| 73 | + temp_results_cache_directory_location = self.get_temp_dir() |
| 74 | + test_plugin_name = 'test' |
| 75 | + test_data = { |
| 76 | + 'name': 'asdf', |
| 77 | + 'version': '1.0.0', |
| 78 | + } |
| 79 | + results_cache.update_results_cache_data( |
| 80 | + results_cache_index=self.test_results_cache_index, |
| 81 | + plugin_name=test_plugin_name, |
| 82 | + results=test_data, |
| 83 | + results_cache_dir=temp_results_cache_directory_location, |
| 84 | + ) |
| 85 | + results_cache_file_location = join( |
| 86 | + temp_results_cache_directory_location, |
| 87 | + 'b0/e3/dd13b9b5980bb1ca7aab89e5490cb136952374489a755947ac004309b035', |
| 88 | + test_plugin_name |
| 89 | + ) |
| 90 | + with open(results_cache_file_location) as f: |
| 91 | + results_cache_data = json.load(f) |
| 92 | + assert results_cache_data == test_data |
0 commit comments