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