-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli_test.go
256 lines (238 loc) · 6.5 KB
/
cli_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
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
package cli
import (
"bytes"
"errors"
"io"
"reflect"
"strings"
"testing"
)
type testCLI struct {
gs1 string
gs2 string
gb1 bool
args []string
}
var errCommandFailure = errors.New("cli: command failure")
func testCommand(args []string) error {
return nil
}
func testCommandFailure(args []string) error {
return errCommandFailure
}
func testCommandErrUsage(args []string) error {
return ErrUsage
}
func TestParse(t *testing.T) {
tests := map[string]*testCLI{
"": &testCLI{},
"-gs1 string": &testCLI{gs1: "string"},
"-gs1=string": &testCLI{gs1: "string"},
"-gb1": &testCLI{gb1: true},
"-gs1 string -gb1": &testCLI{gs1: "string", gb1: true},
"-gb1 -gs1 string": &testCLI{gs1: "string", gb1: true},
"--gb1 --gs1 string": &testCLI{gs1: "string", gb1: true},
}
for line, want := range tests {
c := &testCLI{}
args := strings.Split(line, " ")
flags := []*Flag{
NewFlag("gs1", &c.gs1),
NewFlag("gs2", &c.gs2),
NewFlag("gb1", &c.gb1, Bool()),
}
have, err := Parse(args, flags)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !reflect.DeepEqual(c, want) {
t.Fatalf("flags for '%s'\nhave %v\nwant %v", line, c, want)
}
if !reflect.DeepEqual(have, c.args) {
if c.args != nil {
t.Fatalf("args for '%s'\nhave %v\nwant %v", line, have, c.args)
}
}
}
}
func TestParseArgs(t *testing.T) {
tests := map[string][]string{
"-gs1 string": []string{},
"-gs1=string": []string{},
"-gb1": []string{},
"-gs1 string -gb1": []string{},
"-gb1 -gs1 string": []string{},
"--gb1 --gs1 string": []string{},
"-": []string{"-"},
"--": []string{"--"},
"-- arg": []string{"--", "arg"},
"arg": []string{"arg"},
"-gs1 string -": []string{"-"},
"-gs1 string --": []string{"--"},
"-gs1 string -- arg": []string{"--", "arg"},
"-gs1 string arg": []string{"arg"},
}
for line, want := range tests {
c := &testCLI{}
args := strings.Split(line, " ")
flags := []*Flag{
NewFlag("gs1", &c.gs1),
NewFlag("gs2", &c.gs2),
NewFlag("gb1", &c.gb1, Bool()),
}
have, err := Parse(args, flags)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !reflect.DeepEqual(have, want) {
t.Fatalf("args for '%s'\nhave %v\nwant %v", line, have, want)
}
}
}
func TestParseUndefined(t *testing.T) {
tests := map[string]struct{}{
"-undefined": struct{}{},
"-undefined string": struct{}{},
"-undefined=string": struct{}{},
"-gs1 string -undefined": struct{}{},
"-gs1 string -undefined string": struct{}{},
"-gs1 string -undefined=string": struct{}{},
"-undefined -gs1 string": struct{}{},
"-undefined string -gs1 string": struct{}{},
"-undefined=string -gs1 string": struct{}{},
}
for line := range tests {
c := &testCLI{}
args := strings.Split(line, " ")
flags := []*Flag{
NewFlag("gs1", &c.gs1),
NewFlag("gs2", &c.gs2),
NewFlag("gb1", &c.gb1, Bool()),
}
want := ErrUndefinedFlag("undefined")
_, err := Parse(args, flags)
if !reflect.DeepEqual(err, want) {
t.Fatalf("should return undefined flag error for '%s'", line)
}
}
}
func TestParseRequiresArg(t *testing.T) {
tests := map[string]struct{}{
"-gs1": struct{}{},
"-gs1 -gb1": struct{}{},
"-gb1 -gs1": struct{}{},
"--gs1": struct{}{},
"--gs1 --gb1": struct{}{},
"--gb1 --gs1": struct{}{},
}
want := ErrRequiresArg("gs1")
for line := range tests {
c := &testCLI{}
args := strings.Split(line, " ")
flags := []*Flag{
NewFlag("gs1", &c.gs1),
NewFlag("gs2", &c.gs2),
NewFlag("gb1", &c.gb1, Bool()),
}
_, err := Parse(args, flags)
if !reflect.DeepEqual(err, want) {
t.Fatalf("error for '%s'\nhave %v\nwant %v", line, err, want)
}
}
}
func TestParseEnv(t *testing.T) {
const env = "TEST_PARSE_ENV"
const want = "env"
t.Setenv(env, want)
c := &testCLI{}
args := []string{}
flags := []*Flag{NewFlag("gs1", &c.gs1, EnvironmentKey(env))}
_, err := Parse(args, flags)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if c.gs1 != want {
t.Fatalf("gs1\nhave '%s'\nwant '%s'", c.gs1, want)
}
}
func TestAddNilHandler(t *testing.T) {
defer func() {
perr := recover()
if perr == nil {
t.Fatalf("nil handler should panic")
}
}()
app := New("appname", newTestUsage(t), nil)
app.Add("foo", nil, nil)
}
func TestAddDuplicateCommand(t *testing.T) {
defer func() {
perr := recover()
if perr == nil {
t.Fatalf("duplicate command should panic")
}
}()
app := New("appname", newTestUsage(t), nil)
app.Add("test", testCommand, nil)
app.Add("test", testCommand, nil)
}
func TestAddDuplicateCommandAlias(t *testing.T) {
defer func() {
perr := recover()
if perr == nil {
t.Fatalf("duplicate command alias should panic")
}
}()
app := New("appname", newTestUsage(t), nil)
app.Add("test", testCommand, nil)
app.Add("aliased", testCommand, nil, Alias("test"))
}
func TestRunDefaultCommand(t *testing.T) {
var buf bytes.Buffer
app := New("appname", newTestUsage(t), nil, Stderr(&buf))
err := app.Run([]string{"appname"})
if err != ErrExitFailure {
t.Fatalf("default command should error")
}
have := buf.String()
want := "README.md\n"
if have != want {
t.Fatalf("should return root usage docs\nhave '%s'\nwant '%s'", have, want)
}
}
func TestRunHelpError(t *testing.T) {
app := New("appname", newTestUsage(t), nil, Stderr(io.Discard))
app.Add("test", func([]string) error { return nil }, nil)
err := app.Run([]string{"appname", "help", "test", "fail"})
if err != ErrExitFailure {
t.Fatalf("help command should error")
}
}
func TestRunCommandError(t *testing.T) {
app := New("appname", newTestUsage(t), nil, Stderr(io.Discard))
app.Add("test", testCommandFailure, nil)
err := app.Run([]string{"appname", "test"})
if err != errCommandFailure {
t.Fatalf("Run error\nhave %v\nwant %v", err, errCommandFailure)
}
}
func TestRunCommandErrUsage(t *testing.T) {
var buf bytes.Buffer
c := &testCLI{}
flags := []*Flag{
NewFlag("gs1", &c.gs1),
NewFlag("gs2", &c.gs2),
NewFlag("gb1", &c.gb1, Bool()),
}
app := New("appname", newTestUsage(t), flags, Stderr(&buf))
app.Add("test", testCommandErrUsage, nil)
err := app.Run([]string{"appname", "-gb1", "-gs1", "string", "test"})
if err != ErrExitFailure {
t.Fatalf("Run error\nhave %v\nwant %v", err, ErrExitFailure)
}
have := buf.String()
want := "test.md\n"
if have != want {
t.Fatalf("should return test command usage docs\nhave '%s'\nwant '%s'", have, want)
}
}