Skip to content

Commit

Permalink
Fix package url for Go modules with no / (#1092)
Browse files Browse the repository at this point in the history
  • Loading branch information
ribbybibby committed Jul 11, 2022
1 parent b3a7b91 commit 2f1aa33
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
6 changes: 4 additions & 2 deletions syft/pkg/url.go
Expand Up @@ -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 {
Expand Down
21 changes: 20 additions & 1 deletion syft/pkg/url_test.go
Expand Up @@ -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{
Expand Down Expand Up @@ -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)
Expand All @@ -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
}

0 comments on commit 2f1aa33

Please sign in to comment.