From de0a63151224c63d699c86e3ca86c1fee94ba258 Mon Sep 17 00:00:00 2001 From: DmitriyLewen Date: Tue, 29 Aug 2023 09:21:26 +0600 Subject: [PATCH] add test for Package() func --- pkg/purl/purl_test.go | 101 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 100 insertions(+), 1 deletion(-) diff --git a/pkg/purl/purl_test.go b/pkg/purl/purl_test.go index c6308e7ed591..85e698b54dbf 100644 --- a/pkg/purl/purl_test.go +++ b/pkg/purl/purl_test.go @@ -411,7 +411,6 @@ func TestNewPackageURL(t *testing.T) { } func TestFromString(t *testing.T) { - testCases := []struct { name string purl string @@ -561,3 +560,103 @@ func TestFromString(t *testing.T) { }) } } + +func TestPackage(t *testing.T) { + tests := []struct { + name string + pkgURL *purl.PackageURL + wantPkg *ftypes.Package + }{ + { + name: "rpm + Qualifiers", + pkgURL: &purl.PackageURL{ + PackageURL: packageurl.PackageURL{ + Type: packageurl.TypeRPM, + Namespace: "redhat", + Name: "nodejs-full-i18n", + Version: "10.21.0-3.module_el8.2.0+391+8da3adc6", + Qualifiers: packageurl.Qualifiers{ + { + Key: "arch", + Value: "x86_64", + }, + { + Key: "epoch", + Value: "1", + }, + { + Key: "modularitylabel", + Value: "nodejs:10:8020020200707141642:6a468ee4", + }, + { + Key: "distro", + Value: "redhat-8", + }, + }, + }, + }, + wantPkg: &ftypes.Package{ + Name: "nodejs-full-i18n", + Version: "10.21.0", + Release: "3.module_el8.2.0+391+8da3adc6", + Arch: "x86_64", + Epoch: 1, + Modularitylabel: "nodejs:10:8020020200707141642:6a468ee4", + }, + }, + { + name: "composer with namespace", + pkgURL: &purl.PackageURL{ + PackageURL: packageurl.PackageURL{ + Type: packageurl.TypeComposer, + Namespace: "symfony", + Name: "contracts", + Version: "v1.0.2", + }, + }, + wantPkg: &ftypes.Package{ + Name: "symfony/contracts", + Version: "v1.0.2", + }, + }, + { + name: "maven with namespace", + pkgURL: &purl.PackageURL{ + PackageURL: packageurl.PackageURL{ + Type: packageurl.TypeMaven, + Namespace: "org.springframework", + Name: "spring-core", + Version: "5.0.4.RELEASE", + Qualifiers: packageurl.Qualifiers{}, + }, + }, + wantPkg: &ftypes.Package{ + Name: "org.springframework:spring-core", + Version: "5.0.4.RELEASE", + }, + }, + { + name: "cocoapods with subpath", + pkgURL: &purl.PackageURL{ + PackageURL: packageurl.PackageURL{ + Type: packageurl.TypeCocoapods, + Version: "4.2.0", + Name: "AppCenter", + Subpath: "Analytics", + Qualifiers: packageurl.Qualifiers{}, + }, + }, + wantPkg: &ftypes.Package{ + Name: "AppCenter/Analytics", + Version: "4.2.0", + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := tt.pkgURL.Package() + assert.Equal(t, tt.wantPkg, got) + }) + } +}