read.c 28.6 KB
Newer Older
1
#include "clar_libgit2.h"
2
#include "fs_path.h"
3

4 5 6 7
static git_buf buf = GIT_BUF_INIT;

void test_config_read__cleanup(void)
{
8
	git_buf_dispose(&buf);
9 10
}

11 12 13 14 15 16 17
void test_config_read__simple_read(void)
{
	git_config *cfg;
	int32_t i;

	cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config0")));

18
	cl_git_pass(git_config_get_int32(&i, cfg, "core.repositoryformatversion"));
19
	cl_assert(i == 0);
20
	cl_git_pass(git_config_get_bool(&i, cfg, "core.filemode"));
21
	cl_assert(i == 1);
22
	cl_git_pass(git_config_get_bool(&i, cfg, "core.bare"));
23
	cl_assert(i == 0);
24
	cl_git_pass(git_config_get_bool(&i, cfg, "core.logallrefupdates"));
25 26 27 28 29 30 31 32 33 34 35 36
	cl_assert(i == 1);

	git_config_free(cfg);
}

void test_config_read__case_sensitive(void)
{
	git_config *cfg;
	int i;

	cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config1")));

37
	cl_git_pass(git_config_get_string_buf(&buf, cfg, "this.that.other"));
38 39
	cl_assert_equal_s("true", buf.ptr);
	git_buf_dispose(&buf);
40 41

	cl_git_pass(git_config_get_string_buf(&buf, cfg, "this.That.other"));
42
	cl_assert_equal_s("yes", buf.ptr);
43

44
	cl_git_pass(git_config_get_bool(&i, cfg, "this.that.other"));
45
	cl_assert(i == 1);
46
	cl_git_pass(git_config_get_bool(&i, cfg, "this.That.other"));
47 48 49
	cl_assert(i == 1);

	/* This one doesn't exist */
50
	cl_must_fail(git_config_get_bool(&i, cfg, "this.thaT.other"));
51 52 53 54 55 56 57 58 59 60 61 62 63 64

	git_config_free(cfg);
}

/*
 * If \ is the last non-space character on the line, we read the next
 * one, separating each line with SP.
 */
void test_config_read__multiline_value(void)
{
	git_config *cfg;

	cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config2")));

65
	cl_git_pass(git_config_get_string_buf(&buf, cfg, "this.That.and"));
66
	cl_assert_equal_s("one one one two two three three", buf.ptr);
67 68 69 70

	git_config_free(cfg);
}

71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
static void clean_test_config(void *unused)
{
	GIT_UNUSED(unused);
	cl_fixture_cleanup("./testconfig");
}

void test_config_read__multiline_value_and_eof(void)
{
	git_config *cfg;

	cl_set_cleanup(&clean_test_config, NULL);
	cl_git_mkfile("./testconfig", "[header]\n  key1 = foo\\\n");
	cl_git_pass(git_config_open_ondisk(&cfg, "./testconfig"));

	cl_git_pass(git_config_get_string_buf(&buf, cfg, "header.key1"));
86
	cl_assert_equal_s("foo", buf.ptr);
87 88 89 90 91 92 93 94 95 96 97 98 99

	git_config_free(cfg);
}

void test_config_read__multiline_eof(void)
{
	git_config *cfg;

	cl_set_cleanup(&clean_test_config, NULL);
	cl_git_mkfile("./testconfig", "[header]\n  key1 = \\\n");
	cl_git_pass(git_config_open_ondisk(&cfg, "./testconfig"));

	cl_git_pass(git_config_get_string_buf(&buf, cfg, "header.key1"));
100
	cl_assert_equal_s("", buf.ptr);
101 102 103 104

	git_config_free(cfg);
}

105 106 107 108 109 110 111 112 113
/*
 * This kind of subsection declaration is case-insensitive
 */
void test_config_read__subsection_header(void)
{
	git_config *cfg;

	cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config3")));

114
	cl_git_pass(git_config_get_string_buf(&buf, cfg, "section.subsection.var"));
115
	cl_assert_equal_s("hello", buf.ptr);
116 117

	/* The subsection is transformed to lower-case */
118
	cl_must_fail(git_config_get_string_buf(&buf, cfg, "section.subSectIon.var"));
119 120 121 122 123 124 125 126 127 128 129

	git_config_free(cfg);
}

void test_config_read__lone_variable(void)
{
	git_config *cfg;
	int i;

	cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config4")));

130 131
	cl_git_fail(git_config_get_int32(&i, cfg, "some.section.variable"));

132
	cl_git_pass(git_config_get_string_buf(&buf, cfg, "some.section.variable"));
133 134
	cl_assert_equal_s("", buf.ptr);
	git_buf_dispose(&buf);
135

136
	cl_git_pass(git_config_get_bool(&i, cfg, "some.section.variable"));
137 138
	cl_assert(i == 1);

139
	cl_git_pass(git_config_get_string_buf(&buf, cfg, "some.section.variableeq"));
140
	cl_assert_equal_s("", buf.ptr);
141 142 143 144

	cl_git_pass(git_config_get_bool(&i, cfg, "some.section.variableeq"));
	cl_assert(i == 0);

145 146 147 148 149 150 151 152 153 154
	git_config_free(cfg);
}

void test_config_read__number_suffixes(void)
{
	git_config *cfg;
	int64_t i;

	cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config5")));

155
	cl_git_pass(git_config_get_int64(&i, cfg, "number.simple"));
156 157
	cl_assert(i == 1);

158
	cl_git_pass(git_config_get_int64(&i, cfg, "number.k"));
159 160
	cl_assert(i == 1 * 1024);

161
	cl_git_pass(git_config_get_int64(&i, cfg, "number.kk"));
162 163
	cl_assert(i == 1 * 1024);

164
	cl_git_pass(git_config_get_int64(&i, cfg, "number.m"));
165 166
	cl_assert(i == 1 * 1024 * 1024);

167
	cl_git_pass(git_config_get_int64(&i, cfg, "number.mm"));
168 169
	cl_assert(i == 1 * 1024 * 1024);

170
	cl_git_pass(git_config_get_int64(&i, cfg, "number.g"));
171 172
	cl_assert(i == 1 * 1024 * 1024 * 1024);

173
	cl_git_pass(git_config_get_int64(&i, cfg, "number.gg"));
174 175 176 177 178 179 180 181 182 183 184 185
	cl_assert(i == 1 * 1024 * 1024 * 1024);

	git_config_free(cfg);
}

void test_config_read__blank_lines(void)
{
	git_config *cfg;
	int i;

	cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config6")));

186
	cl_git_pass(git_config_get_bool(&i, cfg, "valid.subsection.something"));
187 188
	cl_assert(i == 1);

189
	cl_git_pass(git_config_get_bool(&i, cfg, "something.else.something"));
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
	cl_assert(i == 0);

	git_config_free(cfg);
}

void test_config_read__invalid_ext_headers(void)
{
	git_config *cfg;
	cl_must_fail(git_config_open_ondisk(&cfg, cl_fixture("config/config7")));
}

void test_config_read__empty_files(void)
{
	git_config *cfg;
	cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config8")));
	git_config_free(cfg);
}

208 209 210 211
void test_config_read__symbol_headers(void)
{
	git_config *cfg;
	cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config20")));
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
	cl_git_pass(git_config_get_string_buf(&buf, cfg, "valid.[subsection].something"));
	cl_assert_equal_s("a", buf.ptr);
	git_buf_dispose(&buf);
	cl_git_pass(git_config_get_string_buf(&buf, cfg, "sec.[subsec]/child.parent"));
	cl_assert_equal_s("grand", buf.ptr);
	git_buf_dispose(&buf);
	cl_git_pass(git_config_get_string_buf(&buf, cfg, "sec2.[subsec2]/child2.type"));
	cl_assert_equal_s("dvcs", buf.ptr);
	git_buf_dispose(&buf);
	cl_git_pass(git_config_get_string_buf(&buf, cfg, "sec3.escape\"quote.vcs"));
	cl_assert_equal_s("git", buf.ptr);
	git_buf_dispose(&buf);
	cl_git_pass(git_config_get_string_buf(&buf, cfg, "sec4.escaping\\slash.lib"));
	cl_assert_equal_s("git2", buf.ptr);
	git_buf_dispose(&buf);
227 228 229
	git_config_free(cfg);
}

Basile Henry committed
230 231 232 233 234 235 236
void test_config_read__multiline_multiple_quoted_comment_chars(void)
{
	git_config *cfg;
	cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config21")));
	git_config_free(cfg);
}

237 238 239 240 241 242 243 244 245 246 247 248 249
void test_config_read__multiline_multiple_quoted_quote_at_beginning_of_line(void)
{
	git_config* cfg;
	cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config22")));
	cl_git_pass(git_config_get_string_buf(&buf, cfg, "alias.m"));
	cl_assert_equal_s("cmd ;; ;; bar", buf.ptr);
	git_buf_dispose(&buf);
	cl_git_pass(git_config_get_string_buf(&buf, cfg, "alias.m2"));
	cl_assert_equal_s("'; ; something '", buf.ptr);
	git_buf_dispose(&buf);
	git_config_free(cfg);
}

250 251 252 253 254 255 256 257 258 259 260 261 262
void test_config_read__header_in_last_line(void)
{
	git_config *cfg;

	cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config10")));
	git_config_free(cfg);
}

void test_config_read__prefixes(void)
{
	git_config *cfg;

	cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config9")));
263
	cl_git_pass(git_config_get_string_buf(&buf, cfg, "remote.ab.url"));
264 265
	cl_assert_equal_s("http://example.com/git/ab", buf.ptr);
	git_buf_dispose(&buf);
266

267
	cl_git_pass(git_config_get_string_buf(&buf, cfg, "remote.abba.url"));
268
	cl_assert_equal_s("http://example.com/git/abba", buf.ptr);
269 270 271 272

	git_config_free(cfg);
}

273 274 275 276 277
void test_config_read__escaping_quotes(void)
{
	git_config *cfg;

	cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config13")));
278
	cl_git_pass(git_config_get_string_buf(&buf, cfg, "core.editor"));
279
	cl_assert_equal_s("\"C:/Program Files/Nonsense/bah.exe\" \"--some option\"", buf.ptr);
280 281 282 283

	git_config_free(cfg);
}

284 285 286 287 288 289 290 291 292 293 294
void test_config_read__invalid_escape_sequence(void)
{
	git_config *cfg;

	cl_set_cleanup(&clean_test_config, NULL);
	cl_git_mkfile("./testconfig", "[header]\n  key1 = \\\\\\;\n  key2 = value2\n");
	cl_git_fail(git_config_open_ondisk(&cfg, "./testconfig"));

	git_config_free(cfg);
}

295 296
static int count_cfg_entries_and_compare_levels(
	const git_config_entry *entry, void *payload)
297 298
{
	int *count = payload;
299 300 301 302 303 304

	if (!strcmp(entry->value, "7") || !strcmp(entry->value, "17"))
		cl_assert(entry->level == GIT_CONFIG_LEVEL_GLOBAL);
	else
		cl_assert(entry->level == GIT_CONFIG_LEVEL_SYSTEM);

305 306 307 308
	(*count)++;
	return 0;
}

309
static int cfg_callback_countdown(const git_config_entry *entry, void *payload)
310 311
{
	int *count = payload;
312
	GIT_UNUSED(entry);
313 314 315 316 317 318 319 320 321 322 323
	(*count)--;
	if (*count == 0)
		return -100;
	return 0;
}

void test_config_read__foreach(void)
{
	git_config *cfg;
	int count, ret;

324 325
	cl_git_pass(git_config_new(&cfg));
	cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config9"),
326
		GIT_CONFIG_LEVEL_SYSTEM, NULL, 0));
327
	cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config15"),
328
		GIT_CONFIG_LEVEL_GLOBAL, NULL, 0));
329 330

	count = 0;
331 332
	cl_git_pass(git_config_foreach(cfg, count_cfg_entries_and_compare_levels, &count));
	cl_assert_equal_i(7, count);
333 334 335

	count = 3;
	cl_git_fail(ret = git_config_foreach(cfg, cfg_callback_countdown, &count));
336
	cl_assert_equal_i(-100, ret);
337 338 339 340

	git_config_free(cfg);
}

341 342
void test_config_read__iterator(void)
{
343 344 345 346 347 348 349 350 351
	const char *keys[] = {
		"core.dummy2",
		"core.verylong",
		"core.dummy",
		"remote.ab.url",
		"remote.abba.url",
		"core.dummy2",
		"core.global"
	};
352 353 354 355 356 357 358
	git_config *cfg;
	git_config_iterator *iter;
	git_config_entry *entry;
	int count, ret;

	cl_git_pass(git_config_new(&cfg));
	cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config9"),
359
		GIT_CONFIG_LEVEL_SYSTEM, NULL, 0));
360
	cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config15"),
361
		GIT_CONFIG_LEVEL_GLOBAL, NULL, 0));
362 363 364 365 366

	count = 0;
	cl_git_pass(git_config_iterator_new(&iter, cfg));

	while ((ret = git_config_next(&entry, iter)) == 0) {
367
		cl_assert_equal_s(entry->name, keys[count]);
368 369 370
		count++;
	}

371
	git_config_iterator_free(iter);
372 373 374 375 376 377 378 379 380 381
	cl_assert_equal_i(GIT_ITEROVER, ret);
	cl_assert_equal_i(7, count);

	count = 3;
	cl_git_pass(git_config_iterator_new(&iter, cfg));

	git_config_iterator_free(iter);
	git_config_free(cfg);
}

382 383 384 385 386 387 388 389
static int count_cfg_entries(const git_config_entry *entry, void *payload)
{
	int *count = payload;
	GIT_UNUSED(entry);
	(*count)++;
	return 0;
}

390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
void test_config_read__foreach_match(void)
{
	git_config *cfg;
	int count;

	cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config9")));

	count = 0;
	cl_git_pass(
		git_config_foreach_match(cfg, "core.*", count_cfg_entries, &count));
	cl_assert_equal_i(3, count);

	count = 0;
	cl_git_pass(
		git_config_foreach_match(cfg, "remote\\.ab.*", count_cfg_entries, &count));
	cl_assert_equal_i(2, count);

	count = 0;
	cl_git_pass(
		git_config_foreach_match(cfg, ".*url$", count_cfg_entries, &count));
	cl_assert_equal_i(2, count);

	count = 0;
	cl_git_pass(
		git_config_foreach_match(cfg, ".*dummy.*", count_cfg_entries, &count));
	cl_assert_equal_i(2, count);

	count = 0;
	cl_git_pass(
		git_config_foreach_match(cfg, ".*nomatch.*", count_cfg_entries, &count));
	cl_assert_equal_i(0, count);

	git_config_free(cfg);
}

425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
static void check_glob_iter(git_config *cfg, const char *regexp, int expected)
{
	git_config_iterator *iter;
	git_config_entry *entry;
	int count, error;

	cl_git_pass(git_config_iterator_glob_new(&iter, cfg, regexp));

	count = 0;
	while ((error = git_config_next(&entry, iter)) == 0)
		count++;

	cl_assert_equal_i(GIT_ITEROVER, error);
	cl_assert_equal_i(expected, count);
	git_config_iterator_free(iter);
}

442 443 444 445 446 447 448 449 450 451 452 453
void test_config_read__iterator_invalid_glob(void)
{
	git_config *cfg;
	git_config_iterator *iter;

	cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config9")));

	cl_git_fail(git_config_iterator_glob_new(&iter, cfg, "*"));

	git_config_free(cfg);
}

454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
void test_config_read__iterator_glob(void)
{
	git_config *cfg;

	cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config9")));

	check_glob_iter(cfg, "core.*", 3);
	check_glob_iter(cfg, "remote\\.ab.*", 2);
	check_glob_iter(cfg, ".*url$", 2);
	check_glob_iter(cfg, ".*dummy.*", 2);
	check_glob_iter(cfg, ".*nomatch.*", 0);

	git_config_free(cfg);
}

469 470 471 472 473 474
void test_config_read__whitespace_not_required_around_assignment(void)
{
	git_config *cfg;

	cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config14")));

475
	cl_git_pass(git_config_get_string_buf(&buf, cfg, "a.b"));
476 477
	cl_assert_equal_s("c", buf.ptr);
	git_buf_dispose(&buf);
478

479
	cl_git_pass(git_config_get_string_buf(&buf, cfg, "d.e"));
480
	cl_assert_equal_s("f", buf.ptr);
481 482 483 484

	git_config_free(cfg);
}

485 486 487
void test_config_read__read_git_config_entry(void)
{
	git_config *cfg;
488
	git_config_entry *entry;
489

490 491
	cl_git_pass(git_config_new(&cfg));
	cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config9"),
492
		GIT_CONFIG_LEVEL_SYSTEM, NULL, 0));
493

494
	cl_git_pass(git_config_get_entry(&entry, cfg, "core.dummy2"));
495 496 497 498
	cl_assert_equal_s("core.dummy2", entry->name);
	cl_assert_equal_s("42", entry->value);
	cl_assert_equal_i(GIT_CONFIG_LEVEL_SYSTEM, entry->level);

499
	git_config_entry_free(entry);
500 501 502 503 504 505 506 507 508 509 510
	git_config_free(cfg);
}

/*
 * At the beginning of the test:
 *  - config9 has: core.dummy2=42
 *  - config15 has: core.dummy2=7
 *  - config16 has: core.dummy2=28
 */
void test_config_read__local_config_overrides_global_config_overrides_system_config(void)
{
511
	git_config *cfg;
512 513 514 515
	int32_t i;

	cl_git_pass(git_config_new(&cfg));
	cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config9"),
516
		GIT_CONFIG_LEVEL_SYSTEM, NULL, 0));
517
	cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config15"),
518
		GIT_CONFIG_LEVEL_GLOBAL, NULL, 0));
519
	cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config16"),
520
		GIT_CONFIG_LEVEL_LOCAL, NULL, 0));
521 522 523 524 525 526 527 528

	cl_git_pass(git_config_get_int32(&i, cfg, "core.dummy2"));
	cl_assert_equal_i(28, i);

	git_config_free(cfg);

	cl_git_pass(git_config_new(&cfg));
	cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config9"),
529
		GIT_CONFIG_LEVEL_SYSTEM, NULL, 0));
530
	cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config15"),
531
		GIT_CONFIG_LEVEL_GLOBAL, NULL, 0));
532 533 534

	cl_git_pass(git_config_get_int32(&i, cfg, "core.dummy2"));
	cl_assert_equal_i(7, i);
535 536

	git_config_free(cfg);
537
}
538

539 540 541 542 543 544 545 546 547 548 549 550 551
/*
 * At the beginning of the test:
 *  - config9 has: core.global does not exist
 *  - config15 has: core.global=17
 *  - config16 has: core.global=29
 *
 * And also:
 *  - config9 has: core.system does not exist
 *  - config15 has: core.system does not exist
 *  - config16 has: core.system=11
 */
void test_config_read__fallback_from_local_to_global_and_from_global_to_system(void)
{
552
	git_config *cfg;
553
	int32_t i;
554

555 556
	cl_git_pass(git_config_new(&cfg));
	cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config9"),
557
		GIT_CONFIG_LEVEL_SYSTEM, NULL, 0));
558
	cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config15"),
559
		GIT_CONFIG_LEVEL_GLOBAL, NULL, 0));
560
	cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config16"),
561
		GIT_CONFIG_LEVEL_LOCAL, NULL, 0));
562 563 564 565 566 567 568 569 570

	cl_git_pass(git_config_get_int32(&i, cfg, "core.global"));
	cl_assert_equal_i(17, i);
	cl_git_pass(git_config_get_int32(&i, cfg, "core.system"));
	cl_assert_equal_i(11, i);

	git_config_free(cfg);
}

571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590
void test_config_read__parent_dir_is_file(void)
{
	git_config *cfg;
	int count;

	cl_git_pass(git_config_new(&cfg));
	/*
	 * Verify we can add non-existing files when the parent directory is not
	 * a directory.
	 */
	cl_git_pass(git_config_add_file_ondisk(cfg, "/dev/null/.gitconfig",
		GIT_CONFIG_LEVEL_SYSTEM, NULL, 0));

	count = 0;
	cl_git_pass(git_config_foreach(cfg, count_cfg_entries_and_compare_levels, &count));
	cl_assert_equal_i(0, count);

	git_config_free(cfg);
}

591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612
/*
 * At the beginning of the test, config18 has:
 *	int32global = 28
 *	int64global = 9223372036854775803
 *	boolglobal = true
 *	stringglobal = I'm a global config value!
 *
 * And config19 has:
 *	int32global = -1
 *	int64global = -2
 *	boolglobal = false
 *	stringglobal = don't find me!
 *
 */
void test_config_read__simple_read_from_specific_level(void)
{
	git_config *cfg, *cfg_specific;
	int i;
	int64_t l, expected = +9223372036854775803;

	cl_git_pass(git_config_new(&cfg));
	cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config18"),
613
		GIT_CONFIG_LEVEL_GLOBAL, NULL, 0));
614
	cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config19"),
615
		GIT_CONFIG_LEVEL_SYSTEM, NULL, 0));
616 617 618 619 620 621 622 623 624

	cl_git_pass(git_config_open_level(&cfg_specific, cfg, GIT_CONFIG_LEVEL_GLOBAL));

	cl_git_pass(git_config_get_int32(&i, cfg_specific, "core.int32global"));
	cl_assert_equal_i(28, i);
	cl_git_pass(git_config_get_int64(&l, cfg_specific, "core.int64global"));
	cl_assert(l == expected);
	cl_git_pass(git_config_get_bool(&i, cfg_specific, "core.boolglobal"));
	cl_assert_equal_b(true, i);
625
	cl_git_pass(git_config_get_string_buf(&buf, cfg_specific, "core.stringglobal"));
626
	cl_assert_equal_s("I'm a global config value!", buf.ptr);
627 628

	git_config_free(cfg_specific);
629
	git_config_free(cfg);
630
}
631 632 633 634 635 636

void test_config_read__can_load_and_parse_an_empty_config_file(void)
{
	git_config *cfg;
	int i;

637 638 639
	cl_set_cleanup(&clean_test_config, NULL);
	cl_git_mkfile("./testconfig", "");
	cl_git_pass(git_config_open_ondisk(&cfg, "./testconfig"));
640 641 642 643
	cl_assert_equal_i(GIT_ENOTFOUND, git_config_get_int32(&i, cfg, "nope.neither"));

	git_config_free(cfg);
}
644 645 646 647 648 649 650 651 652 653 654

void test_config_read__corrupt_header(void)
{
	git_config *cfg;

	cl_set_cleanup(&clean_test_config, NULL);
	cl_git_mkfile("./testconfig", "[sneaky ] \"quoted closing quote mark\\\"");
	cl_git_fail(git_config_open_ondisk(&cfg, "./testconfig"));

	git_config_free(cfg);
}
655

656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675
void test_config_read__corrupt_header2(void)
{
	git_config *cfg;

	cl_set_cleanup(&clean_test_config, NULL);
	cl_git_mkfile("./testconfig", "[unclosed \"bracket\"\n    lib = git2\n");
	cl_git_fail(git_config_open_ondisk(&cfg, "./testconfig"));

	git_config_free(cfg);
}

void test_config_read__corrupt_header3(void)
{
	git_config *cfg;

	cl_set_cleanup(&clean_test_config, NULL);
	cl_git_mkfile("./testconfig", "[unclosed \"slash\\\"]\n    lib = git2\n");
	cl_git_fail(git_config_open_ondisk(&cfg, "./testconfig"));

	git_config_free(cfg);
676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710
}

void test_config_read__invalid_key_chars(void)
{
	git_config *cfg;

	cl_set_cleanup(&clean_test_config, NULL);
	cl_git_mkfile("./testconfig", "[foo]\n    has_underscore = git2\n");
	cl_git_fail(git_config_open_ondisk(&cfg, "./testconfig"));

	cl_git_rewritefile("./testconfig", "[foo]\n  has/slash = git2\n");
	cl_git_fail(git_config_open_ondisk(&cfg, "./testconfig"));

	cl_git_rewritefile("./testconfig", "[foo]\n  has+plus = git2\n");
	cl_git_fail(git_config_open_ondisk(&cfg, "./testconfig"));

	cl_git_rewritefile("./testconfig", "[no_key]\n  = git2\n");
	cl_git_fail(git_config_open_ondisk(&cfg, "./testconfig"));

	git_config_free(cfg);
}

void test_config_read__lone_variable_with_trailing_whitespace(void)
{
	git_config *cfg;
	int b;

	cl_set_cleanup(&clean_test_config, NULL);
	cl_git_mkfile("./testconfig", "[foo]\n    lonevariable   \n");
	cl_git_pass(git_config_open_ondisk(&cfg, "./testconfig"));

	cl_git_pass(git_config_get_bool(&b, cfg, "foo.lonevariable"));
	cl_assert_equal_b(true, b);

	git_config_free(cfg);
711 712
}

713 714 715 716 717 718 719 720
void test_config_read__override_variable(void)
{
	git_config *cfg;

	cl_set_cleanup(&clean_test_config, NULL);
	cl_git_mkfile("./testconfig", "[some] var = one\nvar = two");
	cl_git_pass(git_config_open_ondisk(&cfg, "./testconfig"));

721
	cl_git_pass(git_config_get_string_buf(&buf, cfg, "some.var"));
722
	cl_assert_equal_s("two", buf.ptr);
723 724 725

	git_config_free(cfg);
}
726 727 728 729 730

void test_config_read__path(void)
{
	git_config *cfg;
	git_buf path = GIT_BUF_INIT;
731 732
	git_str home_path = GIT_STR_INIT;
	git_str expected_path = GIT_STR_INIT;
733

734 735
	cl_fake_homedir(&home_path);

736
	cl_git_mkfile("./testconfig", "[some]\n path = ~/somefile");
737
	cl_git_pass(git_fs_path_join_unrooted(&expected_path, "somefile", home_path.ptr, NULL));
738 739 740 741

	cl_git_pass(git_config_open_ondisk(&cfg, "./testconfig"));
	cl_git_pass(git_config_get_path(&path, cfg, "some.path"));
	cl_assert_equal_s(expected_path.ptr, path.ptr);
742
	git_buf_dispose(&path);
743 744

	cl_git_mkfile("./testconfig", "[some]\n path = ~/");
745
	cl_git_pass(git_fs_path_join_unrooted(&expected_path, "", home_path.ptr, NULL));
746 747 748

	cl_git_pass(git_config_get_path(&path, cfg, "some.path"));
	cl_assert_equal_s(expected_path.ptr, path.ptr);
749
	git_buf_dispose(&path);
750 751

	cl_git_mkfile("./testconfig", "[some]\n path = ~");
752
	cl_git_pass(git_str_sets(&expected_path, home_path.ptr));
753 754 755

	cl_git_pass(git_config_get_path(&path, cfg, "some.path"));
	cl_assert_equal_s(expected_path.ptr, path.ptr);
756
	git_buf_dispose(&path);
757 758 759 760

	cl_git_mkfile("./testconfig", "[some]\n path = ~user/foo");
	cl_git_fail(git_config_get_path(&path, cfg, "some.path"));

761 762
	git_str_dispose(&home_path);
	git_str_dispose(&expected_path);
763 764
	git_config_free(cfg);
}
765 766 767 768 769 770 771 772 773 774 775 776 777

void test_config_read__crlf_style_line_endings(void)
{
	git_buf buf = GIT_BUF_INIT;
	git_config *cfg;

	cl_set_cleanup(&clean_test_config, NULL);
	cl_git_mkfile("./testconfig", "[some]\r\n var = value\r\n");
	cl_git_pass(git_config_open_ondisk(&cfg, "./testconfig"));
	cl_git_pass(git_config_get_string_buf(&buf, cfg, "some.var"));
	cl_assert_equal_s(buf.ptr, "value");

	git_config_free(cfg);
778
	git_buf_dispose(&buf);
779 780 781 782 783 784 785 786 787 788 789 790 791 792
}

void test_config_read__trailing_crlf(void)
{
	git_buf buf = GIT_BUF_INIT;
	git_config *cfg;

	cl_set_cleanup(&clean_test_config, NULL);
	cl_git_mkfile("./testconfig", "[some]\r\n var = value\r\n\r\n");
	cl_git_pass(git_config_open_ondisk(&cfg, "./testconfig"));
	cl_git_pass(git_config_get_string_buf(&buf, cfg, "some.var"));
	cl_assert_equal_s(buf.ptr, "value");

	git_config_free(cfg);
793
	git_buf_dispose(&buf);
794
}
795 796 797 798 799 800 801 802 803 804 805 806 807

void test_config_read__bom(void)
{
	git_buf buf = GIT_BUF_INIT;
	git_config *cfg;

	cl_set_cleanup(&clean_test_config, NULL);
	cl_git_mkfile("./testconfig", "\xEF\xBB\xBF[some]\n var = value\n");
	cl_git_pass(git_config_open_ondisk(&cfg, "./testconfig"));
	cl_git_pass(git_config_get_string_buf(&buf, cfg, "some.var"));
	cl_assert_equal_s(buf.ptr, "value");

	git_config_free(cfg);
808
	git_buf_dispose(&buf);
809
}
Nelson Elhage committed
810

811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880
void test_config_read__arbitrary_whitespace_before_subsection(void)
{
	git_buf buf = GIT_BUF_INIT;
	git_config *cfg;

	cl_set_cleanup(&clean_test_config, NULL);
	cl_git_mkfile("./testconfig", "[some \t \"subsection\"]\n var = value\n");
	cl_git_pass(git_config_open_ondisk(&cfg, "./testconfig"));
	cl_git_pass(git_config_get_string_buf(&buf, cfg, "some.subsection.var"));
	cl_assert_equal_s(buf.ptr, "value");

	git_config_free(cfg);
	git_buf_dispose(&buf);
}

void test_config_read__no_whitespace_after_subsection(void)
{
	git_config *cfg;

	cl_set_cleanup(&clean_test_config, NULL);
	cl_git_mkfile("./testconfig", "[some \"subsection\" ]\n var = value\n");
	cl_git_fail(git_config_open_ondisk(&cfg, "./testconfig"));

	git_config_free(cfg);
}

void test_config_read__invalid_space_section(void)
{
	git_config *cfg;

	cl_set_cleanup(&clean_test_config, NULL);
	cl_git_mkfile("./testconfig", "\xEF\xBB\xBF[some section]\n var = value\n");
	cl_git_fail(git_config_open_ondisk(&cfg, "./testconfig"));

	git_config_free(cfg);
}

void test_config_read__invalid_quoted_first_section(void)
{
	git_config *cfg;

	cl_set_cleanup(&clean_test_config, NULL);
	cl_git_mkfile("./testconfig", "\xEF\xBB\xBF[\"some\"]\n var = value\n");
	cl_git_fail(git_config_open_ondisk(&cfg, "./testconfig"));

	git_config_free(cfg);
}

void test_config_read__invalid_unquoted_subsection(void)
{
	git_config *cfg;

	cl_set_cleanup(&clean_test_config, NULL);
	cl_git_mkfile("./testconfig", "\xEF\xBB\xBF[some sub section]\n var = value\n");
	cl_git_fail(git_config_open_ondisk(&cfg, "./testconfig"));

	git_config_free(cfg);
}

void test_config_read__invalid_quoted_third_section(void)
{
	git_config *cfg;

	cl_set_cleanup(&clean_test_config, NULL);
	cl_git_mkfile("./testconfig", "\xEF\xBB\xBF[some sub \"section\"]\n var = value\n");
	cl_git_fail(git_config_open_ondisk(&cfg, "./testconfig"));

	git_config_free(cfg);
}

881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897
void test_config_read__unreadable_file_ignored(void)
{
	git_buf buf = GIT_BUF_INIT;
	git_config *cfg;
	int ret;

	cl_set_cleanup(&clean_test_config, NULL);
	cl_git_mkfile("./testconfig", "[some] var = value\n[some \"OtheR\"] var = value");
	cl_git_pass(p_chmod("./testconfig", 0));

	ret = git_config_open_ondisk(&cfg, "./test/config");
	cl_assert(ret == 0 || ret == GIT_ENOTFOUND);

	git_config_free(cfg);
	git_buf_dispose(&buf);
}

898 899 900 901 902 903 904 905 906 907 908
void test_config_read__single_line(void)
{
	git_buf buf = GIT_BUF_INIT;
	git_config *cfg;

	cl_set_cleanup(&clean_test_config, NULL);
	cl_git_mkfile("./testconfig", "[some] var = value\n[some \"OtheR\"] var = value");
	cl_git_pass(git_config_open_ondisk(&cfg, "./testconfig"));
	cl_git_pass(git_config_get_string_buf(&buf, cfg, "some.var"));
	cl_assert_equal_s(buf.ptr, "value");

909
	git_buf_dispose(&buf);
910 911 912 913 914 915
	cl_git_pass(git_config_get_string_buf(&buf, cfg, "some.OtheR.var"));
	cl_assert_equal_s(buf.ptr, "value");

	git_config_free(cfg);
	cl_git_mkfile("./testconfig", "[some] var = value\n[some \"OtheR\"]var = value");
	cl_git_pass(git_config_open_ondisk(&cfg, "./testconfig"));
916
	git_buf_dispose(&buf);
917 918 919
	cl_git_pass(git_config_get_string_buf(&buf, cfg, "some.var"));
	cl_assert_equal_s(buf.ptr, "value");

920
	git_buf_dispose(&buf);
921 922 923 924 925 926 927
	cl_git_pass(git_config_get_string_buf(&buf, cfg, "some.OtheR.var"));
	cl_assert_equal_s(buf.ptr, "value");

	git_config_free(cfg);
	git_buf_dispose(&buf);
}

928 929 930 931 932 933 934 935
static int read_nosection_cb(const git_config_entry *entry, void *payload) {
	int *seen = (int*)payload;
	if (strcmp(entry->name, "key") == 0) {
		(*seen)++;
	}
	return 0;
}

Nelson Elhage committed
936 937 938 939 940
/* This would ideally issue a warning, if we had a way to do so. */
void test_config_read__nosection(void)
{
	git_config *cfg;
	git_buf buf = GIT_BUF_INIT;
941
	int seen = 0;
Nelson Elhage committed
942 943 944

	cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config-nosection")));

945 946 947 948 949 950 951 952 953 954 955
	/*
	 * Given a key with no section, we do not allow reading it,
	 * but we do include it in an iteration over the config
	 * store. This appears to match how git's own APIs (and
	 * git-config(1)) behave.
	 */

	cl_git_fail_with(git_config_get_string_buf(&buf, cfg, "key"), GIT_EINVALIDSPEC);

	cl_git_pass(git_config_foreach(cfg, read_nosection_cb, &seen));
	cl_assert_equal_i(seen, 1);
Nelson Elhage committed
956 957 958 959

	git_buf_dispose(&buf);
	git_config_free(cfg);
}
960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983

enum {
	MAP_TRUE = 0,
	MAP_FALSE = 1,
	MAP_ALWAYS = 2
};

static git_configmap _test_map1[] = {
	{GIT_CONFIGMAP_STRING, "always", MAP_ALWAYS},
	{GIT_CONFIGMAP_FALSE, NULL, MAP_FALSE},
	{GIT_CONFIGMAP_TRUE, NULL, MAP_TRUE},
};

static git_configmap _test_map2[] = {
	{GIT_CONFIGMAP_INT32, NULL, 0},
};

void test_config_read__get_mapped(void)
{
	git_config *cfg;
	int val;
	int known_good;

	cl_set_cleanup(&clean_test_config, NULL);
Sven Strickroth committed
984 985 986 987 988 989 990 991 992 993
	cl_git_mkfile("./testconfig", "[header]\n"
								  "  key1 = 1\n"
								  "  key2 = true\n"
								  "  key3\n"
								  "  key4 = always\n"
								  "  key5 = false\n"
								  "  key6 = 0\n"
								  "  key7 = never\n"
								  "  key8 = On\n"
								  "  key9 = off\n");
994 995
	cl_git_pass(git_config_open_ondisk(&cfg, "./testconfig"));

996
	/* check parsing bool and string */
997 998 999 1000
	cl_git_pass(git_config_get_mapped(&val, cfg, "header.key1", _test_map1, ARRAY_SIZE(_test_map1)));
	cl_assert_equal_i(val, MAP_TRUE);
	cl_git_pass(git_config_get_mapped(&val, cfg, "header.key2", _test_map1, ARRAY_SIZE(_test_map1)));
	cl_assert_equal_i(val, MAP_TRUE);
1001 1002
	cl_git_pass(git_config_get_mapped(&val, cfg, "header.key3", _test_map1, ARRAY_SIZE(_test_map1)));
	cl_assert_equal_i(val, MAP_TRUE);
1003 1004
	cl_git_pass(git_config_get_mapped(&val, cfg, "header.key8", _test_map1, ARRAY_SIZE(_test_map1)));
	cl_assert_equal_i(val, MAP_TRUE);
1005 1006 1007 1008 1009 1010 1011 1012

	cl_git_pass(git_config_get_mapped(&val, cfg, "header.key4", _test_map1, ARRAY_SIZE(_test_map1)));
	cl_assert_equal_i(val, MAP_ALWAYS);

	cl_git_pass(git_config_get_mapped(&val, cfg, "header.key5", _test_map1, ARRAY_SIZE(_test_map1)));
	cl_assert_equal_i(val, MAP_FALSE);
	cl_git_pass(git_config_get_mapped(&val, cfg, "header.key6", _test_map1, ARRAY_SIZE(_test_map1)));
	cl_assert_equal_i(val, MAP_FALSE);
1013 1014
	cl_git_pass(git_config_get_mapped(&val, cfg, "header.key9", _test_map1, ARRAY_SIZE(_test_map1)));
	cl_assert_equal_i(val, MAP_FALSE);
1015 1016 1017

	cl_git_fail(git_config_get_mapped(&val, cfg, "header.key7", _test_map1, ARRAY_SIZE(_test_map1)));

1018
	/* check parsing int values */
1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
	cl_git_pass(git_config_get_mapped(&val, cfg, "header.key1", _test_map2, ARRAY_SIZE(_test_map2)));
	cl_git_pass(git_config_get_int32(&known_good, cfg, "header.key1"));
	cl_assert_equal_i(val, known_good);
	cl_git_pass(git_config_get_mapped(&val, cfg, "header.key6", _test_map2, ARRAY_SIZE(_test_map2)));
	cl_git_pass(git_config_get_int32(&known_good, cfg, "header.key6"));
	cl_assert_equal_i(val, known_good);

	cl_git_fail(git_config_get_mapped(&val, cfg, "header.key2", _test_map2, ARRAY_SIZE(_test_map2)));
	cl_git_fail(git_config_get_mapped(&val, cfg, "header.key3", _test_map2, ARRAY_SIZE(_test_map2)));
	cl_git_fail(git_config_get_mapped(&val, cfg, "header.key4", _test_map2, ARRAY_SIZE(_test_map2)));
	cl_git_fail(git_config_get_mapped(&val, cfg, "header.key5", _test_map2, ARRAY_SIZE(_test_map2)));
	cl_git_fail(git_config_get_mapped(&val, cfg, "header.key7", _test_map2, ARRAY_SIZE(_test_map2)));
1031 1032
	cl_git_fail(git_config_get_mapped(&val, cfg, "header.key8", _test_map2, ARRAY_SIZE(_test_map2)));
	cl_git_fail(git_config_get_mapped(&val, cfg, "header.key9", _test_map2, ARRAY_SIZE(_test_map2)));
1033 1034 1035

	git_config_free(cfg);
}