simplify test assertions + rename file contents cataloger size limiter var

Signed-off-by: Alex Goodman <alex.goodman@anchore.com>
This commit is contained in:
Alex Goodman 2021-04-12 17:24:08 -04:00
parent 51b13f8221
commit b5d4b2f7b2
No known key found for this signature in database
GPG Key ID: 5CB45AE22BAB7EA7
3 changed files with 17 additions and 30 deletions

View File

@ -11,14 +11,14 @@ import (
) )
type ContentsCataloger struct { type ContentsCataloger struct {
globs []string globs []string
skipFilesAboveSize int64 skipFilesAboveSizeInBytes int64
} }
func NewContentsCataloger(globs []string, skipFilesAboveSize int64) (*ContentsCataloger, error) { func NewContentsCataloger(globs []string, skipFilesAboveSize int64) (*ContentsCataloger, error) {
return &ContentsCataloger{ return &ContentsCataloger{
globs: globs, globs: globs,
skipFilesAboveSize: skipFilesAboveSize, skipFilesAboveSizeInBytes: skipFilesAboveSize,
}, nil }, nil
} }
@ -37,7 +37,7 @@ func (i *ContentsCataloger) Catalog(resolver source.FileResolver) (map[source.Lo
return nil, err return nil, err
} }
if i.skipFilesAboveSize > 0 && metadata.Size > i.skipFilesAboveSize { if i.skipFilesAboveSizeInBytes > 0 && metadata.Size > i.skipFilesAboveSizeInBytes {
continue continue
} }
@ -61,7 +61,7 @@ func (i *ContentsCataloger) catalogLocation(resolver source.FileResolver, locati
buf := &bytes.Buffer{} buf := &bytes.Buffer{}
if _, err = io.Copy(base64.NewEncoder(base64.StdEncoding, buf), contentReader); err != nil { 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 return buf.String(), nil

View File

@ -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"} allFiles := []string{"test-fixtures/last/path.txt", "test-fixtures/another-path.txt", "test-fixtures/a-path.txt"}
tests := []struct { tests := []struct {
name string name string
globs []string globs []string
maxSize int64 maxSize int64
files []string files []string
expected map[source.Location]string expected map[source.Location]string
catalogErr bool
}{ }{
{ {
name: "multi-pattern", name: "multi-pattern",
@ -68,20 +67,11 @@ func TestContentsCataloger(t *testing.T) {
for _, test := range tests { for _, test := range tests {
t.Run(test.name, func(t *testing.T) { t.Run(test.name, func(t *testing.T) {
c, err := NewContentsCataloger(test.globs, test.maxSize) c, err := NewContentsCataloger(test.globs, test.maxSize)
if err != nil { assert.NoError(t, err)
t.Fatalf("could not create cataloger: %+v", err)
}
resolver := source.NewMockResolverForPaths(test.files...) resolver := source.NewMockResolverForPaths(test.files...)
actual, err := c.Catalog(resolver) actual, err := c.Catalog(resolver)
if err != nil && !test.catalogErr { assert.NoError(t, err)
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.Equal(t, test.expected, actual, "mismatched contents") assert.Equal(t, test.expected, actual, "mismatched contents")
}) })

View File

@ -6,6 +6,8 @@ import (
"io/ioutil" "io/ioutil"
"testing" "testing"
"github.com/stretchr/testify/assert"
"github.com/anchore/syft/syft/pkg" "github.com/anchore/syft/syft/pkg"
"github.com/anchore/syft/syft/source" "github.com/anchore/syft/syft/source"
) )
@ -46,13 +48,8 @@ func TestGenericCataloger(t *testing.T) {
} }
actualPkgs, err := cataloger.Catalog(resolver) actualPkgs, err := cataloger.Catalog(resolver)
if err != nil { assert.NoError(t, err)
t.Fatalf("cataloger catalog action failed: %+v", err) assert.Len(t, actualPkgs, len(expectedPkgs))
}
if len(actualPkgs) != len(expectedPkgs) {
t.Fatalf("unexpected packages len: %d != %d", len(expectedPkgs), len(actualPkgs))
}
for _, p := range actualPkgs { for _, p := range actualPkgs {
ref := p.Locations[0] ref := p.Locations[0]