t1502: test option negation

Add tests for checking the "git rev-parse --parseopt" flag "!" and whether options can be negated with a "no-" prefix. Signed-off-by: René Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed Aug 5, 2023 at 16:39 UTC d5dc68f73041f95c1179fb092005e2326bdd8a7b
3 files changed +63
t/t1502-rev-parse-parseopt.sh
+44
@@ -3,6 +3,22 @@
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>...
@@ -278,4 +294,32 @@ test_expect_success 'test --parseopt help output: multi-line blurb after empty l
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
t/t1502/optionspec-neg new
+8
@@ -0,0 +1,8 @@
1 +some-command [options] <args>...
2 +
3 +some-command does foo and bar!
4 +--
5 +foo can be negated
6 +no-bar can be positivated
7 +positive-only! cannot be negated
8 +no-negative! cannot be positivated
t/t1502/optionspec-neg.help new
+11
@@ -0,0 +1,11 @@
1 +cat <<\EOF
2 +usage: some-command [options] <args>...
3 +
4 + some-command does foo and bar!
5 +
6 + --foo can be negated
7 + --no-bar can be positivated
8 + --positive-only cannot be negated
9 + --no-negative cannot be positivated
10 +
11 +EOF