]> git.proxmox.com Git - libgit2.git/blob - tests/t15-config.c
Merge pull request #392 from sschuberth/development
[libgit2.git] / tests / t15-config.c
1 /*
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.
5 *
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.)
14 *
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.
19 *
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.
24 */
25 #include "test_lib.h"
26 #include "test_helpers.h"
27
28 #include <git2.h>
29 #include "filebuf.h"
30
31 #define CONFIG_BASE TEST_RESOURCES "/config"
32
33 /*
34 * This one is so we know the code isn't completely broken
35 */
36 BEGIN_TEST(config0, "read a simple configuration")
37 git_config *cfg;
38 int i;
39
40 must_pass(git_config_open_ondisk(&cfg, CONFIG_BASE "/config0"));
41 must_pass(git_config_get_int(cfg, "core.repositoryformatversion", &i));
42 must_be_true(i == 0);
43 must_pass(git_config_get_bool(cfg, "core.filemode", &i));
44 must_be_true(i == 1);
45 must_pass(git_config_get_bool(cfg, "core.bare", &i));
46 must_be_true(i == 0);
47 must_pass(git_config_get_bool(cfg, "core.logallrefupdates", &i));
48 must_be_true(i == 1);
49
50 git_config_free(cfg);
51 END_TEST
52
53 /*
54 * [this "that"] and [this "That] are different namespaces. Make sure
55 * each returns the correct one.
56 */
57 BEGIN_TEST(config1, "case sensitivity")
58 git_config *cfg;
59 int i;
60 const char *str;
61
62 must_pass(git_config_open_ondisk(&cfg, CONFIG_BASE "/config1"));
63
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"));
68
69 must_pass(git_config_get_bool(cfg, "this.that.other", &i));
70 must_be_true(i == 1);
71 must_pass(git_config_get_bool(cfg, "this.That.other", &i));
72 must_be_true(i == 1);
73
74 /* This one doesn't exist */
75 must_fail(git_config_get_bool(cfg, "this.thaT.other", &i));
76
77 git_config_free(cfg);
78 END_TEST
79
80 /*
81 * If \ is the last non-space character on the line, we read the next
82 * one, separating each line with SP.
83 */
84 BEGIN_TEST(config2, "parse a multiline value")
85 git_config *cfg;
86 const char *str;
87
88 must_pass(git_config_open_ondisk(&cfg, CONFIG_BASE "/config2"));
89
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"));
92
93 git_config_free(cfg);
94 END_TEST
95
96 /*
97 * This kind of subsection declaration is case-insensitive
98 */
99 BEGIN_TEST(config3, "parse a [section.subsection] header")
100 git_config *cfg;
101 const char *str;
102
103 must_pass(git_config_open_ondisk(&cfg, CONFIG_BASE "/config3"));
104
105 must_pass(git_config_get_string(cfg, "section.subsection.var", &str));
106 must_be_true(!strcmp(str, "hello"));
107
108 /* Avoid a false positive */
109 str = "nohello";
110 must_pass(git_config_get_string(cfg, "section.subSectIon.var", &str));
111 must_be_true(!strcmp(str, "hello"));
112
113 git_config_free(cfg);
114 END_TEST
115
116 BEGIN_TEST(config4, "a variable name on its own is valid")
117 git_config *cfg;
118 const char *str;
119 int i;
120
121 must_pass(git_config_open_ondisk(&cfg, CONFIG_BASE "/config4"));
122
123 must_pass(git_config_get_string(cfg, "some.section.variable", &str));
124 must_be_true(str == NULL);
125
126 must_pass(git_config_get_bool(cfg, "some.section.variable", &i));
127 must_be_true(i == 1);
128
129
130 git_config_free(cfg);
131 END_TEST
132
133 BEGIN_TEST(config5, "test number suffixes")
134 git_config *cfg;
135 long int i;
136
137 must_pass(git_config_open_ondisk(&cfg, CONFIG_BASE "/config5"));
138
139 must_pass(git_config_get_long(cfg, "number.simple", &i));
140 must_be_true(i == 1);
141
142 must_pass(git_config_get_long(cfg, "number.k", &i));
143 must_be_true(i == 1 * 1024);
144
145 must_pass(git_config_get_long(cfg, "number.kk", &i));
146 must_be_true(i == 1 * 1024);
147
148 must_pass(git_config_get_long(cfg, "number.m", &i));
149 must_be_true(i == 1 * 1024 * 1024);
150
151 must_pass(git_config_get_long(cfg, "number.mm", &i));
152 must_be_true(i == 1 * 1024 * 1024);
153
154 must_pass(git_config_get_long(cfg, "number.g", &i));
155 must_be_true(i == 1 * 1024 * 1024 * 1024);
156
157 must_pass(git_config_get_long(cfg, "number.gg", &i));
158 must_be_true(i == 1 * 1024 * 1024 * 1024);
159
160 git_config_free(cfg);
161 END_TEST
162
163 BEGIN_TEST(config6, "test blank lines")
164 git_config *cfg;
165 int i;
166
167 must_pass(git_config_open_ondisk(&cfg, CONFIG_BASE "/config6"));
168
169 must_pass(git_config_get_bool(cfg, "valid.subsection.something", &i));
170 must_be_true(i == 1);
171
172 must_pass(git_config_get_bool(cfg, "something.else.something", &i));
173 must_be_true(i == 0);
174
175 git_config_free(cfg);
176 END_TEST
177
178 BEGIN_TEST(config7, "test for invalid ext headers")
179 git_config *cfg;
180
181 must_fail(git_config_open_ondisk(&cfg, CONFIG_BASE "/config7"));
182
183 END_TEST
184
185 BEGIN_TEST(config8, "don't fail on empty files")
186 git_config *cfg;
187
188 must_pass(git_config_open_ondisk(&cfg, CONFIG_BASE "/config8"));
189
190 git_config_free(cfg);
191 END_TEST
192
193 BEGIN_TEST(config9, "replace a value")
194 git_config *cfg;
195 int i;
196
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);
201
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);
206
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);
210
211 END_TEST
212
213 BEGIN_TEST(config10, "a repo's config overrides the global config")
214 git_repository *repo;
215 git_config *cfg;
216 int version;
217
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);
224 END_TEST
225
226 BEGIN_TEST(config11, "fall back to the global config")
227 git_repository *repo;
228 git_config *cfg;
229 int num;
230
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);
237 END_TEST
238
239 BEGIN_TEST(config12, "delete a value")
240 git_config *cfg;
241 int i;
242
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);
247
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);
251
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);
256 END_TEST
257
258 BEGIN_TEST(config13, "can't delete a non-existent value")
259 git_config *cfg;
260
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);
265 END_TEST
266
267 BEGIN_TEST(config14, "don't fail horribly if a section header is in the last line")
268 git_config *cfg;
269
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);
273 END_TEST
274
275 BEGIN_TEST(config15, "add a variable in an existing section")
276 git_config *cfg;
277 int i;
278
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);
286 END_TEST
287
288 BEGIN_TEST(config16, "add a variable in a new section")
289 git_config *cfg;
290 int i;
291 git_filebuf buf;
292
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);
300
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));
305 END_TEST
306
307 BEGIN_SUITE(config)
308 ADD_TEST(config0);
309 ADD_TEST(config1);
310 ADD_TEST(config2);
311 ADD_TEST(config3);
312 ADD_TEST(config4);
313 ADD_TEST(config5);
314 ADD_TEST(config6);
315 ADD_TEST(config7);
316 ADD_TEST(config8);
317 ADD_TEST(config9);
318 ADD_TEST(config10);
319 ADD_TEST(config11);
320 ADD_TEST(config12);
321 ADD_TEST(config13);
322 ADD_TEST(config14);
323 ADD_TEST(config15);
324 ADD_TEST(config16);
325 END_SUITE