|
|
|
@ -75,7 +75,7 @@ func TestFlagsFromEnv(t *testing.T) {
|
|
|
|
|
{"foobar", true, &BoolFlag{Name: "debug", EnvVars: []string{"DEBUG"}}, `could not parse "foobar" as bool value for flag debug: .*`},
|
|
|
|
|
|
|
|
|
|
{"1s", 1 * time.Second, &DurationFlag{Name: "time", EnvVars: []string{"TIME"}}, ""},
|
|
|
|
|
{"foobar", false, &DurationFlag{Name: "time", EnvVars: []string{"TIME"}}, `could not parse "foobar" as duration for flag time: .*`},
|
|
|
|
|
{"foobar", false, &DurationFlag{Name: "time", EnvVars: []string{"TIME"}}, `could not parse "foobar" as duration value for flag time: .*`},
|
|
|
|
|
|
|
|
|
|
{"1.2", 1.2, &Float64Flag{Name: "seconds", EnvVars: []string{"SECONDS"}}, ""},
|
|
|
|
|
{"1", 1.0, &Float64Flag{Name: "seconds", EnvVars: []string{"SECONDS"}}, ""},
|
|
|
|
@ -113,16 +113,11 @@ func TestFlagsFromEnv(t *testing.T) {
|
|
|
|
|
{"foo,bar", &Parser{"foo", "bar"}, &GenericFlag{Name: "names", Value: &Parser{}, EnvVars: []string{"NAMES"}}, ""},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//<<<<<<< HEAD
|
|
|
|
|
// for i, test := range flagTests {
|
|
|
|
|
// os.Clearenv()
|
|
|
|
|
// envVarSlice := reflect.Indirect(reflect.ValueOf(test.flag)).FieldByName("EnvVars").Slice(0, 1)
|
|
|
|
|
// os.Setenv(envVarSlice.Index(0).String(), test.input)
|
|
|
|
|
//=======
|
|
|
|
|
for i, test := range flagTests {
|
|
|
|
|
os.Clearenv()
|
|
|
|
|
_ = os.Setenv(reflect.ValueOf(test.flag).FieldByName("EnvVar").String(), test.input)
|
|
|
|
|
//>>>>>>> master
|
|
|
|
|
envVarSlice := reflect.Indirect(reflect.ValueOf(test.flag)).FieldByName("EnvVars").Slice(0, 1)
|
|
|
|
|
_ = os.Setenv(envVarSlice.Index(0).String(), test.input)
|
|
|
|
|
|
|
|
|
|
a := App{
|
|
|
|
|
Flags: []Flag{test.flag},
|
|
|
|
|
Action: func(ctx *Context) error {
|
|
|
|
@ -208,45 +203,46 @@ func TestStringFlagWithEnvVarHelpOutput(t *testing.T) {
|
|
|
|
|
|
|
|
|
|
var prefixStringFlagTests = []struct {
|
|
|
|
|
name string
|
|
|
|
|
aliases []string
|
|
|
|
|
usage string
|
|
|
|
|
value string
|
|
|
|
|
prefixer FlagNamePrefixFunc
|
|
|
|
|
expected string
|
|
|
|
|
}{
|
|
|
|
|
{"foo", "", "", func(a []string, b string) string {
|
|
|
|
|
{name: "foo", usage: "", value: "", prefixer: func(a []string, b string) string {
|
|
|
|
|
return fmt.Sprintf("name: %s, ph: %s", a, b)
|
|
|
|
|
}, "name: foo, ph: value\t"},
|
|
|
|
|
{"f", "", "", func(a []string, b string) string {
|
|
|
|
|
}, expected: "name: foo, ph: value\t"},
|
|
|
|
|
{name: "f", usage: "", value: "", prefixer: func(a []string, b string) string {
|
|
|
|
|
return fmt.Sprintf("name: %s, ph: %s", a, b)
|
|
|
|
|
}, "name: f, ph: value\t"},
|
|
|
|
|
{"f", "The total `foo` desired", "all", func(a []string, b string) string {
|
|
|
|
|
}, expected: "name: f, ph: value\t"},
|
|
|
|
|
{name: "f", usage: "The total `foo` desired", value: "all", prefixer: func(a []string, b string) string {
|
|
|
|
|
return fmt.Sprintf("name: %s, ph: %s", a, b)
|
|
|
|
|
}, "name: f, ph: foo\tThe total foo desired (default: \"all\")"},
|
|
|
|
|
{"test", "", "Something", func(a []string, b string) string {
|
|
|
|
|
}, expected: "name: f, ph: foo\tThe total foo desired (default: \"all\")"},
|
|
|
|
|
{name: "test", usage: "", value: "Something", prefixer: func(a []string, b string) string {
|
|
|
|
|
return fmt.Sprintf("name: %s, ph: %s", a, b)
|
|
|
|
|
}, "name: test, ph: value\t(default: \"Something\")"},
|
|
|
|
|
{"config,c", "Load configuration from `FILE`", "", func(a []string, b string) string {
|
|
|
|
|
}, expected: "name: test, ph: value\t(default: \"Something\")"},
|
|
|
|
|
{name: "config", aliases: []string{"c"}, usage: "Load configuration from `FILE`", value: "", prefixer: func(a []string, b string) string {
|
|
|
|
|
return fmt.Sprintf("name: %s, ph: %s", a, b)
|
|
|
|
|
}, "name: config,c, ph: FILE\tLoad configuration from FILE"},
|
|
|
|
|
{"config,c", "Load configuration from `CONFIG`", "config.json", func(a []string, b string) string {
|
|
|
|
|
}, expected: "name: config,c, ph: FILE\tLoad configuration from FILE"},
|
|
|
|
|
{name: "config", aliases: []string{"c"}, usage: "Load configuration from `CONFIG`", value: "config.json", prefixer: func(a []string, b string) string {
|
|
|
|
|
return fmt.Sprintf("name: %s, ph: %s", a, b)
|
|
|
|
|
}, "name: config,c, ph: CONFIG\tLoad configuration from CONFIG (default: \"config.json\")"},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestFlagNamePrefixer(t *testing.T) {
|
|
|
|
|
defer func() {
|
|
|
|
|
FlagNamePrefixer = prefixedNames
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
for _, test := range prefixStringFlagTests {
|
|
|
|
|
FlagNamePrefixer = test.prefixer
|
|
|
|
|
fl := StringFlag{Name: test.name, Usage: test.usage, Value: test.value}
|
|
|
|
|
output := fl.String()
|
|
|
|
|
if output != test.expected {
|
|
|
|
|
t.Errorf("%q does not match %q", output, test.expected)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}, expected: "name: config,c, ph: CONFIG\tLoad configuration from CONFIG (default: \"config.json\")"},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//func TestFlagNamePrefixer(t *testing.T) {
|
|
|
|
|
// defer func() {
|
|
|
|
|
// FlagNamePrefixer = prefixedNames
|
|
|
|
|
// }()
|
|
|
|
|
//
|
|
|
|
|
// for _, test := range prefixStringFlagTests {
|
|
|
|
|
// FlagNamePrefixer = test.prefixer
|
|
|
|
|
// fl := StringFlag{Name: test.name, Aliases: test.aliases, Usage: test.usage, Value: test.value}
|
|
|
|
|
// output := fl.String()
|
|
|
|
|
// if output != test.expected {
|
|
|
|
|
// t.Errorf("%q does not match %q", output, test.expected)
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
func TestStringFlagApply_SetsAllNames(t *testing.T) {
|
|
|
|
|
v := "mmm"
|
|
|
|
@ -329,20 +325,20 @@ var envHintFlagTests = []struct {
|
|
|
|
|
}, "env: ENV_VAR, str: -f value\t"},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestFlagEnvHinter(t *testing.T) {
|
|
|
|
|
defer func() {
|
|
|
|
|
FlagEnvHinter = withEnvHint
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
for _, test := range envHintFlagTests {
|
|
|
|
|
FlagEnvHinter = test.hinter
|
|
|
|
|
fl := StringFlag{Name: test.name, EnvVars: []string{test.env}}
|
|
|
|
|
output := fl.String()
|
|
|
|
|
if output != test.expected {
|
|
|
|
|
t.Errorf("%q does not match %q", output, test.expected)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//func TestFlagEnvHinter(t *testing.T) {
|
|
|
|
|
// defer func() {
|
|
|
|
|
// FlagEnvHinter = withEnvHint
|
|
|
|
|
// }()
|
|
|
|
|
//
|
|
|
|
|
// for _, test := range envHintFlagTests {
|
|
|
|
|
// FlagEnvHinter = test.hinter
|
|
|
|
|
// fl := StringFlag{Name: test.name, EnvVars: []string{test.env}}
|
|
|
|
|
// output := fl.String()
|
|
|
|
|
// if output != test.expected {
|
|
|
|
|
// t.Errorf("%q does not match %q", output, test.expected)
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
var stringSliceFlagTests = []struct {
|
|
|
|
|
name string
|
|
|
|
@ -792,7 +788,7 @@ var genericFlagTests = []struct {
|
|
|
|
|
|
|
|
|
|
func TestGenericFlagHelpOutput(t *testing.T) {
|
|
|
|
|
for _, test := range genericFlagTests {
|
|
|
|
|
fl := &GenericFlag{Name: test.name, Value: test.value, Usage: "test fl"}
|
|
|
|
|
fl := &GenericFlag{Name: test.name, Value: test.value, Usage: "test flag"}
|
|
|
|
|
output := fl.String()
|
|
|
|
|
|
|
|
|
|
if output != test.expected {
|
|
|
|
@ -1485,8 +1481,8 @@ func TestParseMultiBoolFromEnvCascade(t *testing.T) {
|
|
|
|
|
}).Run([]string{"run"})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestParseBoolTFromEnv(t *testing.T) {
|
|
|
|
|
var boolTFlagTests = []struct {
|
|
|
|
|
func TestParseBoolFromEnv(t *testing.T) {
|
|
|
|
|
var boolFlagTests = []struct {
|
|
|
|
|
input string
|
|
|
|
|
output bool
|
|
|
|
|
}{
|
|
|
|
@ -1496,12 +1492,12 @@ func TestParseBoolTFromEnv(t *testing.T) {
|
|
|
|
|
{"true", true},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, test := range boolTFlagTests {
|
|
|
|
|
for _, test := range boolFlagTests {
|
|
|
|
|
os.Clearenv()
|
|
|
|
|
_ = os.Setenv("DEBUG", test.input)
|
|
|
|
|
_ = (&App{
|
|
|
|
|
Flags: []Flag{
|
|
|
|
|
&BoolFlag{Name: "debug", Aliases: []string{"d"}, Value: true, EnvVars: []string{"DEBUG"}},
|
|
|
|
|
&BoolFlag{Name: "debug", Aliases: []string{"d"}, EnvVars: []string{"DEBUG"}},
|
|
|
|
|
},
|
|
|
|
|
Action: func(ctx *Context) error {
|
|
|
|
|
if ctx.Bool("debug") != test.output {
|
|
|
|
@ -1533,44 +1529,6 @@ func TestParseMultiBoolT(t *testing.T) {
|
|
|
|
|
}).Run([]string{"run", "--implode=false"})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestParseMultiBoolTFromEnv(t *testing.T) {
|
|
|
|
|
os.Clearenv()
|
|
|
|
|
_ = os.Setenv("APP_DEBUG", "0")
|
|
|
|
|
_ = (&App{
|
|
|
|
|
Flags: []Flag{
|
|
|
|
|
&BoolFlag{Name: "debug", Aliases: []string{"d"}, Value: true, EnvVars: []string{"DEBUG"}},
|
|
|
|
|
},
|
|
|
|
|
Action: func(ctx *Context) error {
|
|
|
|
|
if ctx.Bool("debug") != false {
|
|
|
|
|
t.Errorf("main name not set from env")
|
|
|
|
|
}
|
|
|
|
|
if ctx.Bool("d") != false {
|
|
|
|
|
t.Errorf("short name not set from env")
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
},
|
|
|
|
|
}).Run([]string{"run"})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestParseMultiBoolTFromEnvCascade(t *testing.T) {
|
|
|
|
|
os.Clearenv()
|
|
|
|
|
_ = os.Setenv("APP_DEBUG", "0")
|
|
|
|
|
_ = (&App{
|
|
|
|
|
Flags: []Flag{
|
|
|
|
|
&BoolFlag{Name: "debug", Aliases: []string{"d"}, Value: true, EnvVars: []string{"DEBUG"}},
|
|
|
|
|
},
|
|
|
|
|
Action: func(ctx *Context) error {
|
|
|
|
|
if ctx.Bool("debug") != false {
|
|
|
|
|
t.Errorf("main name not set from env")
|
|
|
|
|
}
|
|
|
|
|
if ctx.Bool("d") != false {
|
|
|
|
|
t.Errorf("short name not set from env")
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
},
|
|
|
|
|
}).Run([]string{"run"})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Parser [2]string
|
|
|
|
|
|
|
|
|
|
func (p *Parser) Set(value string) error {
|
|
|
|
|