Skip to content

Commit

Permalink
WIP - confusing test cases
Browse files Browse the repository at this point in the history
Signed-off-by: Will Murphy <[email protected]>
  • Loading branch information
willmurphyscode committed Aug 18, 2023
1 parent 74e6851 commit caa6941
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 20 deletions.
8 changes: 3 additions & 5 deletions grype/pkg/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/anchore/syft/syft/linux"
"github.com/anchore/syft/syft/pkg"
cpes "github.com/anchore/syft/syft/pkg/cataloger/common/cpe"
"github.com/anchore/syft/syft/sbom"
)

// the source-rpm field has something akin to "util-linux-ng-2.17.2-12.28.el6_9.2.src.rpm"
Expand Down Expand Up @@ -103,17 +102,16 @@ func (p Package) String() string {
return fmt.Sprintf("Pkg(type=%s, name=%s, version=%s, upstreams=%d)", p.Type, p.Name, p.Version, len(p.Upstreams))
}

func removePackagesByOverlap(sbm *sbom.SBOM) *pkg.Collection {
catalog := sbm.Artifacts.Packages
func removePackagesByOverlap(catalog *pkg.Collection, relationships []artifact.Relationship, distro *linux.Release) *pkg.Collection {
byOverlap := map[artifact.ID]artifact.Relationship{}
for _, r := range sbm.Relationships {
for _, r := range relationships {
if r.Type == artifact.OwnershipByFileOverlapRelationship {
byOverlap[r.To.ID()] = r
}
}

out := pkg.NewCollection()
comprehensiveDistroFeed := distroFeedIsComprehensive(sbm.Artifacts.LinuxDistribution)
comprehensiveDistroFeed := distroFeedIsComprehensive(distro)
for p := range catalog.Enumerate() {
r, ok := byOverlap[p.ID()]
if ok {
Expand Down
12 changes: 6 additions & 6 deletions grype/pkg/package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,7 @@ func intRef(i int) *int {
return &i
}

func Test_RemoveBinaryPackagesByOverlap(t *testing.T) {
func Test_RemovePackagesByOverlap(t *testing.T) {
tests := []struct {
name string
sbom *sbom.SBOM
Expand Down Expand Up @@ -709,16 +709,16 @@ func Test_RemoveBinaryPackagesByOverlap(t *testing.T) {
{
name: "does not exclude if OS package owns OS package",
sbom: catalogWithOverlaps(
[]string{"rpm:perl@19.2-r1", "rpm:libperl@19.2"},
[]string{"rpm:perl@19.2-r1 -> rpm:libperl@19.2"}),
expectedPackages: []string{"apk:[email protected]", "rpm:[email protected]-r1"},
[]string{"rpm:perl@5.3-r1", "rpm:libperl@5.3"},
[]string{"rpm:perl@5.3-r1 -> rpm:libperl@5.3"}),
expectedPackages: []string{"rpm:[email protected]", "rpm:[email protected]-r1"},
},
{
name: "does not exclude if owning package is non-OS",
sbom: catalogWithOverlaps(
[]string{"python:[email protected]", "python:[email protected]"},
[]string{"python:[email protected] -> python:[email protected]"}),
expectedPackages: []string{"apk:[email protected]", "rpm:[email protected]"},
expectedPackages: []string{"python:[email protected]", "python:[email protected]"},
},
{
name: "python bindings for system RPM install",
Expand All @@ -737,7 +737,7 @@ func Test_RemoveBinaryPackagesByOverlap(t *testing.T) {
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
catalog := removePackagesByOverlap(test.sbom)
catalog := removePackagesByOverlap(test.sbom.Artifacts.Packages, test.sbom.Relationships, test.sbom.Artifacts.LinuxDistribution)
pkgs := FromCollection(catalog, SynthesisConfig{})
var pkgNames []string
for _, p := range pkgs {
Expand Down
9 changes: 1 addition & 8 deletions grype/pkg/syft_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,8 @@ func syftProvider(userInput string, config ProviderConfig) ([]Package, Context,
if err != nil {
return nil, Context{}, nil, err
}
s := &sbom.SBOM{
Artifacts: sbom.Artifacts{
Packages: catalog,
LinuxDistribution: theDistro,
},
Relationships: relationships,
}

catalog = removePackagesByOverlap(s)
catalog = removePackagesByOverlap(catalog, relationships, theDistro)

srcDescription := src.Describe()

Expand Down
2 changes: 1 addition & 1 deletion grype/pkg/syft_sbom_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func syftSBOMProvider(userInput string, config ProviderConfig) ([]Package, Conte
return nil, Context{}, nil, err
}

catalog := removePackagesByOverlap(s)
catalog := removePackagesByOverlap(s.Artifacts.Packages, s.Relationships, s.Artifacts.LinuxDistribution)

return FromCollection(catalog, config.SynthesisConfig), Context{
Source: &s.Source,
Expand Down

0 comments on commit caa6941

Please sign in to comment.