github.com/google/osv-scalibr@v0.4.1/extractor/filesystem/sbom/cdx/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  	cdxmeta "github.com/google/osv-scalibr/extractor/filesystem/sbom/cdx/metadata"
    22  	cdxpurl "github.com/google/osv-scalibr/extractor/filesystem/sbom/cdx/purl"
    23  	"github.com/google/osv-scalibr/purl"
    24  )
    25  
    26  func TestMakePackageURL(t *testing.T) {
    27  	tests := []struct {
    28  		desc     string
    29  		metadata any
    30  		want     *purl.PackageURL
    31  	}{
    32  		{
    33  			desc:     "metadata_not_available",
    34  			metadata: nil,
    35  			want:     nil,
    36  		},
    37  		{
    38  			desc: "metadata_available",
    39  			metadata: &cdxmeta.Metadata{
    40  				PURL: &purl.PackageURL{
    41  					Type:      purl.TypePyPi,
    42  					Name:      "name",
    43  					Namespace: "namespace",
    44  					Version:   "1.2.3",
    45  				},
    46  				CPEs: []string{},
    47  			},
    48  			want: &purl.PackageURL{
    49  				Type:      purl.TypePyPi,
    50  				Name:      "name",
    51  				Namespace: "namespace",
    52  				Version:   "1.2.3",
    53  			},
    54  		},
    55  	}
    56  
    57  	for _, tt := range tests {
    58  		t.Run(tt.desc, func(t *testing.T) {
    59  			got := cdxpurl.MakePackageURL(tt.metadata)
    60  			if diff := cmp.Diff(tt.want, got); diff != "" {
    61  				t.Errorf("cdxpurl.MakePackageURL(%v): unexpected PURL (-want +got):\n%s", tt.metadata, diff)
    62  			}
    63  		})
    64  	}
    65  }