XXX Corresponds to hack in context.IsSet for flags with EnvVar field Should be moved to `flag_test` in v2
(t *testing.T)
| 4370 | // XXX Corresponds to hack in context.IsSet for flags with EnvVar field |
| 4371 | // Should be moved to `flag_test` in v2 |
| 4372 | func TestCommand_IsSet_fromEnv(t *testing.T) { |
| 4373 | var ( |
| 4374 | timeoutIsSet, tIsSet bool |
| 4375 | noEnvVarIsSet, nIsSet bool |
| 4376 | passwordIsSet, pIsSet bool |
| 4377 | unparsableIsSet, uIsSet bool |
| 4378 | ) |
| 4379 | |
| 4380 | t.Setenv("APP_TIMEOUT_SECONDS", "15.5") |
| 4381 | t.Setenv("APP_PASSWORD", "") |
| 4382 | |
| 4383 | cmd := &Command{ |
| 4384 | Flags: []Flag{ |
| 4385 | &FloatFlag{Name: "timeout", Aliases: []string{"t"}, Local: true, Sources: EnvVars("APP_TIMEOUT_SECONDS")}, |
| 4386 | &StringFlag{Name: "password", Aliases: []string{"p"}, Local: true, Sources: EnvVars("APP_PASSWORD")}, |
| 4387 | &FloatFlag{Name: "unparsable", Aliases: []string{"u"}, Local: true, Sources: EnvVars("APP_UNPARSABLE")}, |
| 4388 | &FloatFlag{Name: "no-env-var", Aliases: []string{"n"}, Local: true}, |
| 4389 | }, |
| 4390 | Action: func(_ context.Context, cmd *Command) error { |
| 4391 | timeoutIsSet = cmd.IsSet("timeout") |
| 4392 | tIsSet = cmd.IsSet("t") |
| 4393 | passwordIsSet = cmd.IsSet("password") |
| 4394 | pIsSet = cmd.IsSet("p") |
| 4395 | unparsableIsSet = cmd.IsSet("unparsable") |
| 4396 | uIsSet = cmd.IsSet("u") |
| 4397 | noEnvVarIsSet = cmd.IsSet("no-env-var") |
| 4398 | nIsSet = cmd.IsSet("n") |
| 4399 | return nil |
| 4400 | }, |
| 4401 | } |
| 4402 | |
| 4403 | r := require.New(t) |
| 4404 | |
| 4405 | r.NoError(cmd.Run(buildTestContext(t), []string{"run"})) |
| 4406 | r.True(timeoutIsSet) |
| 4407 | r.True(tIsSet) |
| 4408 | r.True(passwordIsSet) |
| 4409 | r.True(pIsSet) |
| 4410 | r.False(noEnvVarIsSet) |
| 4411 | r.False(nIsSet) |
| 4412 | |
| 4413 | t.Setenv("APP_UNPARSABLE", "foobar") |
| 4414 | |
| 4415 | r.Error(cmd.Run(buildTestContext(t), []string{"run"})) |
| 4416 | r.False(unparsableIsSet) |
| 4417 | r.False(uIsSet) |
| 4418 | } |
| 4419 | |
| 4420 | func TestCommand_NumFlags(t *testing.T) { |
| 4421 | rootCmd := &Command{ |
nothing calls this directly
no test coverage detected