-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathcommand_test.go
More file actions
320 lines (279 loc) · 8.55 KB
/
command_test.go
File metadata and controls
320 lines (279 loc) · 8.55 KB
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
package grumble
import (
"testing"
)
// ---------------------------------------------------------------------------
// TestCommandValidate
// ---------------------------------------------------------------------------
func TestCommandValidate(t *testing.T) {
tests := []struct {
name string
cmd Command
wantErr bool
}{
{
name: "empty name",
cmd: Command{Name: "", Help: "some help"},
wantErr: true,
},
{
name: "name starts with dash",
cmd: Command{Name: "-bad", Help: "some help"},
wantErr: true,
},
{
name: "empty help",
cmd: Command{Name: "good", Help: ""},
wantErr: true,
},
{
name: "valid name and help",
cmd: Command{Name: "good", Help: "does things"},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := tt.cmd.validate()
if tt.wantErr && err == nil {
t.Fatal("expected error, got nil")
}
if !tt.wantErr && err != nil {
t.Fatalf("expected no error, got: %v", err)
}
})
}
}
// ---------------------------------------------------------------------------
// TestCommandAddCommand
// ---------------------------------------------------------------------------
func TestCommandAddCommand(t *testing.T) {
parent := &Command{Name: "parent", Help: "parent help"}
child := &Command{Name: "child", Help: "child help"}
parent.AddCommand(child)
if child.Parent() != parent {
t.Fatal("child.Parent() should equal parent")
}
// Add a second child and verify both are present.
child2 := &Command{Name: "child2", Help: "child2 help"}
parent.AddCommand(child2)
if child2.Parent() != parent {
t.Fatal("child2.Parent() should equal parent")
}
all := parent.commands.All()
if len(all) != 2 {
t.Fatalf("expected 2 children, got %d", len(all))
}
if all[0].Name != "child" {
t.Fatalf("expected first child named 'child', got %q", all[0].Name)
}
if all[1].Name != "child2" {
t.Fatalf("expected second child named 'child2', got %q", all[1].Name)
}
}
// ---------------------------------------------------------------------------
// TestCommandAddCommandPanics
// ---------------------------------------------------------------------------
func TestCommandAddCommandPanics(t *testing.T) {
parent := &Command{Name: "parent", Help: "parent help"}
defer func() {
if r := recover(); r == nil {
t.Fatal("expected panic when adding command with empty name")
}
}()
// Empty name should fail validation and cause a panic.
parent.AddCommand(&Command{Name: "", Help: "bad"})
}
// ---------------------------------------------------------------------------
// TestCommandRegisterFlagsAndArgs
// ---------------------------------------------------------------------------
func TestCommandRegisterFlagsAndArgs(t *testing.T) {
t.Run("with help flag", func(t *testing.T) {
cmd := Command{
Name: "test",
Help: "test help",
Flags: func(f *Flags) {
f.String("v", "verbose", "no", "enable verbose")
},
Args: func(a *Args) {
a.String("filename", "the file to process")
},
}
cmd.registerFlagsAndArgs(true)
// Parse flags to verify they were registered.
flagRes := make(FlagMap)
remaining, err := cmd.flags.parse([]string{"--verbose", "yes", "-h", "--", "myfile.txt"}, flagRes)
if err != nil {
t.Fatalf("flag parse error: %v", err)
}
if flagRes["verbose"] == nil {
t.Fatal("expected 'verbose' flag to be registered")
}
if flagRes["help"] == nil {
t.Fatal("expected 'help' flag to be registered when addHelpFlag=true")
}
// Parse args to verify they were registered.
argRes := make(ArgMap)
_, err = cmd.args.parse(remaining, argRes)
if err != nil {
t.Fatalf("arg parse error: %v", err)
}
if argRes["filename"] == nil {
t.Fatal("expected 'filename' arg to be registered")
}
if argRes["filename"].Value != "myfile.txt" {
t.Fatalf("expected filename='myfile.txt', got %v", argRes["filename"].Value)
}
})
t.Run("without help flag", func(t *testing.T) {
cmd := Command{
Name: "test",
Help: "test help",
Flags: func(f *Flags) {
f.Int("p", "port", 8080, "port number")
},
}
cmd.registerFlagsAndArgs(false)
// Only the user-defined flag should exist.
flagRes := make(FlagMap)
_, err := cmd.flags.parse([]string{}, flagRes)
if err != nil {
t.Fatalf("flag parse error: %v", err)
}
if flagRes["port"] == nil {
t.Fatal("expected 'port' flag to be registered")
}
if _, ok := flagRes["help"]; ok {
t.Fatal("did not expect 'help' flag when addHelpFlag=false")
}
})
t.Run("nil flags and args callbacks", func(t *testing.T) {
cmd := Command{
Name: "bare",
Help: "bare help",
}
cmd.registerFlagsAndArgs(true)
// Should still have the help flag.
flagRes := make(FlagMap)
_, err := cmd.flags.parse([]string{}, flagRes)
if err != nil {
t.Fatalf("flag parse error: %v", err)
}
if flagRes["help"] == nil {
t.Fatal("expected 'help' flag even when Flags callback is nil")
}
})
}
// ---------------------------------------------------------------------------
// TestCommandFlagsInheritance
// ---------------------------------------------------------------------------
func TestCommandFlagsInheritance(t *testing.T) {
parent := &Command{
Name: "parent",
Help: "parent help",
Flags: func(f *Flags) {
f.Bool("d", "debug", false, "enable debug")
},
}
parent.registerFlagsAndArgs(true)
child := &Command{
Name: "child",
Help: "child help",
Flags: func(f *Flags) {
f.String("o", "output", "stdout", "output destination")
},
}
parent.AddCommand(child)
// Parse parent flags independently.
parentFlags := make(FlagMap)
_, err := parent.flags.parse([]string{"--debug"}, parentFlags)
if err != nil {
t.Fatalf("parent flag parse error: %v", err)
}
if parentFlags["debug"] == nil {
t.Fatal("expected 'debug' flag on parent")
}
// Parse child flags independently.
childFlags := make(FlagMap)
_, err = child.flags.parse([]string{"--output", "/tmp/out"}, childFlags)
if err != nil {
t.Fatalf("child flag parse error: %v", err)
}
if childFlags["output"] == nil {
t.Fatal("expected 'output' flag on child")
}
if childFlags["output"].Value != "/tmp/out" {
t.Fatalf("expected output='/tmp/out', got %v", childFlags["output"].Value)
}
// Parent should not have child's flag.
parentFlags2 := make(FlagMap)
_, err = parent.flags.parse([]string{"--output", "x"}, parentFlags2)
if err == nil {
t.Fatal("expected error when parsing child flag on parent")
}
}
// ---------------------------------------------------------------------------
// TestNewContext
// ---------------------------------------------------------------------------
func TestNewContext(t *testing.T) {
cmd := &Command{Name: "ctx-cmd", Help: "ctx help"}
flagMap := FlagMap{
"verbose": &FlagMapItem{Value: true, IsDefault: false},
}
argMap := ArgMap{
"file": &ArgMapItem{Value: "test.txt", IsDefault: false},
}
ctx := newContext(nil, cmd, flagMap, argMap)
if ctx.App != nil {
t.Fatal("expected App to be nil")
}
if ctx.Command != cmd {
t.Fatal("expected Command to match")
}
if ctx.Flags == nil {
t.Fatal("expected Flags to be non-nil")
}
if !ctx.Flags.Bool("verbose") {
t.Fatal("expected verbose flag to be true")
}
if ctx.Args == nil {
t.Fatal("expected Args to be non-nil")
}
if ctx.Args.String("file") != "test.txt" {
t.Fatalf("expected file arg 'test.txt', got %q", ctx.Args.String("file"))
}
}
// ---------------------------------------------------------------------------
// TestContextFields
// ---------------------------------------------------------------------------
func TestContextFields(t *testing.T) {
// Verify that all Context struct fields are accessible and settable.
cmd := &Command{Name: "my-cmd", Help: "my help"}
ctx := &Context{
App: nil,
Command: cmd,
Flags: make(FlagMap),
Args: make(ArgMap),
}
if ctx.Command.Name != "my-cmd" {
t.Fatalf("expected command name 'my-cmd', got %q", ctx.Command.Name)
}
// Stop() requires a real App with a closer/readline, so we just verify
// the method exists by confirming the Context type has it. Calling it
// with a nil App would panic, which is expected behaviour.
defer func() {
if r := recover(); r == nil {
t.Fatal("expected panic when calling Stop() with nil App")
}
}()
ctx.Stop()
}
// ---------------------------------------------------------------------------
// TestParentNilByDefault
// ---------------------------------------------------------------------------
func TestParentNilByDefault(t *testing.T) {
cmd := &Command{Name: "root", Help: "root help"}
if cmd.Parent() != nil {
t.Fatal("expected Parent() to be nil for a standalone command")
}
}