github.com/anchore/syft@v1.4.2-0.20240516191711-1bec1fc5d397/cmd/syft/internal/options/catalog_test.go (about) 1 package options 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8 ) 9 10 func TestCatalog_PostLoad(t *testing.T) { 11 12 tests := []struct { 13 name string 14 options Catalog 15 assert func(t *testing.T, options Catalog) 16 wantErr assert.ErrorAssertionFunc 17 }{ 18 { 19 name: "mutually exclusive cataloger flags (cat / def-cat)", 20 options: Catalog{ 21 Catalogers: []string{"foo,bar", "42"}, 22 DefaultCatalogers: []string{"some,thing"}, 23 Scope: "squashed", 24 }, 25 wantErr: assert.Error, 26 }, 27 { 28 name: "mutually exclusive cataloger flags (cat / sel-cat)", 29 options: Catalog{ 30 Catalogers: []string{"foo,bar", "42"}, 31 SelectCatalogers: []string{"some,thing"}, 32 Scope: "squashed", 33 }, 34 wantErr: assert.Error, 35 }, 36 { 37 name: "allow old cataloger flags", 38 options: Catalog{ 39 Catalogers: []string{"foo,bar"}, 40 Scope: "squashed", 41 }, 42 assert: func(t *testing.T, options Catalog) { 43 assert.Equal(t, []string{"bar", "foo"}, options.DefaultCatalogers) // note: sorted order 44 assert.Equal(t, []string{"bar", "foo"}, options.Catalogers) // note: sorted order 45 }, 46 }, 47 { 48 name: "allow new cataloger flags", 49 options: Catalog{ 50 SelectCatalogers: []string{"foo,bar", "42"}, 51 DefaultCatalogers: []string{"some,thing"}, 52 Scope: "squashed", 53 }, 54 assert: func(t *testing.T, options Catalog) { 55 assert.Equal(t, []string{"42", "bar", "foo"}, options.SelectCatalogers) // note: sorted order 56 assert.Equal(t, []string{"some", "thing"}, options.DefaultCatalogers) // note: sorted order 57 assert.Empty(t, options.Catalogers) 58 }, 59 }, 60 } 61 for _, tt := range tests { 62 t.Run(tt.name, func(t *testing.T) { 63 if tt.wantErr == nil { 64 tt.wantErr = assert.NoError 65 } 66 tt.wantErr(t, tt.options.PostLoad(), fmt.Sprintf("PostLoad()")) 67 if tt.assert != nil { 68 tt.assert(t, tt.options) 69 } 70 }) 71 } 72 }