def test_process_go(default_request): pkg = Package.from_json({ "name": "example.com/org/project", "type": "go-package", "version": "1.1.1" }) pkg.id = 1 expected_purl = "pkg:golang/example.com%2Forg%[email protected]" dep = Package.from_json({ "name": "example.com/org/project/lib", "type": "go-package", "version": "2.2.2" }) dep.id = 2 expected_dep_purl = "pkg:golang/example.com%2Forg%2Fproject%[email protected]" src = Package.from_json({ "name": "example.com/anotherorg/project", "type": "gomod", "version": "3.3.3" }) src.id = 3 expected_src_purl = "pkg:golang/example.com%2Fanotherorg%[email protected]" cm = ContentManifest(default_request) # emulate to_json behavior to setup internal packages cache cm._gomod_data.setdefault(pkg.name, { "purl": "not-important", "dependencies": [] }) cm._gopkg_data.setdefault(pkg.id, { "name": pkg.name, "purl": expected_purl, "dependencies": [], "sources": [] }) cm.process_go_package(pkg, dep) cm.process_gomod(pkg, src) cm.set_go_package_sources() expected_contents = { pkg.id: { "purl": expected_purl, "dependencies": [{ "purl": expected_dep_purl }], "sources": [{ "purl": expected_src_purl }], } } assert cm._gopkg_data assert pkg.id in cm._gopkg_data assert cm._gopkg_data == expected_contents
def test_set_go_package_sources_replace_parent_purl(gopkg_name, gomod_data, expected_parent_purl, default_request): pre_replaced_dependencies = [ { "purl": f"{PARENT_PURL_PLACEHOLDER}#staging/src/k8s.io/foo" }, { "purl": f"{PARENT_PURL_PLACEHOLDER}#staging/src/k8s.io/bar" }, { "purl": "pkg:golang/example.com/[email protected]" }, ] post_replaced_dependencies = [ { "purl": f"{expected_parent_purl}#staging/src/k8s.io/foo" }, { "purl": f"{expected_parent_purl}#staging/src/k8s.io/bar" }, { "purl": "pkg:golang/example.com/[email protected]" }, ] cm = ContentManifest(default_request, []) cm._gomod_data = gomod_data cm._gopkg_data = { 1: { "name": gopkg_name, "purl": "not-important", "dependencies": pre_replaced_dependencies, "sources": [], }, } cm.set_go_package_sources() assert cm._gopkg_data == { 1: { # name is popped by set_go_package_sources() "purl": "not-important", "dependencies": post_replaced_dependencies, "sources": [], }, }
def test_set_go_package_sources(mock_warning, app, pkg_name, gomod_data, warn, default_request): cm = ContentManifest(default_request) main_purl = "pkg:golang/a-package" main_package_id = 1 cm._gopkg_data = { main_package_id: { "name": pkg_name, "purl": main_purl, "sources": [], "dependencies": [] } } cm._gomod_data = gomod_data cm.set_go_package_sources() sources = [] for v in gomod_data.values(): if any(k in pkg_name for k in gomod_data.keys()): sources += v expected = { main_package_id: { "purl": main_purl, "dependencies": [], "sources": sources } } assert cm._gopkg_data == expected if warn: mock_warning.assert_called_once_with( "Could not find a Go module for %s", main_purl) else: mock_warning.assert_not_called()