]> git.ipfire.org Git - thirdparty/git.git/blame - t/t1308-config-set.sh
Merge branch 'en/merge-recursive-directory-rename-fixes'
[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
5. ./test-lib.sh
6
7# 'check_config get_* section.key value' verifies that the entry for
8# section.key is 'value'
9check_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 &&
0e2678af 21 test_expect_code $expect_code test-tool config "$op" "$key" >actual &&
4c715ebb
TA
22 test_cmp expect actual
23}
24
25test_expect_success 'setup default config' '
dc05179b 26 cat >.git/config <<-\EOF
4c715ebb
TA
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 EOF
61'
62
63test_expect_success 'get value for a simple key' '
64 check_config get_value case.penguin "very blue"
65'
66
67test_expect_success 'get value for a key with value as an empty string' '
68 check_config get_value case.my ""
69'
70
71test_expect_success 'get value for a key with value as NULL' '
72 check_config get_value case.foo "(NULL)"
73'
74
75test_expect_success 'upper case key' '
76 check_config get_value case.UPPERCASE "true" &&
77 check_config get_value case.uppercase "true"
78'
79
80test_expect_success 'mixed case key' '
81 check_config get_value case.MixedCase "true" &&
82 check_config get_value case.MIXEDCASE "true" &&
83 check_config get_value case.mixedcase "true"
84'
85
86test_expect_success 'key and value with mixed case' '
87 check_config get_value case.Movie "BadPhysics"
88'
89
90test_expect_success 'key with case sensitive subsection' '
91 check_config get_value "my.Foo bAr.hi" "mixed-case" &&
92 check_config get_value "my.FOO BAR.hi" "upper-case" &&
93 check_config get_value "my.foo bar.hi" "lower-case"
94'
95
96test_expect_success 'key with case insensitive section header' '
97 check_config get_value cores.baz "ball" &&
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'
102
103test_expect_success 'key with case insensitive section header & variable' '
104 check_config get_value CORES.BAZ "ball" &&
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'
109
110test_expect_success 'find value with misspelled key' '
111 check_config expect_code 1 get_value "my.fOo Bar.hi" "Value not found for \"my.fOo Bar.hi\""
112'
113
114test_expect_success 'find value with the highest priority' '
115 check_config get_value case.baz "hask"
116'
117
118test_expect_success 'find integer value for a key' '
119 check_config get_int lamb.chop 65
120'
121
8a7b034d
TA
122test_expect_success 'find string value for a key' '
123 check_config get_string case.baz hask &&
124 check_config expect_code 1 get_string case.ba "Value not found for \"case.ba\""
125'
126
127test_expect_success 'check line error when NULL string is queried' '
0e2678af 128 test_expect_code 128 test-tool config get_string case.foo 2>result &&
8a7b034d
TA
129 test_i18ngrep "fatal: .*case\.foo.*\.git/config.*line 7" result
130'
131
4c715ebb
TA
132test_expect_success 'find integer if value is non parse-able' '
133 check_config expect_code 128 get_int lamb.head
134'
135
136test_expect_success 'find bool value for the entered key' '
137 check_config get_bool goat.head 1 &&
138 check_config get_bool goat.skin 0 &&
139 check_config get_bool goat.nose 1 &&
140 check_config get_bool goat.horns 1 &&
141 check_config get_bool goat.legs 1
142'
143
144test_expect_success 'find multiple values' '
145 check_config get_value_multi case.baz sam bat hask
146'
147
148test_expect_success 'find value from a configset' '
149 cat >config2 <<-\EOF &&
150 [case]
151 baz = lama
152 [my]
153 new = silk
154 [case]
155 baz = ball
156 EOF
157 echo silk >expect &&
0e2678af 158 test-tool config configset_get_value my.new config2 .git/config >actual &&
4c715ebb
TA
159 test_cmp expect actual
160'
161
162test_expect_success 'find value with highest priority from a configset' '
163 echo hask >expect &&
0e2678af 164 test-tool config configset_get_value case.baz config2 .git/config >actual &&
4c715ebb
TA
165 test_cmp expect actual
166'
167
168test_expect_success 'find value_list for a key from a configset' '
8c1cfd58
TA
169 cat >expect <<-\EOF &&
170 lama
171 ball
4c715ebb
TA
172 sam
173 bat
174 hask
4c715ebb 175 EOF
8c1cfd58 176 test-tool config configset_get_value_multi case.baz config2 .git/config >actual &&
4c715ebb
TA
177 test_cmp expect actual
178'
179
180test_expect_success 'proper error on non-existent files' '
181 echo "Error (-1) reading configuration file non-existent-file." >expect &&
0e2678af 182 test_expect_code 2 test-tool config configset_get_value foo.bar non-existent-file 2>actual &&
4c715ebb
TA
183 test_cmp expect actual
184'
185
e2d90fd1
NTND
186test_expect_success 'proper error on directory "files"' '
187 echo "Error (-1) reading configuration file a-directory." >expect &&
188 mkdir a-directory &&
0e2678af 189 test_expect_code 2 test-tool config configset_get_value foo.bar a-directory 2>output &&
e9d983f1 190 grep "^warning:" output &&
e2d90fd1
NTND
191 grep "^Error" output >actual &&
192 test_cmp expect actual
193'
194
4c715ebb
TA
195test_expect_success POSIXPERM,SANITY 'proper error on non-accessible files' '
196 chmod -r .git/config &&
197 test_when_finished "chmod +r .git/config" &&
198 echo "Error (-1) reading configuration file .git/config." >expect &&
0e2678af 199 test_expect_code 2 test-tool config configset_get_value foo.bar .git/config 2>output &&
e9d983f1 200 grep "^warning:" output &&
11dc1fcb 201 grep "^Error" output >actual &&
4c715ebb
TA
202 test_cmp expect actual
203'
204
205test_expect_success 'proper error on error in default config files' '
206 cp .git/config .git/config.old &&
207 test_when_finished "mv .git/config.old .git/config" &&
208 echo "[" >>.git/config &&
473166b9 209 echo "fatal: bad config line 34 in file .git/config" >expect &&
0e2678af 210 test_expect_code 128 test-tool config get_value foo.bar 2>actual &&
1edbaac3 211 test_i18ncmp expect actual
4c715ebb
TA
212'
213
214test_expect_success 'proper error on error in custom config files' '
215 echo "[" >>syntax-error &&
473166b9 216 echo "fatal: bad config line 1 in file syntax-error" >expect &&
0e2678af 217 test_expect_code 128 test-tool config configset_get_value foo.bar syntax-error 2>actual &&
1edbaac3 218 test_i18ncmp expect actual
4c715ebb
TA
219'
220
79e9ce21
TA
221test_expect_success 'check line errors for malformed values' '
222 mv .git/config .git/config.old &&
223 test_when_finished "mv .git/config.old .git/config" &&
224 cat >.git/config <<-\EOF &&
225 [alias]
226 br
227 EOF
228 test_expect_code 128 git br 2>result &&
e4feff48
JS
229 test_i18ngrep "missing value for .alias\.br" result &&
230 test_i18ngrep "fatal: .*\.git/config" result &&
231 test_i18ngrep "fatal: .*line 2" result
79e9ce21
TA
232'
233
638fa623 234test_expect_success 'error on modifying repo config without repo' '
a3c45d12 235 nongit test_must_fail git config a.b c 2>err &&
1d28ff4c 236 test_i18ngrep "not in a git directory" err
638fa623
JS
237'
238
0d44a2da
JK
239cmdline_config="'foo.bar=from-cmdline'"
240test_expect_success 'iteration shows correct origins' '
241 echo "[foo]bar = from-repo" >.git/config &&
242 echo "[foo]bar = from-home" >.gitconfig &&
b738396c
JS
243 if test_have_prereq MINGW
244 then
245 # Use Windows path (i.e. *not* $HOME)
246 HOME_GITCONFIG=$(pwd)/.gitconfig
247 else
248 # Do not get fooled by symbolic links, i.e. $HOME != $(pwd)
249 HOME_GITCONFIG=$HOME/.gitconfig
250 fi &&
0d44a2da
JK
251 cat >expect <<-EOF &&
252 key=foo.bar
253 value=from-home
254 origin=file
b738396c 255 name=$HOME_GITCONFIG
9acc5911 256 scope=global
0d44a2da
JK
257
258 key=foo.bar
259 value=from-repo
260 origin=file
261 name=.git/config
9acc5911 262 scope=repo
0d44a2da
JK
263
264 key=foo.bar
265 value=from-cmdline
266 origin=command line
267 name=
9acc5911 268 scope=cmdline
0d44a2da 269 EOF
0e2678af 270 GIT_CONFIG_PARAMETERS=$cmdline_config test-tool config iterate >actual &&
0d44a2da
JK
271 test_cmp expect actual
272'
273
4c715ebb 274test_done