@@ -14,18 +14,23 @@ const (
1414
1515 expectedParentCmdHelpOutput = `Usage: mockParentCmd Mock parent command usage.
1616
17- Mock parent command description.
17+ Description: Mock parent command description.
1818
1919Commands:
20- s,sc,sub,mockSubCmd - A simple mock subcommand with aliases.
20+ s, sc, sub, mockSubCmd - A simple mock subcommand with aliases and its own subcommand .
2121`
2222)
2323
2424var subCmd = & mockSubCmd {buf : new (bytes.Buffer )}
2525
2626type (
27+ // Mock for root/parent command.
2728 mockParentCmd struct {}
28- mockSubCmd struct { buf * bytes.Buffer }
29+ // Mock subcommand with aliases and a nested
30+ // sucommand of its own.
31+ mockSubCmd struct { buf * bytes.Buffer }
32+ // Mock subcommand with aliases and no nested subcommands.
33+ mockSubCmdNoNested struct {}
2934)
3035
3136func (c * mockParentCmd ) Run (fl * pflag.FlagSet ) {}
@@ -47,14 +52,29 @@ func (c *mockSubCmd) Run(fl *pflag.FlagSet) {
4752 }
4853}
4954
55+ func (c * mockSubCmd ) Subcommands () []Command {
56+ return []Command {new (mockSubCmd )}
57+ }
58+
5059func (c * mockSubCmd ) Write (b []byte ) (int , error ) { return c .buf .Write (b ) }
5160
5261func (c * mockSubCmd ) Spec () CommandSpec {
5362 return CommandSpec {
5463 Name : "mockSubCmd" ,
5564 Usage : "Test a subcommand." ,
5665 Aliases : []string {"s" , "sc" , "sub" },
57- Desc : "A simple mock subcommand with aliases." ,
66+ Desc : "A simple mock subcommand with aliases and its own subcommand." ,
67+ }
68+ }
69+
70+ func (c * mockSubCmdNoNested ) Run (fl * pflag.FlagSet ) {}
71+
72+ func (c * mockSubCmdNoNested ) Spec () CommandSpec {
73+ return CommandSpec {
74+ Name : "mockSubCmdNoNested" ,
75+ Usage : "Used for help output tests." ,
76+ Aliases : []string {"s" , "sc" , "sub" },
77+ Desc : "A simple mock subcommand with aliases and no nested subcommands." ,
5878 }
5979}
6080
@@ -103,3 +123,49 @@ func TestSubcmdAliasesInParentCmdHelpOutput(t *testing.T) {
103123 expected := expectedParentCmdHelpOutput
104124 assert .Equal (t , "display_subcmd_aliases_in_parentcmd_help_output" , expected , got )
105125}
126+
127+ func TestSubCmdHelpOutput (t * testing.T ) {
128+ withNested := `Usage: mockSubCmd Test a subcommand.
129+
130+ Aliases: s, sc, sub
131+
132+ Description: A simple mock subcommand with aliases and its own subcommand.
133+
134+ Commands:
135+ s, sc, sub, mockSubCmd - A simple mock subcommand with aliases and its own subcommand.
136+ `
137+
138+ noNested := `Usage: mockSubCmdNoNested Used for help output tests.
139+
140+ Aliases: s, sc, sub
141+
142+ Description: A simple mock subcommand with aliases and no nested subcommands.
143+ `
144+
145+ for _ , test := range []struct {
146+ cmd Command
147+ name , expected string
148+ }{
149+ {
150+ name : "subcmd w/nested subcmd." ,
151+ expected : withNested ,
152+ cmd : new (mockSubCmd ),
153+ },
154+ {
155+ name : "subcmd w/no nested subcmds." ,
156+ expected : noNested ,
157+ cmd : new (mockSubCmdNoNested ),
158+ },
159+ } {
160+ t .Run (test .name , func (t * testing.T ) {
161+ buf := new (bytes.Buffer )
162+ name := test .cmd .Spec ().Name
163+ fl := pflag .NewFlagSet (name , pflag .ExitOnError )
164+ // If the help output is not written to the buffer
165+ // in the format we expect then the test will fail.
166+ renderHelp (name , test .cmd , fl , buf )
167+ got := string (buf .Bytes ())
168+ assert .Equal (t , t .Name (), test .expected , got )
169+ })
170+ }
171+ }
0 commit comments