Skip to content

Commit

Permalink
fix: anchore#953 Derive language from pURL - anchore#953
Browse files Browse the repository at this point in the history
Signed-off-by: Jon McEwen <jon_mcewen@hotmail.com>
  • Loading branch information
jonmcewen committed Apr 13, 2022
1 parent 02a8fb6 commit f87d695
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
4 changes: 4 additions & 0 deletions internal/formats/common/cyclonedxhelpers/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ func decodeComponent(c *cyclonedx.Component) *pkg.Package {
p.Type = pkg.TypeFromPURL(p.PURL)
}

if p.Language == "" {
p.Language = pkg.LanguageFromPURL(p.PURL)
}

return p
}

Expand Down
28 changes: 28 additions & 0 deletions internal/formats/common/cyclonedxhelpers/component_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,31 @@ func Test_deriveBomRef(t *testing.T) {
})
}
}

func Test_decodeComponent(t *testing.T) {
javaComponentWithNoSyftProperties := cyclonedx.Component{
Name: "ch.qos.logback/logback-classic",
Version: "1.2.3",
PackageURL: "pkg:maven/ch.qos.logback/logback-classic@1.2.3",
Type: "library",
BOMRef: "pkg:maven/ch.qos.logback/logback-classic@1.2.3",
}

tests := []struct {
name string
component cyclonedx.Component
want pkg.Language
}{
{
name: "derive language from pURL if missing",
component: javaComponentWithNoSyftProperties,
want: pkg.Java,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.want, decodeComponent(&tt.component).Language)
})
}
}

0 comments on commit f87d695

Please sign in to comment.