|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "os" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "cdr.dev/slog/sloggers/slogtest/assert" |
| 9 | + "github.com/spf13/pflag" |
| 10 | +) |
| 11 | + |
| 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)} |
| 25 | + |
| 26 | +type ( |
| 27 | + mockParentCmd struct{} |
| 28 | + mockSubCmd struct{ buf *bytes.Buffer } |
| 29 | +) |
| 30 | + |
| 31 | +func (c *mockParentCmd) Run(fl *pflag.FlagSet) {} |
| 32 | + |
| 33 | +func (c *mockParentCmd) Subcommands() []Command { return []Command{subCmd} } |
| 34 | + |
| 35 | +func (c *mockParentCmd) Spec() CommandSpec { |
| 36 | + return CommandSpec{ |
| 37 | + Name: "mockParentCmd", |
| 38 | + Usage: "Mock parent command usage.", |
| 39 | + Desc: "Mock parent command description.", |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +func (c *mockSubCmd) Run(fl *pflag.FlagSet) { |
| 44 | + c.buf = new(bytes.Buffer) |
| 45 | + if _, err := c.Write([]byte(success)); err != nil { |
| 46 | + fl.Usage() |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +func (c *mockSubCmd) Write(b []byte) (int, error) { return c.buf.Write(b) } |
| 51 | + |
| 52 | +func (c *mockSubCmd) Spec() CommandSpec { |
| 53 | + return CommandSpec{ |
| 54 | + Name: "mockSubCmd", |
| 55 | + Usage: "Test a subcommand.", |
| 56 | + Aliases: []string{"s", "sc", "sub"}, |
| 57 | + Desc: "A simple mock subcommand with aliases.", |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +func 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. |
| 88 | + got := string(subCmd.buf.Bytes()) |
| 89 | + assert.Equal(t, test.name, test.expected, got) |
| 90 | + }) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 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) |
| 105 | +} |
0 commit comments