From c7aac252f1b9115886ff47f2ffbba0db3d693d99 Mon Sep 17 00:00:00 2001 From: Tristan Zajonc Date: Wed, 12 Aug 2015 22:14:26 -0700 Subject: [PATCH] add tests --- app.go | 2 +- app_test.go | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++++ command.go | 2 +- 3 files changed, 95 insertions(+), 2 deletions(-) diff --git a/app.go b/app.go index 26dfd7d..4d40014 100644 --- a/app.go +++ b/app.go @@ -13,7 +13,7 @@ import ( type App struct { // The name of the program. Defaults to os.Args[0] Name string - // Name of command for help, defaults to Name + // Full name of command for help, defaults to Name HelpName string // Description of the program. Usage string diff --git a/app_test.go b/app_test.go index 2b53b4f..ada5d69 100644 --- a/app_test.go +++ b/app_test.go @@ -763,6 +763,99 @@ func TestApp_Run_SubcommandFullPath(t *testing.T) { } } +func TestApp_Run_SubcommandHelpName(t *testing.T) { + app := NewApp() + buf := new(bytes.Buffer) + app.Writer = buf + app.Name = "command" + subCmd := Command{ + Name: "bar", + HelpName: "custom", + Usage: "does bar things", + } + cmd := Command{ + Name: "foo", + Description: "foo commands", + Subcommands: []Command{subCmd}, + } + app.Commands = []Command{cmd} + + err := app.Run([]string{"command", "foo", "bar", "--help"}) + if err != nil { + t.Error(err) + } + + output := buf.String() + if !strings.Contains(output, "custom - does bar things") { + t.Errorf("expected HelpName for subcommand: %s", output) + } + if !strings.Contains(output, "custom [arguments...]") { + t.Errorf("expected HelpName to subcommand: %s", output) + } +} + +func TestApp_Run_CommandHelpName(t *testing.T) { + app := NewApp() + buf := new(bytes.Buffer) + app.Writer = buf + app.Name = "command" + subCmd := Command{ + Name: "bar", + Usage: "does bar things", + } + cmd := Command{ + Name: "foo", + HelpName: "custom", + Description: "foo commands", + Subcommands: []Command{subCmd}, + } + app.Commands = []Command{cmd} + + err := app.Run([]string{"command", "foo", "bar", "--help"}) + if err != nil { + t.Error(err) + } + + output := buf.String() + if !strings.Contains(output, "command foo bar - does bar things") { + t.Errorf("expected full path to subcommand: %s", output) + } + if !strings.Contains(output, "command foo bar [arguments...]") { + t.Errorf("expected full path to subcommand: %s", output) + } +} + +func TestApp_Run_CommandSubcommandHelpName(t *testing.T) { + app := NewApp() + buf := new(bytes.Buffer) + app.Writer = buf + app.Name = "base" + subCmd := Command{ + Name: "bar", + HelpName: "custom", + Usage: "does bar things", + } + cmd := Command{ + Name: "foo", + Description: "foo commands", + Subcommands: []Command{subCmd}, + } + app.Commands = []Command{cmd} + + err := app.Run([]string{"command", "foo", "--help"}) + if err != nil { + t.Error(err) + } + + output := buf.String() + if !strings.Contains(output, "base foo - foo commands") { + t.Errorf("expected full path to subcommand: %s", output) + } + if !strings.Contains(output, "base foo command [command options] [arguments...]") { + t.Errorf("expected full path to subcommand: %s", output) + } +} + func TestApp_Run_Help(t *testing.T) { var helpArguments = [][]string{{"boom", "--help"}, {"boom", "-h"}, {"boom", "help"}} diff --git a/command.go b/command.go index 9491c30..fac754d 100644 --- a/command.go +++ b/command.go @@ -39,7 +39,7 @@ type Command struct { // Boolean to hide built-in help command HideHelp bool - // Name of command for help, defaults to full command name + // Full name of command for help, defaults to full command name, including parent commands. HelpName string commandNamePath []string }