1 #include "clar_libgit2.h"
3 void test_config_read__simple_read(void)
8 cl_git_pass(git_config_open_ondisk(&cfg
, cl_fixture("config/config0")));
10 cl_git_pass(git_config_get_int32(&i
, cfg
, "core.repositoryformatversion"));
12 cl_git_pass(git_config_get_bool(&i
, cfg
, "core.filemode"));
14 cl_git_pass(git_config_get_bool(&i
, cfg
, "core.bare"));
16 cl_git_pass(git_config_get_bool(&i
, cfg
, "core.logallrefupdates"));
22 void test_config_read__case_sensitive(void)
28 cl_git_pass(git_config_open_ondisk(&cfg
, cl_fixture("config/config1")));
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");
35 cl_git_pass(git_config_get_bool(&i
, cfg
, "this.that.other"));
37 cl_git_pass(git_config_get_bool(&i
, cfg
, "this.That.other"));
40 /* This one doesn't exist */
41 cl_must_fail(git_config_get_bool(&i
, cfg
, "this.thaT.other"));
47 * If \ is the last non-space character on the line, we read the next
48 * one, separating each line with SP.
50 void test_config_read__multiline_value(void)
55 cl_git_pass(git_config_open_ondisk(&cfg
, cl_fixture("config/config2")));
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");
64 * This kind of subsection declaration is case-insensitive
66 void test_config_read__subsection_header(void)
71 cl_git_pass(git_config_open_ondisk(&cfg
, cl_fixture("config/config3")));
73 cl_git_pass(git_config_get_string(&str
, cfg
, "section.subsection.var"));
74 cl_assert_equal_s(str
, "hello");
76 /* The subsection is transformed to lower-case */
77 cl_must_fail(git_config_get_string(&str
, cfg
, "section.subSectIon.var"));
82 void test_config_read__lone_variable(void)
88 cl_git_pass(git_config_open_ondisk(&cfg
, cl_fixture("config/config4")));
90 cl_git_fail(git_config_get_int32(&i
, cfg
, "some.section.variable"));
92 cl_git_pass(git_config_get_string(&str
, cfg
, "some.section.variable"));
93 cl_assert_equal_s(str
, "");
95 cl_git_pass(git_config_get_bool(&i
, cfg
, "some.section.variable"));
98 cl_git_pass(git_config_get_string(&str
, cfg
, "some.section.variableeq"));
99 cl_assert_equal_s(str
, "");
101 cl_git_pass(git_config_get_bool(&i
, cfg
, "some.section.variableeq"));
104 git_config_free(cfg
);
107 void test_config_read__number_suffixes(void)
112 cl_git_pass(git_config_open_ondisk(&cfg
, cl_fixture("config/config5")));
114 cl_git_pass(git_config_get_int64(&i
, cfg
, "number.simple"));
117 cl_git_pass(git_config_get_int64(&i
, cfg
, "number.k"));
118 cl_assert(i
== 1 * 1024);
120 cl_git_pass(git_config_get_int64(&i
, cfg
, "number.kk"));
121 cl_assert(i
== 1 * 1024);
123 cl_git_pass(git_config_get_int64(&i
, cfg
, "number.m"));
124 cl_assert(i
== 1 * 1024 * 1024);
126 cl_git_pass(git_config_get_int64(&i
, cfg
, "number.mm"));
127 cl_assert(i
== 1 * 1024 * 1024);
129 cl_git_pass(git_config_get_int64(&i
, cfg
, "number.g"));
130 cl_assert(i
== 1 * 1024 * 1024 * 1024);
132 cl_git_pass(git_config_get_int64(&i
, cfg
, "number.gg"));
133 cl_assert(i
== 1 * 1024 * 1024 * 1024);
135 git_config_free(cfg
);
138 void test_config_read__blank_lines(void)
143 cl_git_pass(git_config_open_ondisk(&cfg
, cl_fixture("config/config6")));
145 cl_git_pass(git_config_get_bool(&i
, cfg
, "valid.subsection.something"));
148 cl_git_pass(git_config_get_bool(&i
, cfg
, "something.else.something"));
151 git_config_free(cfg
);
154 void test_config_read__invalid_ext_headers(void)
157 cl_must_fail(git_config_open_ondisk(&cfg
, cl_fixture("config/config7")));
160 void test_config_read__empty_files(void)
163 cl_git_pass(git_config_open_ondisk(&cfg
, cl_fixture("config/config8")));
164 git_config_free(cfg
);
167 void test_config_read__symbol_headers(void)
170 cl_git_pass(git_config_open_ondisk(&cfg
, cl_fixture("config/config20")));
171 git_config_free(cfg
);
174 void test_config_read__header_in_last_line(void)
178 cl_git_pass(git_config_open_ondisk(&cfg
, cl_fixture("config/config10")));
179 git_config_free(cfg
);
182 void test_config_read__prefixes(void)
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");
191 cl_git_pass(git_config_get_string(&str
, cfg
, "remote.abba.url"));
192 cl_assert_equal_s(str
, "http://example.com/git/abba");
194 git_config_free(cfg
);
197 void test_config_read__escaping_quotes(void)
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
);
206 git_config_free(cfg
);
209 static int count_cfg_entries_and_compare_levels(
210 const git_config_entry
*entry
, void *payload
)
212 int *count
= payload
;
214 if (!strcmp(entry
->value
, "7") || !strcmp(entry
->value
, "17"))
215 cl_assert(entry
->level
== GIT_CONFIG_LEVEL_GLOBAL
);
217 cl_assert(entry
->level
== GIT_CONFIG_LEVEL_SYSTEM
);
223 static int cfg_callback_countdown(const git_config_entry
*entry
, void *payload
)
225 int *count
= payload
;
233 void test_config_read__foreach(void)
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));
245 cl_git_pass(git_config_foreach(cfg
, count_cfg_entries_and_compare_levels
, &count
));
246 cl_assert_equal_i(7, count
);
249 cl_git_fail(ret
= git_config_foreach(cfg
, cfg_callback_countdown
, &count
));
250 cl_assert_equal_i(GIT_EUSER
, ret
);
252 git_config_free(cfg
);
255 void test_config_read__iterator(void)
258 git_config_iterator
*iter
;
259 git_config_entry
*entry
;
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));
269 cl_git_pass(git_config_iterator_new(&iter
, cfg
));
271 while ((ret
= git_config_next(&entry
, iter
)) == 0) {
275 git_config_iterator_free(iter
);
276 cl_assert_equal_i(GIT_ITEROVER
, ret
);
277 cl_assert_equal_i(7, count
);
280 cl_git_pass(git_config_iterator_new(&iter
, cfg
));
282 git_config_iterator_free(iter
);
283 git_config_free(cfg
);
286 static int count_cfg_entries(const git_config_entry
*entry
, void *payload
)
288 int *count
= payload
;
294 void test_config_read__foreach_match(void)
299 cl_git_pass(git_config_open_ondisk(&cfg
, cl_fixture("config/config9")));
303 git_config_foreach_match(cfg
, "core.*", count_cfg_entries
, &count
));
304 cl_assert_equal_i(3, count
);
308 git_config_foreach_match(cfg
, "remote\\.ab.*", count_cfg_entries
, &count
));
309 cl_assert_equal_i(2, count
);
313 git_config_foreach_match(cfg
, ".*url$", count_cfg_entries
, &count
));
314 cl_assert_equal_i(2, count
);
318 git_config_foreach_match(cfg
, ".*dummy.*", count_cfg_entries
, &count
));
319 cl_assert_equal_i(2, count
);
323 git_config_foreach_match(cfg
, ".*nomatch.*", count_cfg_entries
, &count
));
324 cl_assert_equal_i(0, count
);
326 git_config_free(cfg
);
329 static void check_glob_iter(git_config
*cfg
, const char *regexp
, int expected
)
331 git_config_iterator
*iter
;
332 git_config_entry
*entry
;
335 cl_git_pass(git_config_iterator_glob_new(&iter
, cfg
, regexp
));
338 while ((error
= git_config_next(&entry
, iter
)) == 0)
341 cl_assert_equal_i(GIT_ITEROVER
, error
);
342 cl_assert_equal_i(expected
, count
);
343 git_config_iterator_free(iter
);
346 void test_config_read__iterator_glob(void)
350 cl_git_pass(git_config_open_ondisk(&cfg
, cl_fixture("config/config9")));
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);
358 git_config_free(cfg
);
361 void test_config_read__whitespace_not_required_around_assignment(void)
366 cl_git_pass(git_config_open_ondisk(&cfg
, cl_fixture("config/config14")));
368 cl_git_pass(git_config_get_string(&str
, cfg
, "a.b"));
369 cl_assert_equal_s(str
, "c");
371 cl_git_pass(git_config_get_string(&str
, cfg
, "d.e"));
372 cl_assert_equal_s(str
, "f");
374 git_config_free(cfg
);
377 void test_config_read__read_git_config_entry(void)
380 const git_config_entry
*entry
;
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));
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
);
391 git_config_free(cfg
);
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
400 void test_config_read__local_config_overrides_global_config_overrides_system_config(void)
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));
413 cl_git_pass(git_config_get_int32(&i
, cfg
, "core.dummy2"));
414 cl_assert_equal_i(28, i
);
416 git_config_free(cfg
);
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));
424 cl_git_pass(git_config_get_int32(&i
, cfg
, "core.dummy2"));
425 cl_assert_equal_i(7, i
);
427 git_config_free(cfg
);
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
437 * - config9 has: core.system does not exist
438 * - config15 has: core.system does not exist
439 * - config16 has: core.system=11
441 void test_config_read__fallback_from_local_to_global_and_from_global_to_system(void)
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));
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
);
459 git_config_free(cfg
);
463 * At the beginning of the test, config18 has:
465 * int64global = 9223372036854775803
467 * stringglobal = I'm a global config value!
473 * stringglobal = don't find me!
476 void test_config_read__simple_read_from_specific_level(void)
478 git_config
*cfg
, *cfg_specific
;
480 int64_t l
, expected
= +9223372036854775803;
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));
489 cl_git_pass(git_config_open_level(&cfg_specific
, cfg
, GIT_CONFIG_LEVEL_GLOBAL
));
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
);
500 git_config_free(cfg_specific
);
501 git_config_free(cfg
);
504 static void clean_test_config(void *unused
)
507 cl_fixture_cleanup("./testconfig");
510 void test_config_read__can_load_and_parse_an_empty_config_file(void)
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"));
520 git_config_free(cfg
);
523 void test_config_read__corrupt_header(void)
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"));
531 git_config_free(cfg
);
534 void test_config_read__corrupt_header2(void)
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"));
542 git_config_free(cfg
);
545 void test_config_read__corrupt_header3(void)
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"));
553 git_config_free(cfg
);
556 void test_config_read__override_variable(void)
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"));
565 cl_git_pass(git_config_get_string(&str
, cfg
, "some.var"));
566 cl_assert_equal_s(str
, "two");
568 git_config_free(cfg
);