1#!/bin/sh
2
3test_description='test git rev-parse --parseopt'
4. ./test-lib.sh
5
6test_expect_success 'setup optionspec' '
7 sed -e "s/^|//" >optionspec <<\EOF
8|some-command [options] <args>...
9|
10|some-command does foo and bar!
11|--
12|h,help show the help
13|
14|foo some nifty option --foo
15|bar= some cool option --bar with an argument
16|b,baz a short and long option
17|
18| An option group Header
19|C? option C with an optional argument
20|d,data? short and long option with an optional argument
21|
22| Argument hints
23|B=arg short option required argument
24|bar2=arg long option required argument
25|e,fuz=with-space short and long option required argument
26|s?some short option optional argument
27|long?data long option optional argument
28|g,fluf?path short and long option optional argument
29|longest=very-long-argument-hint a very long argument hint
30|pair=key=value with an equals sign in the hint
31|aswitch help te=t contains? fl*g characters!`
32|bswitch=hint hint has trailing tab character
33|cswitch switch has trailing tab character
34|short-hint=a with a one symbol hint
35|
36|Extras
37|extra1 line above used to cause a segfault but no longer does
38EOF
39'
40
41test_expect_success 'test --parseopt help output' '
42 sed -e "s/^|//" >expect <<\END_EXPECT &&
43|cat <<\EOF
44|usage: some-command [options] <args>...
45|
46| some-command does foo and bar!
47|
48| -h, --help show the help
49| --foo some nifty option --foo
50| --bar ... some cool option --bar with an argument
51| -b, --baz a short and long option
52|
53|An option group Header
54| -C[...] option C with an optional argument
55| -d, --data[=...] short and long option with an optional argument
56|
57|Argument hints
58| -B <arg> short option required argument
59| --bar2 <arg> long option required argument
60| -e, --fuz <with-space>
61| short and long option required argument
62| -s[<some>] short option optional argument
63| --long[=<data>] long option optional argument
64| -g, --fluf[=<path>] short and long option optional argument
65| --longest <very-long-argument-hint>
66| a very long argument hint
67| --pair <key=value> with an equals sign in the hint
68| --aswitch help te=t contains? fl*g characters!`
69| --bswitch <hint> hint has trailing tab character
70| --cswitch switch has trailing tab character
71| --short-hint <a> with a one symbol hint
72|
73|Extras
74| --extra1 line above used to cause a segfault but no longer does
75|
76|EOF
77END_EXPECT
78 test_expect_code 129 git rev-parse --parseopt -- -h > output < optionspec &&
79 test_i18ncmp expect output
80'
81
82test_expect_success 'setup expect.1' "
83 cat > expect <<EOF
84set -- --foo --bar 'ham' -b --aswitch -- 'arg'
85EOF
86"
87
88test_expect_success 'test --parseopt' '
89 git rev-parse --parseopt -- --foo --bar=ham --baz --aswitch arg < optionspec > output &&
90 test_cmp expect output
91'
92
93test_expect_success 'test --parseopt with mixed options and arguments' '
94 git rev-parse --parseopt -- --foo arg --bar=ham --baz --aswitch < optionspec > output &&
95 test_cmp expect output
96'
97
98test_expect_success 'setup expect.2' "
99 cat > expect <<EOF
100set -- --foo -- 'arg' '--bar=ham'
101EOF
102"
103
104test_expect_success 'test --parseopt with --' '
105 git rev-parse --parseopt -- --foo -- arg --bar=ham < optionspec > output &&
106 test_cmp expect output
107'
108
109test_expect_success 'test --parseopt --stop-at-non-option' '
110 git rev-parse --parseopt --stop-at-non-option -- --foo arg --bar=ham < optionspec > output &&
111 test_cmp expect output
112'
113
114test_expect_success 'setup expect.3' "
115 cat > expect <<EOF
116set -- --foo -- '--' 'arg' '--bar=ham'
117EOF
118"
119
120test_expect_success 'test --parseopt --keep-dashdash' '
121 git rev-parse --parseopt --keep-dashdash -- --foo -- arg --bar=ham < optionspec > output &&
122 test_cmp expect output
123'
124
125test_expect_success 'setup expect.4' "
126 cat >expect <<EOF
127set -- --foo -- '--' 'arg' '--spam=ham'
128EOF
129"
130
131test_expect_success 'test --parseopt --keep-dashdash --stop-at-non-option with --' '
132 git rev-parse --parseopt --keep-dashdash --stop-at-non-option -- --foo -- arg --spam=ham <optionspec >output &&
133 test_cmp expect output
134'
135
136test_expect_success 'setup expect.5' "
137 cat > expect <<EOF
138set -- --foo -- 'arg' '--spam=ham'
139EOF
140"
141
142test_expect_success 'test --parseopt --keep-dashdash --stop-at-non-option without --' '
143 git rev-parse --parseopt --keep-dashdash --stop-at-non-option -- --foo arg --spam=ham <optionspec >output &&
144 test_cmp expect output
145'
146
147test_expect_success 'setup expect.6' "
148 cat > expect <<EOF
149set -- --foo --bar='z' --baz -C'Z' --data='A' -- 'arg'
150EOF
151"
152
153test_expect_success 'test --parseopt --stuck-long' '
154 git rev-parse --parseopt --stuck-long -- --foo --bar=z -b arg -CZ -dA <optionspec >output &&
155 test_cmp expect output
156'
157
158test_expect_success 'setup expect.7' "
159 cat > expect <<EOF
160set -- --data='' -C --baz -- 'arg'
161EOF
162"
163
164test_expect_success 'test --parseopt --stuck-long and empty optional argument' '
165 git rev-parse --parseopt --stuck-long -- --data= arg -C -b <optionspec >output &&
166 test_cmp expect output
167'
168
169test_expect_success 'setup expect.8' "
170 cat > expect <<EOF
171set -- --data --baz -- 'arg'
172EOF
173"
174
175test_expect_success 'test --parseopt --stuck-long and long option with unset optional argument' '
176 git rev-parse --parseopt --stuck-long -- --data arg -b <optionspec >output &&
177 test_cmp expect output
178'
179
180test_expect_success 'test --parseopt --stuck-long and short option with unset optional argument' '
181 git rev-parse --parseopt --stuck-long -- -d arg -b <optionspec >output &&
182 test_cmp expect output
183'
184
185test_done