8ca62ef453443e7c0f95e0e3cb3bb871b8273ed0
   1#!/bin/sh
   2#
   3# Copyright (c) 2007 Johannes Schindelin
   4#
   5
   6test_description='our own option parser'
   7
   8. ./test-lib.sh
   9
  10cat > expect.err << EOF
  11usage: test-parse-options <options>
  12
  13    -b, --boolean         get a boolean
  14    -4, --or4             bitwise-or boolean with ...0100
  15    --neg-or4             same as --no-or4
  16
  17    -i, --integer <n>     get a integer
  18    -j <n>                get a integer, too
  19    --set23               set integer to 23
  20    -t <time>             get timestamp of <time>
  21    -L, --length <str>    get length of <str>
  22
  23String options
  24    -s, --string <string>
  25                          get a string
  26    --string2 <str>       get another string
  27    --st <st>             get another string (pervert ordering)
  28    -o <str>              get another string
  29    --default-string      set string to default
  30
  31Magic arguments
  32    --quux                means --quux
  33    -NUM                  set integer to NUM
  34
  35Standard options
  36    --abbrev[=<n>]        use <n> digits to display SHA-1s
  37    -v, --verbose         be verbose
  38    -n, --dry-run         dry run
  39    -q, --quiet           be quiet
  40
  41EOF
  42
  43test_expect_success 'test help' '
  44        test_must_fail test-parse-options -h > output 2> output.err &&
  45        test ! -s output &&
  46        test_cmp expect.err output.err
  47'
  48
  49cat > expect << EOF
  50boolean: 2
  51integer: 1729
  52timestamp: 0
  53string: 123
  54abbrev: 7
  55verbose: 2
  56quiet: no
  57dry run: yes
  58EOF
  59
  60test_expect_success 'short options' '
  61        test-parse-options -s123 -b -i 1729 -b -vv -n > output 2> output.err &&
  62        test_cmp expect output &&
  63        test ! -s output.err
  64'
  65
  66cat > expect << EOF
  67boolean: 2
  68integer: 1729
  69timestamp: 0
  70string: 321
  71abbrev: 10
  72verbose: 2
  73quiet: no
  74dry run: no
  75EOF
  76
  77test_expect_success 'long options' '
  78        test-parse-options --boolean --integer 1729 --boolean --string2=321 \
  79                --verbose --verbose --no-dry-run --abbrev=10 \
  80                > output 2> output.err &&
  81        test ! -s output.err &&
  82        test_cmp expect output
  83'
  84
  85test_expect_success 'missing required value' '
  86        test-parse-options -s;
  87        test $? = 129 &&
  88        test-parse-options --string;
  89        test $? = 129
  90'
  91
  92cat > expect << EOF
  93boolean: 1
  94integer: 13
  95timestamp: 0
  96string: 123
  97abbrev: 7
  98verbose: 0
  99quiet: no
 100dry run: no
 101arg 00: a1
 102arg 01: b1
 103arg 02: --boolean
 104EOF
 105
 106test_expect_success 'intermingled arguments' '
 107        test-parse-options a1 --string 123 b1 --boolean -j 13 -- --boolean \
 108                > output 2> output.err &&
 109        test ! -s output.err &&
 110        test_cmp expect output
 111'
 112
 113cat > expect << EOF
 114boolean: 0
 115integer: 2
 116timestamp: 0
 117string: (not set)
 118abbrev: 7
 119verbose: 0
 120quiet: no
 121dry run: no
 122EOF
 123
 124test_expect_success 'unambiguously abbreviated option' '
 125        test-parse-options --int 2 --boolean --no-bo > output 2> output.err &&
 126        test ! -s output.err &&
 127        test_cmp expect output
 128'
 129
 130test_expect_success 'unambiguously abbreviated option with "="' '
 131        test-parse-options --int=2 > output 2> output.err &&
 132        test ! -s output.err &&
 133        test_cmp expect output
 134'
 135
 136test_expect_success 'ambiguously abbreviated option' '
 137        test-parse-options --strin 123;
 138        test $? = 129
 139'
 140
 141cat > expect << EOF
 142boolean: 0
 143integer: 0
 144timestamp: 0
 145string: 123
 146abbrev: 7
 147verbose: 0
 148quiet: no
 149dry run: no
 150EOF
 151
 152test_expect_success 'non ambiguous option (after two options it abbreviates)' '
 153        test-parse-options --st 123 > output 2> output.err &&
 154        test ! -s output.err &&
 155        test_cmp expect output
 156'
 157
 158cat > typo.err << EOF
 159error: did you mean \`--boolean\` (with two dashes ?)
 160EOF
 161
 162test_expect_success 'detect possible typos' '
 163        test_must_fail test-parse-options -boolean > output 2> output.err &&
 164        test ! -s output &&
 165        test_cmp typo.err output.err
 166'
 167
 168cat > expect <<EOF
 169boolean: 0
 170integer: 0
 171timestamp: 0
 172string: (not set)
 173abbrev: 7
 174verbose: 0
 175quiet: no
 176dry run: no
 177arg 00: --quux
 178EOF
 179
 180test_expect_success 'keep some options as arguments' '
 181        test-parse-options --quux > output 2> output.err &&
 182        test ! -s output.err &&
 183        test_cmp expect output
 184'
 185
 186cat > expect <<EOF
 187boolean: 0
 188integer: 0
 189timestamp: 1
 190string: default
 191abbrev: 7
 192verbose: 0
 193quiet: yes
 194dry run: no
 195arg 00: foo
 196EOF
 197
 198test_expect_success 'OPT_DATE() and OPT_SET_PTR() work' '
 199        test-parse-options -t "1970-01-01 00:00:01 +0000" --default-string \
 200                foo -q > output 2> output.err &&
 201        test ! -s output.err &&
 202        test_cmp expect output
 203'
 204
 205cat > expect <<EOF
 206Callback: "four", 0
 207boolean: 5
 208integer: 4
 209timestamp: 0
 210string: (not set)
 211abbrev: 7
 212verbose: 0
 213quiet: no
 214dry run: no
 215EOF
 216
 217test_expect_success 'OPT_CALLBACK() and OPT_BIT() work' '
 218        test-parse-options --length=four -b -4 > output 2> output.err &&
 219        test ! -s output.err &&
 220        test_cmp expect output
 221'
 222
 223cat > expect <<EOF
 224Callback: "not set", 1
 225EOF
 226
 227test_expect_success 'OPT_CALLBACK() and callback errors work' '
 228        test_must_fail test-parse-options --no-length > output 2> output.err &&
 229        test_cmp expect output &&
 230        test_cmp expect.err output.err
 231'
 232
 233cat > expect <<EOF
 234boolean: 1
 235integer: 23
 236timestamp: 0
 237string: (not set)
 238abbrev: 7
 239verbose: 0
 240quiet: no
 241dry run: no
 242EOF
 243
 244test_expect_success 'OPT_BIT() and OPT_SET_INT() work' '
 245        test-parse-options --set23 -bbbbb --no-or4 > output 2> output.err &&
 246        test ! -s output.err &&
 247        test_cmp expect output
 248'
 249
 250test_expect_success 'OPT_NEGBIT() and OPT_SET_INT() work' '
 251        test-parse-options --set23 -bbbbb --neg-or4 > output 2> output.err &&
 252        test ! -s output.err &&
 253        test_cmp expect output
 254'
 255
 256cat > expect <<EOF
 257boolean: 6
 258integer: 0
 259timestamp: 0
 260string: (not set)
 261abbrev: 7
 262verbose: 0
 263quiet: no
 264dry run: no
 265EOF
 266
 267test_expect_success 'OPT_BIT() works' '
 268        test-parse-options -bb --or4 > output 2> output.err &&
 269        test ! -s output.err &&
 270        test_cmp expect output
 271'
 272
 273test_expect_success 'OPT_NEGBIT() works' '
 274        test-parse-options -bb --no-neg-or4 > output 2> output.err &&
 275        test ! -s output.err &&
 276        test_cmp expect output
 277'
 278
 279cat > expect <<EOF
 280boolean: 0
 281integer: 12345
 282timestamp: 0
 283string: (not set)
 284abbrev: 7
 285verbose: 0
 286quiet: no
 287dry run: no
 288EOF
 289
 290test_expect_success 'OPT_NUMBER_CALLBACK() works' '
 291        test-parse-options -12345 > output 2> output.err &&
 292        test ! -s output.err &&
 293        test_cmp expect output
 294'
 295
 296test_done