Johannes Schindelin | beb4743 | 2007-10-13 17:34:45 +0100 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | # |
| 3 | # Copyright (c) 2007 Johannes Schindelin |
| 4 | # |
| 5 | |
| 6 | test_description='our own option parser' |
| 7 | |
| 8 | . ./test-lib.sh |
| 9 | |
Pranit Bauva | 8425b7e | 2016-04-12 23:02:17 +0000 | [diff] [blame] | 10 | cat >expect <<\EOF |
Nguyễn Thái Ngọc Duy | 2f17c78 | 2018-09-09 19:36:29 +0200 | [diff] [blame] | 11 | usage: test-tool parse-options <options> |
Johannes Schindelin | beb4743 | 2007-10-13 17:34:45 +0100 | [diff] [blame] | 12 | |
Brandon Casey | c97ee17 | 2017-09-24 21:08:03 -0700 | [diff] [blame] | 13 | A helper function for the parse-options API. |
| 14 | |
René Scharfe | b9e63dd | 2012-02-25 20:11:16 +0100 | [diff] [blame] | 15 | --yes get a boolean |
| 16 | -D, --no-doubt begins with 'no-' |
| 17 | -B, --no-fear be brave |
| 18 | -b, --boolean increment by one |
Stephan Beyer | 010a2da | 2008-06-22 17:04:26 +0200 | [diff] [blame] | 19 | -4, --or4 bitwise-or boolean with ...0100 |
René Scharfe | 2f4b97f | 2009-05-07 21:44:17 +0200 | [diff] [blame] | 20 | --neg-or4 same as --no-or4 |
Stephan Beyer | 010a2da | 2008-06-22 17:04:26 +0200 | [diff] [blame] | 21 | |
Johannes Schindelin | beb4743 | 2007-10-13 17:34:45 +0100 | [diff] [blame] | 22 | -i, --integer <n> get a integer |
| 23 | -j <n> get a integer, too |
Charles Bailey | 2a514ed | 2015-06-21 19:25:44 +0100 | [diff] [blame] | 24 | -m, --magnitude <n> get a magnitude |
Stephan Beyer | 010a2da | 2008-06-22 17:04:26 +0200 | [diff] [blame] | 25 | --set23 set integer to 23 |
Paolo Bonzini | 62e7a6f | 2020-02-20 15:15:15 +0100 | [diff] [blame] | 26 | --mode1 set integer to 1 (cmdmode option) |
| 27 | --mode2 set integer to 2 (cmdmode option) |
Stephan Beyer | 010a2da | 2008-06-22 17:04:26 +0200 | [diff] [blame] | 28 | -L, --length <str> get length of <str> |
Michael J Gruber | 41dbcd4 | 2011-02-15 14:09:13 +0100 | [diff] [blame] | 29 | -F, --file <file> set file to <file> |
Johannes Schindelin | beb4743 | 2007-10-13 17:34:45 +0100 | [diff] [blame] | 30 | |
Stephan Beyer | 010a2da | 2008-06-22 17:04:26 +0200 | [diff] [blame] | 31 | String options |
Johannes Schindelin | beb4743 | 2007-10-13 17:34:45 +0100 | [diff] [blame] | 32 | -s, --string <string> |
| 33 | get a string |
| 34 | --string2 <str> get another string |
Johannes Schindelin | 243e061 | 2007-11-05 13:15:21 +0000 | [diff] [blame] | 35 | --st <st> get another string (pervert ordering) |
Pierre Habouzit | 3a9f0f4 | 2008-01-26 12:26:57 +0100 | [diff] [blame] | 36 | -o <str> get another string |
Jeff King | c8ba163 | 2011-06-09 11:55:23 -0400 | [diff] [blame] | 37 | --list <str> add str to list |
Johannes Schindelin | beb4743 | 2007-10-13 17:34:45 +0100 | [diff] [blame] | 38 | |
Stephan Beyer | 010a2da | 2008-06-22 17:04:26 +0200 | [diff] [blame] | 39 | Magic arguments |
Pierre Habouzit | 580d5bf | 2008-03-02 11:35:56 +0100 | [diff] [blame] | 40 | --quux means --quux |
René Scharfe | e0319ff | 2009-05-07 21:45:08 +0200 | [diff] [blame] | 41 | -NUM set integer to NUM |
René Scharfe | 51a9949 | 2009-05-07 21:45:42 +0200 | [diff] [blame] | 42 | + same as -b |
Andreas Schwab | 6bbfd1f | 2009-09-25 20:44:44 +0200 | [diff] [blame] | 43 | --ambiguous positive ambiguity |
| 44 | --no-ambiguous negative ambiguity |
Pierre Habouzit | 580d5bf | 2008-03-02 11:35:56 +0100 | [diff] [blame] | 45 | |
Stephan Beyer | 010a2da | 2008-06-22 17:04:26 +0200 | [diff] [blame] | 46 | Standard options |
| 47 | --abbrev[=<n>] use <n> digits to display SHA-1s |
| 48 | -v, --verbose be verbose |
| 49 | -n, --dry-run dry run |
| 50 | -q, --quiet be quiet |
Junio C Hamano | ab6b28b | 2016-05-05 14:36:55 -0700 | [diff] [blame] | 51 | --expect <string> expected output in the variable dump |
Stephan Beyer | 010a2da | 2008-06-22 17:04:26 +0200 | [diff] [blame] | 52 | |
Nguyễn Thái Ngọc Duy | 5c38742 | 2019-04-29 17:05:25 +0700 | [diff] [blame] | 53 | Alias |
| 54 | -A, --alias-source <string> |
| 55 | get a string |
| 56 | -Z, --alias-target <string> |
Junio C Hamano | 7c28058 | 2020-03-16 13:22:54 -0700 | [diff] [blame] | 57 | alias of --alias-source |
Nguyễn Thái Ngọc Duy | 5c38742 | 2019-04-29 17:05:25 +0700 | [diff] [blame] | 58 | |
Johannes Schindelin | beb4743 | 2007-10-13 17:34:45 +0100 | [diff] [blame] | 59 | EOF |
| 60 | |
| 61 | test_expect_success 'test help' ' |
Nguyễn Thái Ngọc Duy | 2f17c78 | 2018-09-09 19:36:29 +0200 | [diff] [blame] | 62 | test_must_fail test-tool parse-options -h >output 2>output.err && |
Junio C Hamano | ca8d148 | 2013-06-09 11:29:20 -0700 | [diff] [blame] | 63 | test_must_be_empty output.err && |
Jiang Xin | 9a00138 | 2012-08-27 13:36:55 +0800 | [diff] [blame] | 64 | test_i18ncmp expect output |
Johannes Schindelin | beb4743 | 2007-10-13 17:34:45 +0100 | [diff] [blame] | 65 | ' |
| 66 | |
Giuseppe Scrivano | 9c7304e | 2010-05-17 17:34:41 +0200 | [diff] [blame] | 67 | mv expect expect.err |
| 68 | |
Junio C Hamano | 8ca65ae | 2016-05-06 11:17:05 -0700 | [diff] [blame] | 69 | check () { |
René Scharfe | b9e63dd | 2012-02-25 20:11:16 +0100 | [diff] [blame] | 70 | what="$1" && |
| 71 | shift && |
| 72 | expect="$1" && |
| 73 | shift && |
Nguyễn Thái Ngọc Duy | 2f17c78 | 2018-09-09 19:36:29 +0200 | [diff] [blame] | 74 | test-tool parse-options --expect="$what $expect" "$@" |
René Scharfe | b9e63dd | 2012-02-25 20:11:16 +0100 | [diff] [blame] | 75 | } |
| 76 | |
Jiang Xin | 9a00138 | 2012-08-27 13:36:55 +0800 | [diff] [blame] | 77 | check_unknown_i18n() { |
| 78 | case "$1" in |
| 79 | --*) |
| 80 | echo error: unknown option \`${1#--}\' >expect ;; |
| 81 | -*) |
| 82 | echo error: unknown switch \`${1#-}\' >expect ;; |
| 83 | esac && |
| 84 | cat expect.err >>expect && |
Nguyễn Thái Ngọc Duy | 2f17c78 | 2018-09-09 19:36:29 +0200 | [diff] [blame] | 85 | test_must_fail test-tool parse-options $* >output 2>output.err && |
Junio C Hamano | ca8d148 | 2013-06-09 11:29:20 -0700 | [diff] [blame] | 86 | test_must_be_empty output && |
Jiang Xin | 9a00138 | 2012-08-27 13:36:55 +0800 | [diff] [blame] | 87 | test_i18ncmp expect output.err |
| 88 | } |
| 89 | |
René Scharfe | b9e63dd | 2012-02-25 20:11:16 +0100 | [diff] [blame] | 90 | test_expect_success 'OPT_BOOL() #1' 'check boolean: 1 --yes' |
| 91 | test_expect_success 'OPT_BOOL() #2' 'check boolean: 1 --no-doubt' |
| 92 | test_expect_success 'OPT_BOOL() #3' 'check boolean: 1 -D' |
| 93 | test_expect_success 'OPT_BOOL() #4' 'check boolean: 1 --no-fear' |
| 94 | test_expect_success 'OPT_BOOL() #5' 'check boolean: 1 -B' |
| 95 | |
| 96 | test_expect_success 'OPT_BOOL() is idempotent #1' 'check boolean: 1 --yes --yes' |
| 97 | test_expect_success 'OPT_BOOL() is idempotent #2' 'check boolean: 1 -DB' |
| 98 | |
| 99 | test_expect_success 'OPT_BOOL() negation #1' 'check boolean: 0 -D --no-yes' |
| 100 | test_expect_success 'OPT_BOOL() negation #2' 'check boolean: 0 -D --no-no-doubt' |
| 101 | |
Brandon Casey | 1a9bf1e | 2017-09-24 21:08:04 -0700 | [diff] [blame] | 102 | test_expect_success 'OPT_BOOL() no negation #1' 'check_unknown_i18n --fear' |
| 103 | test_expect_success 'OPT_BOOL() no negation #2' 'check_unknown_i18n --no-no-fear' |
René Scharfe | b9e63dd | 2012-02-25 20:11:16 +0100 | [diff] [blame] | 104 | |
René Scharfe | 0f1930c5 | 2012-02-25 20:14:54 +0100 | [diff] [blame] | 105 | test_expect_success 'OPT_BOOL() positivation' 'check boolean: 0 -D --doubt' |
René Scharfe | b9e63dd | 2012-02-25 20:11:16 +0100 | [diff] [blame] | 106 | |
Charles Bailey | 81a48cc | 2015-06-21 19:25:43 +0100 | [diff] [blame] | 107 | test_expect_success 'OPT_INT() negative' 'check integer: -2345 -i -2345' |
| 108 | |
Charles Bailey | 2a514ed | 2015-06-21 19:25:44 +0100 | [diff] [blame] | 109 | test_expect_success 'OPT_MAGNITUDE() simple' ' |
| 110 | check magnitude: 2345678 -m 2345678 |
| 111 | ' |
| 112 | |
| 113 | test_expect_success 'OPT_MAGNITUDE() kilo' ' |
| 114 | check magnitude: 239616 -m 234k |
| 115 | ' |
| 116 | |
| 117 | test_expect_success 'OPT_MAGNITUDE() mega' ' |
| 118 | check magnitude: 104857600 -m 100m |
| 119 | ' |
| 120 | |
| 121 | test_expect_success 'OPT_MAGNITUDE() giga' ' |
| 122 | check magnitude: 1073741824 -m 1g |
| 123 | ' |
| 124 | |
| 125 | test_expect_success 'OPT_MAGNITUDE() 3giga' ' |
| 126 | check magnitude: 3221225472 -m 3g |
| 127 | ' |
| 128 | |
Pranit Bauva | 8425b7e | 2016-04-12 23:02:17 +0000 | [diff] [blame] | 129 | cat >expect <<\EOF |
Johannes Schindelin | beb4743 | 2007-10-13 17:34:45 +0100 | [diff] [blame] | 130 | boolean: 2 |
| 131 | integer: 1729 |
Charles Bailey | 2a514ed | 2015-06-21 19:25:44 +0100 | [diff] [blame] | 132 | magnitude: 16384 |
Junio C Hamano | c4aca9c | 2008-07-30 12:53:45 -0700 | [diff] [blame] | 133 | timestamp: 0 |
Johannes Schindelin | beb4743 | 2007-10-13 17:34:45 +0100 | [diff] [blame] | 134 | string: 123 |
Stephan Beyer | 010a2da | 2008-06-22 17:04:26 +0200 | [diff] [blame] | 135 | abbrev: 7 |
| 136 | verbose: 2 |
Pranit Bauva | 36e6a5b | 2016-04-12 23:02:17 +0000 | [diff] [blame] | 137 | quiet: 0 |
Stephan Beyer | 010a2da | 2008-06-22 17:04:26 +0200 | [diff] [blame] | 138 | dry run: yes |
Stephen Boyd | df217ed | 2009-05-23 11:53:13 -0700 | [diff] [blame] | 139 | file: prefix/my.file |
Johannes Schindelin | beb4743 | 2007-10-13 17:34:45 +0100 | [diff] [blame] | 140 | EOF |
| 141 | |
| 142 | test_expect_success 'short options' ' |
Nguyễn Thái Ngọc Duy | 2f17c78 | 2018-09-09 19:36:29 +0200 | [diff] [blame] | 143 | test-tool parse-options -s123 -b -i 1729 -m 16k -b -vv -n -F my.file \ |
Charles Bailey | 2a514ed | 2015-06-21 19:25:44 +0100 | [diff] [blame] | 144 | >output 2>output.err && |
Junio C Hamano | 3af8286 | 2008-05-23 22:28:56 -0700 | [diff] [blame] | 145 | test_cmp expect output && |
Junio C Hamano | ca8d148 | 2013-06-09 11:29:20 -0700 | [diff] [blame] | 146 | test_must_be_empty output.err |
Johannes Schindelin | beb4743 | 2007-10-13 17:34:45 +0100 | [diff] [blame] | 147 | ' |
Stephan Beyer | 010a2da | 2008-06-22 17:04:26 +0200 | [diff] [blame] | 148 | |
Pranit Bauva | 8425b7e | 2016-04-12 23:02:17 +0000 | [diff] [blame] | 149 | cat >expect <<\EOF |
Johannes Schindelin | beb4743 | 2007-10-13 17:34:45 +0100 | [diff] [blame] | 150 | boolean: 2 |
| 151 | integer: 1729 |
Charles Bailey | 2a514ed | 2015-06-21 19:25:44 +0100 | [diff] [blame] | 152 | magnitude: 16384 |
Junio C Hamano | c4aca9c | 2008-07-30 12:53:45 -0700 | [diff] [blame] | 153 | timestamp: 0 |
Johannes Schindelin | beb4743 | 2007-10-13 17:34:45 +0100 | [diff] [blame] | 154 | string: 321 |
Stephan Beyer | 010a2da | 2008-06-22 17:04:26 +0200 | [diff] [blame] | 155 | abbrev: 10 |
| 156 | verbose: 2 |
Pranit Bauva | 36e6a5b | 2016-04-12 23:02:17 +0000 | [diff] [blame] | 157 | quiet: 0 |
Stephan Beyer | 010a2da | 2008-06-22 17:04:26 +0200 | [diff] [blame] | 158 | dry run: no |
Stephen Boyd | df217ed | 2009-05-23 11:53:13 -0700 | [diff] [blame] | 159 | file: prefix/fi.le |
Johannes Schindelin | beb4743 | 2007-10-13 17:34:45 +0100 | [diff] [blame] | 160 | EOF |
| 161 | |
| 162 | test_expect_success 'long options' ' |
Nguyễn Thái Ngọc Duy | 2f17c78 | 2018-09-09 19:36:29 +0200 | [diff] [blame] | 163 | test-tool parse-options --boolean --integer 1729 --magnitude 16k \ |
Charles Bailey | 2a514ed | 2015-06-21 19:25:44 +0100 | [diff] [blame] | 164 | --boolean --string2=321 --verbose --verbose --no-dry-run \ |
| 165 | --abbrev=10 --file fi.le --obsolete \ |
| 166 | >output 2>output.err && |
Junio C Hamano | ca8d148 | 2013-06-09 11:29:20 -0700 | [diff] [blame] | 167 | test_must_be_empty output.err && |
Junio C Hamano | 3af8286 | 2008-05-23 22:28:56 -0700 | [diff] [blame] | 168 | test_cmp expect output |
Johannes Schindelin | beb4743 | 2007-10-13 17:34:45 +0100 | [diff] [blame] | 169 | ' |
| 170 | |
Olivier Marin | d5d745f | 2008-07-21 20:30:36 +0200 | [diff] [blame] | 171 | test_expect_success 'missing required value' ' |
Nguyễn Thái Ngọc Duy | 2f17c78 | 2018-09-09 19:36:29 +0200 | [diff] [blame] | 172 | test_expect_code 129 test-tool parse-options -s && |
| 173 | test_expect_code 129 test-tool parse-options --string && |
| 174 | test_expect_code 129 test-tool parse-options --file |
Olivier Marin | d5d745f | 2008-07-21 20:30:36 +0200 | [diff] [blame] | 175 | ' |
| 176 | |
Pranit Bauva | 8425b7e | 2016-04-12 23:02:17 +0000 | [diff] [blame] | 177 | cat >expect <<\EOF |
Johannes Schindelin | beb4743 | 2007-10-13 17:34:45 +0100 | [diff] [blame] | 178 | boolean: 1 |
| 179 | integer: 13 |
Charles Bailey | 2a514ed | 2015-06-21 19:25:44 +0100 | [diff] [blame] | 180 | magnitude: 0 |
Junio C Hamano | c4aca9c | 2008-07-30 12:53:45 -0700 | [diff] [blame] | 181 | timestamp: 0 |
Johannes Schindelin | beb4743 | 2007-10-13 17:34:45 +0100 | [diff] [blame] | 182 | string: 123 |
Stephan Beyer | 010a2da | 2008-06-22 17:04:26 +0200 | [diff] [blame] | 183 | abbrev: 7 |
Pranit Bauva | e0070e8 | 2016-05-05 15:20:00 +0530 | [diff] [blame] | 184 | verbose: -1 |
Pranit Bauva | 36e6a5b | 2016-04-12 23:02:17 +0000 | [diff] [blame] | 185 | quiet: 0 |
Stephan Beyer | 010a2da | 2008-06-22 17:04:26 +0200 | [diff] [blame] | 186 | dry run: no |
Stephen Boyd | df217ed | 2009-05-23 11:53:13 -0700 | [diff] [blame] | 187 | file: (not set) |
Johannes Schindelin | beb4743 | 2007-10-13 17:34:45 +0100 | [diff] [blame] | 188 | arg 00: a1 |
| 189 | arg 01: b1 |
| 190 | arg 02: --boolean |
| 191 | EOF |
| 192 | |
| 193 | test_expect_success 'intermingled arguments' ' |
Nguyễn Thái Ngọc Duy | 2f17c78 | 2018-09-09 19:36:29 +0200 | [diff] [blame] | 194 | test-tool parse-options a1 --string 123 b1 --boolean -j 13 -- --boolean \ |
Pranit Bauva | 8425b7e | 2016-04-12 23:02:17 +0000 | [diff] [blame] | 195 | >output 2>output.err && |
Junio C Hamano | ca8d148 | 2013-06-09 11:29:20 -0700 | [diff] [blame] | 196 | test_must_be_empty output.err && |
Junio C Hamano | 3af8286 | 2008-05-23 22:28:56 -0700 | [diff] [blame] | 197 | test_cmp expect output |
Johannes Schindelin | beb4743 | 2007-10-13 17:34:45 +0100 | [diff] [blame] | 198 | ' |
| 199 | |
Pranit Bauva | 8425b7e | 2016-04-12 23:02:17 +0000 | [diff] [blame] | 200 | cat >expect <<\EOF |
Johannes Schindelin | 7f275b9 | 2007-10-14 17:54:06 +0100 | [diff] [blame] | 201 | boolean: 0 |
| 202 | integer: 2 |
Charles Bailey | 2a514ed | 2015-06-21 19:25:44 +0100 | [diff] [blame] | 203 | magnitude: 0 |
Junio C Hamano | c4aca9c | 2008-07-30 12:53:45 -0700 | [diff] [blame] | 204 | timestamp: 0 |
Johannes Schindelin | 7f275b9 | 2007-10-14 17:54:06 +0100 | [diff] [blame] | 205 | string: (not set) |
Stephan Beyer | 010a2da | 2008-06-22 17:04:26 +0200 | [diff] [blame] | 206 | abbrev: 7 |
Pranit Bauva | e0070e8 | 2016-05-05 15:20:00 +0530 | [diff] [blame] | 207 | verbose: -1 |
Pranit Bauva | 36e6a5b | 2016-04-12 23:02:17 +0000 | [diff] [blame] | 208 | quiet: 0 |
Stephan Beyer | 010a2da | 2008-06-22 17:04:26 +0200 | [diff] [blame] | 209 | dry run: no |
Stephen Boyd | df217ed | 2009-05-23 11:53:13 -0700 | [diff] [blame] | 210 | file: (not set) |
Johannes Schindelin | 7f275b9 | 2007-10-14 17:54:06 +0100 | [diff] [blame] | 211 | EOF |
| 212 | |
| 213 | test_expect_success 'unambiguously abbreviated option' ' |
Johannes Schindelin | b02e7d5 | 2019-04-12 02:37:24 -0700 | [diff] [blame] | 214 | GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \ |
Nguyễn Thái Ngọc Duy | 2f17c78 | 2018-09-09 19:36:29 +0200 | [diff] [blame] | 215 | test-tool parse-options --int 2 --boolean --no-bo >output 2>output.err && |
Junio C Hamano | ca8d148 | 2013-06-09 11:29:20 -0700 | [diff] [blame] | 216 | test_must_be_empty output.err && |
Junio C Hamano | 3af8286 | 2008-05-23 22:28:56 -0700 | [diff] [blame] | 217 | test_cmp expect output |
Johannes Schindelin | 7f275b9 | 2007-10-14 17:54:06 +0100 | [diff] [blame] | 218 | ' |
| 219 | |
| 220 | test_expect_success 'unambiguously abbreviated option with "="' ' |
Johannes Schindelin | b02e7d5 | 2019-04-12 02:37:24 -0700 | [diff] [blame] | 221 | GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \ |
Nguyễn Thái Ngọc Duy | 2f17c78 | 2018-09-09 19:36:29 +0200 | [diff] [blame] | 222 | test-tool parse-options --expect="integer: 2" --int=2 |
Johannes Schindelin | 7f275b9 | 2007-10-14 17:54:06 +0100 | [diff] [blame] | 223 | ' |
| 224 | |
Junio C Hamano | 41ac414 | 2008-02-01 01:50:53 -0800 | [diff] [blame] | 225 | test_expect_success 'ambiguously abbreviated option' ' |
Johannes Schindelin | b02e7d5 | 2019-04-12 02:37:24 -0700 | [diff] [blame] | 226 | test_expect_code 129 env GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \ |
| 227 | test-tool parse-options --strin 123 |
Johannes Schindelin | 7f275b9 | 2007-10-14 17:54:06 +0100 | [diff] [blame] | 228 | ' |
| 229 | |
Johannes Schindelin | 243e061 | 2007-11-05 13:15:21 +0000 | [diff] [blame] | 230 | test_expect_success 'non ambiguous option (after two options it abbreviates)' ' |
Johannes Schindelin | b02e7d5 | 2019-04-12 02:37:24 -0700 | [diff] [blame] | 231 | GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \ |
Nguyễn Thái Ngọc Duy | 2f17c78 | 2018-09-09 19:36:29 +0200 | [diff] [blame] | 232 | test-tool parse-options --expect="string: 123" --st 123 |
Johannes Schindelin | 243e061 | 2007-11-05 13:15:21 +0000 | [diff] [blame] | 233 | ' |
| 234 | |
Nguyễn Thái Ngọc Duy | 5c38742 | 2019-04-29 17:05:25 +0700 | [diff] [blame] | 235 | test_expect_success 'Alias options do not contribute to abbreviation' ' |
| 236 | test-tool parse-options --alias-source 123 >output && |
| 237 | grep "^string: 123" output && |
| 238 | test-tool parse-options --alias-target 123 >output && |
| 239 | grep "^string: 123" output && |
| 240 | test_must_fail test-tool parse-options --alias && |
| 241 | GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \ |
| 242 | test-tool parse-options --alias 123 >output && |
| 243 | grep "^string: 123" output |
| 244 | ' |
| 245 | |
Pranit Bauva | 8425b7e | 2016-04-12 23:02:17 +0000 | [diff] [blame] | 246 | cat >typo.err <<\EOF |
Jacques Bodin-Hullin | 395518c | 2020-02-05 13:07:23 +0000 | [diff] [blame] | 247 | error: did you mean `--boolean` (with two dashes)? |
Pierre Habouzit | 3a9f0f4 | 2008-01-26 12:26:57 +0100 | [diff] [blame] | 248 | EOF |
| 249 | |
| 250 | test_expect_success 'detect possible typos' ' |
Nguyễn Thái Ngọc Duy | 2f17c78 | 2018-09-09 19:36:29 +0200 | [diff] [blame] | 251 | test_must_fail test-tool parse-options -boolean >output 2>output.err && |
Junio C Hamano | ca8d148 | 2013-06-09 11:29:20 -0700 | [diff] [blame] | 252 | test_must_be_empty output && |
Nguyễn Thái Ngọc Duy | 8900342 | 2018-11-10 06:16:13 +0100 | [diff] [blame] | 253 | test_i18ncmp typo.err output.err |
Pierre Habouzit | 3a9f0f4 | 2008-01-26 12:26:57 +0100 | [diff] [blame] | 254 | ' |
| 255 | |
Pranit Bauva | 8425b7e | 2016-04-12 23:02:17 +0000 | [diff] [blame] | 256 | cat >typo.err <<\EOF |
Jacques Bodin-Hullin | 395518c | 2020-02-05 13:07:23 +0000 | [diff] [blame] | 257 | error: did you mean `--ambiguous` (with two dashes)? |
René Scharfe | 38916c5 | 2012-03-03 12:00:29 +0100 | [diff] [blame] | 258 | EOF |
| 259 | |
| 260 | test_expect_success 'detect possible typos' ' |
Nguyễn Thái Ngọc Duy | 2f17c78 | 2018-09-09 19:36:29 +0200 | [diff] [blame] | 261 | test_must_fail test-tool parse-options -ambiguous >output 2>output.err && |
Junio C Hamano | ca8d148 | 2013-06-09 11:29:20 -0700 | [diff] [blame] | 262 | test_must_be_empty output && |
Nguyễn Thái Ngọc Duy | 8900342 | 2018-11-10 06:16:13 +0100 | [diff] [blame] | 263 | test_i18ncmp typo.err output.err |
René Scharfe | 38916c5 | 2012-03-03 12:00:29 +0100 | [diff] [blame] | 264 | ' |
| 265 | |
Pierre Habouzit | 580d5bf | 2008-03-02 11:35:56 +0100 | [diff] [blame] | 266 | test_expect_success 'keep some options as arguments' ' |
Nguyễn Thái Ngọc Duy | 2f17c78 | 2018-09-09 19:36:29 +0200 | [diff] [blame] | 267 | test-tool parse-options --expect="arg 00: --quux" --quux |
Pierre Habouzit | 580d5bf | 2008-03-02 11:35:56 +0100 | [diff] [blame] | 268 | ' |
| 269 | |
Pranit Bauva | 8425b7e | 2016-04-12 23:02:17 +0000 | [diff] [blame] | 270 | cat >expect <<\EOF |
Stephan Beyer | 010a2da | 2008-06-22 17:04:26 +0200 | [diff] [blame] | 271 | Callback: "four", 0 |
| 272 | boolean: 5 |
| 273 | integer: 4 |
Charles Bailey | 2a514ed | 2015-06-21 19:25:44 +0100 | [diff] [blame] | 274 | magnitude: 0 |
Junio C Hamano | c4aca9c | 2008-07-30 12:53:45 -0700 | [diff] [blame] | 275 | timestamp: 0 |
Stephan Beyer | 010a2da | 2008-06-22 17:04:26 +0200 | [diff] [blame] | 276 | string: (not set) |
| 277 | abbrev: 7 |
Pranit Bauva | e0070e8 | 2016-05-05 15:20:00 +0530 | [diff] [blame] | 278 | verbose: -1 |
Pranit Bauva | 36e6a5b | 2016-04-12 23:02:17 +0000 | [diff] [blame] | 279 | quiet: 0 |
Stephan Beyer | 010a2da | 2008-06-22 17:04:26 +0200 | [diff] [blame] | 280 | dry run: no |
Stephen Boyd | df217ed | 2009-05-23 11:53:13 -0700 | [diff] [blame] | 281 | file: (not set) |
Stephan Beyer | 010a2da | 2008-06-22 17:04:26 +0200 | [diff] [blame] | 282 | EOF |
| 283 | |
| 284 | test_expect_success 'OPT_CALLBACK() and OPT_BIT() work' ' |
Nguyễn Thái Ngọc Duy | 2f17c78 | 2018-09-09 19:36:29 +0200 | [diff] [blame] | 285 | test-tool parse-options --length=four -b -4 >output 2>output.err && |
Junio C Hamano | ca8d148 | 2013-06-09 11:29:20 -0700 | [diff] [blame] | 286 | test_must_be_empty output.err && |
Stephan Beyer | 010a2da | 2008-06-22 17:04:26 +0200 | [diff] [blame] | 287 | test_cmp expect output |
| 288 | ' |
| 289 | |
Brandon Casey | 1a9bf1e | 2017-09-24 21:08:04 -0700 | [diff] [blame] | 290 | test_expect_success 'OPT_CALLBACK() and callback errors work' ' |
Nguyễn Thái Ngọc Duy | 2f17c78 | 2018-09-09 19:36:29 +0200 | [diff] [blame] | 291 | test_must_fail test-tool parse-options --no-length >output 2>output.err && |
SZEDER Gábor | 1c5e94f | 2018-08-19 23:57:25 +0200 | [diff] [blame] | 292 | test_must_be_empty output && |
Paul-Sebastian Ungureanu | 3bb0923 | 2018-03-22 20:43:51 +0200 | [diff] [blame] | 293 | test_must_be_empty output.err |
Stephan Beyer | 010a2da | 2008-06-22 17:04:26 +0200 | [diff] [blame] | 294 | ' |
| 295 | |
Pranit Bauva | 8425b7e | 2016-04-12 23:02:17 +0000 | [diff] [blame] | 296 | cat >expect <<\EOF |
Stephan Beyer | 010a2da | 2008-06-22 17:04:26 +0200 | [diff] [blame] | 297 | boolean: 1 |
| 298 | integer: 23 |
Charles Bailey | 2a514ed | 2015-06-21 19:25:44 +0100 | [diff] [blame] | 299 | magnitude: 0 |
Junio C Hamano | c4aca9c | 2008-07-30 12:53:45 -0700 | [diff] [blame] | 300 | timestamp: 0 |
Stephan Beyer | 010a2da | 2008-06-22 17:04:26 +0200 | [diff] [blame] | 301 | string: (not set) |
| 302 | abbrev: 7 |
Pranit Bauva | e0070e8 | 2016-05-05 15:20:00 +0530 | [diff] [blame] | 303 | verbose: -1 |
Pranit Bauva | 36e6a5b | 2016-04-12 23:02:17 +0000 | [diff] [blame] | 304 | quiet: 0 |
Stephan Beyer | 010a2da | 2008-06-22 17:04:26 +0200 | [diff] [blame] | 305 | dry run: no |
Stephen Boyd | df217ed | 2009-05-23 11:53:13 -0700 | [diff] [blame] | 306 | file: (not set) |
Stephan Beyer | 010a2da | 2008-06-22 17:04:26 +0200 | [diff] [blame] | 307 | EOF |
| 308 | |
| 309 | test_expect_success 'OPT_BIT() and OPT_SET_INT() work' ' |
Nguyễn Thái Ngọc Duy | 2f17c78 | 2018-09-09 19:36:29 +0200 | [diff] [blame] | 310 | test-tool parse-options --set23 -bbbbb --no-or4 >output 2>output.err && |
Junio C Hamano | ca8d148 | 2013-06-09 11:29:20 -0700 | [diff] [blame] | 311 | test_must_be_empty output.err && |
Stephan Beyer | 010a2da | 2008-06-22 17:04:26 +0200 | [diff] [blame] | 312 | test_cmp expect output |
| 313 | ' |
| 314 | |
René Scharfe | 2f4b97f | 2009-05-07 21:44:17 +0200 | [diff] [blame] | 315 | test_expect_success 'OPT_NEGBIT() and OPT_SET_INT() work' ' |
Nguyễn Thái Ngọc Duy | 2f17c78 | 2018-09-09 19:36:29 +0200 | [diff] [blame] | 316 | test-tool parse-options --set23 -bbbbb --neg-or4 >output 2>output.err && |
Junio C Hamano | ca8d148 | 2013-06-09 11:29:20 -0700 | [diff] [blame] | 317 | test_must_be_empty output.err && |
René Scharfe | 2f4b97f | 2009-05-07 21:44:17 +0200 | [diff] [blame] | 318 | test_cmp expect output |
| 319 | ' |
| 320 | |
René Scharfe | 2f4b97f | 2009-05-07 21:44:17 +0200 | [diff] [blame] | 321 | test_expect_success 'OPT_BIT() works' ' |
Nguyễn Thái Ngọc Duy | 2f17c78 | 2018-09-09 19:36:29 +0200 | [diff] [blame] | 322 | test-tool parse-options --expect="boolean: 6" -bb --or4 |
René Scharfe | 2f4b97f | 2009-05-07 21:44:17 +0200 | [diff] [blame] | 323 | ' |
| 324 | |
| 325 | test_expect_success 'OPT_NEGBIT() works' ' |
Nguyễn Thái Ngọc Duy | 2f17c78 | 2018-09-09 19:36:29 +0200 | [diff] [blame] | 326 | test-tool parse-options --expect="boolean: 6" -bb --no-neg-or4 |
René Scharfe | 2f4b97f | 2009-05-07 21:44:17 +0200 | [diff] [blame] | 327 | ' |
Stephan Beyer | 010a2da | 2008-06-22 17:04:26 +0200 | [diff] [blame] | 328 | |
Paolo Bonzini | 62e7a6f | 2020-02-20 15:15:15 +0100 | [diff] [blame] | 329 | test_expect_success 'OPT_CMDMODE() works' ' |
| 330 | test-tool parse-options --expect="integer: 1" --mode1 |
| 331 | ' |
| 332 | |
| 333 | test_expect_success 'OPT_CMDMODE() detects incompatibility' ' |
| 334 | test_must_fail test-tool parse-options --mode1 --mode2 >output 2>output.err && |
| 335 | test_must_be_empty output && |
| 336 | test_i18ngrep "incompatible with --mode" output.err |
| 337 | ' |
| 338 | |
| 339 | test_expect_success 'OPT_CMDMODE() detects incompatibility with something else' ' |
| 340 | test_must_fail test-tool parse-options --set23 --mode2 >output 2>output.err && |
| 341 | test_must_be_empty output && |
| 342 | test_i18ngrep "incompatible with something else" output.err |
| 343 | ' |
| 344 | |
René Scharfe | b9e63dd | 2012-02-25 20:11:16 +0100 | [diff] [blame] | 345 | test_expect_success 'OPT_COUNTUP() with PARSE_OPT_NODASH works' ' |
Nguyễn Thái Ngọc Duy | 2f17c78 | 2018-09-09 19:36:29 +0200 | [diff] [blame] | 346 | test-tool parse-options --expect="boolean: 6" + + + + + + |
René Scharfe | 51a9949 | 2009-05-07 21:45:42 +0200 | [diff] [blame] | 347 | ' |
| 348 | |
René Scharfe | e0319ff | 2009-05-07 21:45:08 +0200 | [diff] [blame] | 349 | test_expect_success 'OPT_NUMBER_CALLBACK() works' ' |
Nguyễn Thái Ngọc Duy | 2f17c78 | 2018-09-09 19:36:29 +0200 | [diff] [blame] | 350 | test-tool parse-options --expect="integer: 12345" -12345 |
René Scharfe | e0319ff | 2009-05-07 21:45:08 +0200 | [diff] [blame] | 351 | ' |
| 352 | |
Pranit Bauva | 8425b7e | 2016-04-12 23:02:17 +0000 | [diff] [blame] | 353 | cat >expect <<\EOF |
Andreas Schwab | 6bbfd1f | 2009-09-25 20:44:44 +0200 | [diff] [blame] | 354 | boolean: 0 |
| 355 | integer: 0 |
Charles Bailey | 2a514ed | 2015-06-21 19:25:44 +0100 | [diff] [blame] | 356 | magnitude: 0 |
Andreas Schwab | 6bbfd1f | 2009-09-25 20:44:44 +0200 | [diff] [blame] | 357 | timestamp: 0 |
| 358 | string: (not set) |
| 359 | abbrev: 7 |
Pranit Bauva | e0070e8 | 2016-05-05 15:20:00 +0530 | [diff] [blame] | 360 | verbose: -1 |
Pranit Bauva | 36e6a5b | 2016-04-12 23:02:17 +0000 | [diff] [blame] | 361 | quiet: 0 |
Andreas Schwab | 6bbfd1f | 2009-09-25 20:44:44 +0200 | [diff] [blame] | 362 | dry run: no |
| 363 | file: (not set) |
| 364 | EOF |
| 365 | |
| 366 | test_expect_success 'negation of OPT_NONEG flags is not ambiguous' ' |
Johannes Schindelin | b02e7d5 | 2019-04-12 02:37:24 -0700 | [diff] [blame] | 367 | GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \ |
Nguyễn Thái Ngọc Duy | 2f17c78 | 2018-09-09 19:36:29 +0200 | [diff] [blame] | 368 | test-tool parse-options --no-ambig >output 2>output.err && |
Junio C Hamano | ca8d148 | 2013-06-09 11:29:20 -0700 | [diff] [blame] | 369 | test_must_be_empty output.err && |
Andreas Schwab | 6bbfd1f | 2009-09-25 20:44:44 +0200 | [diff] [blame] | 370 | test_cmp expect output |
| 371 | ' |
| 372 | |
Pranit Bauva | 8425b7e | 2016-04-12 23:02:17 +0000 | [diff] [blame] | 373 | cat >>expect <<\EOF |
Jeff King | c8ba163 | 2011-06-09 11:55:23 -0400 | [diff] [blame] | 374 | list: foo |
| 375 | list: bar |
| 376 | list: baz |
| 377 | EOF |
| 378 | test_expect_success '--list keeps list of strings' ' |
Nguyễn Thái Ngọc Duy | 2f17c78 | 2018-09-09 19:36:29 +0200 | [diff] [blame] | 379 | test-tool parse-options --list foo --list=bar --list=baz >output && |
Jeff King | c8ba163 | 2011-06-09 11:55:23 -0400 | [diff] [blame] | 380 | test_cmp expect output |
| 381 | ' |
| 382 | |
| 383 | test_expect_success '--no-list resets list' ' |
Nguyễn Thái Ngọc Duy | 2f17c78 | 2018-09-09 19:36:29 +0200 | [diff] [blame] | 384 | test-tool parse-options --list=other --list=irrelevant --list=options \ |
Jeff King | c8ba163 | 2011-06-09 11:55:23 -0400 | [diff] [blame] | 385 | --no-list --list=foo --list=bar --list=baz >output && |
| 386 | test_cmp expect output |
| 387 | ' |
| 388 | |
Pranit Bauva | 7d17715 | 2016-05-05 15:19:58 +0530 | [diff] [blame] | 389 | test_expect_success 'multiple quiet levels' ' |
Nguyễn Thái Ngọc Duy | 2f17c78 | 2018-09-09 19:36:29 +0200 | [diff] [blame] | 390 | test-tool parse-options --expect="quiet: 3" -q -q -q |
Pranit Bauva | 7d17715 | 2016-05-05 15:19:58 +0530 | [diff] [blame] | 391 | ' |
| 392 | |
Pranit Bauva | 7d17715 | 2016-05-05 15:19:58 +0530 | [diff] [blame] | 393 | test_expect_success 'multiple verbose levels' ' |
Nguyễn Thái Ngọc Duy | 2f17c78 | 2018-09-09 19:36:29 +0200 | [diff] [blame] | 394 | test-tool parse-options --expect="verbose: 3" -v -v -v |
Pranit Bauva | 7d17715 | 2016-05-05 15:19:58 +0530 | [diff] [blame] | 395 | ' |
| 396 | |
Pranit Bauva | 7d17715 | 2016-05-05 15:19:58 +0530 | [diff] [blame] | 397 | test_expect_success '--no-quiet sets --quiet to 0' ' |
Nguyễn Thái Ngọc Duy | 2f17c78 | 2018-09-09 19:36:29 +0200 | [diff] [blame] | 398 | test-tool parse-options --expect="quiet: 0" --no-quiet |
Pranit Bauva | 7d17715 | 2016-05-05 15:19:58 +0530 | [diff] [blame] | 399 | ' |
| 400 | |
Pranit Bauva | 7d17715 | 2016-05-05 15:19:58 +0530 | [diff] [blame] | 401 | test_expect_success '--no-quiet resets multiple -q to 0' ' |
Nguyễn Thái Ngọc Duy | 2f17c78 | 2018-09-09 19:36:29 +0200 | [diff] [blame] | 402 | test-tool parse-options --expect="quiet: 0" -q -q -q --no-quiet |
Pranit Bauva | 7d17715 | 2016-05-05 15:19:58 +0530 | [diff] [blame] | 403 | ' |
| 404 | |
Pranit Bauva | 7d17715 | 2016-05-05 15:19:58 +0530 | [diff] [blame] | 405 | test_expect_success '--no-verbose sets verbose to 0' ' |
Nguyễn Thái Ngọc Duy | 2f17c78 | 2018-09-09 19:36:29 +0200 | [diff] [blame] | 406 | test-tool parse-options --expect="verbose: 0" --no-verbose |
Pranit Bauva | 7d17715 | 2016-05-05 15:19:58 +0530 | [diff] [blame] | 407 | ' |
| 408 | |
Pranit Bauva | 7d17715 | 2016-05-05 15:19:58 +0530 | [diff] [blame] | 409 | test_expect_success '--no-verbose resets multiple verbose to 0' ' |
Nguyễn Thái Ngọc Duy | 2f17c78 | 2018-09-09 19:36:29 +0200 | [diff] [blame] | 410 | test-tool parse-options --expect="verbose: 0" -v -v -v --no-verbose |
Pranit Bauva | 7d17715 | 2016-05-05 15:19:58 +0530 | [diff] [blame] | 411 | ' |
| 412 | |
Johannes Schindelin | b02e7d5 | 2019-04-12 02:37:24 -0700 | [diff] [blame] | 413 | test_expect_success 'GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS works' ' |
| 414 | GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \ |
| 415 | test-tool parse-options --ye && |
| 416 | test_must_fail env GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=true \ |
| 417 | test-tool parse-options --ye |
| 418 | ' |
| 419 | |
Jeff King | 51b4594 | 2019-08-06 10:40:16 -0400 | [diff] [blame] | 420 | test_expect_success '--end-of-options treats remainder as args' ' |
| 421 | test-tool parse-options \ |
| 422 | --expect="verbose: -1" \ |
| 423 | --expect="arg 00: --verbose" \ |
| 424 | --end-of-options --verbose |
| 425 | ' |
| 426 | |
Johannes Schindelin | beb4743 | 2007-10-13 17:34:45 +0100 | [diff] [blame] | 427 | test_done |