Add tests about additional help topics

This commit is contained in:
Eric Paris 2015-04-07 11:56:08 -04:00
parent f576d29563
commit 86a16864ea
2 changed files with 23 additions and 6 deletions

View File

@ -71,6 +71,12 @@ var cmdRootWithRun = &Command{
}, },
} }
var cmdSubNoRun = &Command{
Use: "subnorun",
Short: "A subcommand without a Run function",
Long: "A long output about a subcommand without a Run function",
}
var cmdVersion1 = &Command{ var cmdVersion1 = &Command{
Use: "version", Use: "version",
Short: "Print the version number", Short: "Print the version number",
@ -96,6 +102,7 @@ func flagInit() {
cmdRootNoRun.ResetFlags() cmdRootNoRun.ResetFlags()
cmdRootSameName.ResetFlags() cmdRootSameName.ResetFlags()
cmdRootWithRun.ResetFlags() cmdRootWithRun.ResetFlags()
cmdSubNoRun.ResetFlags()
cmdRootNoRun.PersistentFlags().StringVarP(&flags2a, "strtwo", "t", "two", strtwoParentHelp) cmdRootNoRun.PersistentFlags().StringVarP(&flags2a, "strtwo", "t", "two", strtwoParentHelp)
cmdEcho.Flags().IntVarP(&flagi1, "intone", "i", 123, "help message for flag intone") cmdEcho.Flags().IntVarP(&flagi1, "intone", "i", 123, "help message for flag intone")
cmdTimes.Flags().IntVarP(&flagi2, "inttwo", "j", 234, "help message for flag inttwo") cmdTimes.Flags().IntVarP(&flagi2, "inttwo", "j", 234, "help message for flag inttwo")
@ -118,6 +125,7 @@ func commandInit() {
cmdRootNoRun.ResetCommands() cmdRootNoRun.ResetCommands()
cmdRootSameName.ResetCommands() cmdRootSameName.ResetCommands()
cmdRootWithRun.ResetCommands() cmdRootWithRun.ResetCommands()
cmdSubNoRun.ResetCommands()
} }
func initialize() *Command { func initialize() *Command {
@ -187,7 +195,7 @@ func fullTester(c *Command, input string) resulter {
// Testing flag with invalid input // Testing flag with invalid input
c.SetOutput(buf) c.SetOutput(buf)
cmdEcho.AddCommand(cmdTimes) cmdEcho.AddCommand(cmdTimes)
c.AddCommand(cmdPrint, cmdEcho) c.AddCommand(cmdPrint, cmdEcho, cmdSubNoRun)
c.SetArgs(strings.Split(input, " ")) c.SetArgs(strings.Split(input, " "))
err := c.Execute() err := c.Execute()
@ -488,11 +496,14 @@ func TestPersistentFlags(t *testing.T) {
} }
func TestHelpCommand(t *testing.T) { func TestHelpCommand(t *testing.T) {
c := fullSetupTest("help echo") x := fullSetupTest("help")
checkResultContains(t, c, cmdEcho.Long) checkResultContains(t, x, cmdRootWithRun.Long)
r := fullSetupTest("help echo times") x = fullSetupTest("help echo")
checkResultContains(t, r, cmdTimes.Long) checkResultContains(t, x, cmdEcho.Long)
x = fullSetupTest("help echo times")
checkResultContains(t, x, cmdTimes.Long)
} }
func TestChildCommandHelp(t *testing.T) { func TestChildCommandHelp(t *testing.T) {
@ -502,6 +513,11 @@ func TestChildCommandHelp(t *testing.T) {
checkResultContains(t, r, strtwoChildHelp) checkResultContains(t, r, strtwoChildHelp)
} }
func TestNonRunChildHelp(t *testing.T) {
x := noRRSetupTest("subnorun")
checkResultContains(t, x, cmdSubNoRun.Long)
}
func TestRunnableRootCommand(t *testing.T) { func TestRunnableRootCommand(t *testing.T) {
fullSetupTest("") fullSetupTest("")

View File

@ -234,7 +234,8 @@ Available Commands: {{range .Commands}}{{if .Runnable}}
{{.LocalFlags.FlagUsages}}{{end}} {{.LocalFlags.FlagUsages}}{{end}}
{{ if .HasInheritedFlags}}Global Flags: {{ if .HasInheritedFlags}}Global Flags:
{{.InheritedFlags.FlagUsages}}{{end}}{{if or (.HasHelpSubCommands) (.HasRunnableSiblings)}} {{.InheritedFlags.FlagUsages}}{{end}}{{if or (.HasHelpSubCommands) (.HasRunnableSiblings)}}
Additional help topics: {{if .HasHelpSubCommands}}{{range .Commands}}{{if not .Runnable}} {{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{if .HasRunnableSiblings }}{{range .Parent.Commands}}{{if .Runnable}}{{if not (eq .Name $cmd.Name) }} Additional help topics:
{{if .HasHelpSubCommands}}{{range .Commands}}{{if not .Runnable}} {{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{if .HasRunnableSiblings }}{{range .Parent.Commands}}{{if .Runnable}}{{if not (eq .Name $cmd.Name) }}
{{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{end}} {{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{end}}
{{end}}{{ if .HasSubCommands }} {{end}}{{ if .HasSubCommands }}
Use "{{.Root.Name}} help [command]" for more information about a command. Use "{{.Root.Name}} help [command]" for more information about a command.