Add more Traverse tests.

This commit is contained in:
Daniel Nephin 2016-06-29 19:32:01 -04:00
parent 6259b8dea9
commit 419884c85a
2 changed files with 73 additions and 6 deletions

View file

@ -125,8 +125,8 @@ type Command struct {
// Must be > 0. // Must be > 0.
SuggestionsMinimumDistance int SuggestionsMinimumDistance int
// TraverseChildCommands parses flags on all parents before the child command // TraverseChildren parses flags on all parents before executing child command
TraverseChildCommands bool TraverseChildren bool
// name is the command name, usually the executable's name. // name is the command name, usually the executable's name.
name string name string
@ -758,7 +758,7 @@ func (c *Command) ExecuteC() (cmd *Command, err error) {
} }
var flags []string var flags []string
if c.TraverseChildCommands { if c.TraverseChildren {
cmd, flags, err = c.Traverse(args) cmd, flags, err = c.Traverse(args)
} else { } else {
cmd, flags, err = c.Find(args) cmd, flags, err = c.Find(args)

View file

@ -348,10 +348,10 @@ func TestSetHelpCommand(t *testing.T) {
} }
} }
func TestRunWithTraverse(t *testing.T) { func TestTraverseWithParentFlags(t *testing.T) {
cmd := &Command{ cmd := &Command{
Use: "do", Use: "do",
TraverseChildCommands: true, TraverseChildren: true,
} }
cmd.Flags().String("foo", "", "foo things") cmd.Flags().String("foo", "", "foo things")
@ -359,7 +359,30 @@ func TestRunWithTraverse(t *testing.T) {
sub.Flags().String("add", "", "add things") sub.Flags().String("add", "", "add things")
cmd.AddCommand(sub) cmd.AddCommand(sub)
c, args, err := cmd.Traverse([]string{"--foo", "ok", "next"}) c, args, err := cmd.Traverse([]string{"--foo", "ok", "next", "--add"})
if err != nil {
t.Fatalf("Expected no error: %s", err)
}
if len(args) != 1 && args[0] != "--add" {
t.Fatalf("wrong args %s", args)
}
if c.Name() != sub.Name() {
t.Fatalf("wrong command %q expected %q", c.Name(), sub.Name())
}
}
func TestTraverseNoParentFlags(t *testing.T) {
cmd := &Command{
Use: "do",
TraverseChildren: true,
}
cmd.Flags().String("foo", "", "foo things")
sub := &Command{Use: "next"}
sub.Flags().String("add", "", "add things")
cmd.AddCommand(sub)
c, args, err := cmd.Traverse([]string{"next"})
if err != nil { if err != nil {
t.Fatalf("Expected no error: %s", err) t.Fatalf("Expected no error: %s", err)
} }
@ -370,3 +393,47 @@ func TestRunWithTraverse(t *testing.T) {
t.Fatalf("wrong command %q expected %q", c.Name(), sub.Name()) t.Fatalf("wrong command %q expected %q", c.Name(), sub.Name())
} }
} }
func TestTraverseWithBadParentFlags(t *testing.T) {
cmd := &Command{
Use: "do",
TraverseChildren: true,
}
sub := &Command{Use: "next"}
sub.Flags().String("add", "", "add things")
cmd.AddCommand(sub)
expected := "got unknown flag: --add"
c, _, err := cmd.Traverse([]string{"--add", "ok", "next"})
if err == nil || strings.Contains(err.Error(), expected) {
t.Fatalf("Expected error %s got %s", expected, err)
}
if c != nil {
t.Fatalf("Expected nil command")
}
}
func TestTraverseWithBadChildFlag(t *testing.T) {
cmd := &Command{
Use: "do",
TraverseChildren: true,
}
cmd.Flags().String("foo", "", "foo things")
sub := &Command{Use: "next"}
cmd.AddCommand(sub)
// Expect no error because the last commands args shouldn't be parsed in
// Traverse
c, args, err := cmd.Traverse([]string{"next", "--add"})
if err != nil {
t.Fatalf("Expected no error: %s", err)
}
if len(args) != 1 && args[0] != "--add" {
t.Fatalf("wrong args %s", args)
}
if c.Name() != sub.Name() {
t.Fatalf("wrong command %q expected %q", c.Name(), sub.Name())
}
}