Skip to content

Commit 3e70c5b

Browse files
authored
Merge pull request #1423 from nexB/1362-resource-not-loaded-before-removal
Load resource from disk before returning None in get_resource()
2 parents 577f769 + 8fbede6 commit 3e70c5b

3 files changed

Lines changed: 25 additions & 4 deletions

File tree

src/scancode/plugin_ignore.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ def logger_debug(*args):
5656
' '.join(isinstance(a, unicode) and a or repr(a) for a in args))
5757

5858

59-
6059
@pre_scan_impl
6160
class ProcessIgnore(PreScanPlugin):
6261
"""
@@ -125,7 +124,7 @@ def process_codebase(self, codebase, ignore=(), include=(), **kwargs):
125124
logger_debug(codebase.get_resource(rid))
126125

127126
remove_resource = codebase.remove_resource
128-
127+
129128
# Then, walk bottom-up and remove the non-included Resources from the
130129
# Codebase if the Resource's rid is in our list of rid's to remove.
131130
for resource in codebase.walk(topdown=False):

src/scancode/resource.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -632,10 +632,10 @@ def get_resource(self, rid):
632632

633633
if rid == 0:
634634
res = attr.evolve(self.root)
635-
elif not rid or rid not in self.resource_ids:
636-
res = None
637635
elif self._use_disk_cache_for_resource(rid):
638636
res = self._load_resource(rid)
637+
elif not rid or rid not in self.resource_ids:
638+
res = None
639639
else:
640640
res = self.resources.get(rid)
641641
res = attr.evolve(res)

tests/scancode/test_plugin_ignore.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,12 @@ def test_ProcessIgnore_with_multiple_ignores(self):
128128
]
129129
self.check_ProcessIgnore(test_dir, expected, ignore)
130130

131+
def test_ProcessIgnore_process_codebase_does_not_fail_to_access_an_ignored_resourced_cached_to_disk(self):
132+
test_dir = self.extract_test_tar('plugin_ignore/user.tgz')
133+
codebase = Codebase(test_dir, max_in_memory=1)
134+
test_plugin = ProcessIgnore()
135+
ignore = ['test']
136+
test_plugin.process_codebase(codebase, ignore=ignore)
131137

132138

133139
class TestScanPluginIgnoreFiles(FileDrivenTesting):
@@ -235,3 +241,19 @@ def test_scancode_multiple_ignores(self):
235241
assert 0 == scan_result['headers'][0]['extra_data']['files_count']
236242
scan_locs = [x['path'] for x in scan_result['files']]
237243
assert [u'user', u'user/src'] == scan_locs
244+
245+
def test_scancode_codebase_attempt_to_access_an_ignored_resourced_cached_to_disk(self):
246+
test_dir = self.extract_test_tar('plugin_ignore/user.tgz')
247+
result_file = self.get_temp_file('json')
248+
args = ['--copyright', '--strip-root', '--ignore', 'test', test_dir, '--max-in-memory', '1', '--json', result_file]
249+
run_scan_click(args)
250+
scan_result = load_json_result(result_file)
251+
assert 2 == scan_result['headers'][0]['extra_data']['files_count']
252+
scan_locs = [x['path'] for x in scan_result['files']]
253+
expected = [
254+
u'user',
255+
u'user/ignore.doc',
256+
u'user/src',
257+
u'user/src/ignore.doc',
258+
]
259+
assert expected == scan_locs

0 commit comments

Comments
 (0)