From 83a760ac9dc18bc42ff137d47af045514c3c0c7b Mon Sep 17 00:00:00 2001 From: Pratik Dey Date: Thu, 10 Jun 2021 18:07:47 +0530 Subject: [PATCH] fixed TypeError due to different attributes over directories and files Signed-off-by: Pratik Dey --- src/commoncode/resource.py | 11 +++++--- .../fingerprint_attribute.json | 26 +++++++++++++++++++ tests/test_resource.py | 9 +++++++ 3 files changed, 42 insertions(+), 4 deletions(-) create mode 100644 tests/data/resource/virtual_codebase/fingerprint_attribute.json diff --git a/src/commoncode/resource.py b/src/commoncode/resource.py index 9ce79e9..c92363f 100644 --- a/src/commoncode/resource.py +++ b/src/commoncode/resource.py @@ -1591,10 +1591,13 @@ def _populate(self, scan_data): if not resources_data: raise Exception('Input has no file-level scan results.') - # We collect the first Resource so we can see what attributes it has and determine - # the root path from its path - sample_resource_data = resources_data[0] - + # We iterate through all the Resource(s) so that we can build attributes each resource contains + + sample_resource_data = dict() + + for resource in resources_data: + sample_resource_data.update(resource) + # Collect the existing attributes of the standard Resource class standard_res_attributes = set(f.name for f in attr.fields(Resource)) # add these properties since they are fields but are serialized diff --git a/tests/data/resource/virtual_codebase/fingerprint_attribute.json b/tests/data/resource/virtual_codebase/fingerprint_attribute.json new file mode 100644 index 0000000..3d6af5d --- /dev/null +++ b/tests/data/resource/virtual_codebase/fingerprint_attribute.json @@ -0,0 +1,26 @@ +{ + "files": [ + { + "path": "apache_to_all_notable_lic_new", + "type": "directory", + "sha1": null, + "md5": null, + "scan_errors": [] + }, + { + "path": "apache_to_all_notable_lic_new/a1.py", + "type": "file", + "sha1": "535b9966048ad50a9d5eb2f167c0a3013ee5cc6f", + "md5": "e5658fe520bbebaf6f3d4be2e2525ab6", + "fingerprint": "e30cf09443e7878dfed3288886e97542", + "scan_errors": [] + }, + { + "path": "apache_to_all_notable_lic_new/a2.py", + "type": "file", + "sha1": "310797523e47db8481aeb06f1634317285115091", + "md5": "19efdad483f68bc9997a5c1f7ba41b26", + "scan_errors": [] + } + ] + } \ No newline at end of file diff --git a/tests/test_resource.py b/tests/test_resource.py index 34e5fe5..ffc52af 100644 --- a/tests/test_resource.py +++ b/tests/test_resource.py @@ -1055,6 +1055,15 @@ def test_virtual_codebase_can_process_minimal_resources_with_only_path(self): ]) ] assert [r.to_dict() for r in codebase.walk()] == expected + + def test_VirtualCodebase_account_fingerprint_attribute(self): + test_file = self.get_test_loc("resource/virtual_codebase/fingerprint_attribute.json") + codebase = VirtualCodebase(test_file) + resources_fingerprint = [resource.fingerprint for resource in codebase.walk()] + assert "e30cf09443e7878dfed3288886e97542" in resources_fingerprint + assert None in resources_fingerprint + assert codebase.get_resource(0) == codebase.root + assert resources_fingerprint.count(None) == 2 class TestCodebaseLowestCommonParent(FileBasedTesting):