github.com/devseccon/trivy@v0.47.1-0.20231123133102-bd902a0bd996/pkg/flag/db_flags_test.go (about) 1 package flag_test 2 3 import ( 4 "testing" 5 6 "github.com/spf13/viper" 7 "github.com/stretchr/testify/assert" 8 "github.com/stretchr/testify/require" 9 "go.uber.org/zap" 10 "go.uber.org/zap/zaptest/observer" 11 12 "github.com/devseccon/trivy/pkg/flag" 13 "github.com/devseccon/trivy/pkg/log" 14 ) 15 16 func TestDBFlagGroup_ToOptions(t *testing.T) { 17 type fields struct { 18 SkipDBUpdate bool 19 DownloadDBOnly bool 20 Light bool 21 } 22 tests := []struct { 23 name string 24 fields fields 25 want flag.DBOptions 26 wantLogs []string 27 assertion require.ErrorAssertionFunc 28 }{ 29 { 30 name: "happy", 31 fields: fields{ 32 SkipDBUpdate: true, 33 DownloadDBOnly: false, 34 }, 35 want: flag.DBOptions{ 36 SkipDBUpdate: true, 37 DownloadDBOnly: false, 38 }, 39 assertion: require.NoError, 40 }, 41 { 42 name: "light", 43 fields: fields{ 44 Light: true, 45 }, 46 want: flag.DBOptions{ 47 Light: true, 48 }, 49 wantLogs: []string{ 50 "'--light' option is deprecated and will be removed. See also: https://github.com/devseccon/trivy/discussions/1649", 51 }, 52 assertion: require.NoError, 53 }, 54 { 55 name: "sad", 56 fields: fields{ 57 SkipDBUpdate: true, 58 DownloadDBOnly: true, 59 }, 60 assertion: func(t require.TestingT, err error, msgs ...interface{}) { 61 require.ErrorContains(t, err, "--skip-db-update and --download-db-only options can not be specified both") 62 }, 63 }, 64 } 65 for _, tt := range tests { 66 t.Run(tt.name, func(t *testing.T) { 67 level := zap.WarnLevel 68 core, obs := observer.New(level) 69 log.Logger = zap.New(core).Sugar() 70 71 viper.Set(flag.SkipDBUpdateFlag.ConfigName, tt.fields.SkipDBUpdate) 72 viper.Set(flag.DownloadDBOnlyFlag.ConfigName, tt.fields.DownloadDBOnly) 73 viper.Set(flag.LightFlag.ConfigName, tt.fields.Light) 74 75 // Assert options 76 f := &flag.DBFlagGroup{ 77 DownloadDBOnly: &flag.DownloadDBOnlyFlag, 78 SkipDBUpdate: &flag.SkipDBUpdateFlag, 79 Light: &flag.LightFlag, 80 } 81 got, err := f.ToOptions() 82 tt.assertion(t, err) 83 assert.Equalf(t, tt.want, got, "ToOptions()") 84 85 // Assert log messages 86 var gotMessages []string 87 for _, entry := range obs.AllUntimed() { 88 gotMessages = append(gotMessages, entry.Message) 89 } 90 assert.Equal(t, tt.wantLogs, gotMessages, tt.name) 91 }) 92 } 93 }