]>
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 | ||
8 | . ./test-lib.sh | |
9 | ||
8425b7ea | 10 | cat >expect <<\EOF |
2f17c78c | 11 | usage: test-tool parse-options <options> |
beb47437 | 12 | |
c97ee171 BC |
13 | A helper function for the parse-options API. |
14 | ||
b9e63ddd RS |
15 | --yes get a boolean |
16 | -D, --no-doubt begins with 'no-' | |
17 | -B, --no-fear be brave | |
18 | -b, --boolean increment by one | |
010a2dac | 19 | -4, --or4 bitwise-or boolean with ...0100 |
2f4b97f9 | 20 | --neg-or4 same as --no-or4 |
010a2dac | 21 | |
beb47437 JS |
22 | -i, --integer <n> get a integer |
23 | -j <n> get a integer, too | |
2a514ed8 | 24 | -m, --magnitude <n> get a magnitude |
010a2dac | 25 | --set23 set integer to 23 |
62e7a6f7 PB |
26 | --mode1 set integer to 1 (cmdmode option) |
27 | --mode2 set integer to 2 (cmdmode option) | |
010a2dac | 28 | -L, --length <str> get length of <str> |
41dbcd45 | 29 | -F, --file <file> set file to <file> |
beb47437 | 30 | |
010a2dac | 31 | String options |
beb47437 JS |
32 | -s, --string <string> |
33 | get a string | |
34 | --string2 <str> get another string | |
243e0614 | 35 | --st <st> get another string (pervert ordering) |
3a9f0f41 | 36 | -o <str> get another string |
c8ba1639 | 37 | --list <str> add str to list |
beb47437 | 38 | |
010a2dac | 39 | Magic arguments |
580d5bff | 40 | --quux means --quux |
e0319ff5 | 41 | -NUM set integer to NUM |
51a9949e | 42 | + same as -b |
6bbfd1fa AS |
43 | --ambiguous positive ambiguity |
44 | --no-ambiguous negative ambiguity | |
580d5bff | 45 | |
010a2dac | 46 | Standard options |
4279000d | 47 | --abbrev[=<n>] use <n> digits to display object names |
010a2dac SB |
48 | -v, --verbose be verbose |
49 | -n, --dry-run dry run | |
50 | -q, --quiet be quiet | |
ab6b28b0 | 51 | --expect <string> expected output in the variable dump |
010a2dac | 52 | |
5c387428 NTND |
53 | Alias |
54 | -A, --alias-source <string> | |
55 | get a string | |
56 | -Z, --alias-target <string> | |
7c280589 | 57 | alias of --alias-source |
5c387428 | 58 | |
beb47437 JS |
59 | EOF |
60 | ||
61 | test_expect_success 'test help' ' | |
2f17c78c | 62 | test_must_fail test-tool parse-options -h >output 2>output.err && |
ca8d148d | 63 | test_must_be_empty output.err && |
9a001381 | 64 | test_i18ncmp expect output |
beb47437 JS |
65 | ' |
66 | ||
9c7304e3 GS |
67 | mv expect expect.err |
68 | ||
8ca65aeb | 69 | check () { |
b9e63ddd RS |
70 | what="$1" && |
71 | shift && | |
72 | expect="$1" && | |
73 | shift && | |
2f17c78c | 74 | test-tool parse-options --expect="$what $expect" "$@" |
b9e63ddd RS |
75 | } |
76 | ||
9a001381 JX |
77 | check_unknown_i18n() { |
78 | case "$1" in | |
79 | --*) | |
80 | echo error: unknown option \`${1#--}\' >expect ;; | |
81 | -*) | |
82 | echo error: unknown switch \`${1#-}\' >expect ;; | |
83 | esac && | |
84 | cat expect.err >>expect && | |
2f17c78c | 85 | test_must_fail test-tool parse-options $* >output 2>output.err && |
ca8d148d | 86 | test_must_be_empty output && |
9a001381 JX |
87 | test_i18ncmp expect output.err |
88 | } | |
89 | ||
b9e63ddd RS |
90 | test_expect_success 'OPT_BOOL() #1' 'check boolean: 1 --yes' |
91 | test_expect_success 'OPT_BOOL() #2' 'check boolean: 1 --no-doubt' | |
92 | test_expect_success 'OPT_BOOL() #3' 'check boolean: 1 -D' | |
93 | test_expect_success 'OPT_BOOL() #4' 'check boolean: 1 --no-fear' | |
94 | test_expect_success 'OPT_BOOL() #5' 'check boolean: 1 -B' | |
95 | ||
96 | test_expect_success 'OPT_BOOL() is idempotent #1' 'check boolean: 1 --yes --yes' | |
97 | test_expect_success 'OPT_BOOL() is idempotent #2' 'check boolean: 1 -DB' | |
98 | ||
99 | test_expect_success 'OPT_BOOL() negation #1' 'check boolean: 0 -D --no-yes' | |
100 | test_expect_success 'OPT_BOOL() negation #2' 'check boolean: 0 -D --no-no-doubt' | |
101 | ||
1a9bf1e1 BC |
102 | test_expect_success 'OPT_BOOL() no negation #1' 'check_unknown_i18n --fear' |
103 | test_expect_success 'OPT_BOOL() no negation #2' 'check_unknown_i18n --no-no-fear' | |
b9e63ddd | 104 | |
0f1930c5 | 105 | test_expect_success 'OPT_BOOL() positivation' 'check boolean: 0 -D --doubt' |
b9e63ddd | 106 | |
81a48cc0 CB |
107 | test_expect_success 'OPT_INT() negative' 'check integer: -2345 -i -2345' |
108 | ||
2a514ed8 CB |
109 | test_expect_success 'OPT_MAGNITUDE() simple' ' |
110 | check magnitude: 2345678 -m 2345678 | |
111 | ' | |
112 | ||
113 | test_expect_success 'OPT_MAGNITUDE() kilo' ' | |
114 | check magnitude: 239616 -m 234k | |
115 | ' | |
116 | ||
117 | test_expect_success 'OPT_MAGNITUDE() mega' ' | |
118 | check magnitude: 104857600 -m 100m | |
119 | ' | |
120 | ||
121 | test_expect_success 'OPT_MAGNITUDE() giga' ' | |
122 | check magnitude: 1073741824 -m 1g | |
123 | ' | |
124 | ||
125 | test_expect_success 'OPT_MAGNITUDE() 3giga' ' | |
126 | check magnitude: 3221225472 -m 3g | |
127 | ' | |
128 | ||
8425b7ea | 129 | cat >expect <<\EOF |
beb47437 JS |
130 | boolean: 2 |
131 | integer: 1729 | |
2a514ed8 | 132 | magnitude: 16384 |
c4aca9cc | 133 | timestamp: 0 |
beb47437 | 134 | string: 123 |
010a2dac SB |
135 | abbrev: 7 |
136 | verbose: 2 | |
36e6a5ba | 137 | quiet: 0 |
010a2dac | 138 | dry run: yes |
df217ed6 | 139 | file: prefix/my.file |
beb47437 JS |
140 | EOF |
141 | ||
142 | test_expect_success 'short options' ' | |
2f17c78c | 143 | test-tool parse-options -s123 -b -i 1729 -m 16k -b -vv -n -F my.file \ |
2a514ed8 | 144 | >output 2>output.err && |
3af82863 | 145 | test_cmp expect output && |
ca8d148d | 146 | test_must_be_empty output.err |
beb47437 | 147 | ' |
010a2dac | 148 | |
8425b7ea | 149 | cat >expect <<\EOF |
beb47437 JS |
150 | boolean: 2 |
151 | integer: 1729 | |
2a514ed8 | 152 | magnitude: 16384 |
c4aca9cc | 153 | timestamp: 0 |
beb47437 | 154 | string: 321 |
010a2dac SB |
155 | abbrev: 10 |
156 | verbose: 2 | |
36e6a5ba | 157 | quiet: 0 |
010a2dac | 158 | dry run: no |
df217ed6 | 159 | file: prefix/fi.le |
beb47437 JS |
160 | EOF |
161 | ||
162 | test_expect_success 'long options' ' | |
2f17c78c | 163 | test-tool parse-options --boolean --integer 1729 --magnitude 16k \ |
2a514ed8 CB |
164 | --boolean --string2=321 --verbose --verbose --no-dry-run \ |
165 | --abbrev=10 --file fi.le --obsolete \ | |
166 | >output 2>output.err && | |
ca8d148d | 167 | test_must_be_empty output.err && |
3af82863 | 168 | test_cmp expect output |
beb47437 JS |
169 | ' |
170 | ||
d5d745f9 | 171 | test_expect_success 'missing required value' ' |
2f17c78c NTND |
172 | test_expect_code 129 test-tool parse-options -s && |
173 | test_expect_code 129 test-tool parse-options --string && | |
174 | test_expect_code 129 test-tool parse-options --file | |
d5d745f9 OM |
175 | ' |
176 | ||
8425b7ea | 177 | cat >expect <<\EOF |
beb47437 JS |
178 | boolean: 1 |
179 | integer: 13 | |
2a514ed8 | 180 | magnitude: 0 |
c4aca9cc | 181 | timestamp: 0 |
beb47437 | 182 | string: 123 |
010a2dac | 183 | abbrev: 7 |
e0070e8b | 184 | verbose: -1 |
36e6a5ba | 185 | quiet: 0 |
010a2dac | 186 | dry run: no |
df217ed6 | 187 | file: (not set) |
beb47437 JS |
188 | arg 00: a1 |
189 | arg 01: b1 | |
190 | arg 02: --boolean | |
191 | EOF | |
192 | ||
193 | test_expect_success 'intermingled arguments' ' | |
2f17c78c | 194 | test-tool parse-options a1 --string 123 b1 --boolean -j 13 -- --boolean \ |
8425b7ea | 195 | >output 2>output.err && |
ca8d148d | 196 | test_must_be_empty output.err && |
3af82863 | 197 | test_cmp expect output |
beb47437 JS |
198 | ' |
199 | ||
8425b7ea | 200 | cat >expect <<\EOF |
7f275b91 JS |
201 | boolean: 0 |
202 | integer: 2 | |
2a514ed8 | 203 | magnitude: 0 |
c4aca9cc | 204 | timestamp: 0 |
7f275b91 | 205 | string: (not set) |
010a2dac | 206 | abbrev: 7 |
e0070e8b | 207 | verbose: -1 |
36e6a5ba | 208 | quiet: 0 |
010a2dac | 209 | dry run: no |
df217ed6 | 210 | file: (not set) |
7f275b91 JS |
211 | EOF |
212 | ||
213 | test_expect_success 'unambiguously abbreviated option' ' | |
b02e7d5d | 214 | GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \ |
2f17c78c | 215 | test-tool parse-options --int 2 --boolean --no-bo >output 2>output.err && |
ca8d148d | 216 | test_must_be_empty output.err && |
3af82863 | 217 | test_cmp expect output |
7f275b91 JS |
218 | ' |
219 | ||
220 | test_expect_success 'unambiguously abbreviated option with "="' ' | |
b02e7d5d | 221 | GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \ |
2f17c78c | 222 | test-tool parse-options --expect="integer: 2" --int=2 |
7f275b91 JS |
223 | ' |
224 | ||
41ac414e | 225 | test_expect_success 'ambiguously abbreviated option' ' |
b02e7d5d JS |
226 | test_expect_code 129 env GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \ |
227 | test-tool parse-options --strin 123 | |
7f275b91 JS |
228 | ' |
229 | ||
243e0614 | 230 | test_expect_success 'non ambiguous option (after two options it abbreviates)' ' |
b02e7d5d | 231 | GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \ |
2f17c78c | 232 | test-tool parse-options --expect="string: 123" --st 123 |
243e0614 JS |
233 | ' |
234 | ||
5c387428 NTND |
235 | test_expect_success 'Alias options do not contribute to abbreviation' ' |
236 | test-tool parse-options --alias-source 123 >output && | |
237 | grep "^string: 123" output && | |
238 | test-tool parse-options --alias-target 123 >output && | |
239 | grep "^string: 123" output && | |
240 | test_must_fail test-tool parse-options --alias && | |
241 | GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \ | |
242 | test-tool parse-options --alias 123 >output && | |
243 | grep "^string: 123" output | |
244 | ' | |
245 | ||
8425b7ea | 246 | cat >typo.err <<\EOF |
395518cf | 247 | error: did you mean `--boolean` (with two dashes)? |
3a9f0f41 PH |
248 | EOF |
249 | ||
250 | test_expect_success 'detect possible typos' ' | |
2f17c78c | 251 | test_must_fail test-tool parse-options -boolean >output 2>output.err && |
ca8d148d | 252 | test_must_be_empty output && |
89003426 | 253 | test_i18ncmp typo.err output.err |
3a9f0f41 PH |
254 | ' |
255 | ||
8425b7ea | 256 | cat >typo.err <<\EOF |
395518cf | 257 | error: did you mean `--ambiguous` (with two dashes)? |
38916c5b RS |
258 | EOF |
259 | ||
260 | test_expect_success 'detect possible typos' ' | |
2f17c78c | 261 | test_must_fail test-tool parse-options -ambiguous >output 2>output.err && |
ca8d148d | 262 | test_must_be_empty output && |
89003426 | 263 | test_i18ncmp typo.err output.err |
38916c5b RS |
264 | ' |
265 | ||
580d5bff | 266 | test_expect_success 'keep some options as arguments' ' |
2f17c78c | 267 | test-tool parse-options --expect="arg 00: --quux" --quux |
580d5bff PH |
268 | ' |
269 | ||
8425b7ea | 270 | cat >expect <<\EOF |
010a2dac SB |
271 | Callback: "four", 0 |
272 | boolean: 5 | |
273 | integer: 4 | |
2a514ed8 | 274 | magnitude: 0 |
c4aca9cc | 275 | timestamp: 0 |
010a2dac SB |
276 | string: (not set) |
277 | abbrev: 7 | |
e0070e8b | 278 | verbose: -1 |
36e6a5ba | 279 | quiet: 0 |
010a2dac | 280 | dry run: no |
df217ed6 | 281 | file: (not set) |
010a2dac SB |
282 | EOF |
283 | ||
284 | test_expect_success 'OPT_CALLBACK() and OPT_BIT() work' ' | |
2f17c78c | 285 | test-tool parse-options --length=four -b -4 >output 2>output.err && |
ca8d148d | 286 | test_must_be_empty output.err && |
010a2dac SB |
287 | test_cmp expect output |
288 | ' | |
289 | ||
1a9bf1e1 | 290 | test_expect_success 'OPT_CALLBACK() and callback errors work' ' |
2f17c78c | 291 | test_must_fail test-tool parse-options --no-length >output 2>output.err && |
1c5e94f4 | 292 | test_must_be_empty output && |
3bb0923f | 293 | test_must_be_empty output.err |
010a2dac SB |
294 | ' |
295 | ||
8425b7ea | 296 | cat >expect <<\EOF |
010a2dac SB |
297 | boolean: 1 |
298 | integer: 23 | |
2a514ed8 | 299 | magnitude: 0 |
c4aca9cc | 300 | timestamp: 0 |
010a2dac SB |
301 | string: (not set) |
302 | abbrev: 7 | |
e0070e8b | 303 | verbose: -1 |
36e6a5ba | 304 | quiet: 0 |
010a2dac | 305 | dry run: no |
df217ed6 | 306 | file: (not set) |
010a2dac SB |
307 | EOF |
308 | ||
309 | test_expect_success 'OPT_BIT() and OPT_SET_INT() work' ' | |
2f17c78c | 310 | test-tool parse-options --set23 -bbbbb --no-or4 >output 2>output.err && |
ca8d148d | 311 | test_must_be_empty output.err && |
010a2dac SB |
312 | test_cmp expect output |
313 | ' | |
314 | ||
2f4b97f9 | 315 | test_expect_success 'OPT_NEGBIT() and OPT_SET_INT() work' ' |
2f17c78c | 316 | test-tool parse-options --set23 -bbbbb --neg-or4 >output 2>output.err && |
ca8d148d | 317 | test_must_be_empty output.err && |
2f4b97f9 RS |
318 | test_cmp expect output |
319 | ' | |
320 | ||
2f4b97f9 | 321 | test_expect_success 'OPT_BIT() works' ' |
2f17c78c | 322 | test-tool parse-options --expect="boolean: 6" -bb --or4 |
2f4b97f9 RS |
323 | ' |
324 | ||
325 | test_expect_success 'OPT_NEGBIT() works' ' | |
2f17c78c | 326 | test-tool parse-options --expect="boolean: 6" -bb --no-neg-or4 |
2f4b97f9 | 327 | ' |
010a2dac | 328 | |
62e7a6f7 PB |
329 | test_expect_success 'OPT_CMDMODE() works' ' |
330 | test-tool parse-options --expect="integer: 1" --mode1 | |
331 | ' | |
332 | ||
333 | test_expect_success 'OPT_CMDMODE() detects incompatibility' ' | |
334 | test_must_fail test-tool parse-options --mode1 --mode2 >output 2>output.err && | |
335 | test_must_be_empty output && | |
336 | test_i18ngrep "incompatible with --mode" output.err | |
337 | ' | |
338 | ||
339 | test_expect_success 'OPT_CMDMODE() detects incompatibility with something else' ' | |
340 | test_must_fail test-tool parse-options --set23 --mode2 >output 2>output.err && | |
341 | test_must_be_empty output && | |
342 | test_i18ngrep "incompatible with something else" output.err | |
343 | ' | |
344 | ||
b9e63ddd | 345 | test_expect_success 'OPT_COUNTUP() with PARSE_OPT_NODASH works' ' |
2f17c78c | 346 | test-tool parse-options --expect="boolean: 6" + + + + + + |
51a9949e RS |
347 | ' |
348 | ||
e0319ff5 | 349 | test_expect_success 'OPT_NUMBER_CALLBACK() works' ' |
2f17c78c | 350 | test-tool parse-options --expect="integer: 12345" -12345 |
e0319ff5 RS |
351 | ' |
352 | ||
8425b7ea | 353 | cat >expect <<\EOF |
6bbfd1fa AS |
354 | boolean: 0 |
355 | integer: 0 | |
2a514ed8 | 356 | magnitude: 0 |
6bbfd1fa AS |
357 | timestamp: 0 |
358 | string: (not set) | |
359 | abbrev: 7 | |
e0070e8b | 360 | verbose: -1 |
36e6a5ba | 361 | quiet: 0 |
6bbfd1fa AS |
362 | dry run: no |
363 | file: (not set) | |
364 | EOF | |
365 | ||
366 | test_expect_success 'negation of OPT_NONEG flags is not ambiguous' ' | |
b02e7d5d | 367 | GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \ |
2f17c78c | 368 | test-tool parse-options --no-ambig >output 2>output.err && |
ca8d148d | 369 | test_must_be_empty output.err && |
6bbfd1fa AS |
370 | test_cmp expect output |
371 | ' | |
372 | ||
8425b7ea | 373 | cat >>expect <<\EOF |
c8ba1639 JK |
374 | list: foo |
375 | list: bar | |
376 | list: baz | |
377 | EOF | |
378 | test_expect_success '--list keeps list of strings' ' | |
2f17c78c | 379 | test-tool parse-options --list foo --list=bar --list=baz >output && |
c8ba1639 JK |
380 | test_cmp expect output |
381 | ' | |
382 | ||
383 | test_expect_success '--no-list resets list' ' | |
2f17c78c | 384 | test-tool parse-options --list=other --list=irrelevant --list=options \ |
c8ba1639 JK |
385 | --no-list --list=foo --list=bar --list=baz >output && |
386 | test_cmp expect output | |
387 | ' | |
388 | ||
7d177152 | 389 | test_expect_success 'multiple quiet levels' ' |
2f17c78c | 390 | test-tool parse-options --expect="quiet: 3" -q -q -q |
7d177152 PB |
391 | ' |
392 | ||
7d177152 | 393 | test_expect_success 'multiple verbose levels' ' |
2f17c78c | 394 | test-tool parse-options --expect="verbose: 3" -v -v -v |
7d177152 PB |
395 | ' |
396 | ||
7d177152 | 397 | test_expect_success '--no-quiet sets --quiet to 0' ' |
2f17c78c | 398 | test-tool parse-options --expect="quiet: 0" --no-quiet |
7d177152 PB |
399 | ' |
400 | ||
7d177152 | 401 | test_expect_success '--no-quiet resets multiple -q to 0' ' |
2f17c78c | 402 | test-tool parse-options --expect="quiet: 0" -q -q -q --no-quiet |
7d177152 PB |
403 | ' |
404 | ||
7d177152 | 405 | test_expect_success '--no-verbose sets verbose to 0' ' |
2f17c78c | 406 | test-tool parse-options --expect="verbose: 0" --no-verbose |
7d177152 PB |
407 | ' |
408 | ||
7d177152 | 409 | test_expect_success '--no-verbose resets multiple verbose to 0' ' |
2f17c78c | 410 | test-tool parse-options --expect="verbose: 0" -v -v -v --no-verbose |
7d177152 PB |
411 | ' |
412 | ||
b02e7d5d JS |
413 | test_expect_success 'GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS works' ' |
414 | GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \ | |
415 | test-tool parse-options --ye && | |
416 | test_must_fail env GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=true \ | |
417 | test-tool parse-options --ye | |
418 | ' | |
419 | ||
51b4594b JK |
420 | test_expect_success '--end-of-options treats remainder as args' ' |
421 | test-tool parse-options \ | |
422 | --expect="verbose: -1" \ | |
423 | --expect="arg 00: --verbose" \ | |
424 | --end-of-options --verbose | |
425 | ' | |
426 | ||
beb47437 | 427 | test_done |