From 1f716e79b96001c5688c57ace8dc21dbcdff8953 Mon Sep 17 00:00:00 2001 From: Alexander Weiss Date: Wed, 8 Jul 2020 21:19:31 +0200 Subject: [PATCH] Add tests for grouping commands --- command_test.go | 73 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/command_test.go b/command_test.go index b3dd030402..e53beab888 100644 --- a/command_test.go +++ b/command_test.go @@ -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)