From 419884c85a86962b23fb42be6c8acece0148772d Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Wed, 29 Jun 2016 19:32:01 -0400 Subject: [PATCH] Add more Traverse tests. --- command.go | 6 ++-- command_test.go | 73 +++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 73 insertions(+), 6 deletions(-) diff --git a/command.go b/command.go index 0e3a65e0..a3e96e8e 100644 --- a/command.go +++ b/command.go @@ -125,8 +125,8 @@ type Command struct { // Must be > 0. SuggestionsMinimumDistance int - // TraverseChildCommands parses flags on all parents before the child command - TraverseChildCommands bool + // TraverseChildren parses flags on all parents before executing child command + TraverseChildren bool // name is the command name, usually the executable's name. name string @@ -758,7 +758,7 @@ func (c *Command) ExecuteC() (cmd *Command, err error) { } var flags []string - if c.TraverseChildCommands { + if c.TraverseChildren { cmd, flags, err = c.Traverse(args) } else { cmd, flags, err = c.Find(args) diff --git a/command_test.go b/command_test.go index 32738fa4..c086caaa 100644 --- a/command_test.go +++ b/command_test.go @@ -348,10 +348,10 @@ func TestSetHelpCommand(t *testing.T) { } } -func TestRunWithTraverse(t *testing.T) { +func TestTraverseWithParentFlags(t *testing.T) { cmd := &Command{ Use: "do", - TraverseChildCommands: true, + TraverseChildren: true, } cmd.Flags().String("foo", "", "foo things") @@ -359,7 +359,30 @@ func TestRunWithTraverse(t *testing.T) { sub.Flags().String("add", "", "add things") 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 { 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()) } } + +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()) + } +}