diff --git a/command_test.go b/command_test.go index 1012ca4ad..3d3afe01d 100644 --- a/command_test.go +++ b/command_test.go @@ -177,88 +177,92 @@ func TestSubcommandExecuteC(t *testing.T) { func TestSubcommandExecuteMissingSubcommand(t *testing.T) { rootCmd := &Command{Use: "root", Run: emptyRun} - childCmd := &Command{Use: "child", Run: nil, ErrorOnUnknownSubcommand: false} - child2Cmd := &Command{Use: "child2", Run: emptyRun} + const childName = "child" + const grandchildName = "grandchild" + childCmd := &Command{Use: childName, Run: nil, ErrorOnUnknownSubcommand: false} + child2Cmd := &Command{Use: grandchildName, Run: emptyRun} rootCmd.AddCommand(childCmd) childCmd.AddCommand(child2Cmd) // test existing command - c, output, err := executeCommandC(rootCmd, "child") + c, output, err := executeCommandC(rootCmd, childName) if !strings.HasPrefix(output, "Usage:") { t.Errorf("Unexpected output: %v", output) } if err != nil { t.Errorf("Unexpected error: %v", err) } - if c.Name() != "child" { + if c.Name() != childName { t.Errorf(`invalid command returned from ExecuteC: expected "child"', got: %q`, c.Name()) } // test existing sub command - c, output, err = executeCommandC(rootCmd, "child", "child2") + c, output, err = executeCommandC(rootCmd, childName, grandchildName) if output != "" { t.Errorf("Unexpected output: %v", output) } if err != nil { t.Errorf("Unexpected error: %v", err) } - if c.Name() != "child2" { - t.Errorf(`invalid command returned from ExecuteC: expected "child"', got: %q`, c.Name()) + if c.Name() != grandchildName { + t.Errorf(`invalid command returned from ExecuteC: expected "grandchild"', got: %q`, c.Name()) } // now test a command which does not exist, we will get no error, just "Usage:" is printed - c, output, err = executeCommandC(rootCmd, "child", "unknownChild") + c, output, err = executeCommandC(rootCmd, childName, "unknownChild") if !strings.HasPrefix(output, "Usage:") { t.Errorf("Expected: 'Usage: ...'\nGot:\n %q\n", output) } if err != nil { t.Errorf("Unexpected error: %v", err) } - if c.Name() != "child" { + if c.Name() != childName { t.Errorf(`invalid command returned from ExecuteC: expected "child"', got: %q`, c.Name()) } } func TestSubcommandExecuteMissingSubcommandWithErrorOnUnknownSubcommand(t *testing.T) { rootCmd := &Command{Use: "root", Run: emptyRun} - childCmd := &Command{Use: "child", Run: nil, ErrorOnUnknownSubcommand: true} - child2Cmd := &Command{Use: "child2", Run: emptyRun} + const childName = "child" + const grandchildName = "grandchild" + childCmd := &Command{Use: childName, Run: nil, ErrorOnUnknownSubcommand: true} + child2Cmd := &Command{Use: grandchildName, Run: emptyRun} rootCmd.AddCommand(childCmd) childCmd.AddCommand(child2Cmd) // test existing command - c, output, err := executeCommandC(rootCmd, "child") + c, output, err := executeCommandC(rootCmd, childName) if !strings.HasPrefix(output, "Error:") { t.Errorf("Unexpected output: %v", output) } if !errors.Is(err, ErrUnknownSubcommand) { t.Errorf("Unexpected error: %v", err) } - if c.Name() != "child" { + if c.Name() != childName { t.Errorf(`invalid command returned from ExecuteC: expected "child"', got: %q`, c.Name()) } // test existing sub command - c, output, err = executeCommandC(rootCmd, "child", "child2") + c, output, err = executeCommandC(rootCmd, childName, grandchildName) if output != "" { t.Errorf("Unexpected output: %v", output) } if err != nil { t.Errorf("Unexpected error: %v", err) } - if c.Name() != "child2" { + if c.Name() != grandchildName { t.Errorf(`invalid command returned from ExecuteC: expected "child"', got: %q`, c.Name()) } // now test a command which does not exist, we expect an error because of the ErrorOnUnknownSubcommand flag - c, output, err = executeCommandC(rootCmd, "child", "unknownChild") + c, output, err = executeCommandC(rootCmd, childName, "unknownChild") if !strings.HasPrefix(output, "Error:") { t.Errorf("Unexpected output: %v", output) } if !errors.Is(err, ErrUnknownSubcommand) { t.Errorf("Unexpected error: %v", err) } - if c.Name() != "child" { + if c.Name() != childName { t.Errorf(`invalid command returned from ExecuteC: expected "child"', got: %q`, c.Name()) } }