Skip to content

Commit

Permalink
Make tests pass
Browse files Browse the repository at this point in the history
Signed-off-by: C0D3 M4513R <28912031+C0D3-M4513R@users.noreply.github.com>
  • Loading branch information
C0D3-M4513R committed Jun 13, 2024
1 parent 5b31935 commit ff260d0
Show file tree
Hide file tree
Showing 28 changed files with 497 additions and 288 deletions.
15 changes: 15 additions & 0 deletions examples/create_custom_sbom/alpine_configuration_cataloger.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"context"
"fmt"
"github.com/anchore/syft/syft/sort"
"io"
"path"

Expand Down Expand Up @@ -125,3 +126,17 @@ type AlpineConfiguration struct {
APKKeys map[string]string `json:"apkKeys" yaml:"apkKeys"`
// Add more data you want to capture as part of the package metadata here...
}

func (m AlpineConfiguration) Compare(other AlpineConfiguration) int {
if i := sort.CompareMapOrd(m.APKKeys, other.APKKeys); i != 0 {
return i
}
return 0
}

func (m AlpineConfiguration) TryCompare(other any) (bool, int) {
if other, exists := other.(AlpineConfiguration); exists {
return true, m.Compare(other)
}
return false, 0
}
8 changes: 5 additions & 3 deletions internal/cmptest/relationship.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmptest

import (
"github.com/anchore/syft/syft/sort"
"github.com/sanity-io/litter"

"github.com/anchore/syft/syft/artifact"
Expand All @@ -20,7 +21,8 @@ var relationshipStringer = litter.Options{

func DefaultRelationshipComparer(x, y artifact.Relationship) bool {
// we just need a stable sort, the ordering does not need to be sensible
xStr := relationshipStringer.Sdump(x)
yStr := relationshipStringer.Sdump(y)
return xStr < yStr
//xStr := relationshipStringer.Sdump(x)
//yStr := relationshipStringer.Sdump(y)
//return xStr < yStr
return sort.Less(x, y)
}
8 changes: 4 additions & 4 deletions internal/relationship/evident_by_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ func TestRelationshipsEvidentBy(t *testing.T) {
catalog: c,
want: []artifact.Relationship{
{
From: pkgB,
To: coordB,
From: pkgA,
To: coordA,
Type: artifact.EvidentByRelationship,
},
{
From: pkgA,
To: coordA,
From: pkgB,
To: coordB,
Type: artifact.EvidentByRelationship,
},
},
Expand Down
15 changes: 15 additions & 0 deletions internal/relationship/index_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package relationship

import (
"github.com/anchore/syft/syft/sort"
"testing"

"github.com/google/go-cmp/cmp"
Expand Down Expand Up @@ -223,3 +224,17 @@ type fakeIdentifiable struct {
func (f fakeIdentifiable) ID() artifact.ID {
return artifact.ID(f.id)
}

func (m fakeIdentifiable) Compare(other fakeIdentifiable) int {
if i := sort.CompareOrd(m.id, other.id); i != 0 {
return i
}
return 0
}

func (m fakeIdentifiable) TryCompare(other any) (bool, int) {
if other, exists := other.(fakeIdentifiable); exists {
return true, m.Compare(other)
}
return false, 0
}
34 changes: 4 additions & 30 deletions internal/relationship/sort.go
Original file line number Diff line number Diff line change
@@ -1,42 +1,16 @@
package relationship

import (
"sort"

"github.com/anchore/syft/syft/artifact"
"github.com/anchore/syft/syft/pkg"
"github.com/anchore/syft/syft/sort"
"slices"
)

// Sort takes a set of package-to-package relationships and sorts them in a stable order by name and version.
// Note: this does not consider package-to-other, other-to-package, or other-to-other relationships.
// TODO: ideally this should be replaced with a more type-agnostic sort function that resides in the artifact package.
func Sort(rels []artifact.Relationship) {
sort.SliceStable(rels, func(i, j int) bool {
return less(rels[i], rels[j])
slices.SortStableFunc(rels, func(i, j artifact.Relationship) int {
return sort.Compare(i, j)
})
}

func less(i, j artifact.Relationship) bool {
iFrom, ok1 := i.From.(pkg.Package)
iTo, ok2 := i.To.(pkg.Package)
jFrom, ok3 := j.From.(pkg.Package)
jTo, ok4 := j.To.(pkg.Package)

if !(ok1 && ok2 && ok3 && ok4) {
return false
}

if iFrom.Name != jFrom.Name {
return iFrom.Name < jFrom.Name
}
if iFrom.Version != jFrom.Version {
return iFrom.Version < jFrom.Version
}
if iTo.Name != jTo.Name {
return iTo.Name < jTo.Name
}
if iTo.Version != jTo.Version {
return iTo.Version < jTo.Version
}
return i.Type < j.Type
}
62 changes: 48 additions & 14 deletions syft/artifact/relationship.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,36 +47,70 @@ type Relationship struct {
From Identifiable
To Identifiable
Type RelationshipType
Data sort.TryComparable
// FIXME map[string]interface{} is not TryComparable. Thus this field can't be.
Data interface{}
}

func (rel Relationship) Compare(other Relationship) int {
if ok, i := rel.From.TryCompare(other.From); ok {
if i != 0 {
if rel.From != nil && other.From == nil {
return 1
}
if rel.From == nil && other.From != nil {
return -1
}
if rel.From != nil {
if i := sort.CompareOrd(reflect.ValueOf(rel.From).Type().Name(), reflect.ValueOf(other.From).Type().Name()); i != 0 {
return i
}
} else {
if i := rel.From.ID().Compare(other.From.ID()); i != 0 {
return i
if ok, i := rel.From.TryCompare(other.From); ok {
if i != 0 {
return i
}
} else {
if i := rel.From.ID().Compare(other.From.ID()); i != 0 {
return i
}
}
}
if ok, i := rel.To.TryCompare(other.To); ok {
if i != 0 {
if rel.To != nil && other.To == nil {
return 1
}
if rel.To == nil && other.To != nil {
return -1
}
if rel.To != nil {
if i := sort.CompareOrd(reflect.ValueOf(rel.To).Type().Name(), reflect.ValueOf(other.To).Type().Name()); i != 0 {
return i
}
} else {
if i := rel.To.ID().Compare(other.To.ID()); i != 0 {
return i
if ok, i := rel.To.TryCompare(other.To); ok {
if i != 0 {
return i
}
} else {
if i := rel.To.ID().Compare(other.To.ID()); i != 0 {
return i
}
}
}

if i := rel.Type.Compare(other.Type); i != 0 {
return i
}

if ok, i := sort.TryCompare(rel.Data, other.Data); ok {
return i
if rel.Data != nil && other.Data == nil {
return 1
}
if rel.Data == nil && other.Data != nil {
return -1
}
if rel.Data != nil {
if i := sort.CompareOrd(reflect.ValueOf(rel.Data).Type().Name(), reflect.ValueOf(other.Data).Type().Name()); i != 0 {
return i
}
if ok, i := sort.TryCompare(rel.Data, other.Data); ok {
return i
}
}

return sort.CompareOrd(reflect.ValueOf(rel.Data).Type().Name(), reflect.ValueOf(other.Data).Type().Name())
return 0
}
11 changes: 10 additions & 1 deletion syft/create_sbom_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ func Test_findDefaultTag(t *testing.T) {
{
name: "unknown",
src: source.Description{
Metadata: struct{}{},
Metadata: emptyMeta{},
},
wantErr: require.Error,
},
Expand All @@ -478,6 +478,15 @@ func Test_findDefaultTag(t *testing.T) {
}
}

type emptyMeta struct{}

func (e emptyMeta) TryCompare(t any) (bool, int) {
if _, ok := t.(emptyMeta); ok {
return true, 0
}
return false, 0
}

func TestCreateSBOMConfig_validate(t *testing.T) {
tests := []struct {
name string
Expand Down
10 changes: 5 additions & 5 deletions syft/format/common/cyclonedxhelpers/to_format_model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,16 @@ func Test_relationships(t *testing.T) {
},
expected: &[]cyclonedx.Dependency{
{
Ref: helpers.DeriveBomRef(p1),
Ref: helpers.DeriveBomRef(p2),
Dependencies: &[]string{
helpers.DeriveBomRef(p2),
helpers.DeriveBomRef(p3),
helpers.DeriveBomRef(p4),
},
},
{
Ref: helpers.DeriveBomRef(p2),
Ref: helpers.DeriveBomRef(p1),
Dependencies: &[]string{
helpers.DeriveBomRef(p4),
helpers.DeriveBomRef(p2),
helpers.DeriveBomRef(p3),
},
},
},
Expand Down
11 changes: 10 additions & 1 deletion syft/format/common/spdxhelpers/to_format_model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ func Test_toPackageChecksums(t *testing.T) {
Name: "test",
Version: "1.0.0",
Language: pkg.Java,
Metadata: struct{}{},
Metadata: emptyMeta{},
},
expected: []spdx.Checksum{},
filesAnalyzed: false,
Expand All @@ -356,6 +356,15 @@ func Test_toPackageChecksums(t *testing.T) {
}
}

type emptyMeta struct{}

func (e emptyMeta) TryCompare(t any) (bool, int) {
if _, ok := t.(emptyMeta); ok {
return true, 0
}
return false, 0
}

func Test_toFileTypes(t *testing.T) {

tests := []struct {
Expand Down
4 changes: 2 additions & 2 deletions syft/format/common/spdxhelpers/to_syft_model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,13 @@ func TestToSyftModel(t *testing.T) {

assert.Len(t, pkgs, 2)

p1 := pkgs[0]
p1 := pkgs[1]
assert.Equal(t, p1.Name, "pkg-1")
p1meta := p1.Metadata.(pkg.ApkDBEntry)
assert.Equal(t, p1meta.OriginPackage, "p1-origin")
assert.Len(t, p1.CPEs, 2)

p2 := pkgs[1]
p2 := pkgs[0]
assert.Equal(t, p2.Name, "pkg-2")
p2meta := p2.Metadata.(pkg.DpkgDBEntry)
assert.Equal(t, p2meta.Source, "p2-origin")
Expand Down
Loading

0 comments on commit ff260d0

Please sign in to comment.