]> git.ipfire.org Git - thirdparty/git.git/blame - t/t0040-parse-options.sh
Merge branch 'ab/detox-gettext-tests'
[thirdparty/git.git] / t / t0040-parse-options.sh
CommitLineData
beb47437
JS
1#!/bin/sh
2#
3# Copyright (c) 2007 Johannes Schindelin
4#
5
6test_description='our own option parser'
7
8. ./test-lib.sh
9
8425b7ea 10cat >expect <<\EOF
2f17c78c 11usage: 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 31String 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 39Magic 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 46Standard 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
53Alias
54 -A, --alias-source <string>
55 get a string
56 -Z, --alias-target <string>
7c280589 57 alias of --alias-source
5c387428 58
beb47437
JS
59EOF
60
61test_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 &&
1108cea7 64 test_cmp expect output
beb47437
JS
65'
66
9c7304e3
GS
67mv expect expect.err
68
8ca65aeb 69check () {
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
77check_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 &&
1108cea7 87 test_cmp expect output.err
9a001381
JX
88}
89
b9e63ddd
RS
90test_expect_success 'OPT_BOOL() #1' 'check boolean: 1 --yes'
91test_expect_success 'OPT_BOOL() #2' 'check boolean: 1 --no-doubt'
92test_expect_success 'OPT_BOOL() #3' 'check boolean: 1 -D'
93test_expect_success 'OPT_BOOL() #4' 'check boolean: 1 --no-fear'
94test_expect_success 'OPT_BOOL() #5' 'check boolean: 1 -B'
95
96test_expect_success 'OPT_BOOL() is idempotent #1' 'check boolean: 1 --yes --yes'
97test_expect_success 'OPT_BOOL() is idempotent #2' 'check boolean: 1 -DB'
98
99test_expect_success 'OPT_BOOL() negation #1' 'check boolean: 0 -D --no-yes'
100test_expect_success 'OPT_BOOL() negation #2' 'check boolean: 0 -D --no-no-doubt'
101
1a9bf1e1
BC
102test_expect_success 'OPT_BOOL() no negation #1' 'check_unknown_i18n --fear'
103test_expect_success 'OPT_BOOL() no negation #2' 'check_unknown_i18n --no-no-fear'
b9e63ddd 104
0f1930c5 105test_expect_success 'OPT_BOOL() positivation' 'check boolean: 0 -D --doubt'
b9e63ddd 106
81a48cc0
CB
107test_expect_success 'OPT_INT() negative' 'check integer: -2345 -i -2345'
108
2a514ed8
CB
109test_expect_success 'OPT_MAGNITUDE() simple' '
110 check magnitude: 2345678 -m 2345678
111'
112
113test_expect_success 'OPT_MAGNITUDE() kilo' '
114 check magnitude: 239616 -m 234k
115'
116
117test_expect_success 'OPT_MAGNITUDE() mega' '
118 check magnitude: 104857600 -m 100m
119'
120
121test_expect_success 'OPT_MAGNITUDE() giga' '
122 check magnitude: 1073741824 -m 1g
123'
124
125test_expect_success 'OPT_MAGNITUDE() 3giga' '
126 check magnitude: 3221225472 -m 3g
127'
128
8425b7ea 129cat >expect <<\EOF
beb47437
JS
130boolean: 2
131integer: 1729
2a514ed8 132magnitude: 16384
c4aca9cc 133timestamp: 0
beb47437 134string: 123
010a2dac
SB
135abbrev: 7
136verbose: 2
36e6a5ba 137quiet: 0
010a2dac 138dry run: yes
df217ed6 139file: prefix/my.file
beb47437
JS
140EOF
141
142test_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 149cat >expect <<\EOF
beb47437
JS
150boolean: 2
151integer: 1729
2a514ed8 152magnitude: 16384
c4aca9cc 153timestamp: 0
beb47437 154string: 321
010a2dac
SB
155abbrev: 10
156verbose: 2
36e6a5ba 157quiet: 0
010a2dac 158dry run: no
df217ed6 159file: prefix/fi.le
beb47437
JS
160EOF
161
162test_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 171test_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 177cat >expect <<\EOF
beb47437
JS
178boolean: 1
179integer: 13
2a514ed8 180magnitude: 0
c4aca9cc 181timestamp: 0
beb47437 182string: 123
010a2dac 183abbrev: 7
e0070e8b 184verbose: -1
36e6a5ba 185quiet: 0
010a2dac 186dry run: no
df217ed6 187file: (not set)
beb47437
JS
188arg 00: a1
189arg 01: b1
190arg 02: --boolean
191EOF
192
193test_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 200cat >expect <<\EOF
7f275b91
JS
201boolean: 0
202integer: 2
2a514ed8 203magnitude: 0
c4aca9cc 204timestamp: 0
7f275b91 205string: (not set)
010a2dac 206abbrev: 7
e0070e8b 207verbose: -1
36e6a5ba 208quiet: 0
010a2dac 209dry run: no
df217ed6 210file: (not set)
7f275b91
JS
211EOF
212
213test_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
220test_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 225test_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 230test_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
235test_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 246cat >typo.err <<\EOF
395518cf 247error: did you mean `--boolean` (with two dashes)?
3a9f0f41
PH
248EOF
249
250test_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 &&
1108cea7 253 test_cmp typo.err output.err
3a9f0f41
PH
254'
255
8425b7ea 256cat >typo.err <<\EOF
395518cf 257error: did you mean `--ambiguous` (with two dashes)?
38916c5b
RS
258EOF
259
260test_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 &&
1108cea7 263 test_cmp typo.err output.err
38916c5b
RS
264'
265
580d5bff 266test_expect_success 'keep some options as arguments' '
2f17c78c 267 test-tool parse-options --expect="arg 00: --quux" --quux
580d5bff
PH
268'
269
8425b7ea 270cat >expect <<\EOF
010a2dac
SB
271Callback: "four", 0
272boolean: 5
273integer: 4
2a514ed8 274magnitude: 0
c4aca9cc 275timestamp: 0
010a2dac
SB
276string: (not set)
277abbrev: 7
e0070e8b 278verbose: -1
36e6a5ba 279quiet: 0
010a2dac 280dry run: no
df217ed6 281file: (not set)
010a2dac
SB
282EOF
283
284test_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 290test_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 296cat >expect <<\EOF
010a2dac
SB
297boolean: 1
298integer: 23
2a514ed8 299magnitude: 0
c4aca9cc 300timestamp: 0
010a2dac
SB
301string: (not set)
302abbrev: 7
e0070e8b 303verbose: -1
36e6a5ba 304quiet: 0
010a2dac 305dry run: no
df217ed6 306file: (not set)
010a2dac
SB
307EOF
308
309test_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 315test_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 321test_expect_success 'OPT_BIT() works' '
2f17c78c 322 test-tool parse-options --expect="boolean: 6" -bb --or4
2f4b97f9
RS
323'
324
325test_expect_success 'OPT_NEGBIT() works' '
2f17c78c 326 test-tool parse-options --expect="boolean: 6" -bb --no-neg-or4
2f4b97f9 327'
010a2dac 328
62e7a6f7
PB
329test_expect_success 'OPT_CMDMODE() works' '
330 test-tool parse-options --expect="integer: 1" --mode1
331'
332
333test_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
339test_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 345test_expect_success 'OPT_COUNTUP() with PARSE_OPT_NODASH works' '
2f17c78c 346 test-tool parse-options --expect="boolean: 6" + + + + + +
51a9949e
RS
347'
348
e0319ff5 349test_expect_success 'OPT_NUMBER_CALLBACK() works' '
2f17c78c 350 test-tool parse-options --expect="integer: 12345" -12345
e0319ff5
RS
351'
352
8425b7ea 353cat >expect <<\EOF
6bbfd1fa
AS
354boolean: 0
355integer: 0
2a514ed8 356magnitude: 0
6bbfd1fa
AS
357timestamp: 0
358string: (not set)
359abbrev: 7
e0070e8b 360verbose: -1
36e6a5ba 361quiet: 0
6bbfd1fa
AS
362dry run: no
363file: (not set)
364EOF
365
366test_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 373cat >>expect <<\EOF
c8ba1639
JK
374list: foo
375list: bar
376list: baz
377EOF
378test_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
383test_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 389test_expect_success 'multiple quiet levels' '
2f17c78c 390 test-tool parse-options --expect="quiet: 3" -q -q -q
7d177152
PB
391'
392
7d177152 393test_expect_success 'multiple verbose levels' '
2f17c78c 394 test-tool parse-options --expect="verbose: 3" -v -v -v
7d177152
PB
395'
396
7d177152 397test_expect_success '--no-quiet sets --quiet to 0' '
2f17c78c 398 test-tool parse-options --expect="quiet: 0" --no-quiet
7d177152
PB
399'
400
7d177152 401test_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 405test_expect_success '--no-verbose sets verbose to 0' '
2f17c78c 406 test-tool parse-options --expect="verbose: 0" --no-verbose
7d177152
PB
407'
408
7d177152 409test_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
413test_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
420test_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 427test_done