-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathparse_test.go
More file actions
251 lines (233 loc) · 7.93 KB
/
Copy pathparse_test.go
File metadata and controls
251 lines (233 loc) · 7.93 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
// Copyright (C) 2020 Michael J. Fromberger. All Rights Reserved.
package command_test
import (
"errors"
"flag"
"fmt"
"io"
"strings"
"testing"
"github.com/creachadair/command"
"github.com/google/go-cmp/cmp"
)
var flags struct {
A string
B string
C string
D string
E string
F string
}
func setFlag(name string, s *string) func(*command.Env, *flag.FlagSet) {
return func(_ *command.Env, fs *flag.FlagSet) {
fs.StringVar(s, name, "", name)
}
}
func newTestRoot(run func(*command.Env) error) *command.C {
return &command.C{
Name: "root",
SetFlags: setFlag("A", &flags.A),
Run: run,
Commands: []*command.C{{
Name: "one",
SetFlags: setFlag("B", &flags.B),
Run: run,
Commands: []*command.C{{
Name: "two",
SetFlags: setFlag("C", &flags.C),
Run: run,
Commands: []*command.C{{
Name: "four",
SetFlags: setFlag("E", &flags.E),
Run: run,
}},
}},
}, {
Name: "three",
SetFlags: setFlag("D", &flags.D),
Run: run,
}, {
Name: "five",
SetFlags: setFlag("A", &flags.F),
Run: func(env *command.Env) error {
if err := run(env); err != nil {
return err
} else if flags.F == "" {
return errors.New("flag -F is empty")
}
return nil
},
}},
}
}
func TestEnv_IsFlagSet(t *testing.T) {
checkFlagSet := func(t *testing.T, name string, want bool) func(*command.Env) error {
return func(env *command.Env) error {
if ok := env.IsFlagSet(name); ok != want {
t.Errorf("IsFlagSet(%q): got %v, want %v (args=%+q)", name, ok, want, env.Args)
}
return nil
}
}
var stringFlag string
var boolFlag bool
tests := []struct {
flagName string
args string
wantSet bool
}{
{"string", "", false},
{"bool", "", false},
{"string", "a b c", false},
{"bool", "a b c", false},
{"string", "--string new", true},
{"string", "--string new a b c", true},
{"string", "a b -string new c", true}, // merged is default
{"bool", "--bool", true},
{"bool", "-bool a b c", true},
{"bool", "a -bool b c", true}, // merged is default
{"string", "-string x -bool a b c", true},
{"string", "-bool -string x a", true},
{"bool", "-string x -bool b", true},
}
for _, tc := range tests {
t.Run(fmt.Sprintf("%s_%v", tc.flagName, tc.wantSet), func(t *testing.T) {
root := &command.C{
Name: "root",
SetFlags: func(_ *command.Env, fs *flag.FlagSet) {
fs.StringVar(&stringFlag, "string", "old", "String value")
fs.BoolVar(&boolFlag, "bool", false, "Boolean value")
},
Run: checkFlagSet(t, tc.flagName, tc.wantSet),
}
args := strings.Fields(tc.args)
if err := command.Run(root.NewEnv(nil), args); err != nil {
t.Errorf("Command failed; %v", err)
}
})
}
}
func TestParse(t *testing.T) {
checkRun := func(wantArgs string) func(env *command.Env) error {
return func(env *command.Env) error {
if diff := cmp.Diff(env.Args, strings.Fields(wantArgs)); diff != "" {
return fmt.Errorf("wrong args (-got, +want):\n%s", diff)
}
return nil
}
}
const noError = ""
const noSuchFlag = "flag provided but not defined"
const missingArg = "flag needs an argument"
const wrongArgs = "wrong args"
tests := []struct {
name string
doMerge bool
args string
wantArgs string
wantErr string
}{
{"rootNoFlags", false, "x y", "x y", noError},
{"rootBadFlag", false, "--nonesuch x y", "", noSuchFlag},
{"rootBadFlagMerged", true, "-nonesuch x", "", noSuchFlag},
{"rootBadFlagStop", false, "-- --nonesuch x y", "--nonesuch x y", noError},
{"rootA", false, "--A=1 x y", "x y", noError},
{"rootADash", false, "-A - x y", "x y", noError},
{"rootStop", false, "-- -A v x y", "-A v x y", noError},
{"rootAMergedFront", true, "--A 1 x y", "x y", noError},
{"rootAMergedMid", true, "x -A 1 y", "x y", noError},
{"rootAMergedBack", true, "x y -A=1", "x y", noError},
{"rootStopAtEnd", true, "-A 1 --", "", noError},
{"oneStopAtEnd", true, "one -A 1 -B 2 --", "", noError},
{"oneNoFlags", false, "one x y", "x y", noError},
{"oneB", false, "one -B w x y", "x y", noError},
{"oneBLast", false, "one x y -B w", "x y -B w", noError},
{"oneBLastMerged", true, "one x y -B w", "x y", noError},
{"oneMissing", false, "one -B", "x y", missingArg},
{"oneABCDash", true, "-A - one -B - two x -C - y", "x y", noError},
{"twoNoFlags", false, "one two x y", "x y", noError},
{"twoMixed", false, "one two -C 2 -B 1 x y", "x y", noSuchFlag},
{"twoMixedMerged", true, "one two -C 2 -B -1 x y", "x y", noError},
{"twoAllFlags", false, "-A 1 one -B 2 two -C 3 x y", "x y", noError},
{"twoMergedFlags", true, "one two -C 3 x --A=1 -B 2 y", "x y", noError},
{"twoMergedFlagsNoStop", true, "one two -C 3 x --A=1 -B 2 y", "x y", noError},
{"twoMergedFlagsStop0", true, "-- one two -C 3 x --A=1 -B 2 y", "x --A=1 y", noError},
{"twoMergedFlagsStopA", true, "one -- two -C 3 x --A=1 -B 2 y", "x -B 2 y", noError},
{"twoMergedFlagsStopB", true, "one two -- -C 3 x --A=1 -B 2 y", "-C 3 x y", noError},
{"twoMergedFlagsStopC", true, "one two -C 3 -- x --A=1 -B 2 y", "x y", noError},
{"threeCDMerged", true, "three -D 4 -C 3", "x y", noSuchFlag},
{"threeBadFlagMerged", true, "three -D=1 -other x y", "x y", noSuchFlag},
{"threeOtherArgMerged", true, "three -D=1 -- -other x y", "x y", wrongArgs},
{"fourAllMixedMerged", true, "one -A=4 two -B=3 -C=2 four x --E 1 y", "x y", noError},
{"fourAllFrontMerged", true, "one two four x y -E=1 -C 2 --B=3 --A 4", "x y", noError},
{"fourBadFlagMerged", true, "-A 1 one two -Q ? four x y", "x y", noSuchFlag},
// Verify that if a subcommand defines the same flag, we can prevent
// it from being "poached" by the enclosing command by using "--".
{"fiveFlagPoachedEarly", true, "-A ok five x y -A alt", "x y", "flag -F is empty"},
{"fiveFlagPoachedLate", true, "five x y -A alt", "x y", "flag -F is empty"},
{"fiveFlagUnpoachedEarly", true, "-A ok -- five x y -A alt", "x y", noError},
{"fiveFlagUnpoachedLate", true, "-- five x y -A alt", "x y", noError},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
env := newTestRoot(checkRun(tc.wantArgs)).NewEnv(nil).MergeFlags(tc.doMerge)
args := strings.Fields(tc.args)
err := command.Run(env, args)
if tc.wantErr != "" {
if err == nil {
t.Error("Run did not report an error as it should")
} else if !strings.Contains(err.Error(), tc.wantErr) {
t.Errorf("Got error %v, want %q", err, tc.wantErr)
} else {
t.Logf("Got expected error: %v", err)
}
} else if err != nil {
t.Errorf("Run: unexpected error: %v", err)
}
})
}
}
func TestHelpFlag(t *testing.T) {
// A --help flag should be recognized even if it is not defined by the flag
// set, as long as it occurs before the non-flag arguments.
root := &command.C{
Name: "cmd",
Commands: []*command.C{{
Name: "sub",
SetFlags: func(_ *command.Env, fs *flag.FlagSet) {
fs.Bool("foo", false, "A flag for testing")
},
Run: func(env *command.Env) error { return nil },
}},
}
tests := []struct {
args string
wantErr string
}{
{"sub", ""},
{"sub - --help", ""}, // free args: - --help
{"sub -- --help", ""}, // free args: --help
{"sub --foo -help", "help requested"},
{"sub --help", "help requested"},
{"sub -foo --help x y -bar", "help requested"},
{"sub -foo --help", "help requested"},
{"sub -foo -bar", "not defined"},
{"sub -foo -help -bar", "help requested"},
{"sub -help", "help requested"},
{"sub a b -help", "help requested"},
{"sub -foo -- -bar", ""}, // free args: -bar
}
for _, tc := range tests {
for _, ok := range []bool{false, true} {
env := root.NewEnv(nil).MergeFlags(ok)
env.Log = io.Discard
args := strings.Fields(tc.args)
err := command.Run(env, args)
if tc.wantErr == "" && err != nil {
t.Errorf("Run merge=%v %q: unexpected error: %v", ok, tc.args, err)
} else if err != nil && !strings.Contains(err.Error(), tc.wantErr) {
t.Errorf("Run merge=%v %q: got error %v, want %q", ok, tc.args, err, tc.wantErr)
}
}
}
}