]>
Commit | Line | Data |
---|---|---|
beb47437 JS |
1 | #!/bin/sh |
2 | # | |
3 | # Copyright (c) 2007 Johannes Schindelin | |
4 | # | |
5 | ||
6 | test_description='our own option parser' | |
7 | ||
c0b80e05 | 8 | TEST_PASSES_SANITIZE_LEAK=true |
beb47437 JS |
9 | . ./test-lib.sh |
10 | ||
8425b7ea | 11 | cat >expect <<\EOF |
2f17c78c | 12 | usage: test-tool parse-options <options> |
beb47437 | 13 | |
c97ee171 BC |
14 | A helper function for the parse-options API. |
15 | ||
e8e5d294 | 16 | --[no-]yes get a boolean |
2a409a1d RS |
17 | -D, --no-doubt begins with 'no-' |
18 | --doubt opposite of --no-doubt | |
b9e63ddd | 19 | -B, --no-fear be brave |
e8e5d294 RS |
20 | -b, --[no-]boolean increment by one |
21 | -4, --[no-]or4 bitwise-or boolean with ...0100 | |
22 | --[no-]neg-or4 same as --no-or4 | |
010a2dac | 23 | |
e8e5d294 RS |
24 | -i, --[no-]integer <n> |
25 | get a integer | |
beb47437 | 26 | -j <n> get a integer, too |
2a514ed8 | 27 | -m, --magnitude <n> get a magnitude |
e8e5d294 | 28 | --[no-]set23 set integer to 23 |
62e7a6f7 PB |
29 | --mode1 set integer to 1 (cmdmode option) |
30 | --mode2 set integer to 2 (cmdmode option) | |
0025dde7 | 31 | --[no-]mode34 (3|4) set integer to 3 or 4 (cmdmode option) |
e8e5d294 RS |
32 | -L, --[no-]length <str> |
33 | get length of <str> | |
34 | -F, --[no-]file <file> | |
35 | set file to <file> | |
beb47437 | 36 | |
010a2dac | 37 | String options |
e8e5d294 RS |
38 | -s, --[no-]string <string> |
39 | get a string | |
40 | --[no-]string2 <str> get another string | |
41 | --[no-]st <st> get another string (pervert ordering) | |
3a9f0f41 | 42 | -o <str> get another string |
448abbba JH |
43 | --longhelp help text of this entry |
44 | spans multiple lines | |
e8e5d294 | 45 | --[no-]list <str> add str to list |
beb47437 | 46 | |
010a2dac | 47 | Magic arguments |
e0319ff5 | 48 | -NUM set integer to NUM |
51a9949e | 49 | + same as -b |
6bbfd1fa AS |
50 | --ambiguous positive ambiguity |
51 | --no-ambiguous negative ambiguity | |
580d5bff | 52 | |
010a2dac | 53 | Standard options |
e8e5d294 RS |
54 | --[no-]abbrev[=<n>] use <n> digits to display object names |
55 | -v, --[no-]verbose be verbose | |
56 | -n, --[no-]dry-run dry run | |
57 | -q, --[no-]quiet be quiet | |
58 | --[no-]expect <string> | |
59 | expected output in the variable dump | |
010a2dac | 60 | |
5c387428 | 61 | Alias |
e8e5d294 | 62 | -A, --[no-]alias-source <string> |
5c387428 | 63 | get a string |
e8e5d294 | 64 | -Z, --[no-]alias-target <string> |
7c280589 | 65 | alias of --alias-source |
5c387428 | 66 | |
beb47437 JS |
67 | EOF |
68 | ||
69 | test_expect_success 'test help' ' | |
2f17c78c | 70 | test_must_fail test-tool parse-options -h >output 2>output.err && |
ca8d148d | 71 | test_must_be_empty output.err && |
1108cea7 | 72 | test_cmp expect output |
beb47437 JS |
73 | ' |
74 | ||
9c7304e3 GS |
75 | mv expect expect.err |
76 | ||
8ca65aeb | 77 | check () { |
b9e63ddd RS |
78 | what="$1" && |
79 | shift && | |
80 | expect="$1" && | |
81 | shift && | |
2f17c78c | 82 | test-tool parse-options --expect="$what $expect" "$@" |
b9e63ddd RS |
83 | } |
84 | ||
9a001381 JX |
85 | check_unknown_i18n() { |
86 | case "$1" in | |
87 | --*) | |
88 | echo error: unknown option \`${1#--}\' >expect ;; | |
89 | -*) | |
90 | echo error: unknown switch \`${1#-}\' >expect ;; | |
91 | esac && | |
92 | cat expect.err >>expect && | |
2f17c78c | 93 | test_must_fail test-tool parse-options $* >output 2>output.err && |
ca8d148d | 94 | test_must_be_empty output && |
1108cea7 | 95 | test_cmp expect output.err |
9a001381 JX |
96 | } |
97 | ||
b9e63ddd RS |
98 | test_expect_success 'OPT_BOOL() #1' 'check boolean: 1 --yes' |
99 | test_expect_success 'OPT_BOOL() #2' 'check boolean: 1 --no-doubt' | |
100 | test_expect_success 'OPT_BOOL() #3' 'check boolean: 1 -D' | |
101 | test_expect_success 'OPT_BOOL() #4' 'check boolean: 1 --no-fear' | |
102 | test_expect_success 'OPT_BOOL() #5' 'check boolean: 1 -B' | |
103 | ||
104 | test_expect_success 'OPT_BOOL() is idempotent #1' 'check boolean: 1 --yes --yes' | |
105 | test_expect_success 'OPT_BOOL() is idempotent #2' 'check boolean: 1 -DB' | |
106 | ||
107 | test_expect_success 'OPT_BOOL() negation #1' 'check boolean: 0 -D --no-yes' | |
108 | test_expect_success 'OPT_BOOL() negation #2' 'check boolean: 0 -D --no-no-doubt' | |
109 | ||
1a9bf1e1 BC |
110 | test_expect_success 'OPT_BOOL() no negation #1' 'check_unknown_i18n --fear' |
111 | test_expect_success 'OPT_BOOL() no negation #2' 'check_unknown_i18n --no-no-fear' | |
b9e63ddd | 112 | |
0f1930c5 | 113 | test_expect_success 'OPT_BOOL() positivation' 'check boolean: 0 -D --doubt' |
b9e63ddd | 114 | |
81a48cc0 CB |
115 | test_expect_success 'OPT_INT() negative' 'check integer: -2345 -i -2345' |
116 | ||
2a514ed8 CB |
117 | test_expect_success 'OPT_MAGNITUDE() simple' ' |
118 | check magnitude: 2345678 -m 2345678 | |
119 | ' | |
120 | ||
121 | test_expect_success 'OPT_MAGNITUDE() kilo' ' | |
122 | check magnitude: 239616 -m 234k | |
123 | ' | |
124 | ||
125 | test_expect_success 'OPT_MAGNITUDE() mega' ' | |
126 | check magnitude: 104857600 -m 100m | |
127 | ' | |
128 | ||
129 | test_expect_success 'OPT_MAGNITUDE() giga' ' | |
130 | check magnitude: 1073741824 -m 1g | |
131 | ' | |
132 | ||
133 | test_expect_success 'OPT_MAGNITUDE() 3giga' ' | |
134 | check magnitude: 3221225472 -m 3g | |
135 | ' | |
136 | ||
8425b7ea | 137 | cat >expect <<\EOF |
beb47437 JS |
138 | boolean: 2 |
139 | integer: 1729 | |
2a514ed8 | 140 | magnitude: 16384 |
c4aca9cc | 141 | timestamp: 0 |
beb47437 | 142 | string: 123 |
010a2dac SB |
143 | abbrev: 7 |
144 | verbose: 2 | |
36e6a5ba | 145 | quiet: 0 |
010a2dac | 146 | dry run: yes |
df217ed6 | 147 | file: prefix/my.file |
beb47437 JS |
148 | EOF |
149 | ||
150 | test_expect_success 'short options' ' | |
2f17c78c | 151 | test-tool parse-options -s123 -b -i 1729 -m 16k -b -vv -n -F my.file \ |
2a514ed8 | 152 | >output 2>output.err && |
3af82863 | 153 | test_cmp expect output && |
ca8d148d | 154 | test_must_be_empty output.err |
beb47437 | 155 | ' |
010a2dac | 156 | |
8425b7ea | 157 | cat >expect <<\EOF |
beb47437 JS |
158 | boolean: 2 |
159 | integer: 1729 | |
2a514ed8 | 160 | magnitude: 16384 |
c4aca9cc | 161 | timestamp: 0 |
beb47437 | 162 | string: 321 |
010a2dac SB |
163 | abbrev: 10 |
164 | verbose: 2 | |
36e6a5ba | 165 | quiet: 0 |
010a2dac | 166 | dry run: no |
df217ed6 | 167 | file: prefix/fi.le |
beb47437 JS |
168 | EOF |
169 | ||
170 | test_expect_success 'long options' ' | |
2f17c78c | 171 | test-tool parse-options --boolean --integer 1729 --magnitude 16k \ |
2a514ed8 CB |
172 | --boolean --string2=321 --verbose --verbose --no-dry-run \ |
173 | --abbrev=10 --file fi.le --obsolete \ | |
174 | >output 2>output.err && | |
ca8d148d | 175 | test_must_be_empty output.err && |
3af82863 | 176 | test_cmp expect output |
beb47437 JS |
177 | ' |
178 | ||
d5d745f9 | 179 | test_expect_success 'missing required value' ' |
62f2ffc5 ÆAB |
180 | cat >expect <<-\EOF && |
181 | error: switch `s'\'' requires a value | |
182 | EOF | |
183 | test_expect_code 129 test-tool parse-options -s 2>actual && | |
184 | test_cmp expect actual && | |
185 | ||
186 | cat >expect <<-\EOF && | |
187 | error: option `string'\'' requires a value | |
188 | EOF | |
189 | test_expect_code 129 test-tool parse-options --string 2>actual && | |
190 | test_cmp expect actual && | |
191 | ||
192 | cat >expect <<-\EOF && | |
193 | error: option `file'\'' requires a value | |
194 | EOF | |
195 | test_expect_code 129 test-tool parse-options --file 2>actual && | |
196 | test_cmp expect actual | |
197 | ' | |
198 | ||
199 | test_expect_success 'superfluous value provided: boolean' ' | |
200 | cat >expect <<-\EOF && | |
201 | error: option `yes'\'' takes no value | |
202 | EOF | |
203 | test_expect_code 129 test-tool parse-options --yes=hi 2>actual && | |
204 | test_cmp expect actual && | |
205 | ||
206 | cat >expect <<-\EOF && | |
207 | error: option `no-yes'\'' takes no value | |
208 | EOF | |
209 | test_expect_code 129 test-tool parse-options --no-yes=hi 2>actual && | |
210 | test_cmp expect actual | |
211 | ' | |
212 | ||
289cb155 RS |
213 | test_expect_success 'superfluous value provided: boolean, abbreviated' ' |
214 | cat >expect <<-\EOF && | |
215 | error: option `yes'\'' takes no value | |
216 | EOF | |
217 | test_expect_code 129 env GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \ | |
218 | test-tool parse-options --ye=hi 2>actual && | |
219 | test_cmp expect actual && | |
220 | ||
221 | cat >expect <<-\EOF && | |
222 | error: option `no-yes'\'' takes no value | |
223 | EOF | |
224 | test_expect_code 129 env GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \ | |
225 | test-tool parse-options --no-ye=hi 2>actual && | |
226 | test_cmp expect actual | |
227 | ' | |
228 | ||
62f2ffc5 ÆAB |
229 | test_expect_success 'superfluous value provided: cmdmode' ' |
230 | cat >expect <<-\EOF && | |
231 | error: option `mode1'\'' takes no value | |
232 | EOF | |
233 | test_expect_code 129 test-tool parse-options --mode1=hi 2>actual && | |
234 | test_cmp expect actual | |
d5d745f9 OM |
235 | ' |
236 | ||
8425b7ea | 237 | cat >expect <<\EOF |
beb47437 JS |
238 | boolean: 1 |
239 | integer: 13 | |
2a514ed8 | 240 | magnitude: 0 |
c4aca9cc | 241 | timestamp: 0 |
beb47437 | 242 | string: 123 |
010a2dac | 243 | abbrev: 7 |
e0070e8b | 244 | verbose: -1 |
36e6a5ba | 245 | quiet: 0 |
010a2dac | 246 | dry run: no |
df217ed6 | 247 | file: (not set) |
beb47437 JS |
248 | arg 00: a1 |
249 | arg 01: b1 | |
250 | arg 02: --boolean | |
251 | EOF | |
252 | ||
253 | test_expect_success 'intermingled arguments' ' | |
2f17c78c | 254 | test-tool parse-options a1 --string 123 b1 --boolean -j 13 -- --boolean \ |
8425b7ea | 255 | >output 2>output.err && |
ca8d148d | 256 | test_must_be_empty output.err && |
3af82863 | 257 | test_cmp expect output |
beb47437 JS |
258 | ' |
259 | ||
8425b7ea | 260 | cat >expect <<\EOF |
7f275b91 JS |
261 | boolean: 0 |
262 | integer: 2 | |
2a514ed8 | 263 | magnitude: 0 |
c4aca9cc | 264 | timestamp: 0 |
7f275b91 | 265 | string: (not set) |
010a2dac | 266 | abbrev: 7 |
e0070e8b | 267 | verbose: -1 |
36e6a5ba | 268 | quiet: 0 |
010a2dac | 269 | dry run: no |
df217ed6 | 270 | file: (not set) |
7f275b91 JS |
271 | EOF |
272 | ||
273 | test_expect_success 'unambiguously abbreviated option' ' | |
b02e7d5d | 274 | GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \ |
2f17c78c | 275 | test-tool parse-options --int 2 --boolean --no-bo >output 2>output.err && |
ca8d148d | 276 | test_must_be_empty output.err && |
3af82863 | 277 | test_cmp expect output |
7f275b91 JS |
278 | ' |
279 | ||
280 | test_expect_success 'unambiguously abbreviated option with "="' ' | |
b02e7d5d | 281 | GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \ |
2f17c78c | 282 | test-tool parse-options --expect="integer: 2" --int=2 |
7f275b91 JS |
283 | ' |
284 | ||
41ac414e | 285 | test_expect_success 'ambiguously abbreviated option' ' |
b02e7d5d JS |
286 | test_expect_code 129 env GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \ |
287 | test-tool parse-options --strin 123 | |
7f275b91 JS |
288 | ' |
289 | ||
243e0614 | 290 | test_expect_success 'non ambiguous option (after two options it abbreviates)' ' |
b02e7d5d | 291 | GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \ |
2f17c78c | 292 | test-tool parse-options --expect="string: 123" --st 123 |
243e0614 JS |
293 | ' |
294 | ||
5c387428 NTND |
295 | test_expect_success 'Alias options do not contribute to abbreviation' ' |
296 | test-tool parse-options --alias-source 123 >output && | |
297 | grep "^string: 123" output && | |
298 | test-tool parse-options --alias-target 123 >output && | |
299 | grep "^string: 123" output && | |
300 | test_must_fail test-tool parse-options --alias && | |
301 | GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \ | |
302 | test-tool parse-options --alias 123 >output && | |
303 | grep "^string: 123" output | |
304 | ' | |
305 | ||
8425b7ea | 306 | cat >typo.err <<\EOF |
395518cf | 307 | error: did you mean `--boolean` (with two dashes)? |
3a9f0f41 PH |
308 | EOF |
309 | ||
310 | test_expect_success 'detect possible typos' ' | |
2f17c78c | 311 | test_must_fail test-tool parse-options -boolean >output 2>output.err && |
ca8d148d | 312 | test_must_be_empty output && |
1108cea7 | 313 | test_cmp typo.err output.err |
3a9f0f41 PH |
314 | ' |
315 | ||
8425b7ea | 316 | cat >typo.err <<\EOF |
395518cf | 317 | error: did you mean `--ambiguous` (with two dashes)? |
38916c5b RS |
318 | EOF |
319 | ||
320 | test_expect_success 'detect possible typos' ' | |
2f17c78c | 321 | test_must_fail test-tool parse-options -ambiguous >output 2>output.err && |
ca8d148d | 322 | test_must_be_empty output && |
1108cea7 | 323 | test_cmp typo.err output.err |
38916c5b RS |
324 | ' |
325 | ||
8425b7ea | 326 | cat >expect <<\EOF |
010a2dac SB |
327 | Callback: "four", 0 |
328 | boolean: 5 | |
329 | integer: 4 | |
2a514ed8 | 330 | magnitude: 0 |
c4aca9cc | 331 | timestamp: 0 |
010a2dac SB |
332 | string: (not set) |
333 | abbrev: 7 | |
e0070e8b | 334 | verbose: -1 |
36e6a5ba | 335 | quiet: 0 |
010a2dac | 336 | dry run: no |
df217ed6 | 337 | file: (not set) |
010a2dac SB |
338 | EOF |
339 | ||
340 | test_expect_success 'OPT_CALLBACK() and OPT_BIT() work' ' | |
2f17c78c | 341 | test-tool parse-options --length=four -b -4 >output 2>output.err && |
ca8d148d | 342 | test_must_be_empty output.err && |
010a2dac SB |
343 | test_cmp expect output |
344 | ' | |
345 | ||
1a9bf1e1 | 346 | test_expect_success 'OPT_CALLBACK() and callback errors work' ' |
2f17c78c | 347 | test_must_fail test-tool parse-options --no-length >output 2>output.err && |
1c5e94f4 | 348 | test_must_be_empty output && |
3bb0923f | 349 | test_must_be_empty output.err |
010a2dac SB |
350 | ' |
351 | ||
8425b7ea | 352 | cat >expect <<\EOF |
010a2dac SB |
353 | boolean: 1 |
354 | integer: 23 | |
2a514ed8 | 355 | magnitude: 0 |
c4aca9cc | 356 | timestamp: 0 |
010a2dac SB |
357 | string: (not set) |
358 | abbrev: 7 | |
e0070e8b | 359 | verbose: -1 |
36e6a5ba | 360 | quiet: 0 |
010a2dac | 361 | dry run: no |
df217ed6 | 362 | file: (not set) |
010a2dac SB |
363 | EOF |
364 | ||
365 | test_expect_success 'OPT_BIT() and OPT_SET_INT() work' ' | |
2f17c78c | 366 | test-tool parse-options --set23 -bbbbb --no-or4 >output 2>output.err && |
ca8d148d | 367 | test_must_be_empty output.err && |
010a2dac SB |
368 | test_cmp expect output |
369 | ' | |
370 | ||
2f4b97f9 | 371 | test_expect_success 'OPT_NEGBIT() and OPT_SET_INT() work' ' |
2f17c78c | 372 | test-tool parse-options --set23 -bbbbb --neg-or4 >output 2>output.err && |
ca8d148d | 373 | test_must_be_empty output.err && |
2f4b97f9 RS |
374 | test_cmp expect output |
375 | ' | |
376 | ||
2f4b97f9 | 377 | test_expect_success 'OPT_BIT() works' ' |
2f17c78c | 378 | test-tool parse-options --expect="boolean: 6" -bb --or4 |
2f4b97f9 RS |
379 | ' |
380 | ||
381 | test_expect_success 'OPT_NEGBIT() works' ' | |
2f17c78c | 382 | test-tool parse-options --expect="boolean: 6" -bb --no-neg-or4 |
2f4b97f9 | 383 | ' |
010a2dac | 384 | |
62e7a6f7 | 385 | test_expect_success 'OPT_CMDMODE() works' ' |
0025dde7 RS |
386 | test-tool parse-options --expect="integer: 1" --mode1 && |
387 | test-tool parse-options --expect="integer: 3" --mode34=3 | |
62e7a6f7 PB |
388 | ' |
389 | ||
0025dde7 | 390 | test_expect_success 'OPT_CMDMODE() detects incompatibility (1)' ' |
62e7a6f7 PB |
391 | test_must_fail test-tool parse-options --mode1 --mode2 >output 2>output.err && |
392 | test_must_be_empty output && | |
a8e23947 JH |
393 | test_grep "mode1" output.err && |
394 | test_grep "mode2" output.err && | |
7854bf49 | 395 | test_grep "cannot be used together" output.err |
62e7a6f7 PB |
396 | ' |
397 | ||
0025dde7 | 398 | test_expect_success 'OPT_CMDMODE() detects incompatibility (2)' ' |
62e7a6f7 PB |
399 | test_must_fail test-tool parse-options --set23 --mode2 >output 2>output.err && |
400 | test_must_be_empty output && | |
a8e23947 JH |
401 | test_grep "mode2" output.err && |
402 | test_grep "set23" output.err && | |
7854bf49 | 403 | test_grep "cannot be used together" output.err |
0025dde7 RS |
404 | ' |
405 | ||
406 | test_expect_success 'OPT_CMDMODE() detects incompatibility (3)' ' | |
407 | test_must_fail test-tool parse-options --mode2 --set23 >output 2>output.err && | |
408 | test_must_be_empty output && | |
a8e23947 JH |
409 | test_grep "mode2" output.err && |
410 | test_grep "set23" output.err && | |
7854bf49 | 411 | test_grep "cannot be used together" output.err |
0025dde7 RS |
412 | ' |
413 | ||
414 | test_expect_success 'OPT_CMDMODE() detects incompatibility (4)' ' | |
415 | test_must_fail test-tool parse-options --mode2 --mode34=3 \ | |
416 | >output 2>output.err && | |
417 | test_must_be_empty output && | |
a8e23947 JH |
418 | test_grep "mode2" output.err && |
419 | test_grep "mode34.3" output.err && | |
7854bf49 | 420 | test_grep "cannot be used together" output.err |
62e7a6f7 PB |
421 | ' |
422 | ||
b9e63ddd | 423 | test_expect_success 'OPT_COUNTUP() with PARSE_OPT_NODASH works' ' |
2f17c78c | 424 | test-tool parse-options --expect="boolean: 6" + + + + + + |
51a9949e RS |
425 | ' |
426 | ||
e0319ff5 | 427 | test_expect_success 'OPT_NUMBER_CALLBACK() works' ' |
2f17c78c | 428 | test-tool parse-options --expect="integer: 12345" -12345 |
e0319ff5 RS |
429 | ' |
430 | ||
8425b7ea | 431 | cat >expect <<\EOF |
6bbfd1fa AS |
432 | boolean: 0 |
433 | integer: 0 | |
2a514ed8 | 434 | magnitude: 0 |
6bbfd1fa AS |
435 | timestamp: 0 |
436 | string: (not set) | |
437 | abbrev: 7 | |
e0070e8b | 438 | verbose: -1 |
36e6a5ba | 439 | quiet: 0 |
6bbfd1fa AS |
440 | dry run: no |
441 | file: (not set) | |
442 | EOF | |
443 | ||
444 | test_expect_success 'negation of OPT_NONEG flags is not ambiguous' ' | |
b02e7d5d | 445 | GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \ |
2f17c78c | 446 | test-tool parse-options --no-ambig >output 2>output.err && |
ca8d148d | 447 | test_must_be_empty output.err && |
6bbfd1fa AS |
448 | test_cmp expect output |
449 | ' | |
450 | ||
8425b7ea | 451 | cat >>expect <<\EOF |
c8ba1639 JK |
452 | list: foo |
453 | list: bar | |
454 | list: baz | |
455 | EOF | |
456 | test_expect_success '--list keeps list of strings' ' | |
2f17c78c | 457 | test-tool parse-options --list foo --list=bar --list=baz >output && |
c8ba1639 JK |
458 | test_cmp expect output |
459 | ' | |
460 | ||
461 | test_expect_success '--no-list resets list' ' | |
2f17c78c | 462 | test-tool parse-options --list=other --list=irrelevant --list=options \ |
c8ba1639 JK |
463 | --no-list --list=foo --list=bar --list=baz >output && |
464 | test_cmp expect output | |
465 | ' | |
466 | ||
7d177152 | 467 | test_expect_success 'multiple quiet levels' ' |
2f17c78c | 468 | test-tool parse-options --expect="quiet: 3" -q -q -q |
7d177152 PB |
469 | ' |
470 | ||
7d177152 | 471 | test_expect_success 'multiple verbose levels' ' |
2f17c78c | 472 | test-tool parse-options --expect="verbose: 3" -v -v -v |
7d177152 PB |
473 | ' |
474 | ||
7d177152 | 475 | test_expect_success '--no-quiet sets --quiet to 0' ' |
2f17c78c | 476 | test-tool parse-options --expect="quiet: 0" --no-quiet |
7d177152 PB |
477 | ' |
478 | ||
7d177152 | 479 | test_expect_success '--no-quiet resets multiple -q to 0' ' |
2f17c78c | 480 | test-tool parse-options --expect="quiet: 0" -q -q -q --no-quiet |
7d177152 PB |
481 | ' |
482 | ||
7d177152 | 483 | test_expect_success '--no-verbose sets verbose to 0' ' |
2f17c78c | 484 | test-tool parse-options --expect="verbose: 0" --no-verbose |
7d177152 PB |
485 | ' |
486 | ||
7d177152 | 487 | test_expect_success '--no-verbose resets multiple verbose to 0' ' |
2f17c78c | 488 | test-tool parse-options --expect="verbose: 0" -v -v -v --no-verbose |
7d177152 PB |
489 | ' |
490 | ||
b02e7d5d JS |
491 | test_expect_success 'GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS works' ' |
492 | GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \ | |
493 | test-tool parse-options --ye && | |
494 | test_must_fail env GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=true \ | |
495 | test-tool parse-options --ye | |
496 | ' | |
497 | ||
51b4594b JK |
498 | test_expect_success '--end-of-options treats remainder as args' ' |
499 | test-tool parse-options \ | |
500 | --expect="verbose: -1" \ | |
501 | --expect="arg 00: --verbose" \ | |
502 | --end-of-options --verbose | |
503 | ' | |
504 | ||
c1b117d3 SG |
505 | test_expect_success 'KEEP_DASHDASH works' ' |
506 | test-tool parse-options-flags --keep-dashdash cmd --opt=1 -- --opt=2 --unknown >actual && | |
507 | cat >expect <<-\EOF && | |
508 | opt: 1 | |
509 | arg 00: -- | |
510 | arg 01: --opt=2 | |
511 | arg 02: --unknown | |
512 | EOF | |
513 | test_cmp expect actual | |
514 | ' | |
515 | ||
516 | test_expect_success 'KEEP_ARGV0 works' ' | |
517 | test-tool parse-options-flags --keep-argv0 cmd arg0 --opt=3 >actual && | |
518 | cat >expect <<-\EOF && | |
519 | opt: 3 | |
520 | arg 00: cmd | |
521 | arg 01: arg0 | |
522 | EOF | |
523 | test_cmp expect actual | |
524 | ' | |
525 | ||
526 | test_expect_success 'STOP_AT_NON_OPTION works' ' | |
527 | test-tool parse-options-flags --stop-at-non-option cmd --opt=4 arg0 --opt=5 --unknown >actual && | |
528 | cat >expect <<-\EOF && | |
529 | opt: 4 | |
530 | arg 00: arg0 | |
531 | arg 01: --opt=5 | |
532 | arg 02: --unknown | |
533 | EOF | |
534 | test_cmp expect actual | |
535 | ' | |
536 | ||
99d86d60 SG |
537 | test_expect_success 'KEEP_UNKNOWN_OPT works' ' |
538 | test-tool parse-options-flags --keep-unknown-opt cmd --unknown=1 --opt=6 -u2 >actual && | |
c1b117d3 SG |
539 | cat >expect <<-\EOF && |
540 | opt: 6 | |
541 | arg 00: --unknown=1 | |
542 | arg 01: -u2 | |
543 | EOF | |
544 | test_cmp expect actual | |
545 | ' | |
546 | ||
547 | test_expect_success 'NO_INTERNAL_HELP works for -h' ' | |
548 | test_expect_code 129 test-tool parse-options-flags --no-internal-help cmd -h 2>err && | |
c1b117d3 SG |
549 | grep "^error: unknown switch \`h$SQ" err && |
550 | grep "^usage: " err | |
551 | ' | |
552 | ||
553 | for help_opt in help help-all | |
554 | do | |
555 | test_expect_success "NO_INTERNAL_HELP works for --$help_opt" " | |
556 | test_expect_code 129 test-tool parse-options-flags --no-internal-help cmd --$help_opt 2>err && | |
c1b117d3 SG |
557 | grep '^error: unknown option \`'$help_opt\' err && |
558 | grep '^usage: ' err | |
559 | " | |
560 | done | |
561 | ||
99d86d60 SG |
562 | test_expect_success 'KEEP_UNKNOWN_OPT | NO_INTERNAL_HELP works' ' |
563 | test-tool parse-options-flags --keep-unknown-opt --no-internal-help cmd -h --help --help-all >actual && | |
c1b117d3 SG |
564 | cat >expect <<-\EOF && |
565 | opt: 0 | |
566 | arg 00: -h | |
567 | arg 01: --help | |
568 | arg 02: --help-all | |
569 | EOF | |
570 | test_cmp expect actual | |
571 | ' | |
572 | ||
fa83cc83 SG |
573 | test_expect_success 'subcommand - no subcommand shows error and usage' ' |
574 | test_expect_code 129 test-tool parse-subcommand cmd 2>err && | |
575 | grep "^error: need a subcommand" err && | |
576 | grep ^usage: err | |
577 | ' | |
578 | ||
579 | test_expect_success 'subcommand - subcommand after -- shows error and usage' ' | |
580 | test_expect_code 129 test-tool parse-subcommand cmd -- subcmd-one 2>err && | |
581 | grep "^error: need a subcommand" err && | |
582 | grep ^usage: err | |
583 | ' | |
584 | ||
585 | test_expect_success 'subcommand - subcommand after --end-of-options shows error and usage' ' | |
586 | test_expect_code 129 test-tool parse-subcommand cmd --end-of-options subcmd-one 2>err && | |
587 | grep "^error: need a subcommand" err && | |
588 | grep ^usage: err | |
589 | ' | |
590 | ||
591 | test_expect_success 'subcommand - unknown subcommand shows error and usage' ' | |
592 | test_expect_code 129 test-tool parse-subcommand cmd nope 2>err && | |
593 | grep "^error: unknown subcommand: \`nope$SQ" err && | |
594 | grep ^usage: err | |
595 | ' | |
596 | ||
597 | test_expect_success 'subcommand - subcommands cannot be abbreviated' ' | |
598 | test_expect_code 129 test-tool parse-subcommand cmd subcmd-o 2>err && | |
599 | grep "^error: unknown subcommand: \`subcmd-o$SQ$" err && | |
600 | grep ^usage: err | |
601 | ' | |
602 | ||
603 | test_expect_success 'subcommand - no negated subcommands' ' | |
604 | test_expect_code 129 test-tool parse-subcommand cmd no-subcmd-one 2>err && | |
605 | grep "^error: unknown subcommand: \`no-subcmd-one$SQ" err && | |
606 | grep ^usage: err | |
607 | ' | |
608 | ||
609 | test_expect_success 'subcommand - simple' ' | |
610 | test-tool parse-subcommand cmd subcmd-two >actual && | |
611 | cat >expect <<-\EOF && | |
612 | opt: 0 | |
613 | fn: subcmd_two | |
614 | arg 00: subcmd-two | |
615 | EOF | |
616 | test_cmp expect actual | |
617 | ' | |
618 | ||
619 | test_expect_success 'subcommand - stop parsing at the first subcommand' ' | |
620 | test-tool parse-subcommand cmd --opt=1 subcmd-two subcmd-one --opt=2 >actual && | |
621 | cat >expect <<-\EOF && | |
622 | opt: 1 | |
623 | fn: subcmd_two | |
624 | arg 00: subcmd-two | |
625 | arg 01: subcmd-one | |
626 | arg 02: --opt=2 | |
627 | EOF | |
628 | test_cmp expect actual | |
629 | ' | |
630 | ||
631 | test_expect_success 'subcommand - KEEP_ARGV0' ' | |
632 | test-tool parse-subcommand --keep-argv0 cmd subcmd-two >actual && | |
633 | cat >expect <<-\EOF && | |
634 | opt: 0 | |
635 | fn: subcmd_two | |
636 | arg 00: cmd | |
637 | arg 01: subcmd-two | |
638 | EOF | |
639 | test_cmp expect actual | |
640 | ' | |
641 | ||
642 | test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL + subcommand not given' ' | |
643 | test-tool parse-subcommand --subcommand-optional cmd >actual && | |
644 | cat >expect <<-\EOF && | |
645 | opt: 0 | |
646 | fn: subcmd_one | |
647 | EOF | |
648 | test_cmp expect actual | |
649 | ' | |
650 | ||
651 | test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL + given subcommand' ' | |
652 | test-tool parse-subcommand --subcommand-optional cmd subcmd-two branch file >actual && | |
653 | cat >expect <<-\EOF && | |
654 | opt: 0 | |
655 | fn: subcmd_two | |
656 | arg 00: subcmd-two | |
657 | arg 01: branch | |
658 | arg 02: file | |
659 | EOF | |
660 | test_cmp expect actual | |
661 | ' | |
662 | ||
663 | test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL + subcommand not given + unknown dashless args' ' | |
664 | test-tool parse-subcommand --subcommand-optional cmd branch file >actual && | |
665 | cat >expect <<-\EOF && | |
666 | opt: 0 | |
667 | fn: subcmd_one | |
668 | arg 00: branch | |
669 | arg 01: file | |
670 | EOF | |
671 | test_cmp expect actual | |
672 | ' | |
673 | ||
674 | test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL + subcommand not given + unknown option' ' | |
675 | test_expect_code 129 test-tool parse-subcommand --subcommand-optional cmd --subcommand-opt 2>err && | |
676 | grep "^error: unknown option" err && | |
677 | grep ^usage: err | |
678 | ' | |
679 | ||
680 | test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL | KEEP_UNKNOWN_OPT + subcommand not given + unknown option' ' | |
681 | test-tool parse-subcommand --subcommand-optional --keep-unknown-opt cmd --subcommand-opt >actual && | |
682 | cat >expect <<-\EOF && | |
683 | opt: 0 | |
684 | fn: subcmd_one | |
685 | arg 00: --subcommand-opt | |
686 | EOF | |
687 | test_cmp expect actual | |
688 | ' | |
689 | ||
690 | test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL | KEEP_UNKNOWN_OPT + subcommand ignored after unknown option' ' | |
691 | test-tool parse-subcommand --subcommand-optional --keep-unknown-opt cmd --subcommand-opt subcmd-two >actual && | |
692 | cat >expect <<-\EOF && | |
693 | opt: 0 | |
694 | fn: subcmd_one | |
695 | arg 00: --subcommand-opt | |
696 | arg 01: subcmd-two | |
697 | EOF | |
698 | test_cmp expect actual | |
699 | ' | |
700 | ||
701 | test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL | KEEP_UNKNOWN_OPT + command and subcommand options cannot be mixed' ' | |
702 | test-tool parse-subcommand --subcommand-optional --keep-unknown-opt cmd --subcommand-opt branch --opt=1 >actual && | |
703 | cat >expect <<-\EOF && | |
704 | opt: 0 | |
705 | fn: subcmd_one | |
706 | arg 00: --subcommand-opt | |
707 | arg 01: branch | |
708 | arg 02: --opt=1 | |
709 | EOF | |
710 | test_cmp expect actual | |
711 | ' | |
712 | ||
713 | test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL | KEEP_UNKNOWN_OPT | KEEP_ARGV0' ' | |
714 | test-tool parse-subcommand --subcommand-optional --keep-unknown-opt --keep-argv0 cmd --subcommand-opt branch >actual && | |
715 | cat >expect <<-\EOF && | |
716 | opt: 0 | |
717 | fn: subcmd_one | |
718 | arg 00: cmd | |
719 | arg 01: --subcommand-opt | |
720 | arg 02: branch | |
721 | EOF | |
722 | test_cmp expect actual | |
723 | ' | |
724 | ||
725 | test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL | KEEP_UNKNOWN_OPT | KEEP_DASHDASH' ' | |
726 | test-tool parse-subcommand --subcommand-optional --keep-unknown-opt --keep-dashdash cmd -- --subcommand-opt file >actual && | |
727 | cat >expect <<-\EOF && | |
728 | opt: 0 | |
729 | fn: subcmd_one | |
730 | arg 00: -- | |
731 | arg 01: --subcommand-opt | |
732 | arg 02: file | |
733 | EOF | |
734 | test_cmp expect actual | |
735 | ' | |
736 | ||
737 | test_expect_success 'subcommand - completion helper' ' | |
738 | test-tool parse-subcommand cmd --git-completion-helper >actual && | |
739 | echo "subcmd-one subcmd-two --opt= --no-opt" >expect && | |
740 | test_cmp expect actual | |
741 | ' | |
742 | ||
743 | test_expect_success 'subcommands are incompatible with STOP_AT_NON_OPTION' ' | |
744 | test_must_fail test-tool parse-subcommand --stop-at-non-option cmd subcmd-one 2>err && | |
745 | grep ^BUG err | |
746 | ' | |
747 | ||
748 | test_expect_success 'subcommands are incompatible with KEEP_UNKNOWN_OPT unless in combination with SUBCOMMAND_OPTIONAL' ' | |
749 | test_must_fail test-tool parse-subcommand --keep-unknown-opt cmd subcmd-two 2>err && | |
750 | grep ^BUG err | |
751 | ' | |
752 | ||
753 | test_expect_success 'subcommands are incompatible with KEEP_DASHDASH unless in combination with SUBCOMMAND_OPTIONAL' ' | |
754 | test_must_fail test-tool parse-subcommand --keep-dashdash cmd subcmd-two 2>err && | |
755 | grep ^BUG err | |
756 | ' | |
757 | ||
84356ff7 PW |
758 | test_expect_success 'negative magnitude' ' |
759 | test_must_fail test-tool parse-options --magnitude -1 >out 2>err && | |
760 | grep "non-negative integer" err && | |
761 | test_must_be_empty out | |
762 | ' | |
7595c0ec PW |
763 | |
764 | test_expect_success 'magnitude with units but no numbers' ' | |
765 | test_must_fail test-tool parse-options --magnitude m >out 2>err && | |
766 | grep "non-negative integer" err && | |
767 | test_must_be_empty out | |
768 | ' | |
769 | ||
beb47437 | 770 | test_done |