@@ -12,10 +12,14 @@ import (
1212var subCmd = new (mockSubCmd )
1313
1414type (
15+ // Mock for root/parent command.
1516 mockParentCmd struct {}
16- mockSubCmd struct {
17+ // Mock subcommand with aliases and a nested sucommand of its own.
18+ mockSubCmd struct {
1719 buf * bytes.Buffer
1820 }
21+ // Mock subcommand with aliases and no nested subcommands.
22+ mockSubCmdNoNested struct {}
1923)
2024
2125func (c * mockParentCmd ) Run (fl * pflag.FlagSet ) {}
@@ -40,6 +44,10 @@ func (c *mockSubCmd) Run(fl *pflag.FlagSet) {
4044 }
4145}
4246
47+ func (c * mockSubCmd ) Subcommands () []Command {
48+ return []Command {new (mockSubCmd )}
49+ }
50+
4351func (c * mockSubCmd ) WriteString (s string ) (int , error ) {
4452 return c .buf .WriteString (s )
4553}
@@ -49,7 +57,18 @@ func (c *mockSubCmd) Spec() CommandSpec {
4957 Name : "mockSubCmd" ,
5058 Usage : "Test a subcommand." ,
5159 Aliases : []string {"s" , "sc" , "sub" },
52- Desc : "A simple mock subcommand with aliases." ,
60+ Desc : "A simple mock subcommand with aliases and its own subcommand." ,
61+ }
62+ }
63+
64+ func (c * mockSubCmdNoNested ) Run (fl * pflag.FlagSet ) {}
65+
66+ func (c * mockSubCmdNoNested ) Spec () CommandSpec {
67+ return CommandSpec {
68+ Name : "mockSubCmdNoNested" ,
69+ Usage : "Used for help output tests." ,
70+ Aliases : []string {"s" , "sc" , "sub" },
71+ Desc : "A simple mock subcommand with aliases and no nested subcommands." ,
5372 }
5473}
5574
@@ -73,19 +92,65 @@ func TestCmdHelpOutput(t *testing.T) {
7392 t .Run (t .Name (), func (t * testing.T ) {
7493 expected := `Usage: mockParentCmd Mock parent command usage.
7594
76- Mock parent command description.
95+ Description: Mock parent command description.
7796
7897Commands:
79- s,sc,sub,mockSubCmd - A simple mock subcommand with aliases.
98+ s, sc, sub, mockSubCmd - A simple mock subcommand with aliases and its own subcommand .
8099`
81100 buf := new (bytes.Buffer )
82101 cmd := new (mockParentCmd )
83102 name := cmd .Spec ().Name
84103 fl := pflag .NewFlagSet (name , pflag .ExitOnError )
85104 // If the help output doesn't contain the subcommand and
86105 // isn't formatted the way we expect the test will fail.
87- renderHelp (name , cmd , fl , buf )
88- got := string ( buf .Bytes () )
106+ renderHelp (buf , name , cmd , fl )
107+ got := buf .String ( )
89108 assert .Equal (t , t .Name (), expected , got )
90109 })
91110}
111+
112+ func TestSubCmdHelpOutput (t * testing.T ) {
113+ withNested := `Usage: mockSubCmd Test a subcommand.
114+
115+ Aliases: s, sc, sub
116+
117+ Description: A simple mock subcommand with aliases and its own subcommand.
118+
119+ Commands:
120+ s, sc, sub, mockSubCmd - A simple mock subcommand with aliases and its own subcommand.
121+ `
122+
123+ noNested := `Usage: mockSubCmdNoNested Used for help output tests.
124+
125+ Aliases: s, sc, sub
126+
127+ Description: A simple mock subcommand with aliases and no nested subcommands.
128+ `
129+
130+ for _ , test := range []struct {
131+ name , expected string
132+ cmd Command
133+ }{
134+ {
135+ name : "subcmd w/nested subcmd." ,
136+ expected : withNested ,
137+ cmd : new (mockSubCmd ),
138+ },
139+ {
140+ name : "subcmd w/no nested subcmds." ,
141+ expected : noNested ,
142+ cmd : new (mockSubCmdNoNested ),
143+ },
144+ } {
145+ t .Run (test .name , func (t * testing.T ) {
146+ buf := new (bytes.Buffer )
147+ name := test .cmd .Spec ().Name
148+ fl := pflag .NewFlagSet (name , pflag .ExitOnError )
149+ // If the help output is not written to the buffer
150+ // in the format we expect then the test will fail.
151+ renderHelp (buf , name , test .cmd , fl )
152+ got := buf .String ()
153+ assert .Equal (t , t .Name (), test .expected , got )
154+ })
155+ }
156+ }
0 commit comments