]> git.ipfire.org Git - thirdparty/git.git/blob - t/t0040-parse-options.sh
The sixth batch
[thirdparty/git.git] / t / t0040-parse-options.sh
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
10 cat >expect <<\EOF
11 usage: test-tool parse-options <options>
12
13 A helper function for the parse-options API.
14
15 --yes get a boolean
16 -D, --no-doubt begins with 'no-'
17 -B, --no-fear be brave
18 -b, --boolean increment by one
19 -4, --or4 bitwise-or boolean with ...0100
20 --neg-or4 same as --no-or4
21
22 -i, --integer <n> get a integer
23 -j <n> get a integer, too
24 -m, --magnitude <n> get a magnitude
25 --set23 set integer to 23
26 --mode1 set integer to 1 (cmdmode option)
27 --mode2 set integer to 2 (cmdmode option)
28 -L, --length <str> get length of <str>
29 -F, --file <file> set file to <file>
30
31 String options
32 -s, --string <string>
33 get a string
34 --string2 <str> get another string
35 --st <st> get another string (pervert ordering)
36 -o <str> get another string
37 --list <str> add str to list
38
39 Magic arguments
40 --quux means --quux
41 -NUM set integer to NUM
42 + same as -b
43 --ambiguous positive ambiguity
44 --no-ambiguous negative ambiguity
45
46 Standard options
47 --abbrev[=<n>] use <n> digits to display SHA-1s
48 -v, --verbose be verbose
49 -n, --dry-run dry run
50 -q, --quiet be quiet
51 --expect <string> expected output in the variable dump
52
53 Alias
54 -A, --alias-source <string>
55 get a string
56 -Z, --alias-target <string>
57 alias of --alias-source
58
59 EOF
60
61 test_expect_success 'test help' '
62 test_must_fail test-tool parse-options -h >output 2>output.err &&
63 test_must_be_empty output.err &&
64 test_i18ncmp expect output
65 '
66
67 mv expect expect.err
68
69 check () {
70 what="$1" &&
71 shift &&
72 expect="$1" &&
73 shift &&
74 test-tool parse-options --expect="$what $expect" "$@"
75 }
76
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 &&
85 test_must_fail test-tool parse-options $* >output 2>output.err &&
86 test_must_be_empty output &&
87 test_i18ncmp expect output.err
88 }
89
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
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'
104
105 test_expect_success 'OPT_BOOL() positivation' 'check boolean: 0 -D --doubt'
106
107 test_expect_success 'OPT_INT() negative' 'check integer: -2345 -i -2345'
108
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
129 cat >expect <<\EOF
130 boolean: 2
131 integer: 1729
132 magnitude: 16384
133 timestamp: 0
134 string: 123
135 abbrev: 7
136 verbose: 2
137 quiet: 0
138 dry run: yes
139 file: prefix/my.file
140 EOF
141
142 test_expect_success 'short options' '
143 test-tool parse-options -s123 -b -i 1729 -m 16k -b -vv -n -F my.file \
144 >output 2>output.err &&
145 test_cmp expect output &&
146 test_must_be_empty output.err
147 '
148
149 cat >expect <<\EOF
150 boolean: 2
151 integer: 1729
152 magnitude: 16384
153 timestamp: 0
154 string: 321
155 abbrev: 10
156 verbose: 2
157 quiet: 0
158 dry run: no
159 file: prefix/fi.le
160 EOF
161
162 test_expect_success 'long options' '
163 test-tool parse-options --boolean --integer 1729 --magnitude 16k \
164 --boolean --string2=321 --verbose --verbose --no-dry-run \
165 --abbrev=10 --file fi.le --obsolete \
166 >output 2>output.err &&
167 test_must_be_empty output.err &&
168 test_cmp expect output
169 '
170
171 test_expect_success 'missing required value' '
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
175 '
176
177 cat >expect <<\EOF
178 boolean: 1
179 integer: 13
180 magnitude: 0
181 timestamp: 0
182 string: 123
183 abbrev: 7
184 verbose: -1
185 quiet: 0
186 dry run: no
187 file: (not set)
188 arg 00: a1
189 arg 01: b1
190 arg 02: --boolean
191 EOF
192
193 test_expect_success 'intermingled arguments' '
194 test-tool parse-options a1 --string 123 b1 --boolean -j 13 -- --boolean \
195 >output 2>output.err &&
196 test_must_be_empty output.err &&
197 test_cmp expect output
198 '
199
200 cat >expect <<\EOF
201 boolean: 0
202 integer: 2
203 magnitude: 0
204 timestamp: 0
205 string: (not set)
206 abbrev: 7
207 verbose: -1
208 quiet: 0
209 dry run: no
210 file: (not set)
211 EOF
212
213 test_expect_success 'unambiguously abbreviated option' '
214 GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
215 test-tool parse-options --int 2 --boolean --no-bo >output 2>output.err &&
216 test_must_be_empty output.err &&
217 test_cmp expect output
218 '
219
220 test_expect_success 'unambiguously abbreviated option with "="' '
221 GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
222 test-tool parse-options --expect="integer: 2" --int=2
223 '
224
225 test_expect_success 'ambiguously abbreviated option' '
226 test_expect_code 129 env GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
227 test-tool parse-options --strin 123
228 '
229
230 test_expect_success 'non ambiguous option (after two options it abbreviates)' '
231 GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
232 test-tool parse-options --expect="string: 123" --st 123
233 '
234
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
246 cat >typo.err <<\EOF
247 error: did you mean `--boolean` (with two dashes)?
248 EOF
249
250 test_expect_success 'detect possible typos' '
251 test_must_fail test-tool parse-options -boolean >output 2>output.err &&
252 test_must_be_empty output &&
253 test_i18ncmp typo.err output.err
254 '
255
256 cat >typo.err <<\EOF
257 error: did you mean `--ambiguous` (with two dashes)?
258 EOF
259
260 test_expect_success 'detect possible typos' '
261 test_must_fail test-tool parse-options -ambiguous >output 2>output.err &&
262 test_must_be_empty output &&
263 test_i18ncmp typo.err output.err
264 '
265
266 test_expect_success 'keep some options as arguments' '
267 test-tool parse-options --expect="arg 00: --quux" --quux
268 '
269
270 cat >expect <<\EOF
271 Callback: "four", 0
272 boolean: 5
273 integer: 4
274 magnitude: 0
275 timestamp: 0
276 string: (not set)
277 abbrev: 7
278 verbose: -1
279 quiet: 0
280 dry run: no
281 file: (not set)
282 EOF
283
284 test_expect_success 'OPT_CALLBACK() and OPT_BIT() work' '
285 test-tool parse-options --length=four -b -4 >output 2>output.err &&
286 test_must_be_empty output.err &&
287 test_cmp expect output
288 '
289
290 test_expect_success 'OPT_CALLBACK() and callback errors work' '
291 test_must_fail test-tool parse-options --no-length >output 2>output.err &&
292 test_must_be_empty output &&
293 test_must_be_empty output.err
294 '
295
296 cat >expect <<\EOF
297 boolean: 1
298 integer: 23
299 magnitude: 0
300 timestamp: 0
301 string: (not set)
302 abbrev: 7
303 verbose: -1
304 quiet: 0
305 dry run: no
306 file: (not set)
307 EOF
308
309 test_expect_success 'OPT_BIT() and OPT_SET_INT() work' '
310 test-tool parse-options --set23 -bbbbb --no-or4 >output 2>output.err &&
311 test_must_be_empty output.err &&
312 test_cmp expect output
313 '
314
315 test_expect_success 'OPT_NEGBIT() and OPT_SET_INT() work' '
316 test-tool parse-options --set23 -bbbbb --neg-or4 >output 2>output.err &&
317 test_must_be_empty output.err &&
318 test_cmp expect output
319 '
320
321 test_expect_success 'OPT_BIT() works' '
322 test-tool parse-options --expect="boolean: 6" -bb --or4
323 '
324
325 test_expect_success 'OPT_NEGBIT() works' '
326 test-tool parse-options --expect="boolean: 6" -bb --no-neg-or4
327 '
328
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
345 test_expect_success 'OPT_COUNTUP() with PARSE_OPT_NODASH works' '
346 test-tool parse-options --expect="boolean: 6" + + + + + +
347 '
348
349 test_expect_success 'OPT_NUMBER_CALLBACK() works' '
350 test-tool parse-options --expect="integer: 12345" -12345
351 '
352
353 cat >expect <<\EOF
354 boolean: 0
355 integer: 0
356 magnitude: 0
357 timestamp: 0
358 string: (not set)
359 abbrev: 7
360 verbose: -1
361 quiet: 0
362 dry run: no
363 file: (not set)
364 EOF
365
366 test_expect_success 'negation of OPT_NONEG flags is not ambiguous' '
367 GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
368 test-tool parse-options --no-ambig >output 2>output.err &&
369 test_must_be_empty output.err &&
370 test_cmp expect output
371 '
372
373 cat >>expect <<\EOF
374 list: foo
375 list: bar
376 list: baz
377 EOF
378 test_expect_success '--list keeps list of strings' '
379 test-tool parse-options --list foo --list=bar --list=baz >output &&
380 test_cmp expect output
381 '
382
383 test_expect_success '--no-list resets list' '
384 test-tool parse-options --list=other --list=irrelevant --list=options \
385 --no-list --list=foo --list=bar --list=baz >output &&
386 test_cmp expect output
387 '
388
389 test_expect_success 'multiple quiet levels' '
390 test-tool parse-options --expect="quiet: 3" -q -q -q
391 '
392
393 test_expect_success 'multiple verbose levels' '
394 test-tool parse-options --expect="verbose: 3" -v -v -v
395 '
396
397 test_expect_success '--no-quiet sets --quiet to 0' '
398 test-tool parse-options --expect="quiet: 0" --no-quiet
399 '
400
401 test_expect_success '--no-quiet resets multiple -q to 0' '
402 test-tool parse-options --expect="quiet: 0" -q -q -q --no-quiet
403 '
404
405 test_expect_success '--no-verbose sets verbose to 0' '
406 test-tool parse-options --expect="verbose: 0" --no-verbose
407 '
408
409 test_expect_success '--no-verbose resets multiple verbose to 0' '
410 test-tool parse-options --expect="verbose: 0" -v -v -v --no-verbose
411 '
412
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
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
427 test_done