Skip to content

Commit ec6f47a

Browse files
committed
Add test for custom package_adder #3034
* Update CHANGELOG.rst Signed-off-by: Jono Yang <[email protected]>
1 parent eb46d73 commit ec6f47a

File tree

3 files changed

+255
-4
lines changed

3 files changed

+255
-4
lines changed

CHANGELOG.rst

+8-4
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,12 @@ Package detection:
166166
of the new format where there is no root conceptually, just a list of files for each
167167
package.
168168

169-
- There is a new resource-level attribute `for_packages` which refers to packages
170-
through package_uuids (pURL + uuid string).
169+
- There is a new resource-level attribute `for_packages` which refers to
170+
packages through package_uids (pURL + uuid string). A `package_adder`
171+
function is now used to associate a Package to a Resource that is part of
172+
it. This gives us the flexibility to use the packagedcode Package handlers
173+
in other contexts where `for_packages` on Resource is not implemented in the
174+
same way as scancode-toolkit.
171175

172176
- The package_data attribute `dependencies` (which is a list of DependentPackages),
173177
now has a new attribute `resolved_package` with a package data mapping.
@@ -331,8 +335,8 @@ Miscellaneous
331335

332336
- Added support for usage of shortcut flags
333337
- `-A` or `--about`
334-
- `-q` or `--quiet`
335-
- `-v` or `--verbose`
338+
- `-q` or `--quiet`
339+
- `-v` or `--verbose`
336340
- `-V` or `--version` can be used.
337341

338342

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
{
2+
"headers": [
3+
{
4+
"tool_name": "scancode-toolkit",
5+
"tool_version": "31.0.0rc3",
6+
"options": {
7+
"input": [
8+
"."
9+
],
10+
"--info": true,
11+
"--json-pp": "../get_package_resources.scan.json",
12+
"--package": true
13+
},
14+
"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.",
15+
"start_timestamp": "2022-08-01T185859.869487",
16+
"end_timestamp": "2022-08-01T185902.232500",
17+
"output_format_version": "2.0.0",
18+
"duration": 2.363025665283203,
19+
"message": null,
20+
"errors": [],
21+
"warnings": [],
22+
"extra_data": {
23+
"system_environment": {
24+
"operating_system": "linux",
25+
"cpu_architecture": "64",
26+
"platform": "Linux-5.4.0-122-generic-x86_64-with-glibc2.27",
27+
"platform_version": "#138~18.04.1-Ubuntu SMP Fri Jun 24 14:14:03 UTC 2022",
28+
"python_version": "3.10.4 (main, May 22 2022, 00:46:26) [GCC 7.5.0]"
29+
},
30+
"spdx_license_list_version": "3.17",
31+
"files_count": 3
32+
}
33+
}
34+
],
35+
"dependencies": [],
36+
"packages": [],
37+
"files": [
38+
{
39+
"path": "get_package_resources",
40+
"type": "directory",
41+
"name": "get_package_resources",
42+
"base_name": "get_package_resources",
43+
"extension": "",
44+
"size": 0,
45+
"date": null,
46+
"sha1": null,
47+
"md5": null,
48+
"sha256": null,
49+
"mime_type": null,
50+
"file_type": null,
51+
"programming_language": null,
52+
"is_binary": false,
53+
"is_text": false,
54+
"is_archive": false,
55+
"is_media": false,
56+
"is_source": false,
57+
"is_script": false,
58+
"package_data": [],
59+
"for_packages": [],
60+
"files_count": 3,
61+
"dirs_count": 1,
62+
"size_count": 63,
63+
"scan_errors": []
64+
},
65+
{
66+
"path": "get_package_resources/package.json",
67+
"type": "file",
68+
"name": "package.json",
69+
"base_name": "package",
70+
"extension": ".json",
71+
"size": 63,
72+
"date": "2022-02-15",
73+
"sha1": "47ad6746a065a22a4c2a119cb425c250bddfbf88",
74+
"md5": "5cbb43ca814ec801e8673e348e8eec19",
75+
"sha256": "aac331f407d4d662a2897eafeb4f05a1c2343218220d99779c9553d3b92bd4ac",
76+
"mime_type": "application/json",
77+
"file_type": "JSON data",
78+
"programming_language": null,
79+
"is_binary": false,
80+
"is_text": true,
81+
"is_archive": false,
82+
"is_media": false,
83+
"is_source": false,
84+
"is_script": false,
85+
"package_data": [
86+
{
87+
"type": "npm",
88+
"namespace": null,
89+
"name": "test",
90+
"version": "0.1.0",
91+
"qualifiers": {},
92+
"subpath": null,
93+
"primary_language": "JavaScript",
94+
"description": null,
95+
"release_date": null,
96+
"parties": [],
97+
"keywords": [],
98+
"homepage_url": null,
99+
"download_url": "https://registry.npmjs.org/test/-/test-0.1.0.tgz",
100+
"size": null,
101+
"sha1": null,
102+
"md5": null,
103+
"sha256": null,
104+
"sha512": null,
105+
"bug_tracking_url": null,
106+
"code_view_url": null,
107+
"vcs_url": null,
108+
"copyright": null,
109+
"license_expression": "mit",
110+
"declared_license": [
111+
"MIT"
112+
],
113+
"notice_text": null,
114+
"source_packages": [],
115+
"file_references": [],
116+
"extra_data": {},
117+
"dependencies": [],
118+
"repository_homepage_url": "https://www.npmjs.com/package/test",
119+
"repository_download_url": "https://registry.npmjs.org/test/-/test-0.1.0.tgz",
120+
"api_data_url": "https://registry.npmjs.org/test/0.1.0",
121+
"datasource_id": "npm_package_json",
122+
"purl": "pkg:npm/[email protected]"
123+
}
124+
],
125+
"for_packages": [],
126+
"files_count": 0,
127+
"dirs_count": 0,
128+
"size_count": 0,
129+
"scan_errors": []
130+
},
131+
{
132+
"path": "get_package_resources/this-should-be-returned",
133+
"type": "file",
134+
"name": "this-should-be-returned",
135+
"base_name": "this-should-be-returned",
136+
"extension": "",
137+
"size": 0,
138+
"date": "2022-02-15",
139+
"sha1": null,
140+
"md5": null,
141+
"sha256": null,
142+
"mime_type": "inode/x-empty",
143+
"file_type": "empty",
144+
"programming_language": null,
145+
"is_binary": false,
146+
"is_text": true,
147+
"is_archive": false,
148+
"is_media": false,
149+
"is_source": false,
150+
"is_script": false,
151+
"package_data": [],
152+
"for_packages": [],
153+
"files_count": 0,
154+
"dirs_count": 0,
155+
"size_count": 0,
156+
"scan_errors": []
157+
},
158+
{
159+
"path": "get_package_resources/node_modules",
160+
"type": "directory",
161+
"name": "node_modules",
162+
"base_name": "node_modules",
163+
"extension": "",
164+
"size": 0,
165+
"date": null,
166+
"sha1": null,
167+
"md5": null,
168+
"sha256": null,
169+
"mime_type": null,
170+
"file_type": null,
171+
"programming_language": null,
172+
"is_binary": false,
173+
"is_text": false,
174+
"is_archive": false,
175+
"is_media": false,
176+
"is_source": false,
177+
"is_script": false,
178+
"package_data": [],
179+
"for_packages": [],
180+
"files_count": 1,
181+
"dirs_count": 0,
182+
"size_count": 0,
183+
"scan_errors": []
184+
},
185+
{
186+
"path": "get_package_resources/node_modules/this-should-not-be-returned",
187+
"type": "file",
188+
"name": "this-should-not-be-returned",
189+
"base_name": "this-should-not-be-returned",
190+
"extension": "",
191+
"size": 0,
192+
"date": "2022-02-15",
193+
"sha1": null,
194+
"md5": null,
195+
"sha256": null,
196+
"mime_type": "inode/x-empty",
197+
"file_type": "empty",
198+
"programming_language": null,
199+
"is_binary": false,
200+
"is_text": true,
201+
"is_archive": false,
202+
"is_media": false,
203+
"is_source": false,
204+
"is_script": false,
205+
"package_data": [],
206+
"for_packages": [],
207+
"files_count": 0,
208+
"dirs_count": 0,
209+
"size_count": 0,
210+
"scan_errors": []
211+
}
212+
]
213+
}

tests/packagedcode/test_package_models.py

+34
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,22 @@
77
# See https://aboutcode.org for more information about nexB OSS projects.
88
#
99

10+
from cgi import test
1011
import os.path
1112

1213
from packagedcode import misc
1314
from packagedcode import models
1415
from packagedcode import ALL_DATAFILE_HANDLERS
1516
from packagedcode.models import PackageData
1617
from packagedcode.models import Party
18+
from packagedcode.plugin_package import get_package_and_deps
1719
from packagedcode.plugin_package import PackageScanner
1820
from packages_test_utils import PackageTester
21+
from scancode.cli_test_utils import purl_with_fake_uuid
1922
from scancode_config import REGEN_TEST_FIXTURES
2023

2124
from commoncode.resource import Codebase
25+
from commoncode.resource import VirtualCodebase
2226

2327

2428
class TestModels(PackageTester):
@@ -177,3 +181,33 @@ def test_add_to_package(self):
177181
test_codebase
178182
)
179183
assert test_package.package_uid in test_resource.for_packages
184+
185+
def test_assembly_custom_package_adder(self):
186+
def test_package_adder(package_uid, resource, codebase):
187+
"""
188+
Add `package_uid` to `resource.extra_data`
189+
"""
190+
if 'for_packages' in resource.extra_data:
191+
resource.extra_data['for_packages'].append(package_uid)
192+
else:
193+
resource.extra_data['for_packages'] = [package_uid]
194+
resource.save(codebase)
195+
196+
# This scan does not contain top-level Packages or Dependencies since we
197+
# want to run `get_packages_and_deps` to create them
198+
test_loc = self.get_test_loc('models/get_package_resources.scan.json')
199+
test_codebase = VirtualCodebase(location=test_loc)
200+
packages, dependencies = get_package_and_deps(test_codebase, test_package_adder)
201+
202+
assert len(packages) == 1
203+
assert not dependencies
204+
205+
package = packages[0]
206+
package_uid = package.package_uid
207+
test_package_uid = purl_with_fake_uuid(package_uid)
208+
209+
for resource in test_codebase.walk():
210+
for_packages = resource.extra_data.get('for_packages', [])
211+
for package_uid in for_packages:
212+
normalized_package_uid = purl_with_fake_uuid(package_uid)
213+
assert normalized_package_uid == test_package_uid

0 commit comments

Comments
 (0)