diff --git a/syft/file/contents_cataloger.go b/syft/file/contents_cataloger.go index 2a67b4c2e..97652868c 100644 --- a/syft/file/contents_cataloger.go +++ b/syft/file/contents_cataloger.go @@ -11,14 +11,14 @@ import ( ) type ContentsCataloger struct { - globs []string - skipFilesAboveSize int64 + globs []string + skipFilesAboveSizeInBytes int64 } func NewContentsCataloger(globs []string, skipFilesAboveSize int64) (*ContentsCataloger, error) { return &ContentsCataloger{ - globs: globs, - skipFilesAboveSize: skipFilesAboveSize, + globs: globs, + skipFilesAboveSizeInBytes: skipFilesAboveSize, }, nil } @@ -37,7 +37,7 @@ func (i *ContentsCataloger) Catalog(resolver source.FileResolver) (map[source.Lo return nil, err } - if i.skipFilesAboveSize > 0 && metadata.Size > i.skipFilesAboveSize { + if i.skipFilesAboveSizeInBytes > 0 && metadata.Size > i.skipFilesAboveSizeInBytes { continue } @@ -61,7 +61,7 @@ func (i *ContentsCataloger) catalogLocation(resolver source.FileResolver, locati buf := &bytes.Buffer{} if _, err = io.Copy(base64.NewEncoder(base64.StdEncoding, buf), contentReader); err != nil { - return "", fmt.Errorf("unable to observe contents of %+v: %+v", location.RealPath, err) + return "", fmt.Errorf("unable to observe contents of %+v: %w", location.RealPath, err) } return buf.String(), nil diff --git a/syft/file/contents_cataloger_test.go b/syft/file/contents_cataloger_test.go index eb83d21aa..c6c1b0db9 100644 --- a/syft/file/contents_cataloger_test.go +++ b/syft/file/contents_cataloger_test.go @@ -11,12 +11,11 @@ func TestContentsCataloger(t *testing.T) { allFiles := []string{"test-fixtures/last/path.txt", "test-fixtures/another-path.txt", "test-fixtures/a-path.txt"} tests := []struct { - name string - globs []string - maxSize int64 - files []string - expected map[source.Location]string - catalogErr bool + name string + globs []string + maxSize int64 + files []string + expected map[source.Location]string }{ { name: "multi-pattern", @@ -68,20 +67,11 @@ func TestContentsCataloger(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { c, err := NewContentsCataloger(test.globs, test.maxSize) - if err != nil { - t.Fatalf("could not create cataloger: %+v", err) - } + assert.NoError(t, err) resolver := source.NewMockResolverForPaths(test.files...) actual, err := c.Catalog(resolver) - if err != nil && !test.catalogErr { - t.Fatalf("could not catalog (but should have been able to): %+v", err) - } else if err == nil && test.catalogErr { - t.Fatalf("expected catalog error but did not get one") - } else if test.catalogErr && err != nil { - return - } - + assert.NoError(t, err) assert.Equal(t, test.expected, actual, "mismatched contents") }) diff --git a/syft/pkg/cataloger/common/generic_cataloger_test.go b/syft/pkg/cataloger/common/generic_cataloger_test.go index 594436447..a30a3d5f7 100644 --- a/syft/pkg/cataloger/common/generic_cataloger_test.go +++ b/syft/pkg/cataloger/common/generic_cataloger_test.go @@ -6,6 +6,8 @@ import ( "io/ioutil" "testing" + "github.com/stretchr/testify/assert" + "github.com/anchore/syft/syft/pkg" "github.com/anchore/syft/syft/source" ) @@ -46,13 +48,8 @@ func TestGenericCataloger(t *testing.T) { } actualPkgs, err := cataloger.Catalog(resolver) - if err != nil { - t.Fatalf("cataloger catalog action failed: %+v", err) - } - - if len(actualPkgs) != len(expectedPkgs) { - t.Fatalf("unexpected packages len: %d != %d", len(expectedPkgs), len(actualPkgs)) - } + assert.NoError(t, err) + assert.Len(t, actualPkgs, len(expectedPkgs)) for _, p := range actualPkgs { ref := p.Locations[0]