Skip to content

Commit

Permalink
test: add cases for checking flag usage output with zsh completion
Browse files Browse the repository at this point in the history
  • Loading branch information
Toalaah committed May 2, 2024
1 parent 89f102a commit fa5316e
Showing 1 changed file with 48 additions and 2 deletions.
50 changes: 48 additions & 2 deletions help_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1080,18 +1080,18 @@ func TestDefaultCompleteWithFlags(t *testing.T) {
origArgv := os.Args
t.Cleanup(func() { os.Args = origArgv })

t.Setenv("SHELL", "bash")

for _, tc := range []struct {
name string
cmd *Command
argv []string
env map[string]string
expected string
}{
{
name: "empty",
cmd: &Command{},
argv: []string{"prog", "cmd"},
env: map[string]string{"SHELL": "bash"},
expected: "",
},
{
Expand All @@ -1113,6 +1113,7 @@ func TestDefaultCompleteWithFlags(t *testing.T) {
},
},
argv: []string{"cmd", "--e", "--generate-shell-completion"},
env: map[string]string{"SHELL": "bash"},
expected: "--excitement\n",
},
{
Expand All @@ -1135,6 +1136,7 @@ func TestDefaultCompleteWithFlags(t *testing.T) {
},
},
argv: []string{"cmd", "--generate-shell-completion"},
env: map[string]string{"SHELL": "bash"},
expected: "futz\n",
},
{
Expand All @@ -1157,15 +1159,59 @@ func TestDefaultCompleteWithFlags(t *testing.T) {
},
},
argv: []string{"cmd", "--url", "http://localhost:8000", "h", "--generate-shell-completion"},
env: map[string]string{"SHELL": "bash"},
expected: "help\n",
},
{
name: "zsh-autocomplete-with-flag-descriptions",
cmd: &Command{
Name: "putz",
Flags: []Flag{
&BoolFlag{Name: "excitement", Usage: "an exciting flag"},
&StringFlag{Name: "hat-shape"},
},
parent: &Command{
Name: "cmd",
Flags: []Flag{
&BoolFlag{Name: "happiness"},
&IntFlag{Name: "everybody-jump-on"},
},
},
},
argv: []string{"cmd", "putz", "-e", "--generate-shell-completion"},
env: map[string]string{"SHELL": "zsh"},
expected: "--excitement:an exciting flag\n",
},
{
name: "zsh-autocomplete-with-empty-flag-descriptions",
cmd: &Command{
Name: "putz",
Flags: []Flag{
&BoolFlag{Name: "excitement"},
&StringFlag{Name: "hat-shape"},
},
parent: &Command{
Name: "cmd",
Flags: []Flag{
&BoolFlag{Name: "happiness"},
&IntFlag{Name: "everybody-jump-on"},
},
},
},
argv: []string{"cmd", "putz", "-e", "--generate-shell-completion"},
env: map[string]string{"SHELL": "zsh"},
expected: "--excitement\n",
},
} {
t.Run(tc.name, func(ct *testing.T) {
writer := &bytes.Buffer{}
rootCmd := tc.cmd.Root()
rootCmd.Writer = writer

os.Args = tc.argv
for k, v := range tc.env {
t.Setenv(k, v)
}
f := DefaultCompleteWithFlags(tc.cmd)
f(context.Background(), tc.cmd)

Expand Down

0 comments on commit fa5316e

Please sign in to comment.