github.com/anchore/syft@v1.4.2-0.20240516191711-1bec1fc5d397/syft/pkg/cataloger/golang/package_test.go (about)

     1  package golang
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  
     8  	"github.com/anchore/syft/syft/pkg"
     9  )
    10  
    11  func Test_packageURL(t *testing.T) {
    12  
    13  	tests := []struct {
    14  		name     string
    15  		pkg      pkg.Package
    16  		expected string
    17  	}{
    18  		{
    19  			name: "gocase",
    20  			pkg: pkg.Package{
    21  				Name:    "github.com/anchore/syft",
    22  				Version: "v0.1.0",
    23  			},
    24  			expected: "pkg:golang/github.com/anchore/syft@v0.1.0",
    25  		},
    26  		{
    27  			name: "golang short name",
    28  			pkg: pkg.Package{
    29  				Name:    "go.opencensus.io",
    30  				Version: "v0.23.0",
    31  			},
    32  			expected: "pkg:golang/go.opencensus.io@v0.23.0",
    33  		},
    34  		{
    35  			name: "golang with subpath",
    36  			pkg: pkg.Package{
    37  				Name:    "github.com/coreos/go-systemd/v22",
    38  				Version: "v22.1.0",
    39  			},
    40  			expected: "pkg:golang/github.com/coreos/go-systemd@v22.1.0#v22",
    41  		},
    42  		{
    43  			name: "golang with subpath deep",
    44  			pkg: pkg.Package{
    45  				Name: "google.golang.org/genproto/googleapis/api/annotations",
    46  			},
    47  			expected: "pkg:golang/google.golang.org/genproto/googleapis#api/annotations",
    48  		},
    49  	}
    50  
    51  	for _, test := range tests {
    52  		t.Run(test.name, func(t *testing.T) {
    53  			assert.Equal(t, test.expected, packageURL(test.pkg.Name, test.pkg.Version))
    54  		})
    55  	}
    56  }