|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +# |
| 3 | +# http://nexb.com and https://github.com/nexB/scancode.io |
| 4 | +# The ScanCode.io software is licensed under the Apache License version 2.0. |
| 5 | +# Data generated with ScanCode.io is provided as-is without warranties. |
| 6 | +# ScanCode is a trademark of nexB Inc. |
| 7 | +# |
| 8 | +# You may not use this software except in compliance with the License. |
| 9 | +# You may obtain a copy of the License at: http://apache.org/licenses/LICENSE-2.0 |
| 10 | +# Unless required by applicable law or agreed to in writing, software distributed |
| 11 | +# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 12 | +# CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 13 | +# specific language governing permissions and limitations under the License. |
| 14 | +# |
| 15 | +# Data Generated with ScanCode.io is provided on an "AS IS" BASIS, WITHOUT WARRANTIES |
| 16 | +# OR CONDITIONS OF ANY KIND, either express or implied. No content created from |
| 17 | +# ScanCode.io should be considered or used as legal advice. Consult an Attorney |
| 18 | +# for any legal advice. |
| 19 | +# |
| 20 | +# ScanCode.io is a free software code scanning tool from nexB Inc. and others. |
| 21 | +# Visit https://github.com/nexB/scancode.io for support and download. |
| 22 | + |
| 23 | +import json |
| 24 | +import sys |
| 25 | +from pathlib import Path |
| 26 | +from unittest import skipIf |
| 27 | + |
| 28 | +from django.core.management import call_command |
| 29 | +from django.test import TestCase |
| 30 | + |
| 31 | +from scanpipe.models import Project |
| 32 | +from scanpipe.pipes import codebase |
| 33 | +from scanpipe.pipes import scancode |
| 34 | + |
| 35 | + |
| 36 | +class ScanPipeCodebasePipesTest(TestCase): |
| 37 | + data_location = Path(__file__).parent.parent / "data" |
| 38 | + |
| 39 | + def test_scanpipe_pipes_codebase_get_codebase_tree(self): |
| 40 | + def _replace_path(virtual_tree_children): |
| 41 | + """ |
| 42 | + Given a list `virtual_tree_children` of mappings, remove instances |
| 43 | + of "virtual_root/" from the paths of mappings and their children, |
| 44 | + recursively. |
| 45 | + """ |
| 46 | + for res in virtual_tree_children: |
| 47 | + path = res["path"] |
| 48 | + path = path.replace("virtual_root/", "") |
| 49 | + res["path"] = path |
| 50 | + _replace_path(res.get("children", [])) |
| 51 | + |
| 52 | + fixtures = self.data_location / "asgiref-3.3.0_fixtures.json" |
| 53 | + call_command("loaddata", fixtures, **{"verbosity": 0}) |
| 54 | + project = Project.objects.get(name="asgiref") |
| 55 | + |
| 56 | + scan_results = self.data_location / "asgiref-3.3.0_scanpipe_output.json" |
| 57 | + virtual_codebase = scancode.get_virtual_codebase(project, scan_results) |
| 58 | + project_codebase = codebase.ProjectCodebase(project) |
| 59 | + |
| 60 | + fields = ["name", "path"] |
| 61 | + |
| 62 | + virtual_tree = codebase.get_codebase_tree(virtual_codebase, fields) |
| 63 | + project_tree = codebase.get_codebase_tree(project_codebase, fields) |
| 64 | + |
| 65 | + with open(self.data_location / "asgiref-3.3.0_tree.json") as f: |
| 66 | + expected = json.loads(f.read()) |
| 67 | + |
| 68 | + self.assertEqual(expected, project_tree) |
| 69 | + |
| 70 | + virtual_tree_children = virtual_tree["children"][0]["children"] |
| 71 | + _replace_path(virtual_tree_children) |
| 72 | + |
| 73 | + self.assertEqual(expected["children"], virtual_tree_children) |
| 74 | + |
| 75 | + def test_scanpipe_pipes_codebase_project_codebase_class_no_resources(self): |
| 76 | + project = Project.objects.create(name="project") |
| 77 | + project_codebase = codebase.ProjectCodebase(project) |
| 78 | + |
| 79 | + self.assertEqual([], list(project_codebase.root_resources)) |
| 80 | + self.assertEqual([], list(project_codebase.resources)) |
| 81 | + self.assertEqual([], list(project_codebase.walk())) |
| 82 | + self.assertEqual(dict(children=[]), project_codebase.get_tree()) |
| 83 | + |
| 84 | + def test_scanpipe_pipes_codebase_project_codebase_class_with_resources(self): |
| 85 | + fixtures = self.data_location / "asgiref-3.3.0_fixtures.json" |
| 86 | + call_command("loaddata", fixtures, **{"verbosity": 0}) |
| 87 | + |
| 88 | + project = Project.objects.get(name="asgiref") |
| 89 | + project_codebase = codebase.ProjectCodebase(project) |
| 90 | + |
| 91 | + expected_root_resources = project.codebaseresources.exclude(path__contains="/") |
| 92 | + expected_root_resources = list(expected_root_resources) |
| 93 | + self.assertEqual(expected_root_resources, list(project_codebase.root_resources)) |
| 94 | + |
| 95 | + self.assertEqual(18, len(project_codebase.resources)) |
| 96 | + |
| 97 | + walk_gen = project_codebase.walk() |
| 98 | + self.assertEqual(next(iter(expected_root_resources)), next(walk_gen)) |
| 99 | + expected = "asgiref-3.3.0-py3-none-any.whl-extract" |
| 100 | + self.assertEqual(expected, next(walk_gen).path) |
| 101 | + |
| 102 | + tree = project_codebase.get_tree() |
| 103 | + with open(self.data_location / "asgiref-3.3.0_tree.json") as f: |
| 104 | + expected = json.loads(f.read()) |
| 105 | + |
| 106 | + self.assertEqual(expected, tree) |
| 107 | + |
| 108 | + @skipIf(sys.platform != "linux", "Ordering differs on macOS.") |
| 109 | + def test_scanpipe_pipes_codebase_project_codebase_class_walk(self): |
| 110 | + fixtures = self.data_location / "asgiref-3.3.0_fixtures.json" |
| 111 | + call_command("loaddata", fixtures, **{"verbosity": 0}) |
| 112 | + |
| 113 | + project = Project.objects.get(name="asgiref") |
| 114 | + project_codebase = codebase.ProjectCodebase(project) |
| 115 | + |
| 116 | + topdown_paths = list(r.path for r in project_codebase.walk(topdown=True)) |
| 117 | + expected_topdown_paths = [ |
| 118 | + "asgiref-3.3.0-py3-none-any.whl", |
| 119 | + "asgiref-3.3.0-py3-none-any.whl-extract", |
| 120 | + "asgiref-3.3.0-py3-none-any.whl-extract/asgiref", |
| 121 | + "asgiref-3.3.0-py3-none-any.whl-extract/asgiref/compatibility.py", |
| 122 | + "asgiref-3.3.0-py3-none-any.whl-extract/asgiref/current_thread_executor.py", |
| 123 | + "asgiref-3.3.0-py3-none-any.whl-extract/asgiref/__init__.py", |
| 124 | + "asgiref-3.3.0-py3-none-any.whl-extract/asgiref/local.py", |
| 125 | + "asgiref-3.3.0-py3-none-any.whl-extract/asgiref/server.py", |
| 126 | + "asgiref-3.3.0-py3-none-any.whl-extract/asgiref/sync.py", |
| 127 | + "asgiref-3.3.0-py3-none-any.whl-extract/asgiref/testing.py", |
| 128 | + "asgiref-3.3.0-py3-none-any.whl-extract/asgiref/timeout.py", |
| 129 | + "asgiref-3.3.0-py3-none-any.whl-extract/asgiref/wsgi.py", |
| 130 | + "asgiref-3.3.0-py3-none-any.whl-extract/asgiref-3.3.0.dist-info", |
| 131 | + "asgiref-3.3.0-py3-none-any.whl-extract/asgiref-3.3.0.dist-info/LICENSE", |
| 132 | + "asgiref-3.3.0-py3-none-any.whl-extract/asgiref-3.3.0.dist-info/METADATA", |
| 133 | + "asgiref-3.3.0-py3-none-any.whl-extract/asgiref-3.3.0.dist-info/RECORD", |
| 134 | + "asgiref-3.3.0-py3-none-any.whl-extract/asgiref-3.3.0.dist-info/" |
| 135 | + "top_level.txt", |
| 136 | + "asgiref-3.3.0-py3-none-any.whl-extract/asgiref-3.3.0.dist-info/WHEEL", |
| 137 | + ] |
| 138 | + self.assertEqual(expected_topdown_paths, topdown_paths) |
| 139 | + |
| 140 | + bottom_up_paths = list(r.path for r in project_codebase.walk(topdown=False)) |
| 141 | + expected_bottom_up_paths = [ |
| 142 | + "asgiref-3.3.0-py3-none-any.whl", |
| 143 | + "asgiref-3.3.0-py3-none-any.whl-extract/asgiref/compatibility.py", |
| 144 | + "asgiref-3.3.0-py3-none-any.whl-extract/asgiref/current_thread_executor.py", |
| 145 | + "asgiref-3.3.0-py3-none-any.whl-extract/asgiref/__init__.py", |
| 146 | + "asgiref-3.3.0-py3-none-any.whl-extract/asgiref/local.py", |
| 147 | + "asgiref-3.3.0-py3-none-any.whl-extract/asgiref/server.py", |
| 148 | + "asgiref-3.3.0-py3-none-any.whl-extract/asgiref/sync.py", |
| 149 | + "asgiref-3.3.0-py3-none-any.whl-extract/asgiref/testing.py", |
| 150 | + "asgiref-3.3.0-py3-none-any.whl-extract/asgiref/timeout.py", |
| 151 | + "asgiref-3.3.0-py3-none-any.whl-extract/asgiref/wsgi.py", |
| 152 | + "asgiref-3.3.0-py3-none-any.whl-extract/asgiref", |
| 153 | + "asgiref-3.3.0-py3-none-any.whl-extract/asgiref-3.3.0.dist-info/LICENSE", |
| 154 | + "asgiref-3.3.0-py3-none-any.whl-extract/asgiref-3.3.0.dist-info/METADATA", |
| 155 | + "asgiref-3.3.0-py3-none-any.whl-extract/asgiref-3.3.0.dist-info/RECORD", |
| 156 | + "asgiref-3.3.0-py3-none-any.whl-extract/asgiref-3.3.0.dist-info/" |
| 157 | + "top_level.txt", |
| 158 | + "asgiref-3.3.0-py3-none-any.whl-extract/asgiref-3.3.0.dist-info/WHEEL", |
| 159 | + "asgiref-3.3.0-py3-none-any.whl-extract/asgiref-3.3.0.dist-info", |
| 160 | + "asgiref-3.3.0-py3-none-any.whl-extract", |
| 161 | + ] |
| 162 | + self.assertEqual(expected_bottom_up_paths, bottom_up_paths) |
0 commit comments