github.com/nextlinux/gosbom@v0.81.1-0.20230627115839-1ff50c281391/gosbom/pkg/cataloger/javascript/package_test.go (about) 1 package javascript 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8 "github.com/stretchr/testify/require" 9 10 "github.com/anchore/packageurl-go" 11 ) 12 13 func Test_packageURL(t *testing.T) { 14 15 tests := []struct { 16 testName string 17 name string 18 version string 19 expected string 20 namespace string 21 }{ 22 { 23 testName: "no namespace", 24 name: "arborist", 25 version: "2.6.2", 26 expected: "pkg:npm/arborist@2.6.2", 27 }, 28 { 29 testName: "split by namespace", 30 name: "npmcli/arborist", 31 version: "2.6.2", 32 expected: "pkg:npm/npmcli/arborist@2.6.2", 33 namespace: "npmcli", 34 }, 35 { 36 testName: "encoding @ symobl", 37 name: "@npmcli/arborist", 38 version: "2.6.2", 39 expected: "pkg:npm/%40npmcli/arborist@2.6.2", 40 namespace: "@npmcli", 41 }, 42 } 43 for _, tt := range tests { 44 t.Run(tt.testName, func(t *testing.T) { 45 actual := packageURL(tt.name, tt.version) 46 assert.Equal(t, tt.expected, actual) 47 decoded, err := packageurl.FromString(actual) 48 require.NoError(t, err) 49 assert.Equal(t, tt.namespace, decoded.Namespace) 50 if decoded.Namespace != "" { 51 assert.Equal(t, tt.name, fmt.Sprintf("%s/%s", decoded.Namespace, decoded.Name)) 52 } else { 53 assert.Equal(t, tt.name, decoded.Name) 54 } 55 assert.Equal(t, tt.version, decoded.Version) 56 }) 57 } 58 }