Sync with 2.44.1
[git/gitster.git] / t / t1502-rev-parse-parseopt.sh
blobb754b9fd74bd17e7a969026a93cd6e70c8771cb8
1 #!/bin/sh
3 test_description='test git rev-parse --parseopt'
4 . ./test-lib.sh
6 check_invalid_long_option () {
7 spec="$1"
8 opt="$2"
9 test_expect_success "test --parseopt invalid switch $opt help output for $spec" '
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
22 test_expect_success 'setup optionspec' '
23 sed -e "s/^|//" >optionspec <<\EOF
24 |some-command [options] <args>...
26 |some-command does foo and bar!
27 |--
28 |h,help! show the help
30 |foo some nifty option --foo
31 |bar= some cool option --bar with an argument
32 |b,baz a short and long option
34 | An option group Header
35 |C? option C with an optional argument
36 |d,data? short and long option with an optional argument
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
47 |aswitch help te=t contains? fl*g characters!`
48 |bswitch=hint hint has trailing tab character
49 |cswitch switch has trailing tab character
50 |short-hint=a with a one symbol hint
52 |Extras
53 |extra1 line above used to cause a segfault but no longer does
54 EOF
57 test_expect_success 'setup optionspec-no-switches' '
58 sed -e "s/^|//" >optionspec_no_switches <<\EOF
59 |some-command [options] <args>...
61 |some-command does foo and bar!
62 |--
63 EOF
66 test_expect_success 'setup optionspec-only-hidden-switches' '
67 sed -e "s/^|//" >optionspec_only_hidden_switches <<\EOF
68 |some-command [options] <args>...
70 |some-command does foo and bar!
71 |--
72 |hidden1* A hidden switch
73 EOF
76 test_expect_success 'test --parseopt help output' '
77 test_expect_code 129 git rev-parse --parseopt -- -h > output < optionspec &&
78 test_cmp "$TEST_DIRECTORY/t1502/optionspec.help" output
81 test_expect_success 'test --parseopt help output no switches' '
82 sed -e "s/^|//" >expect <<\END_EXPECT &&
83 |cat <<\EOF
84 |usage: some-command [options] <args>...
86 | some-command does foo and bar!
88 |EOF
89 END_EXPECT
90 test_expect_code 129 git rev-parse --parseopt -- -h > output < optionspec_no_switches &&
91 test_cmp expect output
94 test_expect_success 'test --parseopt help output hidden switches' '
95 sed -e "s/^|//" >expect <<\END_EXPECT &&
96 |cat <<\EOF
97 |usage: some-command [options] <args>...
99 | some-command does foo and bar!
101 |EOF
102 END_EXPECT
103 test_expect_code 129 git rev-parse --parseopt -- -h > output < optionspec_only_hidden_switches &&
104 test_cmp expect output
107 test_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>...
112 | some-command does foo and bar!
114 | --[no-]hidden1 A hidden switch
116 |EOF
117 END_EXPECT
118 test_expect_code 129 git rev-parse --parseopt -- --help-all > output < optionspec_only_hidden_switches &&
119 test_cmp expect output
122 test_expect_success 'test --parseopt invalid switch help output' '
124 cat <<-\EOF &&
125 error: unknown option `does-not-exist'\''
127 sed -e 1d -e \$d <"$TEST_DIRECTORY/t1502/optionspec.help"
128 } >expect &&
129 test_expect_code 129 git rev-parse --parseopt -- --does-not-exist 1>/dev/null 2>output < optionspec &&
130 test_cmp expect output
133 test_expect_success 'setup expect.1' "
134 cat > expect <<EOF
135 set -- --foo --bar 'ham' -b --aswitch -- 'arg'
139 test_expect_success 'test --parseopt' '
140 git rev-parse --parseopt -- --foo --bar=ham --baz --aswitch arg < optionspec > output &&
141 test_cmp expect output
144 test_expect_success 'test --parseopt with mixed options and arguments' '
145 git rev-parse --parseopt -- --foo arg --bar=ham --baz --aswitch < optionspec > output &&
146 test_cmp expect output
149 test_expect_success 'setup expect.2' "
150 cat > expect <<EOF
151 set -- --foo -- 'arg' '--bar=ham'
155 test_expect_success 'test --parseopt with --' '
156 git rev-parse --parseopt -- --foo -- arg --bar=ham < optionspec > output &&
157 test_cmp expect output
160 test_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
165 test_expect_success 'setup expect.3' "
166 cat > expect <<EOF
167 set -- --foo -- '--' 'arg' '--bar=ham'
171 test_expect_success 'test --parseopt --keep-dashdash' '
172 git rev-parse --parseopt --keep-dashdash -- --foo -- arg --bar=ham < optionspec > output &&
173 test_cmp expect output
176 test_expect_success 'setup expect.4' "
177 cat >expect <<EOF
178 set -- --foo -- '--' 'arg' '--spam=ham'
182 test_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
187 test_expect_success 'setup expect.5' "
188 cat > expect <<EOF
189 set -- --foo -- 'arg' '--spam=ham'
193 test_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
198 test_expect_success 'setup expect.6' "
199 cat > expect <<EOF
200 set -- --foo --bar='z' --baz -C'Z' --data='A' -- 'arg'
204 test_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
209 test_expect_success 'setup expect.7' "
210 cat > expect <<EOF
211 set -- --data='' -C --baz -- 'arg'
215 test_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
220 test_expect_success 'setup expect.8' "
221 cat > expect <<EOF
222 set -- --data --baz -- 'arg'
226 test_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
231 test_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
236 test_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]
242 |h,help! show the help
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]
251 | -h, --help show the help
253 |EOF
254 END_EXPECT
256 test_must_fail git rev-parse --parseopt -- -h <spec >actual &&
257 test_cmp expect actual
260 test_expect_success 'test --parseopt invalid opt-spec' '
261 test_write_lines x -- "=, x" >spec &&
262 echo "fatal: missing opt-spec before option flags" >expect &&
263 test_must_fail git rev-parse --parseopt -- <spec 2>err &&
264 test_cmp expect err
267 test_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]
272 |multi
273 |line
274 |blurb
276 |h,help! show the help
279 sed -e "s/^|//" >expect <<-\END_EXPECT &&
280 |cat <<\EOF
281 |usage: cmd [--some-option]
282 | or: [--another-option]
284 | multi
285 | line
286 | blurb
288 | -h, --help show the help
290 |EOF
291 END_EXPECT
293 test_must_fail git rev-parse --parseopt -- -h <spec >actual &&
294 test_cmp expect actual
297 test_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
303 test_expect_success 'test --parseopt valid options for optionspec-neg' '
304 cat >expect <<-\EOF &&
305 set -- --foo --no-foo --no-bar --positive-only --no-negative --
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
312 test_expect_success 'test --parseopt positivated option for optionspec-neg' '
313 cat >expect <<-\EOF &&
314 set -- --no-no-bar --no-no-bar --
316 git rev-parse --parseopt -- <"$TEST_DIRECTORY/t1502/optionspec-neg" >output \
317 --no-no-bar --bar &&
318 test_cmp expect output
321 check_invalid_long_option optionspec-neg --no-positive-only
322 check_invalid_long_option optionspec-neg --negative
323 check_invalid_long_option optionspec-neg --no-no-negative
325 test_expect_success 'ambiguous: --no matches both --noble and --no-noble' '
326 cat >spec <<-\EOF &&
327 some-command [options]
329 noble The feudal switch.
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
336 test_done