t / t0040-parse-options.shon commit t/helper: merge test-chmtime into test-tool (0e49649)
   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 <<\EOF
  11usage: test-parse-options <options>
  12
  13    A helper function for the parse-options API.
  14
  15    --yes                 get a boolean
  16    -D, --no-doubt        begins with 'no-'
  17    -B, --no-fear         be brave
  18    -b, --boolean         increment by one
  19    -4, --or4             bitwise-or boolean with ...0100
  20    --neg-or4             same as --no-or4
  21
  22    -i, --integer <n>     get a integer
  23    -j <n>                get a integer, too
  24    -m, --magnitude <n>   get a magnitude
  25    --set23               set integer to 23
  26    -t <time>             get timestamp of <time>
  27    -L, --length <str>    get length of <str>
  28    -F, --file <file>     set file to <file>
  29
  30String options
  31    -s, --string <string>
  32                          get a string
  33    --string2 <str>       get another string
  34    --st <st>             get another string (pervert ordering)
  35    -o <str>              get another string
  36    --list <str>          add str to list
  37
  38Magic arguments
  39    --quux                means --quux
  40    -NUM                  set integer to NUM
  41    +                     same as -b
  42    --ambiguous           positive ambiguity
  43    --no-ambiguous        negative ambiguity
  44
  45Standard options
  46    --abbrev[=<n>]        use <n> digits to display SHA-1s
  47    -v, --verbose         be verbose
  48    -n, --dry-run         dry run
  49    -q, --quiet           be quiet
  50    --expect <string>     expected output in the variable dump
  51
  52EOF
  53
  54test_expect_success 'test help' '
  55        test_must_fail test-parse-options -h >output 2>output.err &&
  56        test_must_be_empty output.err &&
  57        test_i18ncmp expect output
  58'
  59
  60mv expect expect.err
  61
  62check () {
  63        what="$1" &&
  64        shift &&
  65        expect="$1" &&
  66        shift &&
  67        test-parse-options --expect="$what $expect" "$@"
  68}
  69
  70check_unknown_i18n() {
  71        case "$1" in
  72        --*)
  73                echo error: unknown option \`${1#--}\' >expect ;;
  74        -*)
  75                echo error: unknown switch \`${1#-}\' >expect ;;
  76        esac &&
  77        cat expect.err >>expect &&
  78        test_must_fail test-parse-options $* >output 2>output.err &&
  79        test_must_be_empty output &&
  80        test_i18ncmp expect output.err
  81}
  82
  83test_expect_success 'OPT_BOOL() #1' 'check boolean: 1 --yes'
  84test_expect_success 'OPT_BOOL() #2' 'check boolean: 1 --no-doubt'
  85test_expect_success 'OPT_BOOL() #3' 'check boolean: 1 -D'
  86test_expect_success 'OPT_BOOL() #4' 'check boolean: 1 --no-fear'
  87test_expect_success 'OPT_BOOL() #5' 'check boolean: 1 -B'
  88
  89test_expect_success 'OPT_BOOL() is idempotent #1' 'check boolean: 1 --yes --yes'
  90test_expect_success 'OPT_BOOL() is idempotent #2' 'check boolean: 1 -DB'
  91
  92test_expect_success 'OPT_BOOL() negation #1' 'check boolean: 0 -D --no-yes'
  93test_expect_success 'OPT_BOOL() negation #2' 'check boolean: 0 -D --no-no-doubt'
  94
  95test_expect_success 'OPT_BOOL() no negation #1' 'check_unknown_i18n --fear'
  96test_expect_success 'OPT_BOOL() no negation #2' 'check_unknown_i18n --no-no-fear'
  97
  98test_expect_success 'OPT_BOOL() positivation' 'check boolean: 0 -D --doubt'
  99
 100test_expect_success 'OPT_INT() negative' 'check integer: -2345 -i -2345'
 101
 102test_expect_success 'OPT_MAGNITUDE() simple' '
 103        check magnitude: 2345678 -m 2345678
 104'
 105
 106test_expect_success 'OPT_MAGNITUDE() kilo' '
 107        check magnitude: 239616 -m 234k
 108'
 109
 110test_expect_success 'OPT_MAGNITUDE() mega' '
 111        check magnitude: 104857600 -m 100m
 112'
 113
 114test_expect_success 'OPT_MAGNITUDE() giga' '
 115        check magnitude: 1073741824 -m 1g
 116'
 117
 118test_expect_success 'OPT_MAGNITUDE() 3giga' '
 119        check magnitude: 3221225472 -m 3g
 120'
 121
 122cat >expect <<\EOF
 123boolean: 2
 124integer: 1729
 125magnitude: 16384
 126timestamp: 0
 127string: 123
 128abbrev: 7
 129verbose: 2
 130quiet: 0
 131dry run: yes
 132file: prefix/my.file
 133EOF
 134
 135test_expect_success 'short options' '
 136        test-parse-options -s123 -b -i 1729 -m 16k -b -vv -n -F my.file \
 137        >output 2>output.err &&
 138        test_cmp expect output &&
 139        test_must_be_empty output.err
 140'
 141
 142cat >expect <<\EOF
 143boolean: 2
 144integer: 1729
 145magnitude: 16384
 146timestamp: 0
 147string: 321
 148abbrev: 10
 149verbose: 2
 150quiet: 0
 151dry run: no
 152file: prefix/fi.le
 153EOF
 154
 155test_expect_success 'long options' '
 156        test-parse-options --boolean --integer 1729 --magnitude 16k \
 157                --boolean --string2=321 --verbose --verbose --no-dry-run \
 158                --abbrev=10 --file fi.le --obsolete \
 159                >output 2>output.err &&
 160        test_must_be_empty output.err &&
 161        test_cmp expect output
 162'
 163
 164test_expect_success 'missing required value' '
 165        test_expect_code 129 test-parse-options -s &&
 166        test_expect_code 129 test-parse-options --string &&
 167        test_expect_code 129 test-parse-options --file
 168'
 169
 170cat >expect <<\EOF
 171boolean: 1
 172integer: 13
 173magnitude: 0
 174timestamp: 0
 175string: 123
 176abbrev: 7
 177verbose: -1
 178quiet: 0
 179dry run: no
 180file: (not set)
 181arg 00: a1
 182arg 01: b1
 183arg 02: --boolean
 184EOF
 185
 186test_expect_success 'intermingled arguments' '
 187        test-parse-options a1 --string 123 b1 --boolean -j 13 -- --boolean \
 188                >output 2>output.err &&
 189        test_must_be_empty output.err &&
 190        test_cmp expect output
 191'
 192
 193cat >expect <<\EOF
 194boolean: 0
 195integer: 2
 196magnitude: 0
 197timestamp: 0
 198string: (not set)
 199abbrev: 7
 200verbose: -1
 201quiet: 0
 202dry run: no
 203file: (not set)
 204EOF
 205
 206test_expect_success 'unambiguously abbreviated option' '
 207        test-parse-options --int 2 --boolean --no-bo >output 2>output.err &&
 208        test_must_be_empty output.err &&
 209        test_cmp expect output
 210'
 211
 212test_expect_success 'unambiguously abbreviated option with "="' '
 213        test-parse-options --expect="integer: 2" --int=2
 214'
 215
 216test_expect_success 'ambiguously abbreviated option' '
 217        test_expect_code 129 test-parse-options --strin 123
 218'
 219
 220test_expect_success 'non ambiguous option (after two options it abbreviates)' '
 221        test-parse-options --expect="string: 123" --st 123
 222'
 223
 224cat >typo.err <<\EOF
 225error: did you mean `--boolean` (with two dashes ?)
 226EOF
 227
 228test_expect_success 'detect possible typos' '
 229        test_must_fail test-parse-options -boolean >output 2>output.err &&
 230        test_must_be_empty output &&
 231        test_cmp typo.err output.err
 232'
 233
 234cat >typo.err <<\EOF
 235error: did you mean `--ambiguous` (with two dashes ?)
 236EOF
 237
 238test_expect_success 'detect possible typos' '
 239        test_must_fail test-parse-options -ambiguous >output 2>output.err &&
 240        test_must_be_empty output &&
 241        test_cmp typo.err output.err
 242'
 243
 244test_expect_success 'keep some options as arguments' '
 245        test-parse-options --expect="arg 00: --quux" --quux
 246'
 247
 248cat >expect <<\EOF
 249boolean: 0
 250integer: 0
 251magnitude: 0
 252timestamp: 1
 253string: (not set)
 254abbrev: 7
 255verbose: -1
 256quiet: 1
 257dry run: no
 258file: (not set)
 259arg 00: foo
 260EOF
 261
 262test_expect_success 'OPT_DATE() works' '
 263        test-parse-options -t "1970-01-01 00:00:01 +0000" \
 264                foo -q >output 2>output.err &&
 265        test_must_be_empty output.err &&
 266        test_cmp expect output
 267'
 268
 269cat >expect <<\EOF
 270Callback: "four", 0
 271boolean: 5
 272integer: 4
 273magnitude: 0
 274timestamp: 0
 275string: (not set)
 276abbrev: 7
 277verbose: -1
 278quiet: 0
 279dry run: no
 280file: (not set)
 281EOF
 282
 283test_expect_success 'OPT_CALLBACK() and OPT_BIT() work' '
 284        test-parse-options --length=four -b -4 >output 2>output.err &&
 285        test_must_be_empty output.err &&
 286        test_cmp expect output
 287'
 288
 289>expect
 290
 291test_expect_success 'OPT_CALLBACK() and callback errors work' '
 292        test_must_fail test-parse-options --no-length >output 2>output.err &&
 293        test_i18ncmp expect output &&
 294        test_i18ncmp expect.err output.err
 295'
 296
 297cat >expect <<\EOF
 298boolean: 1
 299integer: 23
 300magnitude: 0
 301timestamp: 0
 302string: (not set)
 303abbrev: 7
 304verbose: -1
 305quiet: 0
 306dry run: no
 307file: (not set)
 308EOF
 309
 310test_expect_success 'OPT_BIT() and OPT_SET_INT() work' '
 311        test-parse-options --set23 -bbbbb --no-or4 >output 2>output.err &&
 312        test_must_be_empty output.err &&
 313        test_cmp expect output
 314'
 315
 316test_expect_success 'OPT_NEGBIT() and OPT_SET_INT() work' '
 317        test-parse-options --set23 -bbbbb --neg-or4 >output 2>output.err &&
 318        test_must_be_empty output.err &&
 319        test_cmp expect output
 320'
 321
 322test_expect_success 'OPT_BIT() works' '
 323        test-parse-options --expect="boolean: 6" -bb --or4
 324'
 325
 326test_expect_success 'OPT_NEGBIT() works' '
 327        test-parse-options --expect="boolean: 6" -bb --no-neg-or4
 328'
 329
 330test_expect_success 'OPT_COUNTUP() with PARSE_OPT_NODASH works' '
 331        test-parse-options --expect="boolean: 6" + + + + + +
 332'
 333
 334test_expect_success 'OPT_NUMBER_CALLBACK() works' '
 335        test-parse-options --expect="integer: 12345" -12345
 336'
 337
 338cat >expect <<\EOF
 339boolean: 0
 340integer: 0
 341magnitude: 0
 342timestamp: 0
 343string: (not set)
 344abbrev: 7
 345verbose: -1
 346quiet: 0
 347dry run: no
 348file: (not set)
 349EOF
 350
 351test_expect_success 'negation of OPT_NONEG flags is not ambiguous' '
 352        test-parse-options --no-ambig >output 2>output.err &&
 353        test_must_be_empty output.err &&
 354        test_cmp expect output
 355'
 356
 357cat >>expect <<\EOF
 358list: foo
 359list: bar
 360list: baz
 361EOF
 362test_expect_success '--list keeps list of strings' '
 363        test-parse-options --list foo --list=bar --list=baz >output &&
 364        test_cmp expect output
 365'
 366
 367test_expect_success '--no-list resets list' '
 368        test-parse-options --list=other --list=irrelevant --list=options \
 369                --no-list --list=foo --list=bar --list=baz >output &&
 370        test_cmp expect output
 371'
 372
 373test_expect_success 'multiple quiet levels' '
 374        test-parse-options --expect="quiet: 3" -q -q -q
 375'
 376
 377test_expect_success 'multiple verbose levels' '
 378        test-parse-options --expect="verbose: 3" -v -v -v
 379'
 380
 381test_expect_success '--no-quiet sets --quiet to 0' '
 382        test-parse-options --expect="quiet: 0" --no-quiet
 383'
 384
 385test_expect_success '--no-quiet resets multiple -q to 0' '
 386        test-parse-options --expect="quiet: 0" -q -q -q --no-quiet
 387'
 388
 389test_expect_success '--no-verbose sets verbose to 0' '
 390        test-parse-options --expect="verbose: 0" --no-verbose
 391'
 392
 393test_expect_success '--no-verbose resets multiple verbose to 0' '
 394        test-parse-options --expect="verbose: 0" -v -v -v --no-verbose
 395'
 396
 397test_done