github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/cli/altsrc/flag_test.go (about) 1 package altsrc 2 3 import ( 4 "flag" 5 "fmt" 6 "os" 7 "strings" 8 "testing" 9 "time" 10 11 "github.com/insionng/yougam/libraries/cli" 12 ) 13 14 type testApplyInputSource struct { 15 Flag FlagInputSourceExtension 16 FlagName string 17 FlagSetName string 18 Expected string 19 ContextValueString string 20 ContextValue flag.Value 21 EnvVarValue string 22 EnvVarName string 23 MapValue interface{} 24 } 25 26 func TestGenericApplyInputSourceValue(t *testing.T) { 27 v := &Parser{"abc", "def"} 28 c := runTest(t, testApplyInputSource{ 29 Flag: NewGenericFlag(cli.GenericFlag{Name: "test", Value: &Parser{}}), 30 FlagName: "test", 31 MapValue: v, 32 }) 33 expect(t, v, c.Generic("test")) 34 } 35 36 func TestGenericApplyInputSourceMethodContextSet(t *testing.T) { 37 p := &Parser{"abc", "def"} 38 c := runTest(t, testApplyInputSource{ 39 Flag: NewGenericFlag(cli.GenericFlag{Name: "test", Value: &Parser{}}), 40 FlagName: "test", 41 MapValue: &Parser{"efg", "hig"}, 42 ContextValueString: p.String(), 43 }) 44 expect(t, p, c.Generic("test")) 45 } 46 47 func TestGenericApplyInputSourceMethodEnvVarSet(t *testing.T) { 48 c := runTest(t, testApplyInputSource{ 49 Flag: NewGenericFlag(cli.GenericFlag{Name: "test", Value: &Parser{}, EnvVar: "TEST"}), 50 FlagName: "test", 51 MapValue: &Parser{"efg", "hij"}, 52 EnvVarName: "TEST", 53 EnvVarValue: "abc,def", 54 }) 55 expect(t, &Parser{"abc", "def"}, c.Generic("test")) 56 } 57 58 func TestStringSliceApplyInputSourceValue(t *testing.T) { 59 c := runTest(t, testApplyInputSource{ 60 Flag: NewStringSliceFlag(cli.StringSliceFlag{Name: "test"}), 61 FlagName: "test", 62 MapValue: []string{"hello", "world"}, 63 }) 64 expect(t, c.StringSlice("test"), []string{"hello", "world"}) 65 } 66 67 func TestStringSliceApplyInputSourceMethodContextSet(t *testing.T) { 68 c := runTest(t, testApplyInputSource{ 69 Flag: NewStringSliceFlag(cli.StringSliceFlag{Name: "test"}), 70 FlagName: "test", 71 MapValue: []string{"hello", "world"}, 72 ContextValueString: "ohno", 73 }) 74 expect(t, c.StringSlice("test"), []string{"ohno"}) 75 } 76 77 func TestStringSliceApplyInputSourceMethodEnvVarSet(t *testing.T) { 78 c := runTest(t, testApplyInputSource{ 79 Flag: NewStringSliceFlag(cli.StringSliceFlag{Name: "test", EnvVar: "TEST"}), 80 FlagName: "test", 81 MapValue: []string{"hello", "world"}, 82 EnvVarName: "TEST", 83 EnvVarValue: "oh,no", 84 }) 85 expect(t, c.StringSlice("test"), []string{"oh", "no"}) 86 } 87 88 func TestIntSliceApplyInputSourceValue(t *testing.T) { 89 c := runTest(t, testApplyInputSource{ 90 Flag: NewIntSliceFlag(cli.IntSliceFlag{Name: "test"}), 91 FlagName: "test", 92 MapValue: []int{1, 2}, 93 }) 94 expect(t, c.IntSlice("test"), []int{1, 2}) 95 } 96 97 func TestIntSliceApplyInputSourceMethodContextSet(t *testing.T) { 98 c := runTest(t, testApplyInputSource{ 99 Flag: NewIntSliceFlag(cli.IntSliceFlag{Name: "test"}), 100 FlagName: "test", 101 MapValue: []int{1, 2}, 102 ContextValueString: "3", 103 }) 104 expect(t, c.IntSlice("test"), []int{3}) 105 } 106 107 func TestIntSliceApplyInputSourceMethodEnvVarSet(t *testing.T) { 108 c := runTest(t, testApplyInputSource{ 109 Flag: NewIntSliceFlag(cli.IntSliceFlag{Name: "test", EnvVar: "TEST"}), 110 FlagName: "test", 111 MapValue: []int{1, 2}, 112 EnvVarName: "TEST", 113 EnvVarValue: "3,4", 114 }) 115 expect(t, c.IntSlice("test"), []int{3, 4}) 116 } 117 118 func TestBoolApplyInputSourceMethodSet(t *testing.T) { 119 c := runTest(t, testApplyInputSource{ 120 Flag: NewBoolFlag(cli.BoolFlag{Name: "test"}), 121 FlagName: "test", 122 MapValue: true, 123 }) 124 expect(t, true, c.Bool("test")) 125 } 126 127 func TestBoolApplyInputSourceMethodContextSet(t *testing.T) { 128 c := runTest(t, testApplyInputSource{ 129 Flag: NewBoolFlag(cli.BoolFlag{Name: "test"}), 130 FlagName: "test", 131 MapValue: false, 132 ContextValueString: "true", 133 }) 134 expect(t, true, c.Bool("test")) 135 } 136 137 func TestBoolApplyInputSourceMethodEnvVarSet(t *testing.T) { 138 c := runTest(t, testApplyInputSource{ 139 Flag: NewBoolFlag(cli.BoolFlag{Name: "test", EnvVar: "TEST"}), 140 FlagName: "test", 141 MapValue: false, 142 EnvVarName: "TEST", 143 EnvVarValue: "true", 144 }) 145 expect(t, true, c.Bool("test")) 146 } 147 148 func TestBoolTApplyInputSourceMethodSet(t *testing.T) { 149 c := runTest(t, testApplyInputSource{ 150 Flag: NewBoolTFlag(cli.BoolTFlag{Name: "test"}), 151 FlagName: "test", 152 MapValue: false, 153 }) 154 expect(t, false, c.BoolT("test")) 155 } 156 157 func TestBoolTApplyInputSourceMethodContextSet(t *testing.T) { 158 c := runTest(t, testApplyInputSource{ 159 Flag: NewBoolTFlag(cli.BoolTFlag{Name: "test"}), 160 FlagName: "test", 161 MapValue: true, 162 ContextValueString: "false", 163 }) 164 expect(t, false, c.BoolT("test")) 165 } 166 167 func TestBoolTApplyInputSourceMethodEnvVarSet(t *testing.T) { 168 c := runTest(t, testApplyInputSource{ 169 Flag: NewBoolTFlag(cli.BoolTFlag{Name: "test", EnvVar: "TEST"}), 170 FlagName: "test", 171 MapValue: true, 172 EnvVarName: "TEST", 173 EnvVarValue: "false", 174 }) 175 expect(t, false, c.BoolT("test")) 176 } 177 178 func TestStringApplyInputSourceMethodSet(t *testing.T) { 179 c := runTest(t, testApplyInputSource{ 180 Flag: NewStringFlag(cli.StringFlag{Name: "test"}), 181 FlagName: "test", 182 MapValue: "hello", 183 }) 184 expect(t, "hello", c.String("test")) 185 } 186 187 func TestStringApplyInputSourceMethodContextSet(t *testing.T) { 188 c := runTest(t, testApplyInputSource{ 189 Flag: NewStringFlag(cli.StringFlag{Name: "test"}), 190 FlagName: "test", 191 MapValue: "hello", 192 ContextValueString: "goodbye", 193 }) 194 expect(t, "goodbye", c.String("test")) 195 } 196 197 func TestStringApplyInputSourceMethodEnvVarSet(t *testing.T) { 198 c := runTest(t, testApplyInputSource{ 199 Flag: NewStringFlag(cli.StringFlag{Name: "test", EnvVar: "TEST"}), 200 FlagName: "test", 201 MapValue: "hello", 202 EnvVarName: "TEST", 203 EnvVarValue: "goodbye", 204 }) 205 expect(t, "goodbye", c.String("test")) 206 } 207 208 func TestIntApplyInputSourceMethodSet(t *testing.T) { 209 c := runTest(t, testApplyInputSource{ 210 Flag: NewIntFlag(cli.IntFlag{Name: "test"}), 211 FlagName: "test", 212 MapValue: 15, 213 }) 214 expect(t, 15, c.Int("test")) 215 } 216 217 func TestIntApplyInputSourceMethodContextSet(t *testing.T) { 218 c := runTest(t, testApplyInputSource{ 219 Flag: NewIntFlag(cli.IntFlag{Name: "test"}), 220 FlagName: "test", 221 MapValue: 15, 222 ContextValueString: "7", 223 }) 224 expect(t, 7, c.Int("test")) 225 } 226 227 func TestIntApplyInputSourceMethodEnvVarSet(t *testing.T) { 228 c := runTest(t, testApplyInputSource{ 229 Flag: NewIntFlag(cli.IntFlag{Name: "test", EnvVar: "TEST"}), 230 FlagName: "test", 231 MapValue: 15, 232 EnvVarName: "TEST", 233 EnvVarValue: "12", 234 }) 235 expect(t, 12, c.Int("test")) 236 } 237 238 func TestDurationApplyInputSourceMethodSet(t *testing.T) { 239 c := runTest(t, testApplyInputSource{ 240 Flag: NewDurationFlag(cli.DurationFlag{Name: "test"}), 241 FlagName: "test", 242 MapValue: time.Duration(30 * time.Second), 243 }) 244 expect(t, time.Duration(30*time.Second), c.Duration("test")) 245 } 246 247 func TestDurationApplyInputSourceMethodContextSet(t *testing.T) { 248 c := runTest(t, testApplyInputSource{ 249 Flag: NewDurationFlag(cli.DurationFlag{Name: "test"}), 250 FlagName: "test", 251 MapValue: time.Duration(30 * time.Second), 252 ContextValueString: time.Duration(15 * time.Second).String(), 253 }) 254 expect(t, time.Duration(15*time.Second), c.Duration("test")) 255 } 256 257 func TestDurationApplyInputSourceMethodEnvVarSet(t *testing.T) { 258 c := runTest(t, testApplyInputSource{ 259 Flag: NewDurationFlag(cli.DurationFlag{Name: "test", EnvVar: "TEST"}), 260 FlagName: "test", 261 MapValue: time.Duration(30 * time.Second), 262 EnvVarName: "TEST", 263 EnvVarValue: time.Duration(15 * time.Second).String(), 264 }) 265 expect(t, time.Duration(15*time.Second), c.Duration("test")) 266 } 267 268 func TestFloat64ApplyInputSourceMethodSet(t *testing.T) { 269 c := runTest(t, testApplyInputSource{ 270 Flag: NewFloat64Flag(cli.Float64Flag{Name: "test"}), 271 FlagName: "test", 272 MapValue: 1.3, 273 }) 274 expect(t, 1.3, c.Float64("test")) 275 } 276 277 func TestFloat64ApplyInputSourceMethodContextSet(t *testing.T) { 278 c := runTest(t, testApplyInputSource{ 279 Flag: NewFloat64Flag(cli.Float64Flag{Name: "test"}), 280 FlagName: "test", 281 MapValue: 1.3, 282 ContextValueString: fmt.Sprintf("%v", 1.4), 283 }) 284 expect(t, 1.4, c.Float64("test")) 285 } 286 287 func TestFloat64ApplyInputSourceMethodEnvVarSet(t *testing.T) { 288 c := runTest(t, testApplyInputSource{ 289 Flag: NewFloat64Flag(cli.Float64Flag{Name: "test", EnvVar: "TEST"}), 290 FlagName: "test", 291 MapValue: 1.3, 292 EnvVarName: "TEST", 293 EnvVarValue: fmt.Sprintf("%v", 1.4), 294 }) 295 expect(t, 1.4, c.Float64("test")) 296 } 297 298 func runTest(t *testing.T, test testApplyInputSource) *cli.Context { 299 inputSource := &MapInputSource{valueMap: map[string]interface{}{test.FlagName: test.MapValue}} 300 set := flag.NewFlagSet(test.FlagSetName, flag.ContinueOnError) 301 c := cli.NewContext(nil, set, nil) 302 if test.EnvVarName != "" && test.EnvVarValue != "" { 303 os.Setenv(test.EnvVarName, test.EnvVarValue) 304 defer os.Setenv(test.EnvVarName, "") 305 } 306 307 test.Flag.Apply(set) 308 if test.ContextValue != nil { 309 flag := set.Lookup(test.FlagName) 310 flag.Value = test.ContextValue 311 } 312 if test.ContextValueString != "" { 313 set.Set(test.FlagName, test.ContextValueString) 314 } 315 test.Flag.ApplyInputSourceValue(c, inputSource) 316 317 return c 318 } 319 320 type Parser [2]string 321 322 func (p *Parser) Set(value string) error { 323 parts := strings.Split(value, ",") 324 if len(parts) != 2 { 325 return fmt.Errorf("invalid format") 326 } 327 328 (*p)[0] = parts[0] 329 (*p)[1] = parts[1] 330 331 return nil 332 } 333 334 func (p *Parser) String() string { 335 return fmt.Sprintf("%s,%s", p[0], p[1]) 336 }