reopen_tempfile(): truncate opened file
[gitweb.git] / t / t0040-parse-options.sh
index fec3fef9a1515a8f971370acbbc922377c531a89..04d474c84fd69121c686f5ca5adc40ce081f0e9d 100755 (executable)
@@ -10,6 +10,8 @@ test_description='our own option parser'
 cat >expect <<\EOF
 usage: test-parse-options <options>
 
+    A helper function for the parse-options API.
+
     --yes                 get a boolean
     -D, --no-doubt        begins with 'no-'
     -B, --no-fear         be brave
@@ -45,6 +47,7 @@ Standard options
     -v, --verbose         be verbose
     -n, --dry-run         dry run
     -q, --quiet           be quiet
+    --expect <string>     expected output in the variable dump
 
 EOF
 
@@ -56,52 +59,12 @@ test_expect_success 'test help' '
 
 mv expect expect.err
 
-cat >expect.template <<\EOF
-boolean: 0
-integer: 0
-magnitude: 0
-timestamp: 0
-string: (not set)
-abbrev: 7
-verbose: -1
-quiet: 0
-dry run: no
-file: (not set)
-EOF
-
-check() {
-       what="$1" &&
-       shift &&
-       expect="$1" &&
-       shift &&
-       sed "s/^$what .*/$what $expect/" <expect.template >expect &&
-       test-parse-options $* >output 2>output.err &&
-       test_must_be_empty output.err &&
-       test_cmp expect output
-}
-
-check_i18n() {
+check () {
        what="$1" &&
        shift &&
        expect="$1" &&
        shift &&
-       sed "s/^$what .*/$what $expect/" <expect.template >expect &&
-       test-parse-options $* >output 2>output.err &&
-       test_must_be_empty output.err &&
-       test_i18ncmp expect output
-}
-
-check_unknown() {
-       case "$1" in
-       --*)
-               echo error: unknown option \`${1#--}\' >expect ;;
-       -*)
-               echo error: unknown switch \`${1#-}\' >expect ;;
-       esac &&
-       cat expect.err >>expect &&
-       test_must_fail test-parse-options $* >output 2>output.err &&
-       test_must_be_empty output &&
-       test_cmp expect output.err
+       test-parse-options --expect="$what $expect" "$@"
 }
 
 check_unknown_i18n() {
@@ -247,32 +210,15 @@ test_expect_success 'unambiguously abbreviated option' '
 '
 
 test_expect_success 'unambiguously abbreviated option with "="' '
-       test-parse-options --int=2 >output 2>output.err &&
-       test_must_be_empty output.err &&
-       test_cmp expect output
+       test-parse-options --expect="integer: 2" --int=2
 '
 
 test_expect_success 'ambiguously abbreviated option' '
        test_expect_code 129 test-parse-options --strin 123
 '
 
-cat >expect <<\EOF
-boolean: 0
-integer: 0
-magnitude: 0
-timestamp: 0
-string: 123
-abbrev: 7
-verbose: -1
-quiet: 0
-dry run: no
-file: (not set)
-EOF
-
 test_expect_success 'non ambiguous option (after two options it abbreviates)' '
-       test-parse-options --st 123 >output 2>output.err &&
-       test_must_be_empty output.err &&
-       test_cmp expect output
+       test-parse-options --expect="string: 123" --st 123
 '
 
 cat >typo.err <<\EOF
@@ -295,24 +241,8 @@ test_expect_success 'detect possible typos' '
        test_cmp typo.err output.err
 '
 
-cat >expect <<\EOF
-boolean: 0
-integer: 0
-magnitude: 0
-timestamp: 0
-string: (not set)
-abbrev: 7
-verbose: -1
-quiet: 0
-dry run: no
-file: (not set)
-arg 00: --quux
-EOF
-
 test_expect_success 'keep some options as arguments' '
-       test-parse-options --quux >output 2>output.err &&
-       test_must_be_empty output.err &&
-       test_cmp expect output
+       test-parse-options --expect="arg 00: --quux" --quux
 '
 
 cat >expect <<\EOF
@@ -356,14 +286,12 @@ test_expect_success 'OPT_CALLBACK() and OPT_BIT() work' '
        test_cmp expect output
 '
 
-cat >expect <<\EOF
-Callback: "not set", 1
-EOF
+>expect
 
 test_expect_success 'OPT_CALLBACK() and callback errors work' '
        test_must_fail test-parse-options --no-length >output 2>output.err &&
        test_i18ncmp expect output &&
-       test_i18ncmp expect.err output.err
+       test_must_be_empty output.err
 '
 
 cat >expect <<\EOF
@@ -391,54 +319,20 @@ test_expect_success 'OPT_NEGBIT() and OPT_SET_INT() work' '
        test_cmp expect output
 '
 
-cat >expect <<\EOF
-boolean: 6
-integer: 0
-magnitude: 0
-timestamp: 0
-string: (not set)
-abbrev: 7
-verbose: -1
-quiet: 0
-dry run: no
-file: (not set)
-EOF
-
 test_expect_success 'OPT_BIT() works' '
-       test-parse-options -bb --or4 >output 2>output.err &&
-       test_must_be_empty output.err &&
-       test_cmp expect output
+       test-parse-options --expect="boolean: 6" -bb --or4
 '
 
 test_expect_success 'OPT_NEGBIT() works' '
-       test-parse-options -bb --no-neg-or4 >output 2>output.err &&
-       test_must_be_empty output.err &&
-       test_cmp expect output
+       test-parse-options --expect="boolean: 6" -bb --no-neg-or4
 '
 
 test_expect_success 'OPT_COUNTUP() with PARSE_OPT_NODASH works' '
-       test-parse-options + + + + + + >output 2>output.err &&
-       test_must_be_empty output.err &&
-       test_cmp expect output
+       test-parse-options --expect="boolean: 6" + + + + + +
 '
 
-cat >expect <<\EOF
-boolean: 0
-integer: 12345
-magnitude: 0
-timestamp: 0
-string: (not set)
-abbrev: 7
-verbose: -1
-quiet: 0
-dry run: no
-file: (not set)
-EOF
-
 test_expect_success 'OPT_NUMBER_CALLBACK() works' '
-       test-parse-options -12345 >output 2>output.err &&
-       test_must_be_empty output.err &&
-       test_cmp expect output
+       test-parse-options --expect="integer: 12345" -12345
 '
 
 cat >expect <<\EOF
@@ -476,118 +370,28 @@ test_expect_success '--no-list resets list' '
        test_cmp expect output
 '
 
-cat >expect <<\EOF
-boolean: 0
-integer: 0
-magnitude: 0
-timestamp: 0
-string: (not set)
-abbrev: 7
-verbose: -1
-quiet: 3
-dry run: no
-file: (not set)
-EOF
-
 test_expect_success 'multiple quiet levels' '
-       test-parse-options -q -q -q >output 2>output.err &&
-       test_must_be_empty output.err &&
-       test_cmp expect output
+       test-parse-options --expect="quiet: 3" -q -q -q
 '
 
-cat >expect <<\EOF
-boolean: 0
-integer: 0
-magnitude: 0
-timestamp: 0
-string: (not set)
-abbrev: 7
-verbose: 3
-quiet: 0
-dry run: no
-file: (not set)
-EOF
-
 test_expect_success 'multiple verbose levels' '
-       test-parse-options -v -v -v >output 2>output.err &&
-       test_must_be_empty output.err &&
-       test_cmp expect output
+       test-parse-options --expect="verbose: 3" -v -v -v
 '
 
-cat >expect <<\EOF
-boolean: 0
-integer: 0
-magnitude: 0
-timestamp: 0
-string: (not set)
-abbrev: 7
-verbose: -1
-quiet: 0
-dry run: no
-file: (not set)
-EOF
-
 test_expect_success '--no-quiet sets --quiet to 0' '
-       test-parse-options --no-quiet >output 2>output.err &&
-       test_must_be_empty output.err &&
-       test_cmp expect output
+       test-parse-options --expect="quiet: 0" --no-quiet
 '
 
-cat >expect <<\EOF
-boolean: 0
-integer: 0
-magnitude: 0
-timestamp: 0
-string: (not set)
-abbrev: 7
-verbose: -1
-quiet: 0
-dry run: no
-file: (not set)
-EOF
-
 test_expect_success '--no-quiet resets multiple -q to 0' '
-       test-parse-options -q -q -q --no-quiet >output 2>output.err &&
-       test_must_be_empty output.err &&
-       test_cmp expect output
+       test-parse-options --expect="quiet: 0" -q -q -q --no-quiet
 '
 
-cat >expect <<\EOF
-boolean: 0
-integer: 0
-magnitude: 0
-timestamp: 0
-string: (not set)
-abbrev: 7
-verbose: 0
-quiet: 0
-dry run: no
-file: (not set)
-EOF
-
 test_expect_success '--no-verbose sets verbose to 0' '
-       test-parse-options --no-verbose >output 2>output.err &&
-       test_must_be_empty output.err &&
-       test_cmp expect output
+       test-parse-options --expect="verbose: 0" --no-verbose
 '
 
-cat >expect <<\EOF
-boolean: 0
-integer: 0
-magnitude: 0
-timestamp: 0
-string: (not set)
-abbrev: 7
-verbose: 0
-quiet: 0
-dry run: no
-file: (not set)
-EOF
-
 test_expect_success '--no-verbose resets multiple verbose to 0' '
-       test-parse-options -v -v -v --no-verbose >output 2>output.err &&
-       test_must_be_empty output.err &&
-       test_cmp expect output
+       test-parse-options --expect="verbose: 0" -v -v -v --no-verbose
 '
 
 test_done