diff --git a/command_test.go b/command_test.go index 5b2da63a..e6973d2a 100644 --- a/command_test.go +++ b/command_test.go @@ -2059,7 +2059,7 @@ func TestFParseErrWhitelistSiblingCommand(t *testing.T) { checkStringContains(t, output, "unknown flag: --unknown") } -func TestFCommandFindSubCommand(t *testing.T) { +func TestFindSubCommand(t *testing.T) { root := &Command{ Use: "root", Run: emptyRun, @@ -2078,22 +2078,11 @@ func TestFCommandFindSubCommand(t *testing.T) { } child.Flags().BoolVar(&opt.a, "a", false, "a") child.Flags().BoolVar(&opt.b, "b", false, "b") - flag := child.Flags().Lookup("a") - if flag != nil { - if flag.NoOptDefVal != "true" { - t.Errorf("ffff %v", flag.NoOptDefVal) - } - } root.AddCommand(child) argsToTest := [][]string{ - {"--a", "--b", "child"}, {"child", "--a"}, {"child", "--b"}, - {"--a=false", "child"}, - {"--a=true", "child"}, - {"--a", "child"}, - {"--b", "child"}, } for _, args := range argsToTest { cmdFound, _, err := root.Find(args) @@ -2104,9 +2093,24 @@ func TestFCommandFindSubCommand(t *testing.T) { t.Errorf("expected found 'child' command with args: %v", args) } } + + argsToTestNotFound := [][]string{ + {"--a", "--b", "child"}, + {"--a", "child"}, + {"--b", "child"}, + } + for _, args := range argsToTestNotFound { + cmdFound, _, err := root.Find(args) + if err != nil { + t.Errorf("Unexpected error: %v with args: %v", err, args) + } + if cmdFound.Name() != "root" { + t.Errorf("expected found 'root' command with args: %v", args) + } + } } -func TestFFindSubCommand(t *testing.T) { +func TestFParseUnknownFlagFromSubCommand(t *testing.T) { root := &Command{ Use: "root", Run: emptyRun, @@ -2127,21 +2131,18 @@ func TestFFindSubCommand(t *testing.T) { child.Flags().BoolVar(&opt.b, "b", false, "b") root.AddCommand(child) - // ok output, err := executeCommand(root, "--a", "child") if err == nil { t.Error("expected unknown flag error") } checkStringContains(t, output, "unknown flag: --a") - // ok output, err = executeCommand(root, "--b", "child") if err == nil { t.Error("expected unknown flag error") } checkStringContains(t, output, "unknown flag: --b") - // fail? output, err = executeCommand(root, "--a", "--b", "child") if err == nil { t.Error("expected unknown flag error")