Fix package url for Go modules with no / (#1092)

This commit is contained in:
Rob Best 2022-07-11 15:07:34 +01:00 committed by GitHub
parent b3a7b912e1
commit 2f1aa33ce8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 3 deletions

View File

@ -47,8 +47,10 @@ func URL(p Package, release *linux.Release) string {
case p.Type == GoModulePkg:
re := regexp.MustCompile(`(/)[^/]*$`)
fields := re.Split(p.Name, -1)
namespace = fields[0]
name = strings.TrimPrefix(p.Name, namespace+"/")
if len(fields) > 1 {
namespace = fields[0]
name = strings.TrimPrefix(p.Name, namespace+"/")
}
case p.Type == NpmPkg:
fields := strings.SplitN(p.Name, "/", 2)
if len(fields) > 1 {

View File

@ -25,6 +25,15 @@ func TestPackageURL(t *testing.T) {
},
expected: "pkg:golang/github.com/anchore/syft@v0.1.0",
},
{
name: "golang short name",
pkg: Package{
Name: "go.opencensus.io",
Version: "v0.23.0",
Type: GoModulePkg,
},
expected: "pkg:golang/go.opencensus.io@v0.23.0",
},
{
name: "pub",
pkg: Package{
@ -237,7 +246,7 @@ func TestPackageURL(t *testing.T) {
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if test.pkg.Type != "" {
if test.pkg.Type != "" && !contains(pkgTypes, string(test.pkg.Type)) {
pkgTypes = append(pkgTypes, string(test.pkg.Type))
}
actual := URL(test.pkg, test.distro)
@ -250,3 +259,13 @@ func TestPackageURL(t *testing.T) {
}
assert.ElementsMatch(t, expectedTypes.List(), pkgTypes, "missing one or more package types to test against (maybe a package type was added?)")
}
func contains(values []string, val string) bool {
for _, v := range values {
if val == v {
return true
}
}
return false
}