]>
git.proxmox.com Git - libgit2.git/blob - tests/t15-config.c
2 * This file is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License, version 2,
4 * as published by the Free Software Foundation.
6 * In addition to the permissions in the GNU General Public License,
7 * the authors give you unlimited permission to link the compiled
8 * version of this file into combinations with other programs,
9 * and to distribute those combinations without any restriction
10 * coming from the use of this file. (The General Public License
11 * restrictions do apply in other respects; for example, they cover
12 * modification of the file, and distribution when not linked into
13 * a combined executable.)
15 * This file is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; see the file COPYING. If not, write to
22 * the Free Software Foundation, 51 Franklin Street, Fifth Floor,
23 * Boston, MA 02110-1301, USA.
26 #include "test_helpers.h"
32 #define CONFIG_BASE TEST_RESOURCES "/config"
35 * This one is so we know the code isn't completely broken
37 BEGIN_TEST(config0
, "read a simple configuration")
41 must_pass(git_config_open_ondisk(&cfg
, CONFIG_BASE
"/config0"));
42 must_pass(git_config_get_int(cfg
, "core.repositoryformatversion", &i
));
44 must_pass(git_config_get_bool(cfg
, "core.filemode", &i
));
46 must_pass(git_config_get_bool(cfg
, "core.bare", &i
));
48 must_pass(git_config_get_bool(cfg
, "core.logallrefupdates", &i
));
55 * [this "that"] and [this "That] are different namespaces. Make sure
56 * each returns the correct one.
58 BEGIN_TEST(config1
, "case sensitivity")
63 must_pass(git_config_open_ondisk(&cfg
, CONFIG_BASE
"/config1"));
65 must_pass(git_config_get_string(cfg
, "this.that.other", &str
));
66 must_be_true(!strcmp(str
, "true"));
67 must_pass(git_config_get_string(cfg
, "this.That.other", &str
));
68 must_be_true(!strcmp(str
, "yes"));
70 must_pass(git_config_get_bool(cfg
, "this.that.other", &i
));
72 must_pass(git_config_get_bool(cfg
, "this.That.other", &i
));
75 /* This one doesn't exist */
76 must_fail(git_config_get_bool(cfg
, "this.thaT.other", &i
));
82 * If \ is the last non-space character on the line, we read the next
83 * one, separating each line with SP.
85 BEGIN_TEST(config2
, "parse a multiline value")
89 must_pass(git_config_open_ondisk(&cfg
, CONFIG_BASE
"/config2"));
91 must_pass(git_config_get_string(cfg
, "this.That.and", &str
));
92 must_be_true(!strcmp(str
, "one one one two two three three"));
98 * This kind of subsection declaration is case-insensitive
100 BEGIN_TEST(config3
, "parse a [section.subsection] header")
104 must_pass(git_config_open_ondisk(&cfg
, CONFIG_BASE
"/config3"));
106 must_pass(git_config_get_string(cfg
, "section.subsection.var", &str
));
107 must_be_true(!strcmp(str
, "hello"));
109 /* Avoid a false positive */
111 must_pass(git_config_get_string(cfg
, "section.subSectIon.var", &str
));
112 must_be_true(!strcmp(str
, "hello"));
114 git_config_free(cfg
);
117 BEGIN_TEST(config4
, "a variable name on its own is valid")
122 must_pass(git_config_open_ondisk(&cfg
, CONFIG_BASE
"/config4"));
124 must_pass(git_config_get_string(cfg
, "some.section.variable", &str
));
125 must_be_true(str
== NULL
);
127 must_pass(git_config_get_bool(cfg
, "some.section.variable", &i
));
128 must_be_true(i
== 1);
131 git_config_free(cfg
);
134 BEGIN_TEST(config5
, "test number suffixes")
138 must_pass(git_config_open_ondisk(&cfg
, CONFIG_BASE
"/config5"));
140 must_pass(git_config_get_long(cfg
, "number.simple", &i
));
141 must_be_true(i
== 1);
143 must_pass(git_config_get_long(cfg
, "number.k", &i
));
144 must_be_true(i
== 1 * 1024);
146 must_pass(git_config_get_long(cfg
, "number.kk", &i
));
147 must_be_true(i
== 1 * 1024);
149 must_pass(git_config_get_long(cfg
, "number.m", &i
));
150 must_be_true(i
== 1 * 1024 * 1024);
152 must_pass(git_config_get_long(cfg
, "number.mm", &i
));
153 must_be_true(i
== 1 * 1024 * 1024);
155 must_pass(git_config_get_long(cfg
, "number.g", &i
));
156 must_be_true(i
== 1 * 1024 * 1024 * 1024);
158 must_pass(git_config_get_long(cfg
, "number.gg", &i
));
159 must_be_true(i
== 1 * 1024 * 1024 * 1024);
161 git_config_free(cfg
);
164 BEGIN_TEST(config6
, "test blank lines")
168 must_pass(git_config_open_ondisk(&cfg
, CONFIG_BASE
"/config6"));
170 must_pass(git_config_get_bool(cfg
, "valid.subsection.something", &i
));
171 must_be_true(i
== 1);
173 must_pass(git_config_get_bool(cfg
, "something.else.something", &i
));
174 must_be_true(i
== 0);
176 git_config_free(cfg
);
179 BEGIN_TEST(config7
, "test for invalid ext headers")
182 must_fail(git_config_open_ondisk(&cfg
, CONFIG_BASE
"/config7"));
186 BEGIN_TEST(config8
, "don't fail on empty files")
189 must_pass(git_config_open_ondisk(&cfg
, CONFIG_BASE
"/config8"));
191 git_config_free(cfg
);
194 BEGIN_TEST(config9
, "replace a value")
197 long long l
, expected
= +9223372036854775803;
199 /* By freeing the config, we make sure we flush the values */
200 must_pass(git_config_open_ondisk(&cfg
, CONFIG_BASE
"/config9"));
201 must_pass(git_config_set_int(cfg
, "core.dummy", 5));
202 git_config_free(cfg
);
204 must_pass(git_config_open_ondisk(&cfg
, CONFIG_BASE
"/config9"));
205 must_pass(git_config_get_int(cfg
, "core.dummy", &i
));
206 must_be_true(i
== 5);
207 git_config_free(cfg
);
209 must_pass(git_config_open_ondisk(&cfg
, CONFIG_BASE
"/config9"));
210 must_pass(git_config_set_int(cfg
, "core.dummy", 1));
211 git_config_free(cfg
);
213 must_pass(git_config_open_ondisk(&cfg
, CONFIG_BASE
"/config9"));
214 must_pass(git_config_set_long(cfg
, "core.verylong", expected
));
215 git_config_free(cfg
);
217 must_pass(git_config_open_ondisk(&cfg
, CONFIG_BASE
"/config9"));
218 must_pass(git_config_get_long(cfg
, "core.verylong", &l
));
219 must_be_true(l
== expected
);
220 git_config_free(cfg
);
222 must_pass(git_config_open_ondisk(&cfg
, CONFIG_BASE
"/config9"));
223 must_fail(git_config_get_int(cfg
, "core.verylong", &i
));
224 git_config_free(cfg
);
226 must_pass(git_config_open_ondisk(&cfg
, CONFIG_BASE
"/config9"));
227 must_pass(git_config_set_long(cfg
, "core.verylong", 1));
228 git_config_free(cfg
);
232 BEGIN_TEST(config10
, "a repo's config overrides the global config")
233 git_repository
*repo
;
238 old_home
= p_getenv("HOME");
239 p_setenv("HOME", CONFIG_BASE
, 1);
241 must_pass(git_repository_open(&repo
, REPOSITORY_FOLDER
));
242 must_pass(git_repository_config(&cfg
, repo
, NULL
));
243 must_pass(git_config_get_int(cfg
, "core.repositoryformatversion", &version
));
244 must_be_true(version
== 0);
245 git_config_free(cfg
);
246 git_repository_free(repo
);
248 p_setenv("HOME", old_home
, 1);
252 BEGIN_TEST(config11
, "fall back to the global config")
253 git_repository
*repo
;
258 old_home
= p_getenv("HOME");
259 p_setenv("HOME", CONFIG_BASE
, 1);
261 must_pass(git_repository_open(&repo
, REPOSITORY_FOLDER
));
262 must_pass(git_repository_config(&cfg
, repo
, NULL
));
263 must_pass(git_config_get_int(cfg
, "core.something", &num
));
264 must_be_true(num
== 2);
265 git_config_free(cfg
);
266 git_repository_free(repo
);
268 p_setenv("HOME", old_home
, 1);
272 BEGIN_TEST(config12
, "delete a value")
276 /* By freeing the config, we make sure we flush the values */
277 must_pass(git_config_open_ondisk(&cfg
, CONFIG_BASE
"/config9"));
278 must_pass(git_config_set_int(cfg
, "core.dummy", 5));
279 git_config_free(cfg
);
281 must_pass(git_config_open_ondisk(&cfg
, CONFIG_BASE
"/config9"));
282 must_pass(git_config_delete(cfg
, "core.dummy"));
283 git_config_free(cfg
);
285 must_pass(git_config_open_ondisk(&cfg
, CONFIG_BASE
"/config9"));
286 must_be_true(git_config_get_int(cfg
, "core.dummy", &i
) == GIT_ENOTFOUND
);
287 must_pass(git_config_set_int(cfg
, "core.dummy", 1));
288 git_config_free(cfg
);
291 BEGIN_TEST(config13
, "can't delete a non-existent value")
294 /* By freeing the config, we make sure we flush the values */
295 must_pass(git_config_open_ondisk(&cfg
, CONFIG_BASE
"/config9"));
296 must_be_true(git_config_delete(cfg
, "core.imaginary") == GIT_ENOTFOUND
);
297 git_config_free(cfg
);
300 BEGIN_TEST(config14
, "don't fail horribly if a section header is in the last line")
303 /* By freeing the config, we make sure we flush the values */
304 must_pass(git_config_open_ondisk(&cfg
, CONFIG_BASE
"/config10"));
305 git_config_free(cfg
);
308 BEGIN_TEST(config15
, "add a variable in an existing section")
312 /* By freeing the config, we make sure we flush the values */
313 must_pass(git_config_open_ondisk(&cfg
, CONFIG_BASE
"/config10"));
314 must_pass(git_config_set_int(cfg
, "empty.tmp", 5));
315 must_pass(git_config_get_int(cfg
, "empty.tmp", &i
));
316 must_be_true(i
== 5);
317 must_pass(git_config_delete(cfg
, "empty.tmp"));
318 git_config_free(cfg
);
321 BEGIN_TEST(config16
, "add a variable in a new section")
326 /* By freeing the config, we make sure we flush the values */
327 must_pass(git_config_open_ondisk(&cfg
, CONFIG_BASE
"/config10"));
328 must_pass(git_config_set_int(cfg
, "section.tmp", 5));
329 must_pass(git_config_get_int(cfg
, "section.tmp", &i
));
330 must_be_true(i
== 5);
331 must_pass(git_config_delete(cfg
, "section.tmp"));
332 git_config_free(cfg
);
334 /* As the section wasn't removed, owerwrite the file */
335 must_pass(git_filebuf_open(&buf
, CONFIG_BASE
"/config10", 0));
336 must_pass(git_filebuf_write(&buf
, "[empty]\n", strlen("[empty]\n")));
337 must_pass(git_filebuf_commit(&buf
));