Skip to content

Commit 2372736

Browse files
committed
Add more tests
1 parent 89e654c commit 2372736

File tree

6 files changed

+118
-55
lines changed

6 files changed

+118
-55
lines changed

command_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ func TestCommand_Run_CustomShellCompleteAcceptsMalformedFlags(t *testing.T) {
454454

455455
osArgs := &stringSliceArgs{v: []string{"bar"}}
456456
osArgs.v = append(osArgs.v, c.testArgs.Slice()...)
457-
osArgs.v = append(osArgs.v, "--generate-shell-completion")
457+
osArgs.v = append(osArgs.v, completionFlag)
458458

459459
r := require.New(t)
460460

@@ -1446,7 +1446,7 @@ func TestCommand_BeforeAfterFuncShellCompletion(t *testing.T) {
14461446
[]string{
14471447
"command",
14481448
"--opt", "succeed",
1449-
"sub", "--generate-shell-completion",
1449+
"sub", completionFlag,
14501450
},
14511451
),
14521452
)
@@ -1810,7 +1810,7 @@ func TestCommand_OrderOfOperations(t *testing.T) {
18101810
cmd, counts := buildCmdCounts()
18111811
r := require.New(t)
18121812

1813-
_ = cmd.Run(buildTestContext(t), []string{"command", "--" + "generate-shell-completion"})
1813+
_ = cmd.Run(buildTestContext(t), []string{"command", completionFlag})
18141814
r.Equal(1, counts.ShellComplete)
18151815
r.Equal(1, counts.Total)
18161816
})
@@ -2410,7 +2410,7 @@ func TestShellCompletionForIncompleteFlags(t *testing.T) {
24102410
Writer: io.Discard,
24112411
}
24122412

2413-
err := cmd.Run(buildTestContext(t), []string{"", "--test-completion", "--" + "generate-shell-completion"})
2413+
err := cmd.Run(buildTestContext(t), []string{"", "--test-completion", completionFlag})
24142414
assert.NoError(t, err, "app should not return an error")
24152415
}
24162416

completion.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99

1010
const (
1111
completionCommandName = "generate-completion"
12+
completionFlagName = "generate-shell-completion"
13+
completionFlag = "--" + completionFlagName
1214
)
1315

1416
var (

completion_test.go

Lines changed: 97 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -57,54 +57,115 @@ func TestCompletionShell(t *testing.T) {
5757
}
5858

5959
func TestCompletionSubcommand(t *testing.T) {
60-
out := &bytes.Buffer{}
60+
tests := []struct {
61+
name string
62+
args []string
63+
contains string
64+
msg string
65+
msgArgs []interface{}
66+
notContains bool
67+
}{
68+
{
69+
name: "subcommand general completion",
70+
args: []string{"foo", "bar", completionFlag},
71+
contains: "xyz",
72+
msg: "Expected output to contain shell name %[1]q",
73+
msgArgs: []interface{}{
74+
"xyz",
75+
},
76+
},
77+
{
78+
name: "subcommand flag completion",
79+
args: []string{"foo", "bar", "-", completionFlag},
80+
contains: "l1",
81+
msg: "Expected output to contain shell name %[1]q",
82+
msgArgs: []interface{}{
83+
"l1",
84+
},
85+
},
86+
{
87+
name: "subcommand flag no completion",
88+
args: []string{"foo", "bar", "--", completionFlag},
89+
contains: "l1",
90+
msg: "Expected output to contain shell name %[1]q",
91+
msgArgs: []interface{}{
92+
"l1",
93+
},
94+
notContains: true,
95+
},
96+
{
97+
name: "sub sub command general completion",
98+
args: []string{"foo", "bar", "xyz", completionFlag},
99+
contains: "-g",
100+
msg: "Expected output to contain flag %[1]q",
101+
msgArgs: []interface{}{
102+
"-g",
103+
},
104+
notContains: true,
105+
},
106+
{
107+
name: "sub sub command flag completion",
108+
args: []string{"foo", "bar", "xyz", "-", completionFlag},
109+
contains: "-g",
110+
msg: "Expected output to contain flag %[1]q",
111+
msgArgs: []interface{}{
112+
"-g",
113+
},
114+
},
115+
{
116+
name: "sub sub command no completion",
117+
args: []string{"foo", "bar", "xyz", "--", completionFlag},
118+
contains: "-g",
119+
msg: "Expected output to contain flag %[1]q",
120+
msgArgs: []interface{}{
121+
"-g",
122+
},
123+
notContains: true,
124+
},
125+
}
61126

62-
cmd := &Command{
63-
EnableShellCompletion: true,
64-
Writer: out,
65-
Commands: []*Command{
66-
{
67-
Name: "bar",
127+
for _, test := range tests {
128+
t.Run(test.name, func(t *testing.T) {
129+
out := &bytes.Buffer{}
130+
131+
cmd := &Command{
132+
EnableShellCompletion: true,
133+
Writer: out,
68134
Commands: []*Command{
69135
{
70-
Name: "xyz",
136+
Name: "bar",
71137
Flags: []Flag{
72138
&StringFlag{
73-
Name: "g",
74-
Aliases: []string{
75-
"t",
139+
Name: "l1",
140+
},
141+
},
142+
Commands: []*Command{
143+
{
144+
Name: "xyz",
145+
Flags: []Flag{
146+
&StringFlag{
147+
Name: "g",
148+
Aliases: []string{
149+
"t",
150+
},
151+
},
76152
},
77153
},
78154
},
79155
},
80156
},
81-
},
82-
},
83-
}
84-
85-
r := require.New(t)
86-
87-
r.NoError(cmd.Run(buildTestContext(t), []string{"foo", "bar", "--generate-shell-completion"}))
88-
r.Containsf(
89-
out.String(), "xyz",
90-
"Expected output to contain shell name %[1]q", "xyz",
91-
)
92-
93-
out.Reset()
94-
95-
r.NoError(cmd.Run(buildTestContext(t), []string{"foo", "bar", "xyz", "-", "--generate-shell-completion"}))
96-
r.Containsf(
97-
out.String(), "-g",
98-
"Expected output to contain flag %[1]q", "-g",
99-
)
157+
}
100158

101-
out.Reset()
159+
r := require.New(t)
102160

103-
r.NoError(cmd.Run(buildTestContext(t), []string{"foo", "bar", "xyz", "--", "--generate-shell-completion"}))
104-
r.NotContainsf(
105-
out.String(), "-g",
106-
"Expected output to not contain flag %[1]q", "-g",
107-
)
161+
r.NoError(cmd.Run(buildTestContext(t), test.args))
162+
if test.notContains {
163+
r.NotContainsf(out.String(), test.contains, test.msg, test.msgArgs...)
164+
} else {
165+
r.Containsf(out.String(), test.contains, test.msg, test.msgArgs...)
166+
}
167+
})
168+
}
108169
}
109170

110171
type mockWriter struct {

flag.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ var (
2828

2929
// GenerateShellCompletionFlag enables shell completion
3030
var GenerateShellCompletionFlag Flag = &BoolFlag{
31-
Name: "generate-shell-completion",
31+
Name: completionFlagName,
3232
Hidden: true,
3333
}
3434

help.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ func checkShellCompleteFlag(c *Command, arguments []string) (bool, []string) {
461461
pos := len(arguments) - 1
462462
lastArg := arguments[pos]
463463

464-
if lastArg != "--generate-shell-completion" {
464+
if lastArg != completionFlag {
465465
return false, arguments
466466
}
467467

help_test.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1188,7 +1188,7 @@ func TestDefaultCompleteWithFlags(t *testing.T) {
11881188
},
11891189
},
11901190
},
1191-
argv: []string{"cmd", "--e", "--generate-shell-completion"},
1191+
argv: []string{"cmd", "--e", completionFlag},
11921192
env: map[string]string{"SHELL": "bash"},
11931193
expected: "--excitement\n",
11941194
},
@@ -1210,7 +1210,7 @@ func TestDefaultCompleteWithFlags(t *testing.T) {
12101210
},
12111211
},
12121212
},
1213-
argv: []string{"cmd", "--e", "--generate-shell-completion"},
1213+
argv: []string{"cmd", "--e", completionFlag},
12141214
env: map[string]string{"SHELL": "bash"},
12151215
expected: "",
12161216
},
@@ -1232,7 +1232,7 @@ func TestDefaultCompleteWithFlags(t *testing.T) {
12321232
},
12331233
},
12341234
},
1235-
argv: []string{"cmd", "--e", "--", "--generate-shell-completion"},
1235+
argv: []string{"cmd", "--e", "--", completionFlag},
12361236
env: map[string]string{"SHELL": "bash"},
12371237
expected: "",
12381238
},
@@ -1255,7 +1255,7 @@ func TestDefaultCompleteWithFlags(t *testing.T) {
12551255
},
12561256
},
12571257
},
1258-
argv: []string{"cmd", "--generate-shell-completion"},
1258+
argv: []string{"cmd", completionFlag},
12591259
env: map[string]string{"SHELL": "bash"},
12601260
expected: "futz\n",
12611261
},
@@ -1278,7 +1278,7 @@ func TestDefaultCompleteWithFlags(t *testing.T) {
12781278
},
12791279
},
12801280
},
1281-
argv: []string{"cmd", "--url", "http://localhost:8000", "h", "--generate-shell-completion"},
1281+
argv: []string{"cmd", "--url", "http://localhost:8000", "h", completionFlag},
12821282
env: map[string]string{"SHELL": "bash"},
12831283
expected: "help\n",
12841284
},
@@ -1298,7 +1298,7 @@ func TestDefaultCompleteWithFlags(t *testing.T) {
12981298
},
12991299
},
13001300
},
1301-
argv: []string{"cmd", "putz", "-e", "--generate-shell-completion"},
1301+
argv: []string{"cmd", "putz", "-e", completionFlag},
13021302
env: map[string]string{"SHELL": "zsh"},
13031303
expected: "--excitement:an exciting flag\n",
13041304
},
@@ -1318,7 +1318,7 @@ func TestDefaultCompleteWithFlags(t *testing.T) {
13181318
},
13191319
},
13201320
},
1321-
argv: []string{"cmd", "putz", "-e", "--generate-shell-completion"},
1321+
argv: []string{"cmd", "putz", "-e", completionFlag},
13221322
env: map[string]string{"SHELL": "zsh"},
13231323
expected: "--excitement\n",
13241324
},
@@ -1764,19 +1764,19 @@ func Test_checkShellCompleteFlag(t *testing.T) {
17641764
}{
17651765
{
17661766
name: "disable-shell-completion",
1767-
arguments: []string{"--generate-shell-completion"},
1767+
arguments: []string{completionFlag},
17681768
cmd: &Command{},
17691769
wantShellCompletion: false,
1770-
wantArgs: []string{"--generate-shell-completion"},
1770+
wantArgs: []string{completionFlag},
17711771
},
17721772
{
17731773
name: "child-disable-shell-completion",
1774-
arguments: []string{"--generate-shell-completion"},
1774+
arguments: []string{completionFlag},
17751775
cmd: &Command{
17761776
parent: &Command{},
17771777
},
17781778
wantShellCompletion: false,
1779-
wantArgs: []string{"--generate-shell-completion"},
1779+
wantArgs: []string{completionFlag},
17801780
},
17811781
{
17821782
name: "last argument isn't --generate-shell-completion",
@@ -1789,7 +1789,7 @@ func Test_checkShellCompleteFlag(t *testing.T) {
17891789
},
17901790
{
17911791
name: "arguments include double dash",
1792-
arguments: []string{"--", "foo", "--generate-shell-completion"},
1792+
arguments: []string{"--", "foo", completionFlag},
17931793
cmd: &Command{
17941794
EnableShellCompletion: true,
17951795
},
@@ -1798,7 +1798,7 @@ func Test_checkShellCompleteFlag(t *testing.T) {
17981798
},
17991799
{
18001800
name: "shell completion",
1801-
arguments: []string{"foo", "--generate-shell-completion"},
1801+
arguments: []string{"foo", completionFlag},
18021802
cmd: &Command{
18031803
EnableShellCompletion: true,
18041804
},

0 commit comments

Comments
 (0)