add env var tests
This commit is contained in:
parent
f4128a02f3
commit
d7ec4e8013
@ -407,6 +407,7 @@ func TestCheckRequiredFlags(t *testing.T) {
|
|||||||
tdata := []struct {
|
tdata := []struct {
|
||||||
testCase string
|
testCase string
|
||||||
parseInput []string
|
parseInput []string
|
||||||
|
envVarInput [2]string
|
||||||
flags []Flag
|
flags []Flag
|
||||||
expectedAnError bool
|
expectedAnError bool
|
||||||
expectedErrorContents []string
|
expectedErrorContents []string
|
||||||
@ -435,6 +436,13 @@ func TestCheckRequiredFlags(t *testing.T) {
|
|||||||
},
|
},
|
||||||
parseInput: []string{"--requiredFlag", "myinput"},
|
parseInput: []string{"--requiredFlag", "myinput"},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
testCase: "required_and_present_via_env_var",
|
||||||
|
flags: []Flag{
|
||||||
|
StringFlag{Name: "requiredFlag", Required: true, EnvVar: "REQUIRED_FLAG"},
|
||||||
|
},
|
||||||
|
envVarInput: [2]string{"REQUIRED_FLAG", "true"},
|
||||||
|
},
|
||||||
{
|
{
|
||||||
testCase: "required_and_optional",
|
testCase: "required_and_optional",
|
||||||
flags: []Flag{
|
flags: []Flag{
|
||||||
@ -452,6 +460,15 @@ func TestCheckRequiredFlags(t *testing.T) {
|
|||||||
parseInput: []string{"--optionalFlag", "myinput"},
|
parseInput: []string{"--optionalFlag", "myinput"},
|
||||||
expectedAnError: true,
|
expectedAnError: true,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
testCase: "required_and_optional_and_optional_present_via_env_var",
|
||||||
|
flags: []Flag{
|
||||||
|
StringFlag{Name: "requiredFlag", Required: true},
|
||||||
|
StringFlag{Name: "optionalFlag", EnvVar: "OPTIONAL_FLAG"},
|
||||||
|
},
|
||||||
|
envVarInput: [2]string{"OPTIONAL_FLAG", "true"},
|
||||||
|
expectedAnError: true,
|
||||||
|
},
|
||||||
{
|
{
|
||||||
testCase: "required_and_optional_and_required_present",
|
testCase: "required_and_optional_and_required_present",
|
||||||
flags: []Flag{
|
flags: []Flag{
|
||||||
@ -495,8 +512,13 @@ func TestCheckRequiredFlags(t *testing.T) {
|
|||||||
flags.Apply(set)
|
flags.Apply(set)
|
||||||
}
|
}
|
||||||
set.Parse(test.parseInput)
|
set.Parse(test.parseInput)
|
||||||
|
if test.envVarInput[0] != "" {
|
||||||
|
os.Clearenv()
|
||||||
|
os.Setenv(test.envVarInput[0], test.envVarInput[1])
|
||||||
|
}
|
||||||
ctx := &Context{}
|
ctx := &Context{}
|
||||||
context := NewContext(ctx.App, set, ctx)
|
context := NewContext(ctx.App, set, ctx)
|
||||||
|
context.Command.Flags = test.flags
|
||||||
|
|
||||||
// logic under test
|
// logic under test
|
||||||
err := checkRequiredFlags(test.flags, context)
|
err := checkRequiredFlags(test.flags, context)
|
||||||
|
Loading…
Reference in New Issue
Block a user