]> git.proxmox.com Git - libgit2.git/blob - tests/config/read.c
Merge pull request #1962 from libgit2/rename-tests
[libgit2.git] / tests / config / read.c
1 #include "clar_libgit2.h"
2
3 void test_config_read__simple_read(void)
4 {
5 git_config *cfg;
6 int32_t i;
7
8 cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config0")));
9
10 cl_git_pass(git_config_get_int32(&i, cfg, "core.repositoryformatversion"));
11 cl_assert(i == 0);
12 cl_git_pass(git_config_get_bool(&i, cfg, "core.filemode"));
13 cl_assert(i == 1);
14 cl_git_pass(git_config_get_bool(&i, cfg, "core.bare"));
15 cl_assert(i == 0);
16 cl_git_pass(git_config_get_bool(&i, cfg, "core.logallrefupdates"));
17 cl_assert(i == 1);
18
19 git_config_free(cfg);
20 }
21
22 void test_config_read__case_sensitive(void)
23 {
24 git_config *cfg;
25 int i;
26 const char *str;
27
28 cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config1")));
29
30 cl_git_pass(git_config_get_string(&str, cfg, "this.that.other"));
31 cl_assert_equal_s(str, "true");
32 cl_git_pass(git_config_get_string(&str, cfg, "this.That.other"));
33 cl_assert_equal_s(str, "yes");
34
35 cl_git_pass(git_config_get_bool(&i, cfg, "this.that.other"));
36 cl_assert(i == 1);
37 cl_git_pass(git_config_get_bool(&i, cfg, "this.That.other"));
38 cl_assert(i == 1);
39
40 /* This one doesn't exist */
41 cl_must_fail(git_config_get_bool(&i, cfg, "this.thaT.other"));
42
43 git_config_free(cfg);
44 }
45
46 /*
47 * If \ is the last non-space character on the line, we read the next
48 * one, separating each line with SP.
49 */
50 void test_config_read__multiline_value(void)
51 {
52 git_config *cfg;
53 const char *str;
54
55 cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config2")));
56
57 cl_git_pass(git_config_get_string(&str, cfg, "this.That.and"));
58 cl_assert_equal_s(str, "one one one two two three three");
59
60 git_config_free(cfg);
61 }
62
63 /*
64 * This kind of subsection declaration is case-insensitive
65 */
66 void test_config_read__subsection_header(void)
67 {
68 git_config *cfg;
69 const char *str;
70
71 cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config3")));
72
73 cl_git_pass(git_config_get_string(&str, cfg, "section.subsection.var"));
74 cl_assert_equal_s(str, "hello");
75
76 /* The subsection is transformed to lower-case */
77 cl_must_fail(git_config_get_string(&str, cfg, "section.subSectIon.var"));
78
79 git_config_free(cfg);
80 }
81
82 void test_config_read__lone_variable(void)
83 {
84 git_config *cfg;
85 const char *str;
86 int i;
87
88 cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config4")));
89
90 cl_git_fail(git_config_get_int32(&i, cfg, "some.section.variable"));
91
92 cl_git_pass(git_config_get_string(&str, cfg, "some.section.variable"));
93 cl_assert_equal_s(str, "");
94
95 cl_git_pass(git_config_get_bool(&i, cfg, "some.section.variable"));
96 cl_assert(i == 1);
97
98 cl_git_pass(git_config_get_string(&str, cfg, "some.section.variableeq"));
99 cl_assert_equal_s(str, "");
100
101 cl_git_pass(git_config_get_bool(&i, cfg, "some.section.variableeq"));
102 cl_assert(i == 0);
103
104 git_config_free(cfg);
105 }
106
107 void test_config_read__number_suffixes(void)
108 {
109 git_config *cfg;
110 int64_t i;
111
112 cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config5")));
113
114 cl_git_pass(git_config_get_int64(&i, cfg, "number.simple"));
115 cl_assert(i == 1);
116
117 cl_git_pass(git_config_get_int64(&i, cfg, "number.k"));
118 cl_assert(i == 1 * 1024);
119
120 cl_git_pass(git_config_get_int64(&i, cfg, "number.kk"));
121 cl_assert(i == 1 * 1024);
122
123 cl_git_pass(git_config_get_int64(&i, cfg, "number.m"));
124 cl_assert(i == 1 * 1024 * 1024);
125
126 cl_git_pass(git_config_get_int64(&i, cfg, "number.mm"));
127 cl_assert(i == 1 * 1024 * 1024);
128
129 cl_git_pass(git_config_get_int64(&i, cfg, "number.g"));
130 cl_assert(i == 1 * 1024 * 1024 * 1024);
131
132 cl_git_pass(git_config_get_int64(&i, cfg, "number.gg"));
133 cl_assert(i == 1 * 1024 * 1024 * 1024);
134
135 git_config_free(cfg);
136 }
137
138 void test_config_read__blank_lines(void)
139 {
140 git_config *cfg;
141 int i;
142
143 cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config6")));
144
145 cl_git_pass(git_config_get_bool(&i, cfg, "valid.subsection.something"));
146 cl_assert(i == 1);
147
148 cl_git_pass(git_config_get_bool(&i, cfg, "something.else.something"));
149 cl_assert(i == 0);
150
151 git_config_free(cfg);
152 }
153
154 void test_config_read__invalid_ext_headers(void)
155 {
156 git_config *cfg;
157 cl_must_fail(git_config_open_ondisk(&cfg, cl_fixture("config/config7")));
158 }
159
160 void test_config_read__empty_files(void)
161 {
162 git_config *cfg;
163 cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config8")));
164 git_config_free(cfg);
165 }
166
167 void test_config_read__symbol_headers(void)
168 {
169 git_config *cfg;
170 cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config20")));
171 git_config_free(cfg);
172 }
173
174 void test_config_read__header_in_last_line(void)
175 {
176 git_config *cfg;
177
178 cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config10")));
179 git_config_free(cfg);
180 }
181
182 void test_config_read__prefixes(void)
183 {
184 git_config *cfg;
185 const char *str;
186
187 cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config9")));
188 cl_git_pass(git_config_get_string(&str, cfg, "remote.ab.url"));
189 cl_assert_equal_s(str, "http://example.com/git/ab");
190
191 cl_git_pass(git_config_get_string(&str, cfg, "remote.abba.url"));
192 cl_assert_equal_s(str, "http://example.com/git/abba");
193
194 git_config_free(cfg);
195 }
196
197 void test_config_read__escaping_quotes(void)
198 {
199 git_config *cfg;
200 const char *str;
201
202 cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config13")));
203 cl_git_pass(git_config_get_string(&str, cfg, "core.editor"));
204 cl_assert_equal_s("\"C:/Program Files/Nonsense/bah.exe\" \"--some option\"", str);
205
206 git_config_free(cfg);
207 }
208
209 static int count_cfg_entries_and_compare_levels(
210 const git_config_entry *entry, void *payload)
211 {
212 int *count = payload;
213
214 if (!strcmp(entry->value, "7") || !strcmp(entry->value, "17"))
215 cl_assert(entry->level == GIT_CONFIG_LEVEL_GLOBAL);
216 else
217 cl_assert(entry->level == GIT_CONFIG_LEVEL_SYSTEM);
218
219 (*count)++;
220 return 0;
221 }
222
223 static int cfg_callback_countdown(const git_config_entry *entry, void *payload)
224 {
225 int *count = payload;
226 GIT_UNUSED(entry);
227 (*count)--;
228 if (*count == 0)
229 return -100;
230 return 0;
231 }
232
233 void test_config_read__foreach(void)
234 {
235 git_config *cfg;
236 int count, ret;
237
238 cl_git_pass(git_config_new(&cfg));
239 cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config9"),
240 GIT_CONFIG_LEVEL_SYSTEM, 0));
241 cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config15"),
242 GIT_CONFIG_LEVEL_GLOBAL, 0));
243
244 count = 0;
245 cl_git_pass(git_config_foreach(cfg, count_cfg_entries_and_compare_levels, &count));
246 cl_assert_equal_i(7, count);
247
248 count = 3;
249 cl_git_fail(ret = git_config_foreach(cfg, cfg_callback_countdown, &count));
250 cl_assert_equal_i(GIT_EUSER, ret);
251
252 git_config_free(cfg);
253 }
254
255 void test_config_read__iterator(void)
256 {
257 git_config *cfg;
258 git_config_iterator *iter;
259 git_config_entry *entry;
260 int count, ret;
261
262 cl_git_pass(git_config_new(&cfg));
263 cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config9"),
264 GIT_CONFIG_LEVEL_SYSTEM, 0));
265 cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config15"),
266 GIT_CONFIG_LEVEL_GLOBAL, 0));
267
268 count = 0;
269 cl_git_pass(git_config_iterator_new(&iter, cfg));
270
271 while ((ret = git_config_next(&entry, iter)) == 0) {
272 count++;
273 }
274
275 git_config_iterator_free(iter);
276 cl_assert_equal_i(GIT_ITEROVER, ret);
277 cl_assert_equal_i(7, count);
278
279 count = 3;
280 cl_git_pass(git_config_iterator_new(&iter, cfg));
281
282 git_config_iterator_free(iter);
283 git_config_free(cfg);
284 }
285
286 static int count_cfg_entries(const git_config_entry *entry, void *payload)
287 {
288 int *count = payload;
289 GIT_UNUSED(entry);
290 (*count)++;
291 return 0;
292 }
293
294 void test_config_read__foreach_match(void)
295 {
296 git_config *cfg;
297 int count;
298
299 cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config9")));
300
301 count = 0;
302 cl_git_pass(
303 git_config_foreach_match(cfg, "core.*", count_cfg_entries, &count));
304 cl_assert_equal_i(3, count);
305
306 count = 0;
307 cl_git_pass(
308 git_config_foreach_match(cfg, "remote\\.ab.*", count_cfg_entries, &count));
309 cl_assert_equal_i(2, count);
310
311 count = 0;
312 cl_git_pass(
313 git_config_foreach_match(cfg, ".*url$", count_cfg_entries, &count));
314 cl_assert_equal_i(2, count);
315
316 count = 0;
317 cl_git_pass(
318 git_config_foreach_match(cfg, ".*dummy.*", count_cfg_entries, &count));
319 cl_assert_equal_i(2, count);
320
321 count = 0;
322 cl_git_pass(
323 git_config_foreach_match(cfg, ".*nomatch.*", count_cfg_entries, &count));
324 cl_assert_equal_i(0, count);
325
326 git_config_free(cfg);
327 }
328
329 static void check_glob_iter(git_config *cfg, const char *regexp, int expected)
330 {
331 git_config_iterator *iter;
332 git_config_entry *entry;
333 int count, error;
334
335 cl_git_pass(git_config_iterator_glob_new(&iter, cfg, regexp));
336
337 count = 0;
338 while ((error = git_config_next(&entry, iter)) == 0)
339 count++;
340
341 cl_assert_equal_i(GIT_ITEROVER, error);
342 cl_assert_equal_i(expected, count);
343 git_config_iterator_free(iter);
344 }
345
346 void test_config_read__iterator_glob(void)
347 {
348 git_config *cfg;
349
350 cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config9")));
351
352 check_glob_iter(cfg, "core.*", 3);
353 check_glob_iter(cfg, "remote\\.ab.*", 2);
354 check_glob_iter(cfg, ".*url$", 2);
355 check_glob_iter(cfg, ".*dummy.*", 2);
356 check_glob_iter(cfg, ".*nomatch.*", 0);
357
358 git_config_free(cfg);
359 }
360
361 void test_config_read__whitespace_not_required_around_assignment(void)
362 {
363 git_config *cfg;
364 const char *str;
365
366 cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config14")));
367
368 cl_git_pass(git_config_get_string(&str, cfg, "a.b"));
369 cl_assert_equal_s(str, "c");
370
371 cl_git_pass(git_config_get_string(&str, cfg, "d.e"));
372 cl_assert_equal_s(str, "f");
373
374 git_config_free(cfg);
375 }
376
377 void test_config_read__read_git_config_entry(void)
378 {
379 git_config *cfg;
380 const git_config_entry *entry;
381
382 cl_git_pass(git_config_new(&cfg));
383 cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config9"),
384 GIT_CONFIG_LEVEL_SYSTEM, 0));
385
386 cl_git_pass(git_config_get_entry(&entry, cfg, "core.dummy2"));
387 cl_assert_equal_s("core.dummy2", entry->name);
388 cl_assert_equal_s("42", entry->value);
389 cl_assert_equal_i(GIT_CONFIG_LEVEL_SYSTEM, entry->level);
390
391 git_config_free(cfg);
392 }
393
394 /*
395 * At the beginning of the test:
396 * - config9 has: core.dummy2=42
397 * - config15 has: core.dummy2=7
398 * - config16 has: core.dummy2=28
399 */
400 void test_config_read__local_config_overrides_global_config_overrides_system_config(void)
401 {
402 git_config *cfg;
403 int32_t i;
404
405 cl_git_pass(git_config_new(&cfg));
406 cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config9"),
407 GIT_CONFIG_LEVEL_SYSTEM, 0));
408 cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config15"),
409 GIT_CONFIG_LEVEL_GLOBAL, 0));
410 cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config16"),
411 GIT_CONFIG_LEVEL_LOCAL, 0));
412
413 cl_git_pass(git_config_get_int32(&i, cfg, "core.dummy2"));
414 cl_assert_equal_i(28, i);
415
416 git_config_free(cfg);
417
418 cl_git_pass(git_config_new(&cfg));
419 cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config9"),
420 GIT_CONFIG_LEVEL_SYSTEM, 0));
421 cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config15"),
422 GIT_CONFIG_LEVEL_GLOBAL, 0));
423
424 cl_git_pass(git_config_get_int32(&i, cfg, "core.dummy2"));
425 cl_assert_equal_i(7, i);
426
427 git_config_free(cfg);
428 }
429
430 /*
431 * At the beginning of the test:
432 * - config9 has: core.global does not exist
433 * - config15 has: core.global=17
434 * - config16 has: core.global=29
435 *
436 * And also:
437 * - config9 has: core.system does not exist
438 * - config15 has: core.system does not exist
439 * - config16 has: core.system=11
440 */
441 void test_config_read__fallback_from_local_to_global_and_from_global_to_system(void)
442 {
443 git_config *cfg;
444 int32_t i;
445
446 cl_git_pass(git_config_new(&cfg));
447 cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config9"),
448 GIT_CONFIG_LEVEL_SYSTEM, 0));
449 cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config15"),
450 GIT_CONFIG_LEVEL_GLOBAL, 0));
451 cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config16"),
452 GIT_CONFIG_LEVEL_LOCAL, 0));
453
454 cl_git_pass(git_config_get_int32(&i, cfg, "core.global"));
455 cl_assert_equal_i(17, i);
456 cl_git_pass(git_config_get_int32(&i, cfg, "core.system"));
457 cl_assert_equal_i(11, i);
458
459 git_config_free(cfg);
460 }
461
462 /*
463 * At the beginning of the test, config18 has:
464 * int32global = 28
465 * int64global = 9223372036854775803
466 * boolglobal = true
467 * stringglobal = I'm a global config value!
468 *
469 * And config19 has:
470 * int32global = -1
471 * int64global = -2
472 * boolglobal = false
473 * stringglobal = don't find me!
474 *
475 */
476 void test_config_read__simple_read_from_specific_level(void)
477 {
478 git_config *cfg, *cfg_specific;
479 int i;
480 int64_t l, expected = +9223372036854775803;
481 const char *s;
482
483 cl_git_pass(git_config_new(&cfg));
484 cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config18"),
485 GIT_CONFIG_LEVEL_GLOBAL, 0));
486 cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config19"),
487 GIT_CONFIG_LEVEL_SYSTEM, 0));
488
489 cl_git_pass(git_config_open_level(&cfg_specific, cfg, GIT_CONFIG_LEVEL_GLOBAL));
490
491 cl_git_pass(git_config_get_int32(&i, cfg_specific, "core.int32global"));
492 cl_assert_equal_i(28, i);
493 cl_git_pass(git_config_get_int64(&l, cfg_specific, "core.int64global"));
494 cl_assert(l == expected);
495 cl_git_pass(git_config_get_bool(&i, cfg_specific, "core.boolglobal"));
496 cl_assert_equal_b(true, i);
497 cl_git_pass(git_config_get_string(&s, cfg_specific, "core.stringglobal"));
498 cl_assert_equal_s("I'm a global config value!", s);
499
500 git_config_free(cfg_specific);
501 git_config_free(cfg);
502 }
503
504 static void clean_test_config(void *unused)
505 {
506 GIT_UNUSED(unused);
507 cl_fixture_cleanup("./testconfig");
508 }
509
510 void test_config_read__can_load_and_parse_an_empty_config_file(void)
511 {
512 git_config *cfg;
513 int i;
514
515 cl_set_cleanup(&clean_test_config, NULL);
516 cl_git_mkfile("./testconfig", "");
517 cl_git_pass(git_config_open_ondisk(&cfg, "./testconfig"));
518 cl_assert_equal_i(GIT_ENOTFOUND, git_config_get_int32(&i, cfg, "nope.neither"));
519
520 git_config_free(cfg);
521 }
522
523 void test_config_read__corrupt_header(void)
524 {
525 git_config *cfg;
526
527 cl_set_cleanup(&clean_test_config, NULL);
528 cl_git_mkfile("./testconfig", "[sneaky ] \"quoted closing quote mark\\\"");
529 cl_git_fail(git_config_open_ondisk(&cfg, "./testconfig"));
530
531 git_config_free(cfg);
532 }
533
534 void test_config_read__corrupt_header2(void)
535 {
536 git_config *cfg;
537
538 cl_set_cleanup(&clean_test_config, NULL);
539 cl_git_mkfile("./testconfig", "[unclosed \"bracket\"\n lib = git2\n");
540 cl_git_fail(git_config_open_ondisk(&cfg, "./testconfig"));
541
542 git_config_free(cfg);
543 }
544
545 void test_config_read__corrupt_header3(void)
546 {
547 git_config *cfg;
548
549 cl_set_cleanup(&clean_test_config, NULL);
550 cl_git_mkfile("./testconfig", "[unclosed \"slash\\\"]\n lib = git2\n");
551 cl_git_fail(git_config_open_ondisk(&cfg, "./testconfig"));
552
553 git_config_free(cfg);
554 }
555
556 void test_config_read__override_variable(void)
557 {
558 git_config *cfg;
559 const char *str;
560
561 cl_set_cleanup(&clean_test_config, NULL);
562 cl_git_mkfile("./testconfig", "[some] var = one\nvar = two");
563 cl_git_pass(git_config_open_ondisk(&cfg, "./testconfig"));
564
565 cl_git_pass(git_config_get_string(&str, cfg, "some.var"));
566 cl_assert_equal_s(str, "two");
567
568 git_config_free(cfg);
569 }