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

@ -12,13 +12,13 @@ import (
type ContentsCataloger struct {
globs []string
skipFilesAboveSize int64
skipFilesAboveSizeInBytes int64
}
func NewContentsCataloger(globs []string, skipFilesAboveSize int64) (*ContentsCataloger, error) {
return &ContentsCataloger{
globs: globs,
skipFilesAboveSize: skipFilesAboveSize,
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

View File

@ -16,7 +16,6 @@ func TestContentsCataloger(t *testing.T) {
maxSize int64
files []string
expected map[source.Location]string
catalogErr bool
}{
{
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")
})

View File

@ -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]