Require ordering of relationships when comparing parser output (#2160)

* require ordering of relationships when comparing parser output

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>

* [wip] fix cataloger test

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>

* change method of relationship sort to simple string dump

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>

---------

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
This commit is contained in:
Alex Goodman
2023-09-20 17:39:18 +00:00
committed by GitHub
parent b8f52d570e
commit 58f8c852df
5 changed files with 32 additions and 0 deletions
@@ -271,6 +271,11 @@ func TestParseConanlock(t *testing.T) {
},
}
// relationships require IDs to be set to be sorted similarly
for i := range expected {
expected[i].SetID()
}
var expectedRelationships = []artifact.Relationship{
{
From: expected[1], // boost
@@ -9,6 +9,7 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/sanity-io/litter"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
@@ -252,6 +253,23 @@ func (p *CatalogTester) TestCataloger(t *testing.T, cataloger pkg.Cataloger) {
}
}
var relationshipStringer = litter.Options{
Compact: true,
StripPackageNames: false,
HidePrivateFields: true, // we want to ignore package IDs
HideZeroValues: true,
StrictGo: true,
//FieldExclusions: ... // these can be added for future values that need to be ignored
//FieldFilter: ...
}
func relationshipLess(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
}
// nolint:funlen
func (p *CatalogTester) assertPkgs(t *testing.T, pkgs []pkg.Package, relationships []artifact.Relationship) {
t.Helper()
@@ -259,6 +277,7 @@ func (p *CatalogTester) assertPkgs(t *testing.T, pkgs []pkg.Package, relationshi
p.compareOptions = append(p.compareOptions,
cmpopts.IgnoreFields(pkg.Package{}, "id"), // note: ID is not deterministic for test purposes
cmpopts.SortSlices(pkg.Less),
cmpopts.SortSlices(relationshipLess),
cmp.Comparer(
func(x, y file.LocationSet) bool {
xs := x.ToSlice()