Skip to content

Commit

Permalink
Add tests for grouping commands
Browse files Browse the repository at this point in the history
  • Loading branch information
aawsome committed Jul 8, 2020
1 parent ad0449e commit 41956f2
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1588,6 +1588,53 @@ func TestEnableCommandSortingIsDisabled(t *testing.T) {
EnableCommandSorting = true
}

func TestUsageWithGroup(t *testing.T) {
var rootCmd = &Command{Use: "root", Short: "test", Run: emptyRun}

rootCmd.AddCommand(&Command{Use: "cmd1", Group: "group1", Run: emptyRun})
rootCmd.AddCommand(&Command{Use: "cmd2", Group: "group2", Run: emptyRun})

output, err := executeCommand(rootCmd, "--help")
if err != nil {
t.Errorf("Unexpected error: %v", err)
}

// help should be ungrouped here
checkStringContains(t, output, "\nAvailable Commands:\n help")
checkStringContains(t, output, "\ngroup1\n cmd1")
checkStringContains(t, output, "\ngroup2\n cmd2")
}

func TestUsageHelpGroup(t *testing.T) {
var rootCmd = &Command{Use: "root", Short: "test", Run: emptyRun}

rootCmd.AddCommand(&Command{Use: "xxx", Group: "group", Run: emptyRun})
rootCmd.SetHelpCommandGroup("group")

output, err := executeCommand(rootCmd, "--help")
if err != nil {
t.Errorf("Unexpected error: %v", err)
}

// now help should be grouped under "group"
checkStringOmits(t, output, "\nAvailable Commands:\n help")
checkStringContains(t, output, "\nAvailable Commands:\n\ngroup\n help")
}

func TestAddGroup(t *testing.T) {
var rootCmd = &Command{Use: "root", Short: "test", Run: emptyRun}

rootCmd.AddGroup(&Group{Group: "group", Title: "Test group"})
rootCmd.AddCommand(&Command{Use: "cmd", Group: "group", Run: emptyRun})

output, err := executeCommand(rootCmd, "--help")
if err != nil {
t.Errorf("Unexpected error: %v", err)
}

checkStringContains(t, output, "\nTest group\n cmd")
}

func TestSetOutput(t *testing.T) {
c := &Command{}
c.SetOutput(nil)
Expand Down

0 comments on commit 41956f2

Please sign in to comment.