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