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