Skip to content

Commit

Permalink
fix linting issue
Browse files Browse the repository at this point in the history
  • Loading branch information
andremueller-cosateq committed Jul 8, 2024
1 parent 9892605 commit fd99350
Showing 1 changed file with 21 additions and 17 deletions.
38 changes: 21 additions & 17 deletions command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}
Expand Down

0 comments on commit fd99350

Please sign in to comment.