]> git.proxmox.com Git - libgit2.git/blob - tests/t15-config.c
Merge pull request #421 from nulltoken/ntk/fix/config-get-set-long
[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 <posix.h>
30 #include "filebuf.h"
31
32 #define CONFIG_BASE TEST_RESOURCES "/config"
33
34 /*
35 * This one is so we know the code isn't completely broken
36 */
37 BEGIN_TEST(config0, "read a simple configuration")
38 git_config *cfg;
39 int i;
40
41 must_pass(git_config_open_ondisk(&cfg, CONFIG_BASE "/config0"));
42 must_pass(git_config_get_int(cfg, "core.repositoryformatversion", &i));
43 must_be_true(i == 0);
44 must_pass(git_config_get_bool(cfg, "core.filemode", &i));
45 must_be_true(i == 1);
46 must_pass(git_config_get_bool(cfg, "core.bare", &i));
47 must_be_true(i == 0);
48 must_pass(git_config_get_bool(cfg, "core.logallrefupdates", &i));
49 must_be_true(i == 1);
50
51 git_config_free(cfg);
52 END_TEST
53
54 /*
55 * [this "that"] and [this "That] are different namespaces. Make sure
56 * each returns the correct one.
57 */
58 BEGIN_TEST(config1, "case sensitivity")
59 git_config *cfg;
60 int i;
61 const char *str;
62
63 must_pass(git_config_open_ondisk(&cfg, CONFIG_BASE "/config1"));
64
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"));
69
70 must_pass(git_config_get_bool(cfg, "this.that.other", &i));
71 must_be_true(i == 1);
72 must_pass(git_config_get_bool(cfg, "this.That.other", &i));
73 must_be_true(i == 1);
74
75 /* This one doesn't exist */
76 must_fail(git_config_get_bool(cfg, "this.thaT.other", &i));
77
78 git_config_free(cfg);
79 END_TEST
80
81 /*
82 * If \ is the last non-space character on the line, we read the next
83 * one, separating each line with SP.
84 */
85 BEGIN_TEST(config2, "parse a multiline value")
86 git_config *cfg;
87 const char *str;
88
89 must_pass(git_config_open_ondisk(&cfg, CONFIG_BASE "/config2"));
90
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"));
93
94 git_config_free(cfg);
95 END_TEST
96
97 /*
98 * This kind of subsection declaration is case-insensitive
99 */
100 BEGIN_TEST(config3, "parse a [section.subsection] header")
101 git_config *cfg;
102 const char *str;
103
104 must_pass(git_config_open_ondisk(&cfg, CONFIG_BASE "/config3"));
105
106 must_pass(git_config_get_string(cfg, "section.subsection.var", &str));
107 must_be_true(!strcmp(str, "hello"));
108
109 /* Avoid a false positive */
110 str = "nohello";
111 must_pass(git_config_get_string(cfg, "section.subSectIon.var", &str));
112 must_be_true(!strcmp(str, "hello"));
113
114 git_config_free(cfg);
115 END_TEST
116
117 BEGIN_TEST(config4, "a variable name on its own is valid")
118 git_config *cfg;
119 const char *str;
120 int i;
121
122 must_pass(git_config_open_ondisk(&cfg, CONFIG_BASE "/config4"));
123
124 must_pass(git_config_get_string(cfg, "some.section.variable", &str));
125 must_be_true(str == NULL);
126
127 must_pass(git_config_get_bool(cfg, "some.section.variable", &i));
128 must_be_true(i == 1);
129
130
131 git_config_free(cfg);
132 END_TEST
133
134 BEGIN_TEST(config5, "test number suffixes")
135 git_config *cfg;
136 long long i;
137
138 must_pass(git_config_open_ondisk(&cfg, CONFIG_BASE "/config5"));
139
140 must_pass(git_config_get_long(cfg, "number.simple", &i));
141 must_be_true(i == 1);
142
143 must_pass(git_config_get_long(cfg, "number.k", &i));
144 must_be_true(i == 1 * 1024);
145
146 must_pass(git_config_get_long(cfg, "number.kk", &i));
147 must_be_true(i == 1 * 1024);
148
149 must_pass(git_config_get_long(cfg, "number.m", &i));
150 must_be_true(i == 1 * 1024 * 1024);
151
152 must_pass(git_config_get_long(cfg, "number.mm", &i));
153 must_be_true(i == 1 * 1024 * 1024);
154
155 must_pass(git_config_get_long(cfg, "number.g", &i));
156 must_be_true(i == 1 * 1024 * 1024 * 1024);
157
158 must_pass(git_config_get_long(cfg, "number.gg", &i));
159 must_be_true(i == 1 * 1024 * 1024 * 1024);
160
161 git_config_free(cfg);
162 END_TEST
163
164 BEGIN_TEST(config6, "test blank lines")
165 git_config *cfg;
166 int i;
167
168 must_pass(git_config_open_ondisk(&cfg, CONFIG_BASE "/config6"));
169
170 must_pass(git_config_get_bool(cfg, "valid.subsection.something", &i));
171 must_be_true(i == 1);
172
173 must_pass(git_config_get_bool(cfg, "something.else.something", &i));
174 must_be_true(i == 0);
175
176 git_config_free(cfg);
177 END_TEST
178
179 BEGIN_TEST(config7, "test for invalid ext headers")
180 git_config *cfg;
181
182 must_fail(git_config_open_ondisk(&cfg, CONFIG_BASE "/config7"));
183
184 END_TEST
185
186 BEGIN_TEST(config8, "don't fail on empty files")
187 git_config *cfg;
188
189 must_pass(git_config_open_ondisk(&cfg, CONFIG_BASE "/config8"));
190
191 git_config_free(cfg);
192 END_TEST
193
194 BEGIN_TEST(config9, "replace a value")
195 git_config *cfg;
196 int i;
197 long long l, expected = +9223372036854775803;
198
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);
203
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);
208
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);
212
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);
216
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);
221
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);
225
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);
229
230 END_TEST
231
232 BEGIN_TEST(config10, "a repo's config overrides the global config")
233 git_repository *repo;
234 git_config *cfg;
235 int version;
236 char *old_home;
237
238 old_home = p_getenv("HOME");
239 p_setenv("HOME", CONFIG_BASE, 1);
240
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);
247
248 p_setenv("HOME", old_home, 1);
249 free(old_home);
250 END_TEST
251
252 BEGIN_TEST(config11, "fall back to the global config")
253 git_repository *repo;
254 git_config *cfg;
255 int num;
256 char *old_home;
257
258 old_home = p_getenv("HOME");
259 p_setenv("HOME", CONFIG_BASE, 1);
260
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);
267
268 p_setenv("HOME", old_home, 1);
269 free(old_home);
270 END_TEST
271
272 BEGIN_TEST(config12, "delete a value")
273 git_config *cfg;
274 int i;
275
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);
280
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);
284
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);
289 END_TEST
290
291 BEGIN_TEST(config13, "can't delete a non-existent value")
292 git_config *cfg;
293
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);
298 END_TEST
299
300 BEGIN_TEST(config14, "don't fail horribly if a section header is in the last line")
301 git_config *cfg;
302
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);
306 END_TEST
307
308 BEGIN_TEST(config15, "add a variable in an existing section")
309 git_config *cfg;
310 int i;
311
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);
319 END_TEST
320
321 BEGIN_TEST(config16, "add a variable in a new section")
322 git_config *cfg;
323 int i;
324 git_filebuf buf;
325
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);
333
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));
338 END_TEST
339
340 BEGIN_SUITE(config)
341 ADD_TEST(config0);
342 ADD_TEST(config1);
343 ADD_TEST(config2);
344 ADD_TEST(config3);
345 ADD_TEST(config4);
346 ADD_TEST(config5);
347 ADD_TEST(config6);
348 ADD_TEST(config7);
349 ADD_TEST(config8);
350 ADD_TEST(config9);
351 ADD_TEST(config10);
352 ADD_TEST(config11);
353 ADD_TEST(config12);
354 ADD_TEST(config13);
355 ADD_TEST(config14);
356 ADD_TEST(config15);
357 ADD_TEST(config16);
358 END_SUITE