| 1 | #!/bin/sh |
| 2 | |
| 3 | test_description='Test git config-set API in different settings' |
| 4 | |
| 5 | . ./test-lib.sh |
| 6 | |
| 7 | # 'check_config get_* section.key value' verifies that the entry for |
| 8 | # section.key is 'value' |
| 9 | check_config () { |
| 10 | if test "$1" = expect_code |
| 11 | then |
| 12 | expect_code="$2" && shift && shift |
| 13 | else |
| 14 | expect_code=0 |
| 15 | fi && |
| 16 | op=$1 key=$2 && shift && shift && |
| 17 | if test $# != 0 |
| 18 | then |
| 19 | printf "%s\n" "$@" |
| 20 | fi >expect && |
| 21 | test_expect_code $expect_code test-tool config "$op" "$key" >actual && |
| 22 | test_cmp expect actual |
| 23 | } |
| 24 | |
| 25 | test_expect_success 'setup default config' ' |
| 26 | cat >.git/config <<-\EOF |
| 27 | [case] |
| 28 | penguin = very blue |
| 29 | Movie = BadPhysics |
| 30 | UPPERCASE = true |
| 31 | MixedCase = true |
| 32 | my = |
| 33 | foo |
| 34 | baz = sam |
| 35 | [Cores] |
| 36 | WhatEver = Second |
| 37 | baz = bar |
| 38 | [cores] |
| 39 | baz = bat |
| 40 | [CORES] |
| 41 | baz = ball |
| 42 | [my "Foo bAr"] |
| 43 | hi = mixed-case |
| 44 | [my "FOO BAR"] |
| 45 | hi = upper-case |
| 46 | [my "foo bar"] |
| 47 | hi = lower-case |
| 48 | [case] |
| 49 | baz = bat |
| 50 | baz = hask |
| 51 | [lamb] |
| 52 | chop = 65 |
| 53 | head = none |
| 54 | [goat] |
| 55 | legs = 4 |
| 56 | head = true |
| 57 | skin = false |
| 58 | nose = 1 |
| 59 | horns |
| 60 | [value] |
| 61 | less |
| 62 | EOF |
| 63 | ' |
| 64 | |
| 65 | test_expect_success 'get value for a simple key' ' |
| 66 | check_config get_value case.penguin "very blue" |
| 67 | ' |
| 68 | |
| 69 | test_expect_success 'get value for a key with value as an empty string' ' |
| 70 | check_config get_value case.my "" |
| 71 | ' |
| 72 | |
| 73 | test_expect_success 'get value for a key with value as NULL' ' |
| 74 | check_config get_value case.foo "(NULL)" |
| 75 | ' |
| 76 | |
| 77 | test_expect_success 'upper case key' ' |
| 78 | check_config get_value case.UPPERCASE "true" && |
| 79 | check_config get_value case.uppercase "true" |
| 80 | ' |
| 81 | |
| 82 | test_expect_success 'mixed case key' ' |
| 83 | check_config get_value case.MixedCase "true" && |
| 84 | check_config get_value case.MIXEDCASE "true" && |
| 85 | check_config get_value case.mixedcase "true" |
| 86 | ' |
| 87 | |
| 88 | test_expect_success 'key and value with mixed case' ' |
| 89 | check_config get_value case.Movie "BadPhysics" |
| 90 | ' |
| 91 | |
| 92 | test_expect_success 'key with case sensitive subsection' ' |
| 93 | check_config get_value "my.Foo bAr.hi" "mixed-case" && |
| 94 | check_config get_value "my.FOO BAR.hi" "upper-case" && |
| 95 | check_config get_value "my.foo bar.hi" "lower-case" |
| 96 | ' |
| 97 | |
| 98 | test_expect_success 'key with case insensitive section header' ' |
| 99 | check_config get_value cores.baz "ball" && |
| 100 | check_config get_value Cores.baz "ball" && |
| 101 | check_config get_value CORES.baz "ball" && |
| 102 | check_config get_value coreS.baz "ball" |
| 103 | ' |
| 104 | |
| 105 | test_expect_success 'key with case insensitive section header & variable' ' |
| 106 | check_config get_value CORES.BAZ "ball" && |
| 107 | check_config get_value cores.baz "ball" && |
| 108 | check_config get_value cores.BaZ "ball" && |
| 109 | check_config get_value cOreS.bAz "ball" |
| 110 | ' |
| 111 | |
| 112 | test_expect_success 'find value with misspelled key' ' |
| 113 | check_config expect_code 1 get_value "my.fOo Bar.hi" "Value not found for \"my.fOo Bar.hi\"" |
| 114 | ' |
| 115 | |
| 116 | test_expect_success 'find value with the highest priority' ' |
| 117 | check_config get_value case.baz "hask" |
| 118 | ' |
| 119 | |
| 120 | test_expect_success 'return value for an existing key' ' |
| 121 | test-tool config get lamb.chop >out 2>err && |
| 122 | test_must_be_empty out && |
| 123 | test_must_be_empty err |
| 124 | ' |
| 125 | |
| 126 | test_expect_success 'return value for value-less key' ' |
| 127 | test-tool config get value.less >out 2>err && |
| 128 | test_must_be_empty out && |
| 129 | test_must_be_empty err |
| 130 | ' |
| 131 | |
| 132 | test_expect_success 'return value for a missing key' ' |
| 133 | cat >expect <<-\EOF && |
| 134 | Value not found for "missing.key" |
| 135 | EOF |
| 136 | test_expect_code 1 test-tool config get missing.key >actual 2>err && |
| 137 | test_cmp actual expect && |
| 138 | test_must_be_empty err |
| 139 | ' |
| 140 | |
| 141 | test_expect_success 'return value for a bad key: CONFIG_INVALID_KEY' ' |
| 142 | cat >expect <<-\EOF && |
| 143 | Key "fails.iskeychar.-" is invalid |
| 144 | EOF |
| 145 | test_expect_code 1 test-tool config get fails.iskeychar.- >actual 2>err && |
| 146 | test_cmp actual expect && |
| 147 | test_must_be_empty out |
| 148 | ' |
| 149 | |
| 150 | test_expect_success 'return value for a bad key: CONFIG_NO_SECTION_OR_NAME' ' |
| 151 | cat >expect <<-\EOF && |
| 152 | Key "keynosection" has no section |
| 153 | EOF |
| 154 | test_expect_code 1 test-tool config get keynosection >actual 2>err && |
| 155 | test_cmp actual expect && |
| 156 | test_must_be_empty out |
| 157 | ' |
| 158 | |
| 159 | test_expect_success 'find integer value for a key' ' |
| 160 | check_config get_int lamb.chop 65 |
| 161 | ' |
| 162 | |
| 163 | test_expect_success 'parse integer value during iteration' ' |
| 164 | check_config git_config_int lamb.chop 65 |
| 165 | ' |
| 166 | |
| 167 | test_expect_success 'find string value for a key' ' |
| 168 | check_config get_string case.baz hask && |
| 169 | check_config expect_code 1 get_string case.ba "Value not found for \"case.ba\"" |
| 170 | ' |
| 171 | |
| 172 | test_expect_success 'check line error when NULL string is queried' ' |
| 173 | test_expect_code 128 test-tool config get_string case.foo 2>result && |
| 174 | test_grep "fatal: .*case\.foo.*\.git/config.*line 7" result |
| 175 | ' |
| 176 | |
| 177 | test_expect_success 'find integer if value is non parse-able' ' |
| 178 | check_config expect_code 128 get_int lamb.head |
| 179 | ' |
| 180 | |
| 181 | test_expect_success 'non parse-able integer value during iteration' ' |
| 182 | check_config expect_code 128 git_config_int lamb.head 2>result && |
| 183 | grep "fatal: bad numeric config value .* in file \.git/config" result |
| 184 | ' |
| 185 | |
| 186 | test_expect_success 'find bool value for the entered key' ' |
| 187 | check_config get_bool goat.head 1 && |
| 188 | check_config get_bool goat.skin 0 && |
| 189 | check_config get_bool goat.nose 1 && |
| 190 | check_config get_bool goat.horns 1 && |
| 191 | check_config get_bool goat.legs 1 |
| 192 | ' |
| 193 | |
| 194 | test_expect_success 'find multiple values' ' |
| 195 | check_config get_value_multi case.baz sam bat hask |
| 196 | ' |
| 197 | |
| 198 | test_NULL_in_multi () { |
| 199 | local op="$1" && |
| 200 | local file="$2" && |
| 201 | |
| 202 | test_expect_success "$op: NULL value in config${file:+ in $file}" ' |
| 203 | config="$file" && |
| 204 | if test -z "$config" |
| 205 | then |
| 206 | config=.git/config && |
| 207 | test_when_finished "mv $config.old $config" && |
| 208 | mv "$config" "$config".old |
| 209 | fi && |
| 210 | |
| 211 | # Value-less in the middle of a list |
| 212 | cat >"$config" <<-\EOF && |
| 213 | [a]key=x |
| 214 | [a]key |
| 215 | [a]key=y |
| 216 | EOF |
| 217 | case "$op" in |
| 218 | *_multi) |
| 219 | cat >expect <<-\EOF |
| 220 | x |
| 221 | (NULL) |
| 222 | y |
| 223 | EOF |
| 224 | ;; |
| 225 | *) |
| 226 | cat >expect <<-\EOF |
| 227 | y |
| 228 | EOF |
| 229 | ;; |
| 230 | esac && |
| 231 | test-tool config "$op" a.key $file >actual && |
| 232 | test_cmp expect actual && |
| 233 | |
| 234 | # Value-less at the end of a least |
| 235 | cat >"$config" <<-\EOF && |
| 236 | [a]key=x |
| 237 | [a]key=y |
| 238 | [a]key |
| 239 | EOF |
| 240 | case "$op" in |
| 241 | *_multi) |
| 242 | cat >expect <<-\EOF |
| 243 | x |
| 244 | y |
| 245 | (NULL) |
| 246 | EOF |
| 247 | ;; |
| 248 | *) |
| 249 | cat >expect <<-\EOF |
| 250 | (NULL) |
| 251 | EOF |
| 252 | ;; |
| 253 | esac && |
| 254 | test-tool config "$op" a.key $file >actual && |
| 255 | test_cmp expect actual |
| 256 | ' |
| 257 | } |
| 258 | |
| 259 | test_NULL_in_multi "get_value_multi" |
| 260 | test_NULL_in_multi "configset_get_value" "my.config" |
| 261 | test_NULL_in_multi "configset_get_value_multi" "my.config" |
| 262 | |
| 263 | test_expect_success 'find value from a configset' ' |
| 264 | cat >config2 <<-\EOF && |
| 265 | [case] |
| 266 | baz = lama |
| 267 | [my] |
| 268 | new = silk |
| 269 | [case] |
| 270 | baz = ball |
| 271 | EOF |
| 272 | echo silk >expect && |
| 273 | test-tool config configset_get_value my.new config2 .git/config >actual && |
| 274 | test_cmp expect actual |
| 275 | ' |
| 276 | |
| 277 | test_expect_success 'find value with highest priority from a configset' ' |
| 278 | echo hask >expect && |
| 279 | test-tool config configset_get_value case.baz config2 .git/config >actual && |
| 280 | test_cmp expect actual |
| 281 | ' |
| 282 | |
| 283 | test_expect_success 'find value_list for a key from a configset' ' |
| 284 | cat >expect <<-\EOF && |
| 285 | lama |
| 286 | ball |
| 287 | sam |
| 288 | bat |
| 289 | hask |
| 290 | EOF |
| 291 | test-tool config configset_get_value_multi case.baz config2 .git/config >actual && |
| 292 | test_cmp expect actual |
| 293 | ' |
| 294 | |
| 295 | test_expect_success 'proper error on non-existent files' ' |
| 296 | echo "Error (-1) reading configuration file non-existent-file." >expect && |
| 297 | test_expect_code 2 test-tool config configset_get_value foo.bar non-existent-file 2>actual && |
| 298 | test_cmp expect actual |
| 299 | ' |
| 300 | |
| 301 | test_expect_success 'proper error on directory "files"' ' |
| 302 | echo "Error (-1) reading configuration file a-directory." >expect && |
| 303 | mkdir a-directory && |
| 304 | test_expect_code 2 test-tool config configset_get_value foo.bar a-directory 2>output && |
| 305 | grep "^warning:" output && |
| 306 | grep "^Error" output >actual && |
| 307 | test_cmp expect actual |
| 308 | ' |
| 309 | |
| 310 | test_expect_success POSIXPERM,SANITY 'proper error on non-accessible files' ' |
| 311 | chmod -r .git/config && |
| 312 | test_when_finished "chmod +r .git/config" && |
| 313 | echo "Error (-1) reading configuration file .git/config." >expect && |
| 314 | test_expect_code 2 test-tool config configset_get_value foo.bar .git/config 2>output && |
| 315 | grep "^warning:" output && |
| 316 | grep "^Error" output >actual && |
| 317 | test_cmp expect actual |
| 318 | ' |
| 319 | |
| 320 | test_expect_success 'proper error on error in default config files' ' |
| 321 | cp .git/config .git/config.old && |
| 322 | test_when_finished "mv .git/config.old .git/config" && |
| 323 | echo "[" >>.git/config && |
| 324 | echo "fatal: bad config line 36 in file .git/config" >expect && |
| 325 | test_expect_code 128 test-tool config get_value foo.bar 2>actual && |
| 326 | test_cmp expect actual |
| 327 | ' |
| 328 | |
| 329 | test_expect_success 'proper error on error in custom config files' ' |
| 330 | echo "[" >>syntax-error && |
| 331 | echo "fatal: bad config line 1 in file syntax-error" >expect && |
| 332 | test_expect_code 128 test-tool config configset_get_value foo.bar syntax-error 2>actual && |
| 333 | test_cmp expect actual |
| 334 | ' |
| 335 | |
| 336 | test_expect_success 'check line errors for malformed values' ' |
| 337 | mv .git/config .git/config.old && |
| 338 | test_when_finished "mv .git/config.old .git/config" && |
| 339 | cat >.git/config <<-\EOF && |
| 340 | [alias] |
| 341 | br |
| 342 | EOF |
| 343 | test_expect_code 128 git br 2>result && |
| 344 | test_grep "missing value for .alias\.br" result && |
| 345 | test_grep "fatal: .*\.git/config" result && |
| 346 | test_grep "fatal: .*line 2" result |
| 347 | ' |
| 348 | |
| 349 | test_expect_success 'error on modifying repo config without repo' ' |
| 350 | nongit test_must_fail git config a.b c 2>err && |
| 351 | test_grep "not in a git directory" err |
| 352 | ' |
| 353 | |
| 354 | cmdline_config="'foo.bar=from-cmdline'" |
| 355 | test_expect_success 'iteration shows correct origins' ' |
| 356 | printf "[ignore]\n\tthis = please\n[foo]bar = from-repo\n" >.git/config && |
| 357 | printf "[foo]\n\tbar = from-home\n" >.gitconfig && |
| 358 | if test_have_prereq MINGW |
| 359 | then |
| 360 | # Use Windows path (i.e. *not* $HOME) |
| 361 | HOME_GITCONFIG=$(pwd)/.gitconfig |
| 362 | else |
| 363 | # Do not get fooled by symbolic links, i.e. $HOME != $(pwd) |
| 364 | HOME_GITCONFIG=$HOME/.gitconfig |
| 365 | fi && |
| 366 | cat >expect <<-EOF && |
| 367 | key=foo.bar |
| 368 | value=from-home |
| 369 | origin=file |
| 370 | name=$HOME_GITCONFIG |
| 371 | lno=2 |
| 372 | scope=global |
| 373 | |
| 374 | key=ignore.this |
| 375 | value=please |
| 376 | origin=file |
| 377 | name=.git/config |
| 378 | lno=2 |
| 379 | scope=local |
| 380 | |
| 381 | key=foo.bar |
| 382 | value=from-repo |
| 383 | origin=file |
| 384 | name=.git/config |
| 385 | lno=3 |
| 386 | scope=local |
| 387 | |
| 388 | key=foo.bar |
| 389 | value=from-cmdline |
| 390 | origin=command line |
| 391 | name= |
| 392 | lno=-1 |
| 393 | scope=command |
| 394 | EOF |
| 395 | GIT_CONFIG_PARAMETERS=$cmdline_config test-tool config iterate >actual && |
| 396 | test_cmp expect actual |
| 397 | ' |
| 398 | |
| 399 | test_done |