]>
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"
31 #define CONFIG_BASE TEST_RESOURCES "/config"
34 * This one is so we know the code isn't completely broken
36 BEGIN_TEST(config0
, "read a simple configuration")
40 must_pass(git_config_open_ondisk(&cfg
, CONFIG_BASE
"/config0"));
41 must_pass(git_config_get_int(cfg
, "core.repositoryformatversion", &i
));
43 must_pass(git_config_get_bool(cfg
, "core.filemode", &i
));
45 must_pass(git_config_get_bool(cfg
, "core.bare", &i
));
47 must_pass(git_config_get_bool(cfg
, "core.logallrefupdates", &i
));
54 * [this "that"] and [this "That] are different namespaces. Make sure
55 * each returns the correct one.
57 BEGIN_TEST(config1
, "case sensitivity")
62 must_pass(git_config_open_ondisk(&cfg
, CONFIG_BASE
"/config1"));
64 must_pass(git_config_get_string(cfg
, "this.that.other", &str
));
65 must_be_true(!strcmp(str
, "true"));
66 must_pass(git_config_get_string(cfg
, "this.That.other", &str
));
67 must_be_true(!strcmp(str
, "yes"));
69 must_pass(git_config_get_bool(cfg
, "this.that.other", &i
));
71 must_pass(git_config_get_bool(cfg
, "this.That.other", &i
));
74 /* This one doesn't exist */
75 must_fail(git_config_get_bool(cfg
, "this.thaT.other", &i
));
81 * If \ is the last non-space character on the line, we read the next
82 * one, separating each line with SP.
84 BEGIN_TEST(config2
, "parse a multiline value")
88 must_pass(git_config_open_ondisk(&cfg
, CONFIG_BASE
"/config2"));
90 must_pass(git_config_get_string(cfg
, "this.That.and", &str
));
91 must_be_true(!strcmp(str
, "one one one two two three three"));
97 * This kind of subsection declaration is case-insensitive
99 BEGIN_TEST(config3
, "parse a [section.subsection] header")
103 must_pass(git_config_open_ondisk(&cfg
, CONFIG_BASE
"/config3"));
105 must_pass(git_config_get_string(cfg
, "section.subsection.var", &str
));
106 must_be_true(!strcmp(str
, "hello"));
108 /* Avoid a false positive */
110 must_pass(git_config_get_string(cfg
, "section.subSectIon.var", &str
));
111 must_be_true(!strcmp(str
, "hello"));
113 git_config_free(cfg
);
116 BEGIN_TEST(config4
, "a variable name on its own is valid")
121 must_pass(git_config_open_ondisk(&cfg
, CONFIG_BASE
"/config4"));
123 must_pass(git_config_get_string(cfg
, "some.section.variable", &str
));
124 must_be_true(str
== NULL
);
126 must_pass(git_config_get_bool(cfg
, "some.section.variable", &i
));
127 must_be_true(i
== 1);
130 git_config_free(cfg
);
133 BEGIN_TEST(config5
, "test number suffixes")
137 must_pass(git_config_open_ondisk(&cfg
, CONFIG_BASE
"/config5"));
139 must_pass(git_config_get_long(cfg
, "number.simple", &i
));
140 must_be_true(i
== 1);
142 must_pass(git_config_get_long(cfg
, "number.k", &i
));
143 must_be_true(i
== 1 * 1024);
145 must_pass(git_config_get_long(cfg
, "number.kk", &i
));
146 must_be_true(i
== 1 * 1024);
148 must_pass(git_config_get_long(cfg
, "number.m", &i
));
149 must_be_true(i
== 1 * 1024 * 1024);
151 must_pass(git_config_get_long(cfg
, "number.mm", &i
));
152 must_be_true(i
== 1 * 1024 * 1024);
154 must_pass(git_config_get_long(cfg
, "number.g", &i
));
155 must_be_true(i
== 1 * 1024 * 1024 * 1024);
157 must_pass(git_config_get_long(cfg
, "number.gg", &i
));
158 must_be_true(i
== 1 * 1024 * 1024 * 1024);
160 git_config_free(cfg
);
163 BEGIN_TEST(config6
, "test blank lines")
167 must_pass(git_config_open_ondisk(&cfg
, CONFIG_BASE
"/config6"));
169 must_pass(git_config_get_bool(cfg
, "valid.subsection.something", &i
));
170 must_be_true(i
== 1);
172 must_pass(git_config_get_bool(cfg
, "something.else.something", &i
));
173 must_be_true(i
== 0);
175 git_config_free(cfg
);
178 BEGIN_TEST(config7
, "test for invalid ext headers")
181 must_fail(git_config_open_ondisk(&cfg
, CONFIG_BASE
"/config7"));
185 BEGIN_TEST(config8
, "don't fail on empty files")
188 must_pass(git_config_open_ondisk(&cfg
, CONFIG_BASE
"/config8"));
190 git_config_free(cfg
);
193 BEGIN_TEST(config9
, "replace a value")
197 /* By freeing the config, we make sure we flush the values */
198 must_pass(git_config_open_ondisk(&cfg
, CONFIG_BASE
"/config9"));
199 must_pass(git_config_set_int(cfg
, "core.dummy", 5));
200 git_config_free(cfg
);
202 must_pass(git_config_open_ondisk(&cfg
, CONFIG_BASE
"/config9"));
203 must_pass(git_config_get_int(cfg
, "core.dummy", &i
));
204 must_be_true(i
== 5);
205 git_config_free(cfg
);
207 must_pass(git_config_open_ondisk(&cfg
, CONFIG_BASE
"/config9"));
208 must_pass(git_config_set_int(cfg
, "core.dummy", 1));
209 git_config_free(cfg
);
213 BEGIN_TEST(config10
, "a repo's config overrides the global config")
214 git_repository
*repo
;
218 must_pass(git_repository_open(&repo
, REPOSITORY_FOLDER
));
219 must_pass(git_repository_config(&cfg
, repo
, CONFIG_BASE
"/.gitconfig", NULL
));
220 must_pass(git_config_get_int(cfg
, "core.repositoryformatversion", &version
));
221 must_be_true(version
== 0);
222 git_config_free(cfg
);
223 git_repository_free(repo
);
226 BEGIN_TEST(config11
, "fall back to the global config")
227 git_repository
*repo
;
231 must_pass(git_repository_open(&repo
, REPOSITORY_FOLDER
));
232 must_pass(git_repository_config(&cfg
, repo
, CONFIG_BASE
"/.gitconfig", NULL
));
233 must_pass(git_config_get_int(cfg
, "core.something", &num
));
234 must_be_true(num
== 2);
235 git_config_free(cfg
);
236 git_repository_free(repo
);
239 BEGIN_TEST(config12
, "delete a value")
243 /* By freeing the config, we make sure we flush the values */
244 must_pass(git_config_open_ondisk(&cfg
, CONFIG_BASE
"/config9"));
245 must_pass(git_config_set_int(cfg
, "core.dummy", 5));
246 git_config_free(cfg
);
248 must_pass(git_config_open_ondisk(&cfg
, CONFIG_BASE
"/config9"));
249 must_pass(git_config_delete(cfg
, "core.dummy"));
250 git_config_free(cfg
);
252 must_pass(git_config_open_ondisk(&cfg
, CONFIG_BASE
"/config9"));
253 must_be_true(git_config_get_int(cfg
, "core.dummy", &i
) == GIT_ENOTFOUND
);
254 must_pass(git_config_set_int(cfg
, "core.dummy", 1));
255 git_config_free(cfg
);
258 BEGIN_TEST(config13
, "can't delete a non-existent value")
261 /* By freeing the config, we make sure we flush the values */
262 must_pass(git_config_open_ondisk(&cfg
, CONFIG_BASE
"/config9"));
263 must_be_true(git_config_delete(cfg
, "core.imaginary") == GIT_ENOTFOUND
);
264 git_config_free(cfg
);
267 BEGIN_TEST(config14
, "don't fail horribly if a section header is in the last line")
270 /* By freeing the config, we make sure we flush the values */
271 must_pass(git_config_open_ondisk(&cfg
, CONFIG_BASE
"/config10"));
272 git_config_free(cfg
);
275 BEGIN_TEST(config15
, "add a variable in an existing section")
279 /* By freeing the config, we make sure we flush the values */
280 must_pass(git_config_open_ondisk(&cfg
, CONFIG_BASE
"/config10"));
281 must_pass(git_config_set_int(cfg
, "empty.tmp", 5));
282 must_pass(git_config_get_int(cfg
, "empty.tmp", &i
));
283 must_be_true(i
== 5);
284 must_pass(git_config_delete(cfg
, "empty.tmp"));
285 git_config_free(cfg
);
288 BEGIN_TEST(config16
, "add a variable in a new section")
293 /* By freeing the config, we make sure we flush the values */
294 must_pass(git_config_open_ondisk(&cfg
, CONFIG_BASE
"/config10"));
295 must_pass(git_config_set_int(cfg
, "section.tmp", 5));
296 must_pass(git_config_get_int(cfg
, "section.tmp", &i
));
297 must_be_true(i
== 5);
298 must_pass(git_config_delete(cfg
, "section.tmp"));
299 git_config_free(cfg
);
301 /* As the section wasn't removed, owerwrite the file */
302 must_pass(git_filebuf_open(&buf
, CONFIG_BASE
"/config10", 0));
303 must_pass(git_filebuf_write(&buf
, "[empty]\n", strlen("[empty]\n")));
304 must_pass(git_filebuf_commit(&buf
));