]> git.ipfire.org Git - thirdparty/git.git/blame - t/t1502-rev-parse-parseopt.sh
The third batch
[thirdparty/git.git] / t / t1502-rev-parse-parseopt.sh
CommitLineData
e1033436
JS
1#!/bin/sh
2
3test_description='test git rev-parse --parseopt'
4. ./test-lib.sh
5
d5dc68f7
RS
6check_invalid_long_option () {
7 spec="$1"
8 opt="$2"
9 test_expect_success "test --parseopt invalid switch $opt help output for $spec" '
10 {
11 cat <<-\EOF &&
12 error: unknown option `'${opt#--}\''
13 EOF
14 sed -e 1d -e \$d <"$TEST_DIRECTORY/t1502/$spec.help"
15 } >expect &&
16 test_expect_code 129 git rev-parse --parseopt -- $opt \
17 2>output <"$TEST_DIRECTORY/t1502/$spec" &&
18 test_cmp expect output
19 '
20}
21
2d893dff
IB
22test_expect_success 'setup optionspec' '
23 sed -e "s/^|//" >optionspec <<\EOF
24|some-command [options] <args>...
25|
26|some-command does foo and bar!
27|--
aa43619b 28|h,help! show the help
2d893dff
IB
29|
30|foo some nifty option --foo
31|bar= some cool option --bar with an argument
32|b,baz a short and long option
33|
34| An option group Header
35|C? option C with an optional argument
36|d,data? short and long option with an optional argument
37|
38| Argument hints
39|B=arg short option required argument
40|bar2=arg long option required argument
41|e,fuz=with-space short and long option required argument
42|s?some short option optional argument
43|long?data long option optional argument
44|g,fluf?path short and long option optional argument
45|longest=very-long-argument-hint a very long argument hint
46|pair=key=value with an equals sign in the hint
f221861e
BC
47|aswitch help te=t contains? fl*g characters!`
48|bswitch=hint hint has trailing tab character
49|cswitch switch has trailing tab character
2d893dff
IB
50|short-hint=a with a one symbol hint
51|
52|Extras
53|extra1 line above used to cause a segfault but no longer does
54EOF
55'
56
c97ee171
BC
57test_expect_success 'setup optionspec-no-switches' '
58 sed -e "s/^|//" >optionspec_no_switches <<\EOF
59|some-command [options] <args>...
60|
61|some-command does foo and bar!
62|--
63EOF
64'
65
66test_expect_success 'setup optionspec-only-hidden-switches' '
67 sed -e "s/^|//" >optionspec_only_hidden_switches <<\EOF
68|some-command [options] <args>...
69|
70|some-command does foo and bar!
71|--
72|hidden1* A hidden switch
73EOF
74'
75
33e75122 76test_expect_success 'test --parseopt help output' '
2b5ec018 77 test_expect_code 129 git rev-parse --parseopt -- -h > output < optionspec &&
8dcb4902 78 test_cmp "$TEST_DIRECTORY/t1502/optionspec.help" output
e1033436
JS
79'
80
a6304fa4 81test_expect_success 'test --parseopt help output no switches' '
c97ee171
BC
82 sed -e "s/^|//" >expect <<\END_EXPECT &&
83|cat <<\EOF
84|usage: some-command [options] <args>...
85|
86| some-command does foo and bar!
87|
88|EOF
89END_EXPECT
90 test_expect_code 129 git rev-parse --parseopt -- -h > output < optionspec_no_switches &&
1108cea7 91 test_cmp expect output
c97ee171
BC
92'
93
a6304fa4 94test_expect_success 'test --parseopt help output hidden switches' '
c97ee171
BC
95 sed -e "s/^|//" >expect <<\END_EXPECT &&
96|cat <<\EOF
97|usage: some-command [options] <args>...
98|
99| some-command does foo and bar!
100|
101|EOF
102END_EXPECT
103 test_expect_code 129 git rev-parse --parseopt -- -h > output < optionspec_only_hidden_switches &&
1108cea7 104 test_cmp expect output
c97ee171
BC
105'
106
107test_expect_success 'test --parseopt help-all output hidden switches' '
108 sed -e "s/^|//" >expect <<\END_EXPECT &&
109|cat <<\EOF
110|usage: some-command [options] <args>...
111|
112| some-command does foo and bar!
113|
e8e5d294 114| --[no-]hidden1 A hidden switch
c97ee171
BC
115|
116|EOF
117END_EXPECT
118 test_expect_code 129 git rev-parse --parseopt -- --help-all > output < optionspec_only_hidden_switches &&
1108cea7 119 test_cmp expect output
c97ee171
BC
120'
121
1a9bf1e1 122test_expect_success 'test --parseopt invalid switch help output' '
8dcb4902
RS
123 {
124 cat <<-\EOF &&
125 error: unknown option `does-not-exist'\''
126 EOF
127 sed -e 1d -e \$d <"$TEST_DIRECTORY/t1502/optionspec.help"
128 } >expect &&
c97ee171 129 test_expect_code 129 git rev-parse --parseopt -- --does-not-exist 1>/dev/null 2>output < optionspec &&
1108cea7 130 test_cmp expect output
c97ee171
BC
131'
132
2d893dff
IB
133test_expect_success 'setup expect.1' "
134 cat > expect <<EOF
f221861e 135set -- --foo --bar 'ham' -b --aswitch -- 'arg'
824af25a 136EOF
2d893dff 137"
824af25a 138
28a8d0f7 139test_expect_success 'test --parseopt' '
f221861e 140 git rev-parse --parseopt -- --foo --bar=ham --baz --aswitch arg < optionspec > output &&
824af25a
UKK
141 test_cmp expect output
142'
143
28a8d0f7 144test_expect_success 'test --parseopt with mixed options and arguments' '
f221861e 145 git rev-parse --parseopt -- --foo arg --bar=ham --baz --aswitch < optionspec > output &&
824af25a
UKK
146 test_cmp expect output
147'
148
2d893dff
IB
149test_expect_success 'setup expect.2' "
150 cat > expect <<EOF
824af25a
UKK
151set -- --foo -- 'arg' '--bar=ham'
152EOF
2d893dff 153"
824af25a
UKK
154
155test_expect_success 'test --parseopt with --' '
156 git rev-parse --parseopt -- --foo -- arg --bar=ham < optionspec > output &&
157 test_cmp expect output
158'
159
6e0800ef
UKK
160test_expect_success 'test --parseopt --stop-at-non-option' '
161 git rev-parse --parseopt --stop-at-non-option -- --foo arg --bar=ham < optionspec > output &&
162 test_cmp expect output
163'
164
2d893dff
IB
165test_expect_success 'setup expect.3' "
166 cat > expect <<EOF
824af25a
UKK
167set -- --foo -- '--' 'arg' '--bar=ham'
168EOF
2d893dff 169"
824af25a
UKK
170
171test_expect_success 'test --parseopt --keep-dashdash' '
172 git rev-parse --parseopt --keep-dashdash -- --foo -- arg --bar=ham < optionspec > output &&
173 test_cmp expect output
174'
175
2d893dff
IB
176test_expect_success 'setup expect.4' "
177 cat >expect <<EOF
29981380
UKK
178set -- --foo -- '--' 'arg' '--spam=ham'
179EOF
2d893dff 180"
29981380
UKK
181
182test_expect_success 'test --parseopt --keep-dashdash --stop-at-non-option with --' '
183 git rev-parse --parseopt --keep-dashdash --stop-at-non-option -- --foo -- arg --spam=ham <optionspec >output &&
184 test_cmp expect output
185'
186
2d893dff
IB
187test_expect_success 'setup expect.5' "
188 cat > expect <<EOF
29981380
UKK
189set -- --foo -- 'arg' '--spam=ham'
190EOF
2d893dff 191"
29981380
UKK
192
193test_expect_success 'test --parseopt --keep-dashdash --stop-at-non-option without --' '
194 git rev-parse --parseopt --keep-dashdash --stop-at-non-option -- --foo arg --spam=ham <optionspec >output &&
195 test_cmp expect output
196'
197
2d893dff
IB
198test_expect_success 'setup expect.6' "
199 cat > expect <<EOF
f8c87212
NV
200set -- --foo --bar='z' --baz -C'Z' --data='A' -- 'arg'
201EOF
2d893dff 202"
f8c87212
NV
203
204test_expect_success 'test --parseopt --stuck-long' '
205 git rev-parse --parseopt --stuck-long -- --foo --bar=z -b arg -CZ -dA <optionspec >output &&
206 test_cmp expect output
207'
208
2d893dff
IB
209test_expect_success 'setup expect.7' "
210 cat > expect <<EOF
f8c87212
NV
211set -- --data='' -C --baz -- 'arg'
212EOF
2d893dff 213"
f8c87212
NV
214
215test_expect_success 'test --parseopt --stuck-long and empty optional argument' '
216 git rev-parse --parseopt --stuck-long -- --data= arg -C -b <optionspec >output &&
217 test_cmp expect output
218'
219
2d893dff
IB
220test_expect_success 'setup expect.8' "
221 cat > expect <<EOF
f8c87212
NV
222set -- --data --baz -- 'arg'
223EOF
2d893dff 224"
f8c87212
NV
225
226test_expect_success 'test --parseopt --stuck-long and long option with unset optional argument' '
227 git rev-parse --parseopt --stuck-long -- --data arg -b <optionspec >output &&
228 test_cmp expect output
229'
230
231test_expect_success 'test --parseopt --stuck-long and short option with unset optional argument' '
232 git rev-parse --parseopt --stuck-long -- -d arg -b <optionspec >output &&
233 test_cmp expect output
234'
235
84122ecd
ÆAB
236test_expect_success 'test --parseopt help output: "wrapped" options normal "or:" lines' '
237 sed -e "s/^|//" >spec <<-\EOF &&
238 |cmd [--some-option]
239 | [--another-option]
240 |cmd [--yet-another-option]
241 |--
aa43619b 242 |h,help! show the help
84122ecd
ÆAB
243 EOF
244
245 sed -e "s/^|//" >expect <<-\END_EXPECT &&
246 |cat <<\EOF
247 |usage: cmd [--some-option]
248 | or: [--another-option]
249 | or: cmd [--yet-another-option]
250 |
251 | -h, --help show the help
252 |
253 |EOF
254 END_EXPECT
255
dca675c6 256 test_must_fail git rev-parse --parseopt -- -h <spec >actual &&
84122ecd
ÆAB
257 test_cmp expect actual
258'
259
f20b9c36
ØW
260test_expect_success 'test --parseopt invalid opt-spec' '
261 test_write_lines x -- "=, x" >spec &&
262 echo "fatal: missing opt-spec before option flags" >expect &&
dca675c6 263 test_must_fail git rev-parse --parseopt -- <spec 2>err &&
f20b9c36
ØW
264 test_cmp expect err
265'
266
84122ecd
ÆAB
267test_expect_success 'test --parseopt help output: multi-line blurb after empty line' '
268 sed -e "s/^|//" >spec <<-\EOF &&
269 |cmd [--some-option]
270 | [--another-option]
271 |
272 |multi
273 |line
274 |blurb
275 |--
aa43619b 276 |h,help! show the help
84122ecd
ÆAB
277 EOF
278
279 sed -e "s/^|//" >expect <<-\END_EXPECT &&
280 |cat <<\EOF
281 |usage: cmd [--some-option]
282 | or: [--another-option]
283 |
284 | multi
285 | line
286 | blurb
287 |
288 | -h, --help show the help
289 |
290 |EOF
291 END_EXPECT
292
dca675c6 293 test_must_fail git rev-parse --parseopt -- -h <spec >actual &&
84122ecd
ÆAB
294 test_cmp expect actual
295'
296
d5dc68f7
RS
297test_expect_success 'test --parseopt help output for optionspec-neg' '
298 test_expect_code 129 git rev-parse --parseopt -- \
299 -h >output <"$TEST_DIRECTORY/t1502/optionspec-neg" &&
300 test_cmp "$TEST_DIRECTORY/t1502/optionspec-neg.help" output
301'
302
303test_expect_success 'test --parseopt valid options for optionspec-neg' '
304 cat >expect <<-\EOF &&
305 set -- --foo --no-foo --no-bar --positive-only --no-negative --
306 EOF
307 git rev-parse --parseopt -- <"$TEST_DIRECTORY/t1502/optionspec-neg" >output \
308 --foo --no-foo --no-bar --positive-only --no-negative &&
309 test_cmp expect output
310'
311
312test_expect_success 'test --parseopt positivated option for optionspec-neg' '
313 cat >expect <<-\EOF &&
314 set -- --no-no-bar --no-no-bar --
315 EOF
316 git rev-parse --parseopt -- <"$TEST_DIRECTORY/t1502/optionspec-neg" >output \
317 --no-no-bar --bar &&
318 test_cmp expect output
319'
320
321check_invalid_long_option optionspec-neg --no-positive-only
322check_invalid_long_option optionspec-neg --negative
323check_invalid_long_option optionspec-neg --no-no-negative
324
0d8a3097
RS
325test_expect_success 'ambiguous: --no matches both --noble and --no-noble' '
326 cat >spec <<-\EOF &&
327 some-command [options]
328 --
329 noble The feudal switch.
330 EOF
331 test_expect_code 129 env GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
332 git rev-parse --parseopt -- <spec 2>err --no &&
333 grep "error: ambiguous option: no (could be --noble or --no-noble)" err
334'
335
e1033436 336test_done