@@ -9,28 +9,20 @@ 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- Mock parent command description.
18-
19- Commands:
20- s,sc,sub,mockSubCmd - A simple mock subcommand with aliases.
21- `
22- )
23-
24- var subCmd = & mockSubCmd {buf : new (bytes.Buffer )}
12+ var subCmd = new (mockSubCmd )
2513
2614type (
2715 mockParentCmd struct {}
28- mockSubCmd struct { buf * bytes.Buffer }
16+ mockSubCmd struct {
17+ buf * bytes.Buffer
18+ }
2919)
3020
3121func (c * mockParentCmd ) Run (fl * pflag.FlagSet ) {}
3222
33- func (c * mockParentCmd ) Subcommands () []Command { return []Command {subCmd } }
23+ func (c * mockParentCmd ) Subcommands () []Command {
24+ return []Command {subCmd }
25+ }
3426
3527func (c * mockParentCmd ) Spec () CommandSpec {
3628 return CommandSpec {
@@ -42,12 +34,15 @@ func (c *mockParentCmd) Spec() CommandSpec {
4234
4335func (c * mockSubCmd ) Run (fl * pflag.FlagSet ) {
4436 c .buf = new (bytes.Buffer )
45- if _ , err := c .Write ([]byte (success )); err != nil {
37+ _ , err := c .WriteString ("success" )
38+ if err != nil {
4639 fl .Usage ()
4740 }
4841}
4942
50- func (c * mockSubCmd ) Write (b []byte ) (int , error ) { return c .buf .Write (b ) }
43+ func (c * mockSubCmd ) WriteString (s string ) (int , error ) {
44+ return c .buf .WriteString (s )
45+ }
5146
5247func (c * mockSubCmd ) Spec () CommandSpec {
5348 return CommandSpec {
@@ -59,47 +54,38 @@ func (c *mockSubCmd) Spec() CommandSpec {
5954}
6055
6156func TestSubCmdAliases (t * testing.T ) {
62- for _ , test := range []struct {
63- name , expected string
64- }{
65- {
66- name : "s" ,
67- expected : success ,
68- },
69- {
70- name : "sc" ,
71- expected : success ,
72- },
73- {
74- name : "sub" ,
75- expected : success ,
76- },
77- } {
78- t .Run (test .name , func (t * testing.T ) {
79- // Since the alias is the name of the test
80- // we can just pass it as the alias arg.
81- os .Args = []string {"mockParentCmd" , test .name }
82- // Based on os.Args, when splitArgs is invoked,
83- // it should be able to deduce the subcommand we want
84- // based on the new alias map it's being passed.
85- RunRoot (& mockParentCmd {})
86- // The success const is never written into the buffer
87- // if the subcommand fails to be invoked by alias.
57+ for _ , alias := range []string {"s" , "sc" , "sub" } {
58+ t .Run (alias , func (t * testing.T ) {
59+ // Setup command.
60+ cmd := new (mockParentCmd )
61+ os .Args = []string {cmd .Spec ().Name , alias }
62+ // Run command.
63+ RunRoot (cmd )
64+ // If "success" isn't written into the buffer
65+ // then we failed to find the subcommand by alias.
8866 got := string (subCmd .buf .Bytes ())
89- assert .Equal (t , test . name , test . expected , got )
67+ assert .Equal (t , t . Name (), "success" , got )
9068 })
9169 }
9270}
9371
94- func TestSubcmdAliasesInParentCmdHelpOutput (t * testing.T ) {
95- buf := new (bytes.Buffer )
96- cmd := & mockParentCmd {}
97- name := cmd .Spec ().Name
98- fl := pflag .NewFlagSet (name , pflag .ExitOnError )
99- // If the help output is not written to the buffer
100- // in the format we expect then the test will fail.
101- renderHelp (name , cmd , fl , buf )
102- got := string (buf .Bytes ())
103- expected := expectedParentCmdHelpOutput
104- assert .Equal (t , "display_subcmd_aliases_in_parentcmd_help_output" , expected , got )
72+ func TestCmdHelpOutput (t * testing.T ) {
73+ t .Run (t .Name (), func (t * testing.T ) {
74+ expected := `Usage: mockParentCmd Mock parent command usage.
75+
76+ Mock parent command description.
77+
78+ Commands:
79+ s,sc,sub,mockSubCmd - A simple mock subcommand with aliases.
80+ `
81+ buf := new (bytes.Buffer )
82+ cmd := new (mockParentCmd )
83+ name := cmd .Spec ().Name
84+ fl := pflag .NewFlagSet (name , pflag .ExitOnError )
85+ // If the help output doesn't contain the subcommand and
86+ // isn't formatted the way we expect the test will fail.
87+ renderHelp (name , cmd , fl , buf )
88+ got := string (buf .Bytes ())
89+ assert .Equal (t , t .Name (), expected , got )
90+ })
10591}
0 commit comments