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