Skip to content

Commit 8ebd2df

Browse files
committed
Do not fail with null sizes and counts
Signed-off-by: Philippe Ombredanne <pombredanne@nexb.com>
1 parent 02fb544 commit 8ebd2df

3 files changed

Lines changed: 38 additions & 5 deletions

File tree

src/commoncode/resource.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -884,7 +884,7 @@ def compute_counts(self, skip_root=False, skip_filtered=False):
884884
files_count += 1
885885
else:
886886
dirs_count += 1
887-
size_count += root.size
887+
size_count += root.size or 0
888888

889889
return files_count, dirs_count, size_count
890890

@@ -1133,9 +1133,9 @@ def _compute_children_counts(self, codebase, skip_filtered=False):
11331133
"""
11341134
files_count = dirs_count = size_count = 0
11351135
for child in self.children(codebase):
1136-
files_count += child.files_count
1137-
dirs_count += child.dirs_count
1138-
size_count += child.size_count
1136+
files_count += child.files_count or 0
1137+
dirs_count += child.dirs_count or 0
1138+
size_count += child.size_count or 0
11391139

11401140
if skip_filtered and child.is_filtered:
11411141
continue
@@ -1144,7 +1144,7 @@ def _compute_children_counts(self, codebase, skip_filtered=False):
11441144
files_count += 1
11451145
else:
11461146
dirs_count += 1
1147-
size_count += child.size
1147+
size_count += child.size or 0
11481148

11491149
self.files_count = files_count
11501150
self.dirs_count = dirs_count
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"headers": [],
3+
"files": [
4+
{
5+
"for_packages": [
6+
"pkg:deb/debian/bash@5.0-4?arch=amd64"
7+
],
8+
"compliance_alert": "",
9+
"path": "/node_16_slim.tar-extract/02c055ef67f5904019f43a41ea5f099996d8e7633749b6e606c400526b2c4b33/bin/bash",
10+
"size": 1168776,
11+
"type": "file",
12+
"name": "bash",
13+
"extension": ""
14+
},
15+
{
16+
"for_packages": [
17+
"pkg:deb/debian/gzip@1.9-3?arch=amd64"
18+
],
19+
"compliance_alert": "",
20+
"path": "/node_16_slim.tar-extract/02c055ef67f5904019f43a41ea5f099996d8e7633749b6e606c400526b2c4b33/bin/uncompress",
21+
"size": null,
22+
"type": "file",
23+
"name": "uncompress",
24+
"extension": ""
25+
}]
26+
}

tests/test_resource.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1325,6 +1325,13 @@ def test_VirtualCodebase_scanning_full_root(self):
13251325
assert "/Users/sesser/code/nexb/scancode-toolkit/samples/README" == resource.path
13261326
assert 1 == codebase.compute_counts()[0]
13271327

1328+
def test_VirtualCodebase_can_compute_counts_witrh_null(self):
1329+
# was failing with
1330+
# size_count += child.size
1331+
# TypeError: unsupported operand type(s) for +=: 'int' and 'NoneType'
1332+
test_file = self.get_test_loc("resource/virtual_codebase/node-16-slim.json")
1333+
codebase = VirtualCodebase(test_file)
1334+
codebase.compute_counts()
13281335

13291336
class TestResource(FileBasedTesting):
13301337
test_data_dir = join(dirname(__file__), 'data')

0 commit comments

Comments
 (0)