From b0de627e28843864add2c5cc438b39ed5da6ffe7 Mon Sep 17 00:00:00 2001 From: Oli Evans Date: Thu, 22 Oct 2020 20:29:17 +0100 Subject: [PATCH] wip: show subcommand ShortHelp bug with test ShortHelp always shows the subcommand help prompt even if there are no subcommands. Add a test to demo it. License: MIT Signed-off-by: Oli Evans --- cli/helptext_test.go | 48 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/cli/helptext_test.go b/cli/helptext_test.go index 7e135683..8210e83d 100644 --- a/cli/helptext_test.go +++ b/cli/helptext_test.go @@ -1,6 +1,7 @@ package cli import ( + "bytes" "strings" "testing" @@ -47,4 +48,51 @@ func TestSynopsisGenerator(t *testing.T) { if !strings.Contains(syn, "[--]") { t.Fatal("Synopsis should contain options finalizer") } + if strings.Contains(syn, "For more information about each command") { + t.Fatal("Synopsis should not contain subcommands") + } +} + +func TestShortHelp(t *testing.T) { + // ShortHelp behaves differently depending on whether the command is the root or not. + root := &cmds.Command{ + Subcommands: map[string]*cmds.Command{ + "ls": { + Helptext: cmds.HelpText{ + ShortDescription: ` + Displays the contents of an IPFS or IPNS object(s) at the given path. + `}, + }, + }, + } + // Ask for the help text for the ls command which has no subcommands + path := []string{"ls"} + buf := new(bytes.Buffer) + ShortHelp("ipfs", root, path, buf) + helpText := buf.String() + t.Logf("Short help text: %s", helpText) + if strings.Contains(helpText, "For more information about each command") { + t.Fatal("ShortHelp should not contain subcommand info") + } +} + +func TestLongHelp(t *testing.T) { + root := &cmds.Command{ + Subcommands: map[string]*cmds.Command{ + "ls": { + Helptext: cmds.HelpText{ + ShortDescription: ` + Displays the contents of an IPFS or IPNS object(s) at the given path. + `}, + }, + }, + } + path := []string{"ls"} + buf := new(bytes.Buffer) + LongHelp("ipfs", root, path, buf) + helpText := buf.String() + t.Logf("Long help text: %s", helpText) + if strings.Contains(helpText, "For more information about each command") { + t.Fatal("LongHelp should not contain subcommand info") + } }