-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
completion_test.go
225 lines (201 loc) · 5.18 KB
/
completion_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package cli
import (
"bytes"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestCompletionDisable(t *testing.T) {
cmd := &Command{}
err := cmd.Run(buildTestContext(t), []string{"foo", completionCommandName})
assert.Error(t, err, "Expected error for no help topic for completion")
}
func TestCompletionEnable(t *testing.T) {
cmd := &Command{
EnableShellCompletion: true,
}
err := cmd.Run(buildTestContext(t), []string{"foo", completionCommandName})
assert.ErrorContains(t, err, "no shell provided")
}
func TestCompletionEnableDiffCommandName(t *testing.T) {
cmd := &Command{
EnableShellCompletion: true,
ShellCompletionCommandName: "junky",
}
err := cmd.Run(buildTestContext(t), []string{"foo", "junky"})
assert.ErrorContains(t, err, "no shell provided")
}
func TestCompletionShell(t *testing.T) {
for k := range shellCompletions {
out := &bytes.Buffer{}
t.Run(k, func(t *testing.T) {
cmd := &Command{
EnableShellCompletion: true,
Writer: out,
}
r := require.New(t)
r.NoError(cmd.Run(buildTestContext(t), []string{"foo", completionCommandName, k}))
r.Containsf(
k, out.String(),
"Expected output to contain shell name %[1]q", k,
)
})
}
}
func TestCompletionSubcommand(t *testing.T) {
tests := []struct {
name string
args []string
contains string
msg string
msgArgs []interface{}
notContains bool
}{
{
name: "subcommand general completion",
args: []string{"foo", "bar", completionFlag},
contains: "xyz",
msg: "Expected output to contain shell name %[1]q",
msgArgs: []interface{}{
"xyz",
},
},
{
name: "subcommand flag completion",
args: []string{"foo", "bar", "-", completionFlag},
contains: "l1",
msg: "Expected output to contain shell name %[1]q",
msgArgs: []interface{}{
"l1",
},
},
{
name: "subcommand flag no completion",
args: []string{"foo", "bar", "--", completionFlag},
contains: "l1",
msg: "Expected output to contain shell name %[1]q",
msgArgs: []interface{}{
"l1",
},
notContains: true,
},
{
name: "sub sub command general completion",
args: []string{"foo", "bar", "xyz", completionFlag},
contains: "-g",
msg: "Expected output to contain flag %[1]q",
msgArgs: []interface{}{
"-g",
},
notContains: true,
},
{
name: "sub sub command flag completion",
args: []string{"foo", "bar", "xyz", "-", completionFlag},
contains: "-g",
msg: "Expected output to contain flag %[1]q",
msgArgs: []interface{}{
"-g",
},
},
{
name: "sub sub command no completion",
args: []string{"foo", "bar", "xyz", "--", completionFlag},
contains: "-g",
msg: "Expected output to contain flag %[1]q",
msgArgs: []interface{}{
"-g",
},
notContains: true,
},
{
name: "sub sub command no completion extra args",
args: []string{"foo", "bar", "xyz", "--", "sargs", completionFlag},
contains: "-g",
msg: "Expected output to contain flag %[1]q",
msgArgs: []interface{}{
"-g",
},
notContains: true,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
out := &bytes.Buffer{}
cmd := &Command{
EnableShellCompletion: true,
Writer: out,
Commands: []*Command{
{
Name: "bar",
Flags: []Flag{
&StringFlag{
Name: "l1",
},
},
Commands: []*Command{
{
Name: "xyz",
Flags: []Flag{
&StringFlag{
Name: "g",
Aliases: []string{
"t",
},
},
},
},
},
},
},
}
r := require.New(t)
r.NoError(cmd.Run(buildTestContext(t), test.args))
t.Log(out.String())
if test.notContains {
r.NotContainsf(out.String(), test.contains, test.msg, test.msgArgs...)
} else {
r.Containsf(out.String(), test.contains, test.msg, test.msgArgs...)
}
})
}
}
type mockWriter struct {
err error
}
func (mw *mockWriter) Write(p []byte) (int, error) {
if mw.err != nil {
return 0, mw.err
}
return len(p), nil
}
func TestCompletionInvalidShell(t *testing.T) {
cmd := &Command{
EnableShellCompletion: true,
}
unknownShellName := "junky-sheell"
err := cmd.Run(buildTestContext(t), []string{"foo", completionCommandName, unknownShellName})
assert.ErrorContains(t, err, "unknown shell junky-sheell")
enableError := true
shellCompletions[unknownShellName] = func(c *Command, appName string) (string, error) {
if enableError {
return "", fmt.Errorf("cant do completion")
}
return "something", nil
}
defer func() {
delete(shellCompletions, unknownShellName)
}()
err = cmd.Run(buildTestContext(t), []string{"foo", completionCommandName, unknownShellName})
assert.ErrorContains(t, err, "cant do completion")
// now disable shell completion error
enableError = false
c := cmd.Command(completionCommandName)
assert.NotNil(t, c)
c.Writer = &mockWriter{
err: fmt.Errorf("writer error"),
}
err = cmd.Run(buildTestContext(t), []string{"foo", completionCommandName, unknownShellName})
assert.ErrorContains(t, err, "writer error")
}