]> git.ipfire.org Git - thirdparty/git.git/blob - t/t1502-rev-parse-parseopt.sh
date: make "iso-strict" conforming for the UTC timezone
[thirdparty/git.git] / t / t1502-rev-parse-parseopt.sh
1 #!/bin/sh
2
3 test_description='test git rev-parse --parseopt'
4 . ./test-lib.sh
5
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
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 |--
28 |h,help! show the help
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
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
51 |
52 |Extras
53 |extra1 line above used to cause a segfault but no longer does
54 EOF
55 '
56
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
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
79 '
80
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>...
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 &&
91 test_cmp expect output
92 '
93
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>...
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 &&
104 test_cmp expect output
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 |
114 | --[no-]hidden1 A hidden switch
115 |
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
120 '
121
122 test_expect_success 'test --parseopt invalid switch help output' '
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 &&
129 test_expect_code 129 git rev-parse --parseopt -- --does-not-exist 1>/dev/null 2>output < optionspec &&
130 test_cmp expect output
131 '
132
133 test_expect_success 'setup expect.1' "
134 cat > expect <<EOF
135 set -- --foo --bar 'ham' -b --aswitch -- 'arg'
136 EOF
137 "
138
139 test_expect_success 'test --parseopt' '
140 git rev-parse --parseopt -- --foo --bar=ham --baz --aswitch arg < optionspec > output &&
141 test_cmp expect output
142 '
143
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
147 '
148
149 test_expect_success 'setup expect.2' "
150 cat > expect <<EOF
151 set -- --foo -- 'arg' '--bar=ham'
152 EOF
153 "
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
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
165 test_expect_success 'setup expect.3' "
166 cat > expect <<EOF
167 set -- --foo -- '--' 'arg' '--bar=ham'
168 EOF
169 "
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
176 test_expect_success 'setup expect.4' "
177 cat >expect <<EOF
178 set -- --foo -- '--' 'arg' '--spam=ham'
179 EOF
180 "
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
187 test_expect_success 'setup expect.5' "
188 cat > expect <<EOF
189 set -- --foo -- 'arg' '--spam=ham'
190 EOF
191 "
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
198 test_expect_success 'setup expect.6' "
199 cat > expect <<EOF
200 set -- --foo --bar='z' --baz -C'Z' --data='A' -- 'arg'
201 EOF
202 "
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
209 test_expect_success 'setup expect.7' "
210 cat > expect <<EOF
211 set -- --data='' -C --baz -- 'arg'
212 EOF
213 "
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
220 test_expect_success 'setup expect.8' "
221 cat > expect <<EOF
222 set -- --data --baz -- 'arg'
223 EOF
224 "
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
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 |--
242 |h,help! show the help
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
256 test_must_fail git rev-parse --parseopt -- -h <spec >actual &&
257 test_cmp expect actual
258 '
259
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
265 '
266
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 |--
276 |h,help! show the help
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
293 test_must_fail git rev-parse --parseopt -- -h <spec >actual &&
294 test_cmp expect actual
295 '
296
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
325 test_done