github.com/argoproj/argo-cd/v3@v3.2.1/cmd/argocd/commands/initialize/cmd_test.go (about) 1 package initialize 2 3 import ( 4 "testing" 5 6 flag "github.com/spf13/pflag" 7 "github.com/stretchr/testify/assert" 8 ) 9 10 type StringFlag struct { 11 // The exact value provided on the flag 12 value string 13 } 14 15 func (f StringFlag) String() string { 16 return f.value 17 } 18 19 func (f *StringFlag) Set(value string) error { 20 f.value = value 21 return nil 22 } 23 24 func (f *StringFlag) Type() string { 25 return "string" 26 } 27 28 func Test_FlagContextNotChanged(t *testing.T) { 29 res := RetrieveContextIfChanged(&flag.Flag{ 30 Name: "", 31 Shorthand: "", 32 Usage: "", 33 Value: &StringFlag{value: "test"}, 34 DefValue: "", 35 Changed: false, 36 NoOptDefVal: "", 37 Deprecated: "", 38 Hidden: false, 39 ShorthandDeprecated: "", 40 Annotations: nil, 41 }) 42 43 assert.Empty(t, res) 44 } 45 46 func Test_FlagContextChanged(t *testing.T) { 47 res := RetrieveContextIfChanged(&flag.Flag{ 48 Name: "", 49 Shorthand: "", 50 Usage: "", 51 Value: &StringFlag{value: "test"}, 52 DefValue: "", 53 Changed: true, 54 NoOptDefVal: "", 55 Deprecated: "", 56 Hidden: false, 57 ShorthandDeprecated: "", 58 Annotations: nil, 59 }) 60 61 assert.Equal(t, "test", res) 62 } 63 64 func Test_FlagContextNil(t *testing.T) { 65 res := RetrieveContextIfChanged(&flag.Flag{ 66 Name: "", 67 Shorthand: "", 68 Usage: "", 69 Value: nil, 70 DefValue: "", 71 Changed: false, 72 NoOptDefVal: "", 73 Deprecated: "", 74 Hidden: false, 75 ShorthandDeprecated: "", 76 Annotations: nil, 77 }) 78 79 assert.Empty(t, res) 80 }