]> git.proxmox.com Git - libgit2.git/blame - tests/config/read.c
rebase: correctly finish rebasing detached heads
[libgit2.git] / tests / config / read.c
CommitLineData
3fd1520c 1#include "clar_libgit2.h"
eac773d9
CMN
2#include "buffer.h"
3#include "path.h"
9462c471 4
9a97f49e
CMN
5static git_buf buf = GIT_BUF_INIT;
6
7void test_config_read__cleanup(void)
8{
9 git_buf_free(&buf);
10}
11
9462c471
VM
12void test_config_read__simple_read(void)
13{
14 git_config *cfg;
15 int32_t i;
16
17 cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config0")));
18
255c38c5 19 cl_git_pass(git_config_get_int32(&i, cfg, "core.repositoryformatversion"));
9462c471 20 cl_assert(i == 0);
255c38c5 21 cl_git_pass(git_config_get_bool(&i, cfg, "core.filemode"));
9462c471 22 cl_assert(i == 1);
255c38c5 23 cl_git_pass(git_config_get_bool(&i, cfg, "core.bare"));
9462c471 24 cl_assert(i == 0);
255c38c5 25 cl_git_pass(git_config_get_bool(&i, cfg, "core.logallrefupdates"));
9462c471
VM
26 cl_assert(i == 1);
27
28 git_config_free(cfg);
29}
30
31void test_config_read__case_sensitive(void)
32{
33 git_config *cfg;
34 int i;
9462c471
VM
35
36 cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config1")));
37
9a97f49e
CMN
38 cl_git_pass(git_config_get_string_buf(&buf, cfg, "this.that.other"));
39 cl_assert_equal_s("true", git_buf_cstr(&buf));
40 git_buf_clear(&buf);
41
42 cl_git_pass(git_config_get_string_buf(&buf, cfg, "this.That.other"));
43 cl_assert_equal_s("yes", git_buf_cstr(&buf));
9462c471 44
255c38c5 45 cl_git_pass(git_config_get_bool(&i, cfg, "this.that.other"));
9462c471 46 cl_assert(i == 1);
255c38c5 47 cl_git_pass(git_config_get_bool(&i, cfg, "this.That.other"));
9462c471
VM
48 cl_assert(i == 1);
49
50 /* This one doesn't exist */
255c38c5 51 cl_must_fail(git_config_get_bool(&i, cfg, "this.thaT.other"));
9462c471
VM
52
53 git_config_free(cfg);
54}
55
56/*
57 * If \ is the last non-space character on the line, we read the next
58 * one, separating each line with SP.
59 */
60void test_config_read__multiline_value(void)
61{
62 git_config *cfg;
9462c471
VM
63
64 cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config2")));
65
9a97f49e
CMN
66 cl_git_pass(git_config_get_string_buf(&buf, cfg, "this.That.and"));
67 cl_assert_equal_s("one one one two two three three", git_buf_cstr(&buf));
9462c471
VM
68
69 git_config_free(cfg);
70}
71
7f2e61f3
ET
72static void clean_test_config(void *unused)
73{
74 GIT_UNUSED(unused);
75 cl_fixture_cleanup("./testconfig");
76}
77
78void test_config_read__multiline_value_and_eof(void)
79{
80 git_config *cfg;
81
82 cl_set_cleanup(&clean_test_config, NULL);
83 cl_git_mkfile("./testconfig", "[header]\n key1 = foo\\\n");
84 cl_git_pass(git_config_open_ondisk(&cfg, "./testconfig"));
85
86 cl_git_pass(git_config_get_string_buf(&buf, cfg, "header.key1"));
87 cl_assert_equal_s("foo", git_buf_cstr(&buf));
88
89 git_config_free(cfg);
90}
91
92void test_config_read__multiline_eof(void)
93{
94 git_config *cfg;
95
96 cl_set_cleanup(&clean_test_config, NULL);
97 cl_git_mkfile("./testconfig", "[header]\n key1 = \\\n");
98 cl_git_pass(git_config_open_ondisk(&cfg, "./testconfig"));
99
100 cl_git_pass(git_config_get_string_buf(&buf, cfg, "header.key1"));
101 cl_assert_equal_s("", git_buf_cstr(&buf));
102
103 git_config_free(cfg);
104}
105
9462c471
VM
106/*
107 * This kind of subsection declaration is case-insensitive
108 */
109void test_config_read__subsection_header(void)
110{
111 git_config *cfg;
9462c471
VM
112
113 cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config3")));
114
9a97f49e
CMN
115 cl_git_pass(git_config_get_string_buf(&buf, cfg, "section.subsection.var"));
116 cl_assert_equal_s("hello", git_buf_cstr(&buf));
9462c471
VM
117
118 /* The subsection is transformed to lower-case */
9a97f49e 119 cl_must_fail(git_config_get_string_buf(&buf, cfg, "section.subSectIon.var"));
9462c471
VM
120
121 git_config_free(cfg);
122}
123
124void test_config_read__lone_variable(void)
125{
126 git_config *cfg;
9462c471
VM
127 int i;
128
129 cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config4")));
130
0da81d2b
CMN
131 cl_git_fail(git_config_get_int32(&i, cfg, "some.section.variable"));
132
9a97f49e
CMN
133 cl_git_pass(git_config_get_string_buf(&buf, cfg, "some.section.variable"));
134 cl_assert_equal_s("", git_buf_cstr(&buf));
135 git_buf_clear(&buf);
9462c471 136
255c38c5 137 cl_git_pass(git_config_get_bool(&i, cfg, "some.section.variable"));
9462c471
VM
138 cl_assert(i == 1);
139
9a97f49e
CMN
140 cl_git_pass(git_config_get_string_buf(&buf, cfg, "some.section.variableeq"));
141 cl_assert_equal_s("", git_buf_cstr(&buf));
47db054d
CMN
142
143 cl_git_pass(git_config_get_bool(&i, cfg, "some.section.variableeq"));
144 cl_assert(i == 0);
145
9462c471
VM
146 git_config_free(cfg);
147}
148
149void test_config_read__number_suffixes(void)
150{
151 git_config *cfg;
152 int64_t i;
153
154 cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config5")));
155
255c38c5 156 cl_git_pass(git_config_get_int64(&i, cfg, "number.simple"));
9462c471
VM
157 cl_assert(i == 1);
158
255c38c5 159 cl_git_pass(git_config_get_int64(&i, cfg, "number.k"));
9462c471
VM
160 cl_assert(i == 1 * 1024);
161
255c38c5 162 cl_git_pass(git_config_get_int64(&i, cfg, "number.kk"));
9462c471
VM
163 cl_assert(i == 1 * 1024);
164
255c38c5 165 cl_git_pass(git_config_get_int64(&i, cfg, "number.m"));
9462c471
VM
166 cl_assert(i == 1 * 1024 * 1024);
167
255c38c5 168 cl_git_pass(git_config_get_int64(&i, cfg, "number.mm"));
9462c471
VM
169 cl_assert(i == 1 * 1024 * 1024);
170
255c38c5 171 cl_git_pass(git_config_get_int64(&i, cfg, "number.g"));
9462c471
VM
172 cl_assert(i == 1 * 1024 * 1024 * 1024);
173
255c38c5 174 cl_git_pass(git_config_get_int64(&i, cfg, "number.gg"));
9462c471
VM
175 cl_assert(i == 1 * 1024 * 1024 * 1024);
176
177 git_config_free(cfg);
178}
179
180void test_config_read__blank_lines(void)
181{
182 git_config *cfg;
183 int i;
184
185 cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config6")));
186
255c38c5 187 cl_git_pass(git_config_get_bool(&i, cfg, "valid.subsection.something"));
9462c471
VM
188 cl_assert(i == 1);
189
255c38c5 190 cl_git_pass(git_config_get_bool(&i, cfg, "something.else.something"));
9462c471
VM
191 cl_assert(i == 0);
192
193 git_config_free(cfg);
194}
195
196void test_config_read__invalid_ext_headers(void)
197{
198 git_config *cfg;
199 cl_must_fail(git_config_open_ondisk(&cfg, cl_fixture("config/config7")));
200}
201
202void test_config_read__empty_files(void)
203{
204 git_config *cfg;
205 cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config8")));
206 git_config_free(cfg);
207}
208
d52a93fa
L
209void test_config_read__symbol_headers(void)
210{
211 git_config *cfg;
212 cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config20")));
213 git_config_free(cfg);
214}
215
9462c471
VM
216void test_config_read__header_in_last_line(void)
217{
218 git_config *cfg;
219
220 cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config10")));
221 git_config_free(cfg);
222}
223
224void test_config_read__prefixes(void)
225{
226 git_config *cfg;
9462c471
VM
227
228 cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config9")));
9a97f49e
CMN
229 cl_git_pass(git_config_get_string_buf(&buf, cfg, "remote.ab.url"));
230 cl_assert_equal_s("http://example.com/git/ab", git_buf_cstr(&buf));
231 git_buf_clear(&buf);
9462c471 232
9a97f49e
CMN
233 cl_git_pass(git_config_get_string_buf(&buf, cfg, "remote.abba.url"));
234 cl_assert_equal_s("http://example.com/git/abba", git_buf_cstr(&buf));
9462c471
VM
235
236 git_config_free(cfg);
237}
238
a7d19b97
CMN
239void test_config_read__escaping_quotes(void)
240{
241 git_config *cfg;
a7d19b97
CMN
242
243 cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config13")));
9a97f49e
CMN
244 cl_git_pass(git_config_get_string_buf(&buf, cfg, "core.editor"));
245 cl_assert_equal_s("\"C:/Program Files/Nonsense/bah.exe\" \"--some option\"", git_buf_cstr(&buf));
a7d19b97
CMN
246
247 git_config_free(cfg);
248}
249
e009a705
ET
250void test_config_read__invalid_escape_sequence(void)
251{
252 git_config *cfg;
253
254 cl_set_cleanup(&clean_test_config, NULL);
255 cl_git_mkfile("./testconfig", "[header]\n key1 = \\\\\\;\n key2 = value2\n");
256 cl_git_fail(git_config_open_ondisk(&cfg, "./testconfig"));
257
258 git_config_free(cfg);
259}
260
a1abe66a 261static int count_cfg_entries_and_compare_levels(
262 const git_config_entry *entry, void *payload)
b3ff1dab
RB
263{
264 int *count = payload;
a1abe66a 265
266 if (!strcmp(entry->value, "7") || !strcmp(entry->value, "17"))
267 cl_assert(entry->level == GIT_CONFIG_LEVEL_GLOBAL);
268 else
269 cl_assert(entry->level == GIT_CONFIG_LEVEL_SYSTEM);
270
b3ff1dab
RB
271 (*count)++;
272 return 0;
273}
274
a1abe66a 275static int cfg_callback_countdown(const git_config_entry *entry, void *payload)
b3ff1dab
RB
276{
277 int *count = payload;
a1abe66a 278 GIT_UNUSED(entry);
b3ff1dab
RB
279 (*count)--;
280 if (*count == 0)
281 return -100;
282 return 0;
283}
284
285void test_config_read__foreach(void)
286{
287 git_config *cfg;
288 int count, ret;
289
a1abe66a 290 cl_git_pass(git_config_new(&cfg));
291 cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config9"),
292 GIT_CONFIG_LEVEL_SYSTEM, 0));
293 cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config15"),
294 GIT_CONFIG_LEVEL_GLOBAL, 0));
b3ff1dab
RB
295
296 count = 0;
a1abe66a 297 cl_git_pass(git_config_foreach(cfg, count_cfg_entries_and_compare_levels, &count));
298 cl_assert_equal_i(7, count);
b3ff1dab
RB
299
300 count = 3;
301 cl_git_fail(ret = git_config_foreach(cfg, cfg_callback_countdown, &count));
25e0b157 302 cl_assert_equal_i(-100, ret);
b3ff1dab
RB
303
304 git_config_free(cfg);
305}
306
5880962d
CMN
307void test_config_read__iterator(void)
308{
309 git_config *cfg;
310 git_config_iterator *iter;
311 git_config_entry *entry;
312 int count, ret;
313
314 cl_git_pass(git_config_new(&cfg));
315 cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config9"),
316 GIT_CONFIG_LEVEL_SYSTEM, 0));
317 cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config15"),
318 GIT_CONFIG_LEVEL_GLOBAL, 0));
319
320 count = 0;
321 cl_git_pass(git_config_iterator_new(&iter, cfg));
322
323 while ((ret = git_config_next(&entry, iter)) == 0) {
324 count++;
325 }
326
54f3a572 327 git_config_iterator_free(iter);
5880962d
CMN
328 cl_assert_equal_i(GIT_ITEROVER, ret);
329 cl_assert_equal_i(7, count);
330
331 count = 3;
332 cl_git_pass(git_config_iterator_new(&iter, cfg));
333
334 git_config_iterator_free(iter);
335 git_config_free(cfg);
336}
337
a1abe66a 338static int count_cfg_entries(const git_config_entry *entry, void *payload)
339{
340 int *count = payload;
341 GIT_UNUSED(entry);
342 (*count)++;
343 return 0;
344}
345
b3ff1dab
RB
346void test_config_read__foreach_match(void)
347{
348 git_config *cfg;
349 int count;
350
351 cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config9")));
352
353 count = 0;
354 cl_git_pass(
355 git_config_foreach_match(cfg, "core.*", count_cfg_entries, &count));
356 cl_assert_equal_i(3, count);
357
358 count = 0;
359 cl_git_pass(
360 git_config_foreach_match(cfg, "remote\\.ab.*", count_cfg_entries, &count));
361 cl_assert_equal_i(2, count);
362
363 count = 0;
364 cl_git_pass(
365 git_config_foreach_match(cfg, ".*url$", count_cfg_entries, &count));
366 cl_assert_equal_i(2, count);
367
368 count = 0;
369 cl_git_pass(
370 git_config_foreach_match(cfg, ".*dummy.*", count_cfg_entries, &count));
371 cl_assert_equal_i(2, count);
372
373 count = 0;
374 cl_git_pass(
375 git_config_foreach_match(cfg, ".*nomatch.*", count_cfg_entries, &count));
376 cl_assert_equal_i(0, count);
377
378 git_config_free(cfg);
379}
380
54f3a572
CMN
381static void check_glob_iter(git_config *cfg, const char *regexp, int expected)
382{
383 git_config_iterator *iter;
384 git_config_entry *entry;
385 int count, error;
386
387 cl_git_pass(git_config_iterator_glob_new(&iter, cfg, regexp));
388
389 count = 0;
390 while ((error = git_config_next(&entry, iter)) == 0)
391 count++;
392
393 cl_assert_equal_i(GIT_ITEROVER, error);
394 cl_assert_equal_i(expected, count);
395 git_config_iterator_free(iter);
396}
397
129022ee
PS
398void test_config_read__iterator_invalid_glob(void)
399{
400 git_config *cfg;
401 git_config_iterator *iter;
402
403 cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config9")));
404
405 cl_git_fail(git_config_iterator_glob_new(&iter, cfg, "*"));
406
407 git_config_free(cfg);
408}
409
54f3a572
CMN
410void test_config_read__iterator_glob(void)
411{
412 git_config *cfg;
413
414 cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config9")));
415
416 check_glob_iter(cfg, "core.*", 3);
417 check_glob_iter(cfg, "remote\\.ab.*", 2);
418 check_glob_iter(cfg, ".*url$", 2);
419 check_glob_iter(cfg, ".*dummy.*", 2);
420 check_glob_iter(cfg, ".*nomatch.*", 0);
421
422 git_config_free(cfg);
423}
424
a1ecddf0
RB
425void test_config_read__whitespace_not_required_around_assignment(void)
426{
427 git_config *cfg;
a1ecddf0
RB
428
429 cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config14")));
430
9a97f49e
CMN
431 cl_git_pass(git_config_get_string_buf(&buf, cfg, "a.b"));
432 cl_assert_equal_s("c", git_buf_cstr(&buf));
433 git_buf_clear(&buf);
a1ecddf0 434
9a97f49e
CMN
435 cl_git_pass(git_config_get_string_buf(&buf, cfg, "d.e"));
436 cl_assert_equal_s("f", git_buf_cstr(&buf));
a1ecddf0
RB
437
438 git_config_free(cfg);
439}
440
a1abe66a 441void test_config_read__read_git_config_entry(void)
442{
443 git_config *cfg;
9a97f49e 444 git_config_entry *entry;
9462c471 445
a1abe66a 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
3ee078c0 450 cl_git_pass(git_config_get_entry(&entry, cfg, "core.dummy2"));
a1abe66a 451 cl_assert_equal_s("core.dummy2", entry->name);
452 cl_assert_equal_s("42", entry->value);
453 cl_assert_equal_i(GIT_CONFIG_LEVEL_SYSTEM, entry->level);
454
9a97f49e 455 git_config_entry_free(entry);
a1abe66a 456 git_config_free(cfg);
457}
458
459/*
460 * At the beginning of the test:
461 * - config9 has: core.dummy2=42
462 * - config15 has: core.dummy2=7
463 * - config16 has: core.dummy2=28
464 */
465void test_config_read__local_config_overrides_global_config_overrides_system_config(void)
466{
9462c471 467 git_config *cfg;
a1abe66a 468 int32_t i;
469
470 cl_git_pass(git_config_new(&cfg));
471 cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config9"),
472 GIT_CONFIG_LEVEL_SYSTEM, 0));
473 cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config15"),
474 GIT_CONFIG_LEVEL_GLOBAL, 0));
475 cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config16"),
476 GIT_CONFIG_LEVEL_LOCAL, 0));
477
478 cl_git_pass(git_config_get_int32(&i, cfg, "core.dummy2"));
479 cl_assert_equal_i(28, i);
480
481 git_config_free(cfg);
482
483 cl_git_pass(git_config_new(&cfg));
484 cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config9"),
485 GIT_CONFIG_LEVEL_SYSTEM, 0));
486 cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config15"),
487 GIT_CONFIG_LEVEL_GLOBAL, 0));
488
489 cl_git_pass(git_config_get_int32(&i, cfg, "core.dummy2"));
490 cl_assert_equal_i(7, i);
9462c471 491
9462c471 492 git_config_free(cfg);
a1abe66a 493}
9462c471 494
a1abe66a 495/*
496 * At the beginning of the test:
497 * - config9 has: core.global does not exist
498 * - config15 has: core.global=17
499 * - config16 has: core.global=29
500 *
501 * And also:
502 * - config9 has: core.system does not exist
503 * - config15 has: core.system does not exist
504 * - config16 has: core.system=11
505 */
506void test_config_read__fallback_from_local_to_global_and_from_global_to_system(void)
507{
9462c471 508 git_config *cfg;
a1abe66a 509 int32_t i;
9462c471 510
a1abe66a 511 cl_git_pass(git_config_new(&cfg));
512 cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config9"),
513 GIT_CONFIG_LEVEL_SYSTEM, 0));
514 cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config15"),
515 GIT_CONFIG_LEVEL_GLOBAL, 0));
516 cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config16"),
517 GIT_CONFIG_LEVEL_LOCAL, 0));
518
519 cl_git_pass(git_config_get_int32(&i, cfg, "core.global"));
520 cl_assert_equal_i(17, i);
521 cl_git_pass(git_config_get_int32(&i, cfg, "core.system"));
522 cl_assert_equal_i(11, i);
523
524 git_config_free(cfg);
525}
526
527/*
528 * At the beginning of the test, config18 has:
529 * int32global = 28
530 * int64global = 9223372036854775803
531 * boolglobal = true
532 * stringglobal = I'm a global config value!
533 *
534 * And config19 has:
535 * int32global = -1
536 * int64global = -2
537 * boolglobal = false
538 * stringglobal = don't find me!
539 *
540 */
541void test_config_read__simple_read_from_specific_level(void)
542{
543 git_config *cfg, *cfg_specific;
544 int i;
545 int64_t l, expected = +9223372036854775803;
a1abe66a 546
547 cl_git_pass(git_config_new(&cfg));
548 cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config18"),
549 GIT_CONFIG_LEVEL_GLOBAL, 0));
550 cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config19"),
551 GIT_CONFIG_LEVEL_SYSTEM, 0));
552
553 cl_git_pass(git_config_open_level(&cfg_specific, cfg, GIT_CONFIG_LEVEL_GLOBAL));
554
555 cl_git_pass(git_config_get_int32(&i, cfg_specific, "core.int32global"));
556 cl_assert_equal_i(28, i);
557 cl_git_pass(git_config_get_int64(&l, cfg_specific, "core.int64global"));
558 cl_assert(l == expected);
559 cl_git_pass(git_config_get_bool(&i, cfg_specific, "core.boolglobal"));
560 cl_assert_equal_b(true, i);
9a97f49e
CMN
561 cl_git_pass(git_config_get_string_buf(&buf, cfg_specific, "core.stringglobal"));
562 cl_assert_equal_s("I'm a global config value!", git_buf_cstr(&buf));
a1abe66a 563
564 git_config_free(cfg_specific);
9462c471 565 git_config_free(cfg);
a1abe66a 566}
270160b9 567
568void test_config_read__can_load_and_parse_an_empty_config_file(void)
569{
570 git_config *cfg;
571 int i;
572
2d9f5b9f
ET
573 cl_set_cleanup(&clean_test_config, NULL);
574 cl_git_mkfile("./testconfig", "");
575 cl_git_pass(git_config_open_ondisk(&cfg, "./testconfig"));
270160b9 576 cl_assert_equal_i(GIT_ENOTFOUND, git_config_get_int32(&i, cfg, "nope.neither"));
577
578 git_config_free(cfg);
579}
2d9f5b9f
ET
580
581void test_config_read__corrupt_header(void)
582{
583 git_config *cfg;
584
585 cl_set_cleanup(&clean_test_config, NULL);
586 cl_git_mkfile("./testconfig", "[sneaky ] \"quoted closing quote mark\\\"");
587 cl_git_fail(git_config_open_ondisk(&cfg, "./testconfig"));
588
589 git_config_free(cfg);
590}
73fc5e01 591
8d741253
L
592void test_config_read__corrupt_header2(void)
593{
594 git_config *cfg;
595
596 cl_set_cleanup(&clean_test_config, NULL);
597 cl_git_mkfile("./testconfig", "[unclosed \"bracket\"\n lib = git2\n");
598 cl_git_fail(git_config_open_ondisk(&cfg, "./testconfig"));
599
600 git_config_free(cfg);
601}
602
603void test_config_read__corrupt_header3(void)
604{
605 git_config *cfg;
606
607 cl_set_cleanup(&clean_test_config, NULL);
608 cl_git_mkfile("./testconfig", "[unclosed \"slash\\\"]\n lib = git2\n");
609 cl_git_fail(git_config_open_ondisk(&cfg, "./testconfig"));
610
23fb4004
ET
611 git_config_free(cfg);
612}
613
614void test_config_read__invalid_key_chars(void)
615{
616 git_config *cfg;
617
618 cl_set_cleanup(&clean_test_config, NULL);
619 cl_git_mkfile("./testconfig", "[foo]\n has_underscore = git2\n");
620 cl_git_fail(git_config_open_ondisk(&cfg, "./testconfig"));
621
622 cl_git_rewritefile("./testconfig", "[foo]\n has/slash = git2\n");
623 cl_git_fail(git_config_open_ondisk(&cfg, "./testconfig"));
624
625 cl_git_rewritefile("./testconfig", "[foo]\n has+plus = git2\n");
626 cl_git_fail(git_config_open_ondisk(&cfg, "./testconfig"));
627
628 cl_git_rewritefile("./testconfig", "[no_key]\n = git2\n");
629 cl_git_fail(git_config_open_ondisk(&cfg, "./testconfig"));
630
631 git_config_free(cfg);
632}
633
634void test_config_read__lone_variable_with_trailing_whitespace(void)
635{
636 git_config *cfg;
637 int b;
638
639 cl_set_cleanup(&clean_test_config, NULL);
640 cl_git_mkfile("./testconfig", "[foo]\n lonevariable \n");
641 cl_git_pass(git_config_open_ondisk(&cfg, "./testconfig"));
642
643 cl_git_pass(git_config_get_bool(&b, cfg, "foo.lonevariable"));
644 cl_assert_equal_b(true, b);
645
8d741253
L
646 git_config_free(cfg);
647}
648
73fc5e01
CMN
649void test_config_read__override_variable(void)
650{
651 git_config *cfg;
73fc5e01
CMN
652
653 cl_set_cleanup(&clean_test_config, NULL);
654 cl_git_mkfile("./testconfig", "[some] var = one\nvar = two");
655 cl_git_pass(git_config_open_ondisk(&cfg, "./testconfig"));
656
9a97f49e
CMN
657 cl_git_pass(git_config_get_string_buf(&buf, cfg, "some.var"));
658 cl_assert_equal_s("two", git_buf_cstr(&buf));
73fc5e01
CMN
659
660 git_config_free(cfg);
661}
eac773d9
CMN
662
663void test_config_read__path(void)
664{
665 git_config *cfg;
666 git_buf path = GIT_BUF_INIT;
667 git_buf old_path = GIT_BUF_INIT;
668 git_buf home_path = GIT_BUF_INIT;
669 git_buf expected_path = GIT_BUF_INIT;
670
671 cl_git_pass(p_mkdir("fakehome", 0777));
672 cl_git_pass(git_path_prettify(&home_path, "fakehome", NULL));
673 cl_git_pass(git_libgit2_opts(GIT_OPT_GET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, &old_path));
674 cl_git_pass(git_libgit2_opts(GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, home_path.ptr));
675 cl_git_mkfile("./testconfig", "[some]\n path = ~/somefile");
676 cl_git_pass(git_path_join_unrooted(&expected_path, "somefile", home_path.ptr, NULL));
677
678 cl_git_pass(git_config_open_ondisk(&cfg, "./testconfig"));
679 cl_git_pass(git_config_get_path(&path, cfg, "some.path"));
680 cl_assert_equal_s(expected_path.ptr, path.ptr);
681 git_buf_free(&path);
682
683 cl_git_mkfile("./testconfig", "[some]\n path = ~/");
684 cl_git_pass(git_path_join_unrooted(&expected_path, "", home_path.ptr, NULL));
685
686 cl_git_pass(git_config_get_path(&path, cfg, "some.path"));
687 cl_assert_equal_s(expected_path.ptr, path.ptr);
688 git_buf_free(&path);
689
690 cl_git_mkfile("./testconfig", "[some]\n path = ~");
691 cl_git_pass(git_buf_sets(&expected_path, home_path.ptr));
692
693 cl_git_pass(git_config_get_path(&path, cfg, "some.path"));
694 cl_assert_equal_s(expected_path.ptr, path.ptr);
695 git_buf_free(&path);
696
697 cl_git_mkfile("./testconfig", "[some]\n path = ~user/foo");
698 cl_git_fail(git_config_get_path(&path, cfg, "some.path"));
699
700 cl_git_pass(git_libgit2_opts(GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, old_path.ptr));
701 git_buf_free(&old_path);
702 git_buf_free(&home_path);
703 git_buf_free(&expected_path);
704 git_config_free(cfg);
705}