From 397434967568d7b2977c62a962aff80f8bb36c26 Mon Sep 17 00:00:00 2001 From: Andre Mueller Date: Mon, 8 Jul 2024 16:17:02 +0200 Subject: [PATCH] fix linting issue --- command_test.go | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) 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()) } }