]> git.ipfire.org Git - thirdparty/git.git/blob - t/t1308-config-set.sh
4be1ab1147cf3f9435c0b27cd7c2836be8761abe
[thirdparty/git.git] / t / t1308-config-set.sh
1 #!/bin/sh
2
3 test_description='Test git config-set API in different settings'
4
5 TEST_PASSES_SANITIZE_LEAK=true
6 . ./test-lib.sh
7
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
24 }
25
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
62 '
63
64 test_expect_success 'get value for a simple key' '
65 check_config get_value case.penguin "very blue"
66 '
67
68 test_expect_success 'get value for a key with value as an empty string' '
69 check_config get_value case.my ""
70 '
71
72 test_expect_success 'get value for a key with value as NULL' '
73 check_config get_value case.foo "(NULL)"
74 '
75
76 test_expect_success 'upper case key' '
77 check_config get_value case.UPPERCASE "true" &&
78 check_config get_value case.uppercase "true"
79 '
80
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"
85 '
86
87 test_expect_success 'key and value with mixed case' '
88 check_config get_value case.Movie "BadPhysics"
89 '
90
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"
95 '
96
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"
102 '
103
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"
109 '
110
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\""
113 '
114
115 test_expect_success 'find value with the highest priority' '
116 check_config get_value case.baz "hask"
117 '
118
119 test_expect_success 'find integer value for a key' '
120 check_config get_int lamb.chop 65
121 '
122
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\""
126 '
127
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
131 '
132
133 test_expect_success 'find integer if value is non parse-able' '
134 check_config expect_code 128 get_int lamb.head
135 '
136
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
143 '
144
145 test_expect_success 'find multiple values' '
146 check_config get_value_multi case.baz sam bat hask
147 '
148
149 test_NULL_in_multi () {
150 local op="$1" &&
151 local file="$2" &&
152
153 test_expect_success "$op: NULL value in config${file:+ in $file}" '
154 config="$file" &&
155 if test -z "$config"
156 then
157 config=.git/config &&
158 test_when_finished "mv $config.old $config" &&
159 mv "$config" "$config".old
160 fi &&
161
162 # Value-less in the middle of a list
163 cat >"$config" <<-\EOF &&
164 [a]key=x
165 [a]key
166 [a]key=y
167 EOF
168 case "$op" in
169 *_multi)
170 cat >expect <<-\EOF
171 x
172 (NULL)
173 y
174 EOF
175 ;;
176 *)
177 cat >expect <<-\EOF
178 y
179 EOF
180 ;;
181 esac &&
182 test-tool config "$op" a.key $file >actual &&
183 test_cmp expect actual &&
184
185 # Value-less at the end of a least
186 cat >"$config" <<-\EOF &&
187 [a]key=x
188 [a]key=y
189 [a]key
190 EOF
191 case "$op" in
192 *_multi)
193 cat >expect <<-\EOF
194 x
195 y
196 (NULL)
197 EOF
198 ;;
199 *)
200 cat >expect <<-\EOF
201 (NULL)
202 EOF
203 ;;
204 esac &&
205 test-tool config "$op" a.key $file >actual &&
206 test_cmp expect actual
207 '
208 }
209
210 test_NULL_in_multi "get_value_multi"
211 test_NULL_in_multi "configset_get_value" "my.config"
212 test_NULL_in_multi "configset_get_value_multi" "my.config"
213
214 test_expect_success 'find value from a configset' '
215 cat >config2 <<-\EOF &&
216 [case]
217 baz = lama
218 [my]
219 new = silk
220 [case]
221 baz = ball
222 EOF
223 echo silk >expect &&
224 test-tool config configset_get_value my.new config2 .git/config >actual &&
225 test_cmp expect actual
226 '
227
228 test_expect_success 'find value with highest priority from a configset' '
229 echo hask >expect &&
230 test-tool config configset_get_value case.baz config2 .git/config >actual &&
231 test_cmp expect actual
232 '
233
234 test_expect_success 'find value_list for a key from a configset' '
235 cat >expect <<-\EOF &&
236 lama
237 ball
238 sam
239 bat
240 hask
241 EOF
242 test-tool config configset_get_value_multi case.baz config2 .git/config >actual &&
243 test_cmp expect actual
244 '
245
246 test_expect_success 'proper error on non-existent files' '
247 echo "Error (-1) reading configuration file non-existent-file." >expect &&
248 test_expect_code 2 test-tool config configset_get_value foo.bar non-existent-file 2>actual &&
249 test_cmp expect actual
250 '
251
252 test_expect_success 'proper error on directory "files"' '
253 echo "Error (-1) reading configuration file a-directory." >expect &&
254 mkdir a-directory &&
255 test_expect_code 2 test-tool config configset_get_value foo.bar a-directory 2>output &&
256 grep "^warning:" output &&
257 grep "^Error" output >actual &&
258 test_cmp expect actual
259 '
260
261 test_expect_success POSIXPERM,SANITY 'proper error on non-accessible files' '
262 chmod -r .git/config &&
263 test_when_finished "chmod +r .git/config" &&
264 echo "Error (-1) reading configuration file .git/config." >expect &&
265 test_expect_code 2 test-tool config configset_get_value foo.bar .git/config 2>output &&
266 grep "^warning:" output &&
267 grep "^Error" output >actual &&
268 test_cmp expect actual
269 '
270
271 test_expect_success 'proper error on error in default config files' '
272 cp .git/config .git/config.old &&
273 test_when_finished "mv .git/config.old .git/config" &&
274 echo "[" >>.git/config &&
275 echo "fatal: bad config line 34 in file .git/config" >expect &&
276 test_expect_code 128 test-tool config get_value foo.bar 2>actual &&
277 test_cmp expect actual
278 '
279
280 test_expect_success 'proper error on error in custom config files' '
281 echo "[" >>syntax-error &&
282 echo "fatal: bad config line 1 in file syntax-error" >expect &&
283 test_expect_code 128 test-tool config configset_get_value foo.bar syntax-error 2>actual &&
284 test_cmp expect actual
285 '
286
287 test_expect_success 'check line errors for malformed values' '
288 mv .git/config .git/config.old &&
289 test_when_finished "mv .git/config.old .git/config" &&
290 cat >.git/config <<-\EOF &&
291 [alias]
292 br
293 EOF
294 test_expect_code 128 git br 2>result &&
295 test_i18ngrep "missing value for .alias\.br" result &&
296 test_i18ngrep "fatal: .*\.git/config" result &&
297 test_i18ngrep "fatal: .*line 2" result
298 '
299
300 test_expect_success 'error on modifying repo config without repo' '
301 nongit test_must_fail git config a.b c 2>err &&
302 test_i18ngrep "not in a git directory" err
303 '
304
305 cmdline_config="'foo.bar=from-cmdline'"
306 test_expect_success 'iteration shows correct origins' '
307 printf "[ignore]\n\tthis = please\n[foo]bar = from-repo\n" >.git/config &&
308 printf "[foo]\n\tbar = from-home\n" >.gitconfig &&
309 if test_have_prereq MINGW
310 then
311 # Use Windows path (i.e. *not* $HOME)
312 HOME_GITCONFIG=$(pwd)/.gitconfig
313 else
314 # Do not get fooled by symbolic links, i.e. $HOME != $(pwd)
315 HOME_GITCONFIG=$HOME/.gitconfig
316 fi &&
317 cat >expect <<-EOF &&
318 key=foo.bar
319 value=from-home
320 origin=file
321 name=$HOME_GITCONFIG
322 lno=2
323 scope=global
324
325 key=ignore.this
326 value=please
327 origin=file
328 name=.git/config
329 lno=2
330 scope=local
331
332 key=foo.bar
333 value=from-repo
334 origin=file
335 name=.git/config
336 lno=3
337 scope=local
338
339 key=foo.bar
340 value=from-cmdline
341 origin=command line
342 name=
343 lno=-1
344 scope=command
345 EOF
346 GIT_CONFIG_PARAMETERS=$cmdline_config test-tool config iterate >actual &&
347 test_cmp expect actual
348 '
349
350 test_done