github.com/google/osv-scalibr@v0.4.1/extractor/standalone/windows/common/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/standalone/windows/common/metadata"
    22  	winpurl "github.com/google/osv-scalibr/extractor/standalone/windows/common/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:    "no_os_version",
    36  			name:    "name",
    37  			version: "version",
    38  			want: &purl.PackageURL{
    39  				Type:      purl.TypeGeneric,
    40  				Namespace: "microsoft",
    41  				Name:      "name",
    42  				Version:   "version",
    43  			},
    44  		},
    45  		{
    46  			desc:    "os_version",
    47  			name:    "name",
    48  			version: "version",
    49  			metadata: &metadata.OSVersion{
    50  				Product:     "product",
    51  				FullVersion: "full-version",
    52  			},
    53  			want: &purl.PackageURL{
    54  				Type:      purl.TypeGeneric,
    55  				Namespace: "microsoft",
    56  				Name:      "name",
    57  				Version:   "version",
    58  				Qualifiers: purl.QualifiersFromMap(map[string]string{
    59  					purl.BuildNumber: "full-version",
    60  				}),
    61  			},
    62  		},
    63  	}
    64  
    65  	for _, tt := range tests {
    66  		t.Run(tt.desc, func(t *testing.T) {
    67  			got := winpurl.MakePackageURL(tt.name, tt.version, tt.metadata)
    68  			if diff := cmp.Diff(tt.want, got); diff != "" {
    69  				t.Errorf("winpurl.MakePackageURL(%v, %v, %v): unexpected PURL (-want +got):\n%s", tt.name, tt.version, tt.metadata, diff)
    70  			}
    71  		})
    72  	}
    73  }