Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: hide subcommand from short help when no subcommands #202

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
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 <oli@tableflip.io>
olizilla committed Oct 22, 2020
commit b0de627e28843864add2c5cc438b39ed5da6ffe7
48 changes: 48 additions & 0 deletions cli/helptext_test.go
Original file line number Diff line number Diff line change
@@ -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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you also add a test that checks on subcommands? The two things I'd like to watch for are:

  1. I'd like some signal that these tests are still useful even if someone changes "For more information about each command" to be rephrased slightly. This could also be aided by checking a constant.
  2. Making sure that subcommands are printed

// 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")
}
}