Skip to content

Commit 8ca60e2

Browse files
committed
Add new VirtualCodebase creation test #1439
* Add test that creates a VirtualCodebase from multiple scans * Check to see if headers exist before attempting to get them when loading scan_data * Raise Exception when importing a scan with no files Signed-off-by: Jono Yang <jyang@nexb.com>
1 parent 60ef115 commit 8ca60e2

4 files changed

Lines changed: 53 additions & 2 deletions

File tree

src/scancode/resource.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1409,8 +1409,14 @@ def _get_scan_data(self, location):
14091409
combined_scan_data = OrderedDict(headers=[], files=[])
14101410
for loc in location:
14111411
scan_data = self._get_scan_data_helper(loc)
1412-
combined_scan_data['headers'].extend(scan_data['headers'])
1413-
combined_scan_data['files'].extend(scan_data['files'])
1412+
headers = scan_data.get('headers')
1413+
if headers:
1414+
combined_scan_data['headers'].extend(headers)
1415+
files = scan_data.get('files')
1416+
if files:
1417+
combined_scan_data['files'].extend(files)
1418+
else:
1419+
raise Exception('Input file does not have Resources to import: {}'.format(loc))
14141420
combined_scan_data['headers'] = sorted(combined_scan_data['headers'], key=lambda x: x['start_timestamp'])
14151421
return combined_scan_data
14161422
return self._get_scan_data_helper(location)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"files_count": 2,
3+
"files": [
4+
{
5+
"path": "samples",
6+
"type": "directory",
7+
"summary": []
8+
},
9+
{
10+
"path": "samples/NOTICE",
11+
"type": "file",
12+
"summary": []
13+
}
14+
]
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"files_count": 2,
3+
"files": [
4+
{
5+
"path": "thirdparty",
6+
"type": "directory",
7+
"summary": []
8+
},
9+
{
10+
"path": "thirdparty/example.zip",
11+
"type": "file",
12+
"summary": []
13+
}
14+
]
15+
}

tests/scancode/test_resource.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1195,3 +1195,18 @@ def test_VirtualCodebase_check_that_already_existing_parent_is_updated_properly(
11951195
])
11961196
]
11971197
assert expected == results
1198+
1199+
def test_VirtualCodebase_create_from_multiple_scans(self):
1200+
test_file_1 = self.get_test_loc('resource/virtual_codebase/combine-1.json')
1201+
test_file_2 = self.get_test_loc('resource/virtual_codebase/combine-2.json')
1202+
input = (test_file_1, test_file_2)
1203+
codebase = VirtualCodebase(input)
1204+
results = sorted(r.to_dict() for r in codebase.walk())
1205+
expected = [
1206+
OrderedDict([(u'path', u'virtual_root'), (u'type', u'directory'), (u'summary', []), (u'scan_errors', [])]),
1207+
OrderedDict([(u'path', u'virtual_root/samples'), (u'type', u'directory'), (u'summary', []), (u'scan_errors', [])]),
1208+
OrderedDict([(u'path', u'virtual_root/samples/NOTICE'), (u'type', u'file'), (u'summary', []), (u'scan_errors', [])]),
1209+
OrderedDict([(u'path', u'virtual_root/thirdparty'), (u'type', u'directory'), (u'summary', []), (u'scan_errors', [])]),
1210+
OrderedDict([(u'path', u'virtual_root/thirdparty/example.zip'), (u'type', u'file'), (u'summary', []), (u'scan_errors', [])])
1211+
]
1212+
assert expected == results

0 commit comments

Comments
 (0)