github.com/google/osv-scalibr@v0.4.1/extractor/filesystem/language/javascript/purl/purl_test.go (about) 1 // Copyright 2025 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package purl_test 16 17 import ( 18 "testing" 19 20 "github.com/google/go-cmp/cmp" 21 "github.com/google/osv-scalibr/extractor/filesystem/language/javascript/packagejson/metadata" 22 npmpurl "github.com/google/osv-scalibr/extractor/filesystem/language/javascript/purl" 23 "github.com/google/osv-scalibr/purl" 24 ) 25 26 func TestMakePackageURL(t *testing.T) { 27 tests := []struct { 28 desc string 29 name string 30 version string 31 metadata any 32 want *purl.PackageURL 33 }{ 34 { 35 desc: "lowercase_name", 36 name: "name", 37 version: "version", 38 want: &purl.PackageURL{ 39 Type: purl.TypeNPM, 40 Name: "name", 41 Version: "version", 42 }, 43 }, 44 { 45 desc: "respects_mixed_case", 46 name: "Name", 47 version: "version", 48 want: &purl.PackageURL{ 49 Type: purl.TypeNPM, 50 Name: "Name", 51 Version: "version", 52 }, 53 }, 54 { 55 desc: "source_public_registry_qualifier_set", 56 name: "Name", 57 version: "version", 58 metadata: &metadata.JavascriptPackageJSONMetadata{ 59 Source: metadata.PublicRegistry, 60 }, 61 want: &purl.PackageURL{ 62 Type: purl.TypeNPM, 63 Name: "Name", 64 Version: "version", 65 Qualifiers: purl.QualifiersFromMap(map[string]string{ 66 "source": "PUBLIC_REGISTRY", 67 }), 68 }, 69 }, 70 { 71 desc: "source_other_qualifier_set", 72 name: "Name", 73 version: "version", 74 metadata: &metadata.JavascriptPackageJSONMetadata{ 75 Source: metadata.Other, 76 }, 77 want: &purl.PackageURL{ 78 Type: purl.TypeNPM, 79 Name: "Name", 80 Version: "version", 81 Qualifiers: purl.QualifiersFromMap(map[string]string{ 82 "source": "OTHER", 83 }), 84 }, 85 }, 86 { 87 desc: "source_local_qualifier_set", 88 name: "Name", 89 version: "version", 90 metadata: &metadata.JavascriptPackageJSONMetadata{ 91 Source: metadata.Local, 92 }, 93 want: &purl.PackageURL{ 94 Type: purl.TypeNPM, 95 Name: "Name", 96 Version: "version", 97 Qualifiers: purl.QualifiersFromMap(map[string]string{ 98 "source": "LOCAL", 99 }), 100 }, 101 }, 102 { 103 desc: "source_unknown_returns_no_qualifier_set", 104 name: "Name", 105 version: "version", 106 metadata: &metadata.JavascriptPackageJSONMetadata{ 107 Source: metadata.Unknown, 108 }, 109 want: &purl.PackageURL{ 110 Type: purl.TypeNPM, 111 Name: "Name", 112 Version: "version", 113 }, 114 }, 115 } 116 117 for _, tt := range tests { 118 t.Run(tt.desc, func(t *testing.T) { 119 got := npmpurl.MakePackageURL(tt.name, tt.version, tt.metadata) 120 if diff := cmp.Diff(tt.want, got); diff != "" { 121 t.Errorf("npmpurl.MakePackageURL(%v, %v): unexpected PURL (-want +got):\n%s", tt.name, tt.version, diff) 122 } 123 }) 124 } 125 }