Skip to content

Commit 007397b

Browse files
committed
Add remove_uuid to check_packages_data
* Override to_dict on PackageWithResources * Update expected test results Signed-off-by: Jono Yang <jyang@nexb.com>
1 parent 7791a57 commit 007397b

4 files changed

Lines changed: 49 additions & 102 deletions

File tree

src/packagedcode/models.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1202,7 +1202,7 @@ def __attrs_post_init__(self, *args, **kwargs):
12021202
self.package_uid = build_package_uid(self.purl)
12031203

12041204
def to_dict(self):
1205-
return super().to_dict(with_details=False)
1205+
return super().to_dict(with_details=False)
12061206

12071207
@classmethod
12081208
def from_package_data(cls, package_data, datafile_path):
@@ -1353,6 +1353,11 @@ class PackageWithResources(Package):
13531353
help='List of Resources for this package.',
13541354
)
13551355

1356+
def to_dict(self):
1357+
package_data = super().to_dict()
1358+
package_data['resources'] = [resource.to_dict() for resource in self.resources]
1359+
return package_data
1360+
13561361

13571362
def get_files_for_packages(codebase):
13581363
"""

tests/packagedcode/data/plugin/get_installed_packages-expected.json

Lines changed: 24 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484
"repository_homepage_url": null,
8585
"repository_download_url": null,
8686
"api_data_url": null,
87-
"package_uid": "pkg:deb/libncurses5@6.1-1ubuntu1.18.04?architecture=amd64&uuid=84fe6563-10bc-4f05-b2f1-853a91c60186",
87+
"package_uid": "pkg:deb/libncurses5@6.1-1ubuntu1.18.04?architecture=amd64&uuid=fixed-uid-done-for-testing-5642512d1758",
8888
"datafile_paths": [
8989
"basic-rootfs.tar.gz/var/lib/dpkg/status"
9090
],
@@ -93,24 +93,8 @@
9393
],
9494
"resources": [
9595
{
96-
"name": "copyright",
97-
"location": "/tmp/scancode-tk-tests -313gcwpc/_ig_hfeg/basic-rootfs.tar.gz/usr/share/doc/libncurses5/copyright",
9896
"path": "basic-rootfs.tar.gz/usr/share/doc/libncurses5/copyright",
99-
"cache_location": null,
100-
"is_file": true,
101-
"is_filtered": false,
102-
"children_names": [],
103-
"size": 0,
104-
"size_count": 0,
105-
"files_count": 0,
106-
"dirs_count": 0,
107-
"scan_errors": [],
108-
"scan_time": 0,
109-
"scan_timings": {
110-
"packages": 0.9765093326568604
111-
},
112-
"extra_data": {},
113-
"is_root": false,
97+
"type": "file",
11498
"package_data": [
11599
{
116100
"type": "deb",
@@ -150,28 +134,13 @@
150134
}
151135
],
152136
"for_packages": [
153-
"pkg:deb/libncurses5@6.1-1ubuntu1.18.04?architecture=amd64&uuid=84fe6563-10bc-4f05-b2f1-853a91c60186"
154-
]
137+
"pkg:deb/libncurses5@6.1-1ubuntu1.18.04?architecture=amd64&uuid=fixed-uid-done-for-testing-5642512d1758"
138+
],
139+
"scan_errors": []
155140
},
156141
{
157-
"name": "libncurses5:amd64.md5sums",
158-
"location": "/tmp/scancode-tk-tests -313gcwpc/_ig_hfeg/basic-rootfs.tar.gz/var/lib/dpkg/info/libncurses5:amd64.md5sums",
159142
"path": "basic-rootfs.tar.gz/var/lib/dpkg/info/libncurses5:amd64.md5sums",
160-
"cache_location": null,
161-
"is_file": true,
162-
"is_filtered": false,
163-
"children_names": [],
164-
"size": 0,
165-
"size_count": 0,
166-
"files_count": 0,
167-
"dirs_count": 0,
168-
"scan_errors": [],
169-
"scan_time": 0,
170-
"scan_timings": {
171-
"packages": 0.00043082237243652344
172-
},
173-
"extra_data": {},
174-
"is_root": false,
143+
"type": "file",
175144
"package_data": [
176145
{
177146
"type": "deb",
@@ -250,8 +219,9 @@
250219
}
251220
],
252221
"for_packages": [
253-
"pkg:deb/libncurses5@6.1-1ubuntu1.18.04?architecture=amd64&uuid=84fe6563-10bc-4f05-b2f1-853a91c60186"
254-
]
222+
"pkg:deb/libncurses5@6.1-1ubuntu1.18.04?architecture=amd64&uuid=fixed-uid-done-for-testing-5642512d1758"
223+
],
224+
"scan_errors": []
255225
}
256226
],
257227
"purl": "pkg:deb/libncurses5@6.1-1ubuntu1.18.04?architecture=amd64"
@@ -323,7 +293,7 @@
323293
"repository_homepage_url": null,
324294
"repository_download_url": null,
325295
"api_data_url": null,
326-
"package_uid": "pkg:deb/libndp0@1.4-2ubuntu0.16.04.1?architecture=amd64&uuid=2b022086-4693-43ba-b3a0-7e09c436c1e4",
296+
"package_uid": "pkg:deb/libndp0@1.4-2ubuntu0.16.04.1?architecture=amd64&uuid=fixed-uid-done-for-testing-5642512d1758",
327297
"datafile_paths": [
328298
"basic-rootfs.tar.gz/var/lib/dpkg/status"
329299
],
@@ -332,24 +302,8 @@
332302
],
333303
"resources": [
334304
{
335-
"name": "copyright",
336-
"location": "/tmp/scancode-tk-tests -313gcwpc/_ig_hfeg/basic-rootfs.tar.gz/usr/share/doc/libndp0/copyright",
337305
"path": "basic-rootfs.tar.gz/usr/share/doc/libndp0/copyright",
338-
"cache_location": null,
339-
"is_file": true,
340-
"is_filtered": false,
341-
"children_names": [],
342-
"size": 0,
343-
"size_count": 0,
344-
"files_count": 0,
345-
"dirs_count": 0,
346-
"scan_errors": [],
347-
"scan_time": 0,
348-
"scan_timings": {
349-
"packages": 0.08331418037414551
350-
},
351-
"extra_data": {},
352-
"is_root": false,
306+
"type": "file",
353307
"package_data": [
354308
{
355309
"type": "deb",
@@ -393,29 +347,14 @@
393347
}
394348
],
395349
"for_packages": [
396-
"pkg:deb/libndp0@1.4-2ubuntu0.16.04.1?architecture=amd64&uuid=2b022086-4693-43ba-b3a0-7e09c436c1e4",
397-
"pkg:deb/libndp0@1.4-2ubuntu0.16.04.1?architecture=amd64&uuid=2b022086-4693-43ba-b3a0-7e09c436c1e4"
398-
]
350+
"pkg:deb/libndp0@1.4-2ubuntu0.16.04.1?architecture=amd64&uuid=fixed-uid-done-for-testing-5642512d1758",
351+
"pkg:deb/libndp0@1.4-2ubuntu0.16.04.1?architecture=amd64&uuid=fixed-uid-done-for-testing-5642512d1758"
352+
],
353+
"scan_errors": []
399354
},
400355
{
401-
"name": "copyright",
402-
"location": "/tmp/scancode-tk-tests -313gcwpc/_ig_hfeg/basic-rootfs.tar.gz/usr/share/doc/libndp0/copyright",
403356
"path": "basic-rootfs.tar.gz/usr/share/doc/libndp0/copyright",
404-
"cache_location": null,
405-
"is_file": true,
406-
"is_filtered": false,
407-
"children_names": [],
408-
"size": 0,
409-
"size_count": 0,
410-
"files_count": 0,
411-
"dirs_count": 0,
412-
"scan_errors": [],
413-
"scan_time": 0,
414-
"scan_timings": {
415-
"packages": 0.08331418037414551
416-
},
417-
"extra_data": {},
418-
"is_root": false,
357+
"type": "file",
419358
"package_data": [
420359
{
421360
"type": "deb",
@@ -459,29 +398,14 @@
459398
}
460399
],
461400
"for_packages": [
462-
"pkg:deb/libndp0@1.4-2ubuntu0.16.04.1?architecture=amd64&uuid=2b022086-4693-43ba-b3a0-7e09c436c1e4",
463-
"pkg:deb/libndp0@1.4-2ubuntu0.16.04.1?architecture=amd64&uuid=2b022086-4693-43ba-b3a0-7e09c436c1e4"
464-
]
401+
"pkg:deb/libndp0@1.4-2ubuntu0.16.04.1?architecture=amd64&uuid=fixed-uid-done-for-testing-5642512d1758",
402+
"pkg:deb/libndp0@1.4-2ubuntu0.16.04.1?architecture=amd64&uuid=fixed-uid-done-for-testing-5642512d1758"
403+
],
404+
"scan_errors": []
465405
},
466406
{
467-
"name": "libndp0:amd64.md5sums",
468-
"location": "/tmp/scancode-tk-tests -313gcwpc/_ig_hfeg/basic-rootfs.tar.gz/var/lib/dpkg/info/libndp0:amd64.md5sums",
469407
"path": "basic-rootfs.tar.gz/var/lib/dpkg/info/libndp0:amd64.md5sums",
470-
"cache_location": null,
471-
"is_file": true,
472-
"is_filtered": false,
473-
"children_names": [],
474-
"size": 0,
475-
"size_count": 0,
476-
"files_count": 0,
477-
"dirs_count": 0,
478-
"scan_errors": [],
479-
"scan_time": 0,
480-
"scan_timings": {
481-
"packages": 0.00034499168395996094
482-
},
483-
"extra_data": {},
484-
"is_root": false,
408+
"type": "file",
485409
"package_data": [
486410
{
487411
"type": "deb",
@@ -551,8 +475,9 @@
551475
}
552476
],
553477
"for_packages": [
554-
"pkg:deb/libndp0@1.4-2ubuntu0.16.04.1?architecture=amd64&uuid=2b022086-4693-43ba-b3a0-7e09c436c1e4"
555-
]
478+
"pkg:deb/libndp0@1.4-2ubuntu0.16.04.1?architecture=amd64&uuid=fixed-uid-done-for-testing-5642512d1758"
479+
],
480+
"scan_errors": []
556481
}
557482
],
558483
"purl": "pkg:deb/libndp0@1.4-2ubuntu0.16.04.1?architecture=amd64"

tests/packagedcode/packages_test_utils.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
from commoncode import testcase
1616
from commoncode import text
17+
from scancode.cli_test_utils import purl_with_fake_uuid
1718

1819
from scancode_config import REGEN_TEST_FIXTURES
1920

@@ -36,14 +37,30 @@ def check_package_data(self, package_data, expected_loc, regen=REGEN_TEST_FIXTUR
3637
regen=regen,
3738
)
3839

39-
def check_packages_data(self, packages_data, expected_loc, must_exist=True, regen=REGEN_TEST_FIXTURES):
40+
def check_packages_data(
41+
self,
42+
packages_data,
43+
expected_loc,
44+
must_exist=True,
45+
remove_uuid=True,
46+
regen=REGEN_TEST_FIXTURES
47+
):
4048
"""
4149
Helper to test a list of package_data objects against an expected JSON file.
4250
"""
4351
expected_loc = self.get_test_loc(expected_loc, must_exist=must_exist)
4452

4553
results = []
4654
for package_data in packages_data:
55+
if remove_uuid and hasattr(package_data, 'package_uid'):
56+
package_data.package_uid = purl_with_fake_uuid(package_data.package_uid)
57+
if remove_uuid and hasattr(package_data, 'resources'):
58+
for resource in package_data.resources:
59+
normalized_package_uids = [
60+
purl_with_fake_uuid(package_uid)
61+
for package_uid in resource.for_packages
62+
]
63+
resource.for_packages = normalized_package_uids
4764
compute_and_set_license_expression(package_data)
4865
results.append(package_data.to_dict())
4966

tests/packagedcode/test_plugin_package.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,4 +218,4 @@ def test_package_get_installed_packages(self):
218218
expected_file = self.get_test_loc('plugin/get_installed_packages-expected.json')
219219
result_file = self.get_temp_file('results.json')
220220
results = list(get_installed_packages(test_dir))
221-
self.check_packages_data(results, expected_file, regen=REGEN_TEST_FIXTURES)
221+
self.check_packages_data(results, expected_file, remove_uuid=True, regen=REGEN_TEST_FIXTURES)

0 commit comments

Comments
 (0)