Consider empty environment variables as set

When assigning values to flags (also when interogatting via
`context.(Global)IsSet`.

For boolean flags, consider empty as `false`.

Using `syscall.Getenv` rather than `os.LookupEnv` in order to support
older Golang versions.
This commit is contained in:
Jesse Szwedko
2016-09-11 20:32:43 -07:00
parent 7a5dacbc41
commit a00c3f5872
5 changed files with 100 additions and 22 deletions

View File

@@ -29,6 +29,38 @@ func TestBoolFlagHelpOutput(t *testing.T) {
}
}
func TestParseBoolFromEnv(t *testing.T) {
var boolFlagTests = []struct {
input string
output bool
}{
{"", false},
{"1", true},
{"false", false},
{"true", true},
}
for _, test := range boolFlagTests {
os.Clearenv()
os.Setenv("DEBUG", test.input)
a := App{
Flags: []Flag{
BoolFlag{Name: "debug, d", EnvVar: "DEBUG"},
},
Action: func(ctx *Context) error {
if ctx.Bool("debug") != test.output {
t.Errorf("expected %+v to be parsed as %+v, instead was %+v", test.input, test.output, ctx.Bool("debug"))
}
if ctx.Bool("d") != test.output {
t.Errorf("expected %+v to be parsed as %+v, instead was %+v", test.input, test.output, ctx.Bool("d"))
}
return nil
},
}
a.Run([]string{"run"})
}
}
var stringFlagTests = []struct {
name string
usage string
@@ -941,6 +973,38 @@ func TestParseMultiBoolFromEnvCascade(t *testing.T) {
a.Run([]string{"run"})
}
func TestParseBoolTFromEnv(t *testing.T) {
var boolTFlagTests = []struct {
input string
output bool
}{
{"", false},
{"1", true},
{"false", false},
{"true", true},
}
for _, test := range boolTFlagTests {
os.Clearenv()
os.Setenv("DEBUG", test.input)
a := App{
Flags: []Flag{
BoolTFlag{Name: "debug, d", EnvVar: "DEBUG"},
},
Action: func(ctx *Context) error {
if ctx.Bool("debug") != test.output {
t.Errorf("expected %+v to be parsed as %+v, instead was %+v", test.input, test.output, ctx.Bool("debug"))
}
if ctx.Bool("d") != test.output {
t.Errorf("expected %+v to be parsed as %+v, instead was %+v", test.input, test.output, ctx.Bool("d"))
}
return nil
},
}
a.Run([]string{"run"})
}
}
func TestParseMultiBoolT(t *testing.T) {
a := App{
Flags: []Flag{