github.com/nextlinux/gosbom@v0.81.1-0.20230627115839-1ff50c281391/gosbom/file/cataloger/filecontent/cataloger_test.go (about) 1 package filecontent 2 3 import ( 4 "testing" 5 6 "github.com/nextlinux/gosbom/gosbom/file" 7 "github.com/stretchr/testify/assert" 8 ) 9 10 func TestContentsCataloger(t *testing.T) { 11 allFiles := []string{"test-fixtures/last/path.txt", "test-fixtures/another-path.txt", "test-fixtures/a-path.txt"} 12 13 tests := []struct { 14 name string 15 globs []string 16 maxSize int64 17 files []string 18 expected map[file.Coordinates]string 19 }{ 20 { 21 name: "multi-pattern", 22 globs: []string{"test-fixtures/last/*.txt", "test-fixtures/*.txt"}, 23 files: allFiles, 24 expected: map[file.Coordinates]string{ 25 file.NewLocation("test-fixtures/last/path.txt").Coordinates: "dGVzdC1maXh0dXJlcy9sYXN0L3BhdGgudHh0IGZpbGUgY29udGVudHMh", 26 file.NewLocation("test-fixtures/another-path.txt").Coordinates: "dGVzdC1maXh0dXJlcy9hbm90aGVyLXBhdGgudHh0IGZpbGUgY29udGVudHMh", 27 file.NewLocation("test-fixtures/a-path.txt").Coordinates: "dGVzdC1maXh0dXJlcy9hLXBhdGgudHh0IGZpbGUgY29udGVudHMh", 28 }, 29 }, 30 { 31 name: "no-patterns", 32 globs: []string{}, 33 files: []string{"test-fixtures/last/path.txt", "test-fixtures/another-path.txt", "test-fixtures/a-path.txt"}, 34 expected: map[file.Coordinates]string{}, 35 }, 36 { 37 name: "all-txt", 38 globs: []string{"**/*.txt"}, 39 files: allFiles, 40 expected: map[file.Coordinates]string{ 41 file.NewLocation("test-fixtures/last/path.txt").Coordinates: "dGVzdC1maXh0dXJlcy9sYXN0L3BhdGgudHh0IGZpbGUgY29udGVudHMh", 42 file.NewLocation("test-fixtures/another-path.txt").Coordinates: "dGVzdC1maXh0dXJlcy9hbm90aGVyLXBhdGgudHh0IGZpbGUgY29udGVudHMh", 43 file.NewLocation("test-fixtures/a-path.txt").Coordinates: "dGVzdC1maXh0dXJlcy9hLXBhdGgudHh0IGZpbGUgY29udGVudHMh", 44 }, 45 }, 46 { 47 name: "subpath", 48 globs: []string{"test-fixtures/*.txt"}, 49 files: allFiles, 50 expected: map[file.Coordinates]string{ 51 file.NewLocation("test-fixtures/another-path.txt").Coordinates: "dGVzdC1maXh0dXJlcy9hbm90aGVyLXBhdGgudHh0IGZpbGUgY29udGVudHMh", 52 file.NewLocation("test-fixtures/a-path.txt").Coordinates: "dGVzdC1maXh0dXJlcy9hLXBhdGgudHh0IGZpbGUgY29udGVudHMh", 53 }, 54 }, 55 { 56 name: "size-filter", 57 maxSize: 42, 58 globs: []string{"**/*.txt"}, 59 files: allFiles, 60 expected: map[file.Coordinates]string{ 61 file.NewLocation("test-fixtures/last/path.txt").Coordinates: "dGVzdC1maXh0dXJlcy9sYXN0L3BhdGgudHh0IGZpbGUgY29udGVudHMh", 62 file.NewLocation("test-fixtures/a-path.txt").Coordinates: "dGVzdC1maXh0dXJlcy9hLXBhdGgudHh0IGZpbGUgY29udGVudHMh", 63 }, 64 }, 65 } 66 67 for _, test := range tests { 68 t.Run(test.name, func(t *testing.T) { 69 c, err := NewCataloger(test.globs, test.maxSize) 70 assert.NoError(t, err) 71 72 resolver := file.NewMockResolverForPaths(test.files...) 73 actual, err := c.Catalog(resolver) 74 assert.NoError(t, err) 75 assert.Equal(t, test.expected, actual, "mismatched contents") 76 77 }) 78 } 79 }