From 5257bca6743443efc710acc1378da4cf5c033131 Mon Sep 17 00:00:00 2001 From: Pratik Dey Date: Mon, 5 Apr 2021 01:06:07 +0530 Subject: [PATCH] Fixed Full Root scans for Virtual Codebase Signed-off-by: Pratik Dey --- src/commoncode/resource.py | 2 +- .../virtual_codebase/path_full_root.json | 36 +++++++++++++++++++ tests/test_resource.py | 7 ++++ 3 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 tests/data/resource/virtual_codebase/path_full_root.json diff --git a/src/commoncode/resource.py b/src/commoncode/resource.py index cbecaed..efc766a 100644 --- a/src/commoncode/resource.py +++ b/src/commoncode/resource.py @@ -1511,7 +1511,7 @@ def _get_or_create_parent(self, path, parent_by_path): Note: the root path and root Resource must already be in `parent_by_path` or else this function does not work. """ - parent_path = parent_directory(path).rstrip('/').rstrip('\\') + parent_path = parent_directory(path).rstrip('/').rstrip('\\').lstrip("/") existing_parent = parent_by_path.get(parent_path) if existing_parent: return existing_parent diff --git a/tests/data/resource/virtual_codebase/path_full_root.json b/tests/data/resource/virtual_codebase/path_full_root.json new file mode 100644 index 0000000..dc768ec --- /dev/null +++ b/tests/data/resource/virtual_codebase/path_full_root.json @@ -0,0 +1,36 @@ +{ + "scancode_notice": "Generated with ScanCode and provided on an \"AS IS\" BASIS, WITHOUT WARRANTIES\nOR CONDITIONS OF ANY KIND, either express or implied. No content created from\nScanCode should be considered or used as legal advice. Consult an Attorney\nfor any legal advice.\nScanCode is a free software code scanning tool from nexB Inc. and others.\nVisit https://github.com/nexB/scancode-toolkit/ for support and download.", + "scancode_version": "2.2.1.post12.6d07756e", + "scancode_options": { + "--info": true, + "--license-score": 0, + "--full-root": true, + "--format": "json-pp" + }, + "files_count": 1, + "files": [ + { + "path": "/Users/sesser/code/nexb/scancode-toolkit/samples/README", + "type": "file", + "name": "README", + "base_name": "README", + "extension": "", + "date": "2017-09-22", + "size": 236, + "sha1": "2e07e32c52d607204fad196052d70e3d18fb8636", + "md5": "effc6856ef85a9250fb1a470792b3f38", + "files_count": null, + "mime_type": "text/plain", + "file_type": "ASCII text", + "programming_language": null, + "is_binary": false, + "is_text": true, + "is_archive": false, + "is_media": false, + "is_source": false, + "is_script": false, + "scan_errors": [] + } + ] + } + \ No newline at end of file diff --git a/tests/test_resource.py b/tests/test_resource.py index 697646c..4611720 100644 --- a/tests/test_resource.py +++ b/tests/test_resource.py @@ -1323,6 +1323,13 @@ def test_VirtualCodebase_create_from_multiple_scans(self): ] assert expected == results + def test_VirtualCodebase_scanning_full_root(self): + test_file = self.get_test_loc("resource/virtual_codebase/path_full_root.json") + codebase = VirtualCodebase(test_file) + resource = sorted(r for r in codebase.walk())[0] + assert resource.path == "/Users/sesser/code/nexb/scancode-toolkit/samples/README" + assert codebase.compute_counts()[0] == 1 + class TestResource(FileBasedTesting): test_data_dir = join(dirname(__file__), 'data')