diff --git a/app_test.go b/app_test.go index e814cbbd77..437af25911 100644 --- a/app_test.go +++ b/app_test.go @@ -558,6 +558,86 @@ func TestApp_RunDefaultCommandWithSubCommand(t *testing.T) { } } +var defaultCommandFlagAppTests = []struct { + cmdName string + flag string + defaultCmd string + expected bool +}{ + {"foobar", "", "foobar", true}, + {"foobar", "-c derp", "foobar", true}, + {"batbaz", "", "foobar", true}, + {"b", "", "", true}, + {"f", "", "", true}, + {"", "", "foobar", true}, + {"", "", "", true}, + {"", "-j", "foobar", true}, + {"", "-j", "foobar", true}, + {"", "-c derp", "foobar", true}, + {"", "--carly=derp", "foobar", true}, + {"", "-j", "foobar", true}, + {"", "-j", "", true}, + {" ", "-j", "foobar", false}, + {"", "", "", true}, + {" ", "", "", false}, + {" ", "-j", "", false}, + {"bat", "", "batbaz", false}, + {"nothing", "", "batbaz", false}, + {"nothing", "", "", false}, + {"nothing", "--jimbob", "batbaz", false}, + {"nothing", "--carly", "", false}, +} + +func TestApp_RunDefaultCommandWithFlags(t *testing.T) { + for _, test := range defaultCommandFlagAppTests { + testTitle := fmt.Sprintf("command=%[1]s-flag=%[2]s-default=%[3]s", test.cmdName, test.flag, test.defaultCmd) + t.Run(testTitle, func(t *testing.T) { + app := &App{ + DefaultCommand: test.defaultCmd, + Flags: []Flag{ + &StringFlag{ + Name: "carly", + Aliases: []string{"c"}, + Required: false, + }, + &BoolFlag{ + Name: "jimbob", + Aliases: []string{"j"}, + Required: false, + Value: true, + }, + }, + Commands: []*Command{ + { + Name: "foobar", + Aliases: []string{"f"}, + }, + {Name: "batbaz", Aliases: []string{"b"}}, + }, + } + + appArgs := []string{"c"} + + if test.flag != "" { + flags := strings.Split(test.flag, " ") + if len(flags) > 1 { + appArgs = append(appArgs, flags...) + } + + flags = strings.Split(test.flag, "=") + if len(flags) > 1 { + appArgs = append(appArgs, flags...) + } + } + + appArgs = append(appArgs, test.cmdName) + + err := app.Run(appArgs) + expect(t, err == nil, test.expected) + }) + } +} + func TestApp_Setup_defaultsReader(t *testing.T) { app := &App{} app.Setup()