Skip to content

Commit c97ee17

Browse files
drafnelgitster
authored andcommitted
t0040,t1502: Demonstrate parse_options bugs
When the option spec contains no switches or only hidden switches, parse_options will emit an extra blank line at the end of help output so that the help text will end in two blank lines instead of one. When parse_options produces internal help output after an error has occurred it will emit blank lines within the usage string to stdout instead of stderr. Update t/helper/test-parse-options.c to have a description body in the usage string to exercise this second bug and mark tests as failing in t0040. Add tests to t1502 to demonstrate both of these problems. Signed-off-by: Brandon Casey <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 697bc88 commit c97ee17

File tree

3 files changed

+107
-3
lines changed

3 files changed

+107
-3
lines changed

t/helper/test-parse-options.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ int cmd_main(int argc, const char **argv)
9999
const char *prefix = "prefix/";
100100
const char *usage[] = {
101101
"test-parse-options <options>",
102+
"",
103+
"A helper function for the parse-options API.",
102104
NULL
103105
};
104106
struct string_list expect = STRING_LIST_INIT_NODUP;

t/t0040-parse-options.sh

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ test_description='our own option parser'
1010
cat >expect <<\EOF
1111
usage: test-parse-options <options>
1212
13+
A helper function for the parse-options API.
14+
1315
--yes get a boolean
1416
-D, --no-doubt begins with 'no-'
1517
-B, --no-fear be brave
@@ -90,8 +92,8 @@ test_expect_success 'OPT_BOOL() is idempotent #2' 'check boolean: 1 -DB'
9092
test_expect_success 'OPT_BOOL() negation #1' 'check boolean: 0 -D --no-yes'
9193
test_expect_success 'OPT_BOOL() negation #2' 'check boolean: 0 -D --no-no-doubt'
9294

93-
test_expect_success 'OPT_BOOL() no negation #1' 'check_unknown_i18n --fear'
94-
test_expect_success 'OPT_BOOL() no negation #2' 'check_unknown_i18n --no-no-fear'
95+
test_expect_failure 'OPT_BOOL() no negation #1' 'check_unknown_i18n --fear'
96+
test_expect_failure 'OPT_BOOL() no negation #2' 'check_unknown_i18n --no-no-fear'
9597

9698
test_expect_success 'OPT_BOOL() positivation' 'check boolean: 0 -D --doubt'
9799

@@ -286,7 +288,7 @@ test_expect_success 'OPT_CALLBACK() and OPT_BIT() work' '
286288

287289
>expect
288290

289-
test_expect_success 'OPT_CALLBACK() and callback errors work' '
291+
test_expect_failure 'OPT_CALLBACK() and callback errors work' '
290292
test_must_fail test-parse-options --no-length >output 2>output.err &&
291293
test_i18ncmp expect output &&
292294
test_i18ncmp expect.err output.err

t/t1502-rev-parse-parseopt.sh

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,25 @@ test_expect_success 'setup optionspec' '
3838
EOF
3939
'
4040

41+
test_expect_success 'setup optionspec-no-switches' '
42+
sed -e "s/^|//" >optionspec_no_switches <<\EOF
43+
|some-command [options] <args>...
44+
|
45+
|some-command does foo and bar!
46+
|--
47+
EOF
48+
'
49+
50+
test_expect_success 'setup optionspec-only-hidden-switches' '
51+
sed -e "s/^|//" >optionspec_only_hidden_switches <<\EOF
52+
|some-command [options] <args>...
53+
|
54+
|some-command does foo and bar!
55+
|--
56+
|hidden1* A hidden switch
57+
EOF
58+
'
59+
4160
test_expect_success 'test --parseopt help output' '
4261
sed -e "s/^|//" >expect <<\END_EXPECT &&
4362
|cat <<\EOF
@@ -79,6 +98,87 @@ END_EXPECT
7998
test_i18ncmp expect output
8099
'
81100

101+
test_expect_failure 'test --parseopt help output no switches' '
102+
sed -e "s/^|//" >expect <<\END_EXPECT &&
103+
|cat <<\EOF
104+
|usage: some-command [options] <args>...
105+
|
106+
| some-command does foo and bar!
107+
|
108+
|EOF
109+
END_EXPECT
110+
test_expect_code 129 git rev-parse --parseopt -- -h > output < optionspec_no_switches &&
111+
test_i18ncmp expect output
112+
'
113+
114+
test_expect_failure 'test --parseopt help output hidden switches' '
115+
sed -e "s/^|//" >expect <<\END_EXPECT &&
116+
|cat <<\EOF
117+
|usage: some-command [options] <args>...
118+
|
119+
| some-command does foo and bar!
120+
|
121+
|EOF
122+
END_EXPECT
123+
test_expect_code 129 git rev-parse --parseopt -- -h > output < optionspec_only_hidden_switches &&
124+
test_i18ncmp expect output
125+
'
126+
127+
test_expect_success 'test --parseopt help-all output hidden switches' '
128+
sed -e "s/^|//" >expect <<\END_EXPECT &&
129+
|cat <<\EOF
130+
|usage: some-command [options] <args>...
131+
|
132+
| some-command does foo and bar!
133+
|
134+
| --hidden1 A hidden switch
135+
|
136+
|EOF
137+
END_EXPECT
138+
test_expect_code 129 git rev-parse --parseopt -- --help-all > output < optionspec_only_hidden_switches &&
139+
test_i18ncmp expect output
140+
'
141+
142+
test_expect_failure 'test --parseopt invalid switch help output' '
143+
sed -e "s/^|//" >expect <<\END_EXPECT &&
144+
|error: unknown option `does-not-exist'\''
145+
|usage: some-command [options] <args>...
146+
|
147+
| some-command does foo and bar!
148+
|
149+
| -h, --help show the help
150+
| --foo some nifty option --foo
151+
| --bar ... some cool option --bar with an argument
152+
| -b, --baz a short and long option
153+
|
154+
|An option group Header
155+
| -C[...] option C with an optional argument
156+
| -d, --data[=...] short and long option with an optional argument
157+
|
158+
|Argument hints
159+
| -B <arg> short option required argument
160+
| --bar2 <arg> long option required argument
161+
| -e, --fuz <with-space>
162+
| short and long option required argument
163+
| -s[<some>] short option optional argument
164+
| --long[=<data>] long option optional argument
165+
| -g, --fluf[=<path>] short and long option optional argument
166+
| --longest <very-long-argument-hint>
167+
| a very long argument hint
168+
| --pair <key=value> with an equals sign in the hint
169+
| --aswitch help te=t contains? fl*g characters!`
170+
| --bswitch <hint> hint has trailing tab character
171+
| --cswitch switch has trailing tab character
172+
| --short-hint <a> with a one symbol hint
173+
|
174+
|Extras
175+
| --extra1 line above used to cause a segfault but no longer does
176+
|
177+
END_EXPECT
178+
test_expect_code 129 git rev-parse --parseopt -- --does-not-exist 1>/dev/null 2>output < optionspec &&
179+
test_i18ncmp expect output
180+
'
181+
82182
test_expect_success 'setup expect.1' "
83183
cat > expect <<EOF
84184
set -- --foo --bar 'ham' -b --aswitch -- 'arg'

0 commit comments

Comments
 (0)