Skip to content

Commit 60ee685

Browse files
committed
Create test for results_cache.py
Signed-off-by: Jono Yang <jyang@nexb.com>
1 parent 630074d commit 60ee685

2 files changed

Lines changed: 104 additions & 7 deletions

File tree

src/scancode/results_cache.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def compute_results_cache_index(location, filename):
5858
return sha256_hasher.hexdigest()
5959

6060

61-
def get_results_cache_directory_location(results_cache_index):
61+
def get_results_cache_directory_location(results_cache_index, results_cache_dir=results_cache_dir):
6262
"""
6363
Return the location of the directory containing the cache files for a given
6464
`results_cache_index` hexstring.
@@ -70,24 +70,28 @@ def get_results_cache_directory_location(results_cache_index):
7070
return os.path.join(results_cache_dir, prefix1, prefix2, directory_name)
7171

7272

73-
def get_results_cache_file_location(results_cache_index, plugin_name):
73+
def get_results_cache_file_location(results_cache_index, plugin_name, results_cache_dir=results_cache_dir):
7474
"""
7575
Return the location of the file containing the cached results of the scanner
7676
`plugin_name` for a resource keyed by `results_cache_index` hexstring.
7777
"""
78-
results_cache_directory_location = get_results_cache_directory_location(results_cache_index=results_cache_index)
78+
results_cache_directory_location = get_results_cache_directory_location(
79+
results_cache_index=results_cache_index,
80+
results_cache_dir=results_cache_dir
81+
)
7982
return os.path.join(results_cache_directory_location, plugin_name)
8083

8184

82-
def get_results_cache_data(results_cache_index, plugin_name):
85+
def get_results_cache_data(results_cache_index, plugin_name, results_cache_dir=results_cache_dir):
8386
"""
8487
Return a mapping containing the results of scan plugin, `plugin_name`, for a
8588
resource keyed by `resource_cache_index` hexstring. If the cache file does
8689
not exist, an empty mapping is returned.
8790
"""
8891
results_cache_file_location = get_results_cache_file_location(
8992
results_cache_index=results_cache_index,
90-
plugin_name=plugin_name
93+
plugin_name=plugin_name,
94+
results_cache_dir=results_cache_dir,
9195
)
9296
if os.path.exists(results_cache_file_location):
9397
with open(results_cache_file_location) as f:
@@ -96,13 +100,14 @@ def get_results_cache_data(results_cache_index, plugin_name):
96100
return {}
97101

98102

99-
def update_results_cache_data(results_cache_index, plugin_name, results):
103+
def update_results_cache_data(results_cache_index, plugin_name, results, results_cache_dir=results_cache_dir):
100104
"""
101105
Update the results cache with the `results` of the scanner `plugin_name`
102106
for the resource keyed by `results_cache_index`.
103107
"""
104108
results_cache_directory_location = get_results_cache_directory_location(
105-
results_cache_index=results_cache_index
109+
results_cache_index=results_cache_index,
110+
results_cache_dir=results_cache_dir,
106111
)
107112
if not os.path.exists(results_cache_directory_location):
108113
create_dir(results_cache_directory_location)
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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

Comments
 (0)