From 35e3fe742cd3b4d00e3ea5595c446eb4df053288 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 | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/command_test.go b/command_test.go index 9640fc5dce..b7c540c7be 100644 --- a/command_test.go +++ b/command_test.go @@ -1577,6 +1577,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)