mirror of
https://github.com/spf13/cobra
synced 2025-05-05 04:47:22 +00:00
Add tests for grouping commands
This commit is contained in:
parent
3f96ff8c4a
commit
19b8f53afc
1 changed files with 73 additions and 0 deletions
|
@ -1767,6 +1767,79 @@ func TestEnableCommandSortingIsDisabled(t *testing.T) {
|
|||
EnableCommandSorting = defaultCommandSorting
|
||||
}
|
||||
|
||||
func TestUsageWithGroup(t *testing.T) {
|
||||
var rootCmd = &Command{Use: "root", Short: "test", Run: emptyRun}
|
||||
rootCmd.CompletionOptions.DisableDefaultCmd = true
|
||||
|
||||
rootCmd.AddGroup(&Group{ID: "group1", Title: "group1"})
|
||||
rootCmd.AddGroup(&Group{ID: "group2", Title: "group2"})
|
||||
|
||||
rootCmd.AddCommand(&Command{Use: "cmd1", GroupID: "group1", Run: emptyRun})
|
||||
rootCmd.AddCommand(&Command{Use: "cmd2", GroupID: "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.CompletionOptions.DisableDefaultCmd = true
|
||||
|
||||
rootCmd.AddGroup(&Group{ID: "group", Title: "group"})
|
||||
rootCmd.AddCommand(&Command{Use: "xxx", GroupID: "group", Run: emptyRun})
|
||||
rootCmd.SetHelpCommandGroupID("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 TestUsageCompletionpGroup(t *testing.T) {
|
||||
var rootCmd = &Command{Use: "root", Short: "test", Run: emptyRun}
|
||||
|
||||
rootCmd.AddGroup(&Group{ID: "group", Title: "group"})
|
||||
rootCmd.AddGroup(&Group{ID: "help", Title: "help"})
|
||||
|
||||
rootCmd.AddCommand(&Command{Use: "xxx", GroupID: "group", Run: emptyRun})
|
||||
rootCmd.SetHelpCommandGroupID("help")
|
||||
rootCmd.SetCompletionCommandGroupID("group")
|
||||
|
||||
output, err := executeCommand(rootCmd, "--help")
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
}
|
||||
|
||||
// now completion should be grouped under "group"
|
||||
checkStringOmits(t, output, "\nAvailable Commands:\n completion")
|
||||
checkStringContains(t, output, "\nAvailable Commands:\n\ngroup\n completion")
|
||||
}
|
||||
|
||||
func TestAddGroup(t *testing.T) {
|
||||
var rootCmd = &Command{Use: "root", Short: "test", Run: emptyRun}
|
||||
|
||||
rootCmd.AddGroup(&Group{ID: "group", Title: "Test group"})
|
||||
rootCmd.AddCommand(&Command{Use: "cmd", GroupID: "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)
|
||||
|
|
Loading…
Add table
Reference in a new issue