fix linting issue

This commit is contained in:
Andre Mueller 2024-07-08 16:17:02 +02:00 committed by Andre Mueller
parent 971a0f75db
commit 4281a8d6a3

View file

@ -177,88 +177,92 @@ func TestSubcommandExecuteC(t *testing.T) {
func TestSubcommandExecuteMissingSubcommand(t *testing.T) { func TestSubcommandExecuteMissingSubcommand(t *testing.T) {
rootCmd := &Command{Use: "root", Run: emptyRun} rootCmd := &Command{Use: "root", Run: emptyRun}
childCmd := &Command{Use: "child", Run: nil, ErrorOnUnknownSubcommand: false} const childName = "child"
child2Cmd := &Command{Use: "child2", Run: emptyRun} const grandchildName = "grandchild"
childCmd := &Command{Use: childName, Run: nil, ErrorOnUnknownSubcommand: false}
child2Cmd := &Command{Use: grandchildName, Run: emptyRun}
rootCmd.AddCommand(childCmd) rootCmd.AddCommand(childCmd)
childCmd.AddCommand(child2Cmd) childCmd.AddCommand(child2Cmd)
// test existing command // test existing command
c, output, err := executeCommandC(rootCmd, "child") c, output, err := executeCommandC(rootCmd, childName)
if !strings.HasPrefix(output, "Usage:") { if !strings.HasPrefix(output, "Usage:") {
t.Errorf("Unexpected output: %v", output) t.Errorf("Unexpected output: %v", output)
} }
if err != nil { if err != nil {
t.Errorf("Unexpected error: %v", err) 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()) t.Errorf(`invalid command returned from ExecuteC: expected "child"', got: %q`, c.Name())
} }
// test existing sub command // test existing sub command
c, output, err = executeCommandC(rootCmd, "child", "child2") c, output, err = executeCommandC(rootCmd, childName, grandchildName)
if output != "" { if output != "" {
t.Errorf("Unexpected output: %v", output) t.Errorf("Unexpected output: %v", output)
} }
if err != nil { if err != nil {
t.Errorf("Unexpected error: %v", err) 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()) 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 // 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:") { if !strings.HasPrefix(output, "Usage:") {
t.Errorf("Expected: 'Usage: ...'\nGot:\n %q\n", output) t.Errorf("Expected: 'Usage: ...'\nGot:\n %q\n", output)
} }
if err != nil { if err != nil {
t.Errorf("Unexpected error: %v", err) 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()) t.Errorf(`invalid command returned from ExecuteC: expected "child"', got: %q`, c.Name())
} }
} }
func TestSubcommandExecuteMissingSubcommandWithErrorOnUnknownSubcommand(t *testing.T) { func TestSubcommandExecuteMissingSubcommandWithErrorOnUnknownSubcommand(t *testing.T) {
rootCmd := &Command{Use: "root", Run: emptyRun} rootCmd := &Command{Use: "root", Run: emptyRun}
childCmd := &Command{Use: "child", Run: nil, ErrorOnUnknownSubcommand: true} const childName = "child"
child2Cmd := &Command{Use: "child2", Run: emptyRun} const grandchildName = "grandchild"
childCmd := &Command{Use: childName, Run: nil, ErrorOnUnknownSubcommand: true}
child2Cmd := &Command{Use: grandchildName, Run: emptyRun}
rootCmd.AddCommand(childCmd) rootCmd.AddCommand(childCmd)
childCmd.AddCommand(child2Cmd) childCmd.AddCommand(child2Cmd)
// test existing command // test existing command
c, output, err := executeCommandC(rootCmd, "child") c, output, err := executeCommandC(rootCmd, childName)
if !strings.HasPrefix(output, "Error:") { if !strings.HasPrefix(output, "Error:") {
t.Errorf("Unexpected output: %v", output) t.Errorf("Unexpected output: %v", output)
} }
if !errors.Is(err, ErrUnknownSubcommand) { if !errors.Is(err, ErrUnknownSubcommand) {
t.Errorf("Unexpected error: %v", err) 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()) t.Errorf(`invalid command returned from ExecuteC: expected "child"', got: %q`, c.Name())
} }
// test existing sub command // test existing sub command
c, output, err = executeCommandC(rootCmd, "child", "child2") c, output, err = executeCommandC(rootCmd, childName, grandchildName)
if output != "" { if output != "" {
t.Errorf("Unexpected output: %v", output) t.Errorf("Unexpected output: %v", output)
} }
if err != nil { if err != nil {
t.Errorf("Unexpected error: %v", err) 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()) 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 // 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:") { if !strings.HasPrefix(output, "Error:") {
t.Errorf("Unexpected output: %v", output) t.Errorf("Unexpected output: %v", output)
} }
if !errors.Is(err, ErrUnknownSubcommand) { if !errors.Is(err, ErrUnknownSubcommand) {
t.Errorf("Unexpected error: %v", err) 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()) t.Errorf(`invalid command returned from ExecuteC: expected "child"', got: %q`, c.Name())
} }
} }