@@ -9,33 +9,24 @@ import (
99 "github.com/spf13/pflag"
1010)
1111
12- const (
13- success = "test successful"
14-
15- expectedParentCmdHelpOutput = `Usage: mockParentCmd Mock parent command usage.
16-
17- Description: Mock parent command description.
18-
19- Commands:
20- s, sc, sub, mockSubCmd - A simple mock subcommand with aliases and its own subcommand.
21- `
22- )
23-
24- var subCmd = & mockSubCmd {buf : new (bytes.Buffer )}
12+ var subCmd = new (mockSubCmd )
2513
2614type (
2715 // Mock for root/parent command.
2816 mockParentCmd struct {}
29- // Mock subcommand with aliases and a nested
30- // sucommand of its own.
31- mockSubCmd struct { buf * bytes.Buffer }
17+ // Mock subcommand with aliases and a nested sucommand of its own.
18+ mockSubCmd struct {
19+ buf * bytes.Buffer
20+ }
3221 // Mock subcommand with aliases and no nested subcommands.
3322 mockSubCmdNoNested struct {}
3423)
3524
3625func (c * mockParentCmd ) Run (fl * pflag.FlagSet ) {}
3726
38- func (c * mockParentCmd ) Subcommands () []Command { return []Command {subCmd } }
27+ func (c * mockParentCmd ) Subcommands () []Command {
28+ return []Command {subCmd }
29+ }
3930
4031func (c * mockParentCmd ) Spec () CommandSpec {
4132 return CommandSpec {
@@ -47,7 +38,8 @@ func (c *mockParentCmd) Spec() CommandSpec {
4738
4839func (c * mockSubCmd ) Run (fl * pflag.FlagSet ) {
4940 c .buf = new (bytes.Buffer )
50- if _ , err := c .Write ([]byte (success )); err != nil {
41+ _ , err := c .WriteString ("success" )
42+ if err != nil {
5143 fl .Usage ()
5244 }
5345}
@@ -56,7 +48,9 @@ func (c *mockSubCmd) Subcommands() []Command {
5648 return []Command {new (mockSubCmd )}
5749}
5850
59- func (c * mockSubCmd ) Write (b []byte ) (int , error ) { return c .buf .Write (b ) }
51+ func (c * mockSubCmd ) WriteString (s string ) (int , error ) {
52+ return c .buf .WriteString (s )
53+ }
6054
6155func (c * mockSubCmd ) Spec () CommandSpec {
6256 return CommandSpec {
@@ -79,49 +73,40 @@ func (c *mockSubCmdNoNested) Spec() CommandSpec {
7973}
8074
8175func TestSubCmdAliases (t * testing.T ) {
82- for _ , test := range []struct {
83- name , expected string
84- }{
85- {
86- name : "s" ,
87- expected : success ,
88- },
89- {
90- name : "sc" ,
91- expected : success ,
92- },
93- {
94- name : "sub" ,
95- expected : success ,
96- },
97- } {
98- t .Run (test .name , func (t * testing.T ) {
99- // Since the alias is the name of the test
100- // we can just pass it as the alias arg.
101- os .Args = []string {"mockParentCmd" , test .name }
102- // Based on os.Args, when splitArgs is invoked,
103- // it should be able to deduce the subcommand we want
104- // based on the new alias map it's being passed.
105- RunRoot (& mockParentCmd {})
106- // The success const is never written into the buffer
107- // if the subcommand fails to be invoked by alias.
76+ for _ , alias := range []string {"s" , "sc" , "sub" } {
77+ t .Run (alias , func (t * testing.T ) {
78+ // Setup command.
79+ cmd := new (mockParentCmd )
80+ os .Args = []string {cmd .Spec ().Name , alias }
81+ // Run command.
82+ RunRoot (cmd )
83+ // If "success" isn't written into the buffer
84+ // then we failed to find the subcommand by alias.
10885 got := string (subCmd .buf .Bytes ())
109- assert .Equal (t , test . name , test . expected , got )
86+ assert .Equal (t , t . Name (), "success" , got )
11087 })
11188 }
11289}
11390
114- func TestSubcmdAliasesInParentCmdHelpOutput (t * testing.T ) {
115- buf := new (bytes.Buffer )
116- cmd := & mockParentCmd {}
117- name := cmd .Spec ().Name
118- fl := pflag .NewFlagSet (name , pflag .ExitOnError )
119- // If the help output is not written to the buffer
120- // in the format we expect then the test will fail.
121- renderHelp (name , cmd , fl , buf )
122- got := string (buf .Bytes ())
123- expected := expectedParentCmdHelpOutput
124- assert .Equal (t , "display_subcmd_aliases_in_parentcmd_help_output" , expected , got )
91+ func TestCmdHelpOutput (t * testing.T ) {
92+ t .Run (t .Name (), func (t * testing.T ) {
93+ expected := `Usage: mockParentCmd Mock parent command usage.
94+
95+ Description: Mock parent command description.
96+
97+ Commands:
98+ s, sc, sub, mockSubCmd - A simple mock subcommand with aliases and its own subcommand.
99+ `
100+ buf := new (bytes.Buffer )
101+ cmd := new (mockParentCmd )
102+ name := cmd .Spec ().Name
103+ fl := pflag .NewFlagSet (name , pflag .ExitOnError )
104+ // If the help output doesn't contain the subcommand and
105+ // isn't formatted the way we expect the test will fail.
106+ renderHelp (name , cmd , fl , buf )
107+ got := string (buf .Bytes ())
108+ assert .Equal (t , t .Name (), expected , got )
109+ })
125110}
126111
127112func TestSubCmdHelpOutput (t * testing.T ) {
0 commit comments