]>
Commit | Line | Data |
---|---|---|
e1033436 JS |
1 | #!/bin/sh |
2 | ||
3 | test_description='test git rev-parse --parseopt' | |
4 | . ./test-lib.sh | |
5 | ||
d5dc68f7 RS |
6 | check_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 |
22 | test_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 | |
54 | EOF | |
55 | ' | |
56 | ||
c97ee171 BC |
57 | test_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 | |-- | |
63 | EOF | |
64 | ' | |
65 | ||
66 | test_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 | |
73 | EOF | |
74 | ' | |
75 | ||
33e75122 | 76 | test_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 | 81 | test_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 | |
89 | END_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 | 94 | test_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 | |
102 | END_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 | ||
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>... | |
111 | | | |
112 | | some-command does foo and bar! | |
113 | | | |
e8e5d294 | 114 | | --[no-]hidden1 A hidden switch |
c97ee171 BC |
115 | | |
116 | |EOF | |
117 | END_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 | 122 | test_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 |
133 | test_expect_success 'setup expect.1' " |
134 | cat > expect <<EOF | |
f221861e | 135 | set -- --foo --bar 'ham' -b --aswitch -- 'arg' |
824af25a | 136 | EOF |
2d893dff | 137 | " |
824af25a | 138 | |
28a8d0f7 | 139 | test_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 | 144 | test_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 |
149 | test_expect_success 'setup expect.2' " |
150 | cat > expect <<EOF | |
824af25a UKK |
151 | set -- --foo -- 'arg' '--bar=ham' |
152 | EOF | |
2d893dff | 153 | " |
824af25a UKK |
154 | |
155 | test_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 |
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 | |
163 | ' | |
164 | ||
2d893dff IB |
165 | test_expect_success 'setup expect.3' " |
166 | cat > expect <<EOF | |
824af25a UKK |
167 | set -- --foo -- '--' 'arg' '--bar=ham' |
168 | EOF | |
2d893dff | 169 | " |
824af25a UKK |
170 | |
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 | |
174 | ' | |
175 | ||
2d893dff IB |
176 | test_expect_success 'setup expect.4' " |
177 | cat >expect <<EOF | |
29981380 UKK |
178 | set -- --foo -- '--' 'arg' '--spam=ham' |
179 | EOF | |
2d893dff | 180 | " |
29981380 UKK |
181 | |
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 | |
185 | ' | |
186 | ||
2d893dff IB |
187 | test_expect_success 'setup expect.5' " |
188 | cat > expect <<EOF | |
29981380 UKK |
189 | set -- --foo -- 'arg' '--spam=ham' |
190 | EOF | |
2d893dff | 191 | " |
29981380 UKK |
192 | |
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 | |
196 | ' | |
197 | ||
2d893dff IB |
198 | test_expect_success 'setup expect.6' " |
199 | cat > expect <<EOF | |
f8c87212 NV |
200 | set -- --foo --bar='z' --baz -C'Z' --data='A' -- 'arg' |
201 | EOF | |
2d893dff | 202 | " |
f8c87212 NV |
203 | |
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 | |
207 | ' | |
208 | ||
2d893dff IB |
209 | test_expect_success 'setup expect.7' " |
210 | cat > expect <<EOF | |
f8c87212 NV |
211 | set -- --data='' -C --baz -- 'arg' |
212 | EOF | |
2d893dff | 213 | " |
f8c87212 NV |
214 | |
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 | |
218 | ' | |
219 | ||
2d893dff IB |
220 | test_expect_success 'setup expect.8' " |
221 | cat > expect <<EOF | |
f8c87212 NV |
222 | set -- --data --baz -- 'arg' |
223 | EOF | |
2d893dff | 224 | " |
f8c87212 NV |
225 | |
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 | |
229 | ' | |
230 | ||
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 | |
234 | ' | |
235 | ||
84122ecd ÆAB |
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] | |
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 |
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 && | |
dca675c6 | 263 | test_must_fail git rev-parse --parseopt -- <spec 2>err && |
f20b9c36 ØW |
264 | test_cmp expect err |
265 | ' | |
266 | ||
84122ecd ÆAB |
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] | |
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 |
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 | |
301 | ' | |
302 | ||
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 -- | |
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 | ||
312 | test_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 | ||
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 | |
324 | ||
0d8a3097 RS |
325 | test_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 | 336 | test_done |