-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathargs_test.go
212 lines (179 loc) · 7.8 KB
/
args_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
package zulu_test
import (
"errors"
"testing"
"github.com/zulucmd/zulu/v2"
"github.com/zulucmd/zulu/v2/internal/testutil"
)
func TestArgs(t *testing.T) {
tests := map[string]struct {
exerr string // Expected error key (see map[string][string])
args zulu.PositionalArgs // Args validator
wValid bool // Define `ValidArgs` in the command
rargs []string // Runtime args
}{
"No/ | ": {"", zulu.NoArgs, false, []string{}},
"No/ | Arb": {"unknown", zulu.NoArgs, false, []string{"one"}},
"No/Valid | Valid": {"unknown", zulu.NoArgs, true, []string{"one"}},
"Nil/ | Arb": {"", nil, false, []string{"a", "b"}},
"Nil/Valid | Valid": {"", nil, true, []string{"one", "two"}},
"Nil/Valid | Invalid": {"invalid", nil, true, []string{"a"}},
"Arbitrary/ | Arb": {"", zulu.ArbitraryArgs, false, []string{"a", "b"}},
"Arbitrary/Valid | Valid": {"", zulu.ArbitraryArgs, true, []string{"one", "two"}},
"Arbitrary/Valid | Invalid": {"invalid", zulu.ArbitraryArgs, true, []string{"a"}},
"MinimumN/ | Arb": {"", zulu.MinimumNArgs(2), false, []string{"a", "b", "c"}},
"MinimumN/Valid | Valid": {"", zulu.MinimumNArgs(2), true, []string{"one", "three"}},
"MinimumN/Valid | Invalid": {"invalid", zulu.MinimumNArgs(2), true, []string{"a", "b"}},
"MinimumN/ | Less": {"less", zulu.MinimumNArgs(2), false, []string{"a"}},
"MinimumN/Valid | Less": {"less", zulu.MinimumNArgs(2), true, []string{"one"}},
"MinimumN/Valid | LessInvalid": {"invalid", zulu.MinimumNArgs(2), true, []string{"a"}},
"MaximumN/ | Arb": {"", zulu.MaximumNArgs(3), false, []string{"a", "b"}},
"MaximumN/Valid | Valid": {"", zulu.MaximumNArgs(2), true, []string{"one", "three"}},
"MaximumN/Valid | Invalid": {"invalid", zulu.MaximumNArgs(2), true, []string{"a", "b"}},
"MaximumN/ | More": {"more", zulu.MaximumNArgs(2), false, []string{"a", "b", "c"}},
"MaximumN/Valid | More": {"more", zulu.MaximumNArgs(2), true, []string{"one", "three", "two"}},
"MaximumN/Valid | MoreInvalid": {"invalid", zulu.MaximumNArgs(2), true, []string{"a", "b", "c"}},
"Exact/ | Arb": {"", zulu.ExactArgs(3), false, []string{"a", "b", "c"}},
"Exact/Valid | Valid": {"", zulu.ExactArgs(3), true, []string{"three", "one", "two"}},
"Exact/Valid | Invalid": {"invalid", zulu.ExactArgs(3), true, []string{"three", "a", "two"}},
"Exact/ | InvalidCount": {"notexact", zulu.ExactArgs(2), false, []string{"a", "b", "c"}},
"Exact/Valid | InvalidCount": {"notexact", zulu.ExactArgs(2), true, []string{"three", "one", "two"}},
"Exact/Valid | InvalidCountInvalid": {"invalid", zulu.ExactArgs(2), true, []string{"three", "a", "two"}},
"Range/ | Arb": {"", zulu.RangeArgs(2, 4), false, []string{"a", "b", "c"}},
"Range/Valid | Valid": {"", zulu.RangeArgs(2, 4), true, []string{"three", "one", "two"}},
"Range/Valid | Invalid": {"invalid", zulu.RangeArgs(2, 4), true, []string{"three", "a", "two"}},
"Range/ | InvalidCount": {"notinrange", zulu.RangeArgs(2, 4), false, []string{"a"}},
"Range/Valid | InvalidCount": {"notinrange", zulu.RangeArgs(2, 4), true, []string{"two"}},
"Range/Valid | InvalidCountInvalid": {"invalid", zulu.RangeArgs(2, 4), true, []string{"a"}},
}
var errStrings = map[string]string{
"invalid": `invalid argument "a" for "c"`,
"unknown": `unknown command "one" for "c"`,
"less": "requires at least 2 arg(s), only received 1",
"more": "accepts at most 2 arg(s), received 3",
"notexact": "accepts 2 arg(s), received 3",
"notinrange": "accepts between 2 and 4 arg(s), received 1",
}
t.Parallel()
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
t.Parallel()
expected, ok := errStrings[tc.exerr]
if tc.exerr != "" && !ok {
t.Fatalf(`key "%s" is not found in map "errStrings"`, tc.exerr)
return
}
c := &zulu.Command{
Use: "c",
Args: tc.args,
RunE: noopRun,
}
if tc.wValid {
c.ValidArgs = []string{"one", "two", "three"}
}
output, err := executeCommand(c, tc.rargs...)
if len(tc.exerr) > 0 {
testutil.AssertNotNilf(t, err, "Expected error")
testutil.AssertEqual(t, expected, err.Error())
return
}
// Expect success
testutil.AssertEqualf(t, "", output, "Unexpected output")
testutil.AssertNilf(t, err, "Unexpected error")
},
)
}
}
// Takes(No)Args
func TestRootTakesNoArgs(t *testing.T) {
rootCmd := &zulu.Command{Use: "root", RunE: noopRun}
childCmd := &zulu.Command{Use: "child", RunE: noopRun}
rootCmd.AddCommand(childCmd)
_, err := executeCommand(rootCmd, "illegal", "args")
testutil.AssertNotNilf(t, err, "Expected an error")
testutil.AssertContains(t, `unknown command "illegal" for "root"`, err.Error())
}
func TestRootTakesArgs(t *testing.T) {
rootCmd := &zulu.Command{Use: "root", Args: zulu.ArbitraryArgs, RunE: noopRun}
childCmd := &zulu.Command{Use: "child", RunE: noopRun}
rootCmd.AddCommand(childCmd)
_, err := executeCommand(rootCmd, "legal", "args")
testutil.AssertNilf(t, err, "Unexpected error")
}
func TestChildTakesNoArgs(t *testing.T) {
rootCmd := &zulu.Command{Use: "root", RunE: noopRun}
childCmd := &zulu.Command{Use: "child", Args: zulu.NoArgs, RunE: noopRun}
rootCmd.AddCommand(childCmd)
_, err := executeCommand(rootCmd, "child", "illegal", "args")
testutil.AssertNotNilf(t, err, "Expected an error")
testutil.AssertContains(t, `unknown command "illegal" for "root child"`, err.Error())
}
func TestChildTakesArgs(t *testing.T) {
rootCmd := &zulu.Command{Use: "root", RunE: noopRun}
childCmd := &zulu.Command{Use: "child", Args: zulu.ArbitraryArgs, RunE: noopRun}
rootCmd.AddCommand(childCmd)
_, err := executeCommand(rootCmd, "child", "legal", "args")
testutil.AssertNilf(t, err, "Unexpected error")
}
func TestMatchAll(t *testing.T) {
// Somewhat contrived example check that ensures there are exactly 3
// arguments, and each argument is exactly 2 bytes long.
pargs := zulu.MatchAll(
zulu.ExactArgs(3),
func(cmd *zulu.Command, args []string) error {
for _, arg := range args {
if len([]byte(arg)) != 2 {
return errors.New("expected to be exactly 2 bytes long")
}
}
return nil
},
)
testCases := map[string]struct {
args []string
fail bool
}{
"happy path": {
[]string{"aa", "bb", "cc"},
false,
},
"incorrect number of args": {
[]string{"aa", "bb", "cc", "dd"},
true,
},
"incorrect number of bytes in one arg": {
[]string{"aa", "bb", "abc"},
true,
},
}
rootCmd := &zulu.Command{Use: "root", Args: pargs, RunE: noopRun}
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
_, err := executeCommand(rootCmd, tc.args...)
if !tc.fail {
testutil.AssertNilf(t, err, "Unexpected error")
} else {
testutil.AssertNotNilf(t, err, "Expected an error")
}
})
}
}
// This test make sure we keep backwards-compatibility with respect
// to the legacyArgs() function.
// It makes sure the root command accepts arguments if it does not have
// sub-commands.
func TestLegacyArgsRootAcceptsArgs(t *testing.T) {
rootCmd := &zulu.Command{Use: "root", Args: nil, RunE: noopRun}
_, err := executeCommand(rootCmd, "somearg")
testutil.AssertNilf(t, err, "Unexpected error")
}
// It makes sure a sub-command accepts arguments and further sub-commands.
func TestLegacyArgsSubcmdAcceptsArgs(t *testing.T) {
rootCmd := &zulu.Command{Use: "root", Args: nil, RunE: noopRun}
childCmd := &zulu.Command{Use: "child", Args: nil, RunE: noopRun}
grandchildCmd := &zulu.Command{Use: "grandchild", Args: nil, RunE: noopRun}
rootCmd.AddCommand(childCmd)
childCmd.AddCommand(grandchildCmd)
_, err := executeCommand(rootCmd, "child", "somearg")
testutil.AssertNilf(t, err, "Unexpected error")
}