Merge branch 'l10n-de-2.40' of github.com:ralfth/git
[alt-git.git] / t / t1308-config-set.sh
blobb38e158d3b212a0157f5d0f862103713c6a39e57
1 #!/bin/sh
3 test_description='Test git config-set API in different settings'
5 TEST_PASSES_SANITIZE_LEAK=true
6 . ./test-lib.sh
8 # 'check_config get_* section.key value' verifies that the entry for
9 # section.key is 'value'
10 check_config () {
11 if test "$1" = expect_code
12 then
13 expect_code="$2" && shift && shift
14 else
15 expect_code=0
16 fi &&
17 op=$1 key=$2 && shift && shift &&
18 if test $# != 0
19 then
20 printf "%s\n" "$@"
21 fi >expect &&
22 test_expect_code $expect_code test-tool config "$op" "$key" >actual &&
23 test_cmp expect actual
26 test_expect_success 'setup default config' '
27 cat >.git/config <<-\EOF
28 [case]
29 penguin = very blue
30 Movie = BadPhysics
31 UPPERCASE = true
32 MixedCase = true
33 my =
34 foo
35 baz = sam
36 [Cores]
37 WhatEver = Second
38 baz = bar
39 [cores]
40 baz = bat
41 [CORES]
42 baz = ball
43 [my "Foo bAr"]
44 hi = mixed-case
45 [my "FOO BAR"]
46 hi = upper-case
47 [my "foo bar"]
48 hi = lower-case
49 [case]
50 baz = bat
51 baz = hask
52 [lamb]
53 chop = 65
54 head = none
55 [goat]
56 legs = 4
57 head = true
58 skin = false
59 nose = 1
60 horns
61 EOF
64 test_expect_success 'get value for a simple key' '
65 check_config get_value case.penguin "very blue"
68 test_expect_success 'get value for a key with value as an empty string' '
69 check_config get_value case.my ""
72 test_expect_success 'get value for a key with value as NULL' '
73 check_config get_value case.foo "(NULL)"
76 test_expect_success 'upper case key' '
77 check_config get_value case.UPPERCASE "true" &&
78 check_config get_value case.uppercase "true"
81 test_expect_success 'mixed case key' '
82 check_config get_value case.MixedCase "true" &&
83 check_config get_value case.MIXEDCASE "true" &&
84 check_config get_value case.mixedcase "true"
87 test_expect_success 'key and value with mixed case' '
88 check_config get_value case.Movie "BadPhysics"
91 test_expect_success 'key with case sensitive subsection' '
92 check_config get_value "my.Foo bAr.hi" "mixed-case" &&
93 check_config get_value "my.FOO BAR.hi" "upper-case" &&
94 check_config get_value "my.foo bar.hi" "lower-case"
97 test_expect_success 'key with case insensitive section header' '
98 check_config get_value cores.baz "ball" &&
99 check_config get_value Cores.baz "ball" &&
100 check_config get_value CORES.baz "ball" &&
101 check_config get_value coreS.baz "ball"
104 test_expect_success 'key with case insensitive section header & variable' '
105 check_config get_value CORES.BAZ "ball" &&
106 check_config get_value cores.baz "ball" &&
107 check_config get_value cores.BaZ "ball" &&
108 check_config get_value cOreS.bAz "ball"
111 test_expect_success 'find value with misspelled key' '
112 check_config expect_code 1 get_value "my.fOo Bar.hi" "Value not found for \"my.fOo Bar.hi\""
115 test_expect_success 'find value with the highest priority' '
116 check_config get_value case.baz "hask"
119 test_expect_success 'find integer value for a key' '
120 check_config get_int lamb.chop 65
123 test_expect_success 'find string value for a key' '
124 check_config get_string case.baz hask &&
125 check_config expect_code 1 get_string case.ba "Value not found for \"case.ba\""
128 test_expect_success 'check line error when NULL string is queried' '
129 test_expect_code 128 test-tool config get_string case.foo 2>result &&
130 test_i18ngrep "fatal: .*case\.foo.*\.git/config.*line 7" result
133 test_expect_success 'find integer if value is non parse-able' '
134 check_config expect_code 128 get_int lamb.head
137 test_expect_success 'find bool value for the entered key' '
138 check_config get_bool goat.head 1 &&
139 check_config get_bool goat.skin 0 &&
140 check_config get_bool goat.nose 1 &&
141 check_config get_bool goat.horns 1 &&
142 check_config get_bool goat.legs 1
145 test_expect_success 'find multiple values' '
146 check_config get_value_multi case.baz sam bat hask
149 test_expect_success 'find value from a configset' '
150 cat >config2 <<-\EOF &&
151 [case]
152 baz = lama
153 [my]
154 new = silk
155 [case]
156 baz = ball
158 echo silk >expect &&
159 test-tool config configset_get_value my.new config2 .git/config >actual &&
160 test_cmp expect actual
163 test_expect_success 'find value with highest priority from a configset' '
164 echo hask >expect &&
165 test-tool config configset_get_value case.baz config2 .git/config >actual &&
166 test_cmp expect actual
169 test_expect_success 'find value_list for a key from a configset' '
170 cat >expect <<-\EOF &&
171 lama
172 ball
175 hask
177 test-tool config configset_get_value_multi case.baz config2 .git/config >actual &&
178 test_cmp expect actual
181 test_expect_success 'proper error on non-existent files' '
182 echo "Error (-1) reading configuration file non-existent-file." >expect &&
183 test_expect_code 2 test-tool config configset_get_value foo.bar non-existent-file 2>actual &&
184 test_cmp expect actual
187 test_expect_success 'proper error on directory "files"' '
188 echo "Error (-1) reading configuration file a-directory." >expect &&
189 mkdir a-directory &&
190 test_expect_code 2 test-tool config configset_get_value foo.bar a-directory 2>output &&
191 grep "^warning:" output &&
192 grep "^Error" output >actual &&
193 test_cmp expect actual
196 test_expect_success POSIXPERM,SANITY 'proper error on non-accessible files' '
197 chmod -r .git/config &&
198 test_when_finished "chmod +r .git/config" &&
199 echo "Error (-1) reading configuration file .git/config." >expect &&
200 test_expect_code 2 test-tool config configset_get_value foo.bar .git/config 2>output &&
201 grep "^warning:" output &&
202 grep "^Error" output >actual &&
203 test_cmp expect actual
206 test_expect_success 'proper error on error in default config files' '
207 cp .git/config .git/config.old &&
208 test_when_finished "mv .git/config.old .git/config" &&
209 echo "[" >>.git/config &&
210 echo "fatal: bad config line 34 in file .git/config" >expect &&
211 test_expect_code 128 test-tool config get_value foo.bar 2>actual &&
212 test_cmp expect actual
215 test_expect_success 'proper error on error in custom config files' '
216 echo "[" >>syntax-error &&
217 echo "fatal: bad config line 1 in file syntax-error" >expect &&
218 test_expect_code 128 test-tool config configset_get_value foo.bar syntax-error 2>actual &&
219 test_cmp expect actual
222 test_expect_success 'check line errors for malformed values' '
223 mv .git/config .git/config.old &&
224 test_when_finished "mv .git/config.old .git/config" &&
225 cat >.git/config <<-\EOF &&
226 [alias]
229 test_expect_code 128 git br 2>result &&
230 test_i18ngrep "missing value for .alias\.br" result &&
231 test_i18ngrep "fatal: .*\.git/config" result &&
232 test_i18ngrep "fatal: .*line 2" result
235 test_expect_success 'error on modifying repo config without repo' '
236 nongit test_must_fail git config a.b c 2>err &&
237 test_i18ngrep "not in a git directory" err
240 cmdline_config="'foo.bar=from-cmdline'"
241 test_expect_success 'iteration shows correct origins' '
242 printf "[ignore]\n\tthis = please\n[foo]bar = from-repo\n" >.git/config &&
243 printf "[foo]\n\tbar = from-home\n" >.gitconfig &&
244 if test_have_prereq MINGW
245 then
246 # Use Windows path (i.e. *not* $HOME)
247 HOME_GITCONFIG=$(pwd)/.gitconfig
248 else
249 # Do not get fooled by symbolic links, i.e. $HOME != $(pwd)
250 HOME_GITCONFIG=$HOME/.gitconfig
251 fi &&
252 cat >expect <<-EOF &&
253 key=foo.bar
254 value=from-home
255 origin=file
256 name=$HOME_GITCONFIG
257 lno=2
258 scope=global
260 key=ignore.this
261 value=please
262 origin=file
263 name=.git/config
264 lno=2
265 scope=local
267 key=foo.bar
268 value=from-repo
269 origin=file
270 name=.git/config
271 lno=3
272 scope=local
274 key=foo.bar
275 value=from-cmdline
276 origin=command line
277 name=
278 lno=-1
279 scope=command
281 GIT_CONFIG_PARAMETERS=$cmdline_config test-tool config iterate >actual &&
282 test_cmp expect actual
285 test_done