path.c 19.4 KB
Newer Older
1
#include "clar_libgit2.h"
2
#include "fileops.h"
Vicent Marti committed
3 4 5 6

static void
check_dirname(const char *A, const char *B)
{
7 8
	git_buf dir = GIT_BUF_INIT;
	char *dir2;
Vicent Marti committed
9

10
	cl_assert(git_path_dirname_r(&dir, A) >= 0);
11
	cl_assert_equal_s(B, dir.ptr);
12
	git_buf_free(&dir);
Vicent Marti committed
13

14
	cl_assert((dir2 = git_path_dirname(A)) != NULL);
15
	cl_assert_equal_s(B, dir2);
16
	git__free(dir2);
Vicent Marti committed
17 18 19 20 21
}

static void
check_basename(const char *A, const char *B)
{
22 23
	git_buf base = GIT_BUF_INIT;
	char *base2;
Vicent Marti committed
24

25
	cl_assert(git_path_basename_r(&base, A) >= 0);
26
	cl_assert_equal_s(B, base.ptr);
27
	git_buf_free(&base);
Vicent Marti committed
28

29
	cl_assert((base2 = git_path_basename(A)) != NULL);
30
	cl_assert_equal_s(B, base2);
31
	git__free(base2);
Vicent Marti committed
32 33 34 35 36 37 38 39
}

static void
check_topdir(const char *A, const char *B)
{
	const char *dir;

	cl_assert((dir = git_path_topdir(A)) != NULL);
40
	cl_assert_equal_s(B, dir);
Vicent Marti committed
41 42 43 44 45
}

static void
check_joinpath(const char *path_a, const char *path_b, const char *expected_path)
{
46
	git_buf joined_path = GIT_BUF_INIT;
Vicent Marti committed
47

48
	cl_git_pass(git_buf_joinpath(&joined_path, path_a, path_b));
49
	cl_assert_equal_s(expected_path, joined_path.ptr);
50 51

	git_buf_free(&joined_path);
Vicent Marti committed
52 53 54 55 56 57 58 59 60 61
}

static void
check_joinpath_n(
	const char *path_a,
	const char *path_b,
	const char *path_c,
	const char *path_d,
	const char *expected_path)
{
62 63 64 65
	git_buf joined_path = GIT_BUF_INIT;

	cl_git_pass(git_buf_join_n(&joined_path, '/', 4,
							   path_a, path_b, path_c, path_d));
66
	cl_assert_equal_s(expected_path, joined_path.ptr);
Vicent Marti committed
67

68
	git_buf_free(&joined_path);
Vicent Marti committed
69 70 71 72
}


/* get the dirname of a path */
nulltoken committed
73
void test_core_path__00_dirname(void)
Vicent Marti committed
74 75 76 77 78 79 80 81 82 83 84 85 86 87
{
	check_dirname(NULL, ".");
	check_dirname("", ".");
	check_dirname("a", ".");
	check_dirname("/", "/");
	check_dirname("/usr", "/");
	check_dirname("/usr/", "/");
	check_dirname("/usr/lib", "/usr");
	check_dirname("/usr/lib/", "/usr");
	check_dirname("/usr/lib//", "/usr");
	check_dirname("usr/lib", "usr");
	check_dirname("usr/lib/", "usr");
	check_dirname("usr/lib//", "usr");
	check_dirname(".git/", ".");
88 89

	check_dirname(REP16("/abc"), REP15("/abc"));
90 91

#ifdef GIT_WIN32
92
	check_dirname("C:/", "C:/");
93
	check_dirname("C:", "C:/");
94 95
	check_dirname("C:/path/", "C:/");
	check_dirname("C:/path", "C:/");
96
	check_dirname("//computername/", "//computername/");
97
	check_dirname("//computername", "//computername/");
98 99 100 101
	check_dirname("//computername/path/", "//computername/");
	check_dirname("//computername/path", "//computername/");
	check_dirname("//computername/sub/path/", "//computername/sub");
	check_dirname("//computername/sub/path", "//computername/sub");
102
#endif
Vicent Marti committed
103 104 105
}

/* get the base name of a path */
nulltoken committed
106
void test_core_path__01_basename(void)
Vicent Marti committed
107 108 109 110 111 112 113 114 115 116
{
	check_basename(NULL, ".");
	check_basename("", ".");
	check_basename("a", "a");
	check_basename("/", "/");
	check_basename("/usr", "usr");
	check_basename("/usr/", "usr");
	check_basename("/usr/lib", "lib");
	check_basename("/usr/lib//", "lib");
	check_basename("usr/lib", "lib");
117 118 119

	check_basename(REP16("/abc"), "abc");
	check_basename(REP1024("/abc"), "abc");
Vicent Marti committed
120 121 122
}

/* get the latest component in a path */
nulltoken committed
123
void test_core_path__02_topdir(void)
Vicent Marti committed
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
{
	check_topdir(".git/", ".git/");
	check_topdir("/.git/", ".git/");
	check_topdir("usr/local/.git/", ".git/");
	check_topdir("./.git/", ".git/");
	check_topdir("/usr/.git/", ".git/");
	check_topdir("/", "/");
	check_topdir("a/", "a/");

	cl_assert(git_path_topdir("/usr/.git") == NULL);
	cl_assert(git_path_topdir(".") == NULL);
	cl_assert(git_path_topdir("") == NULL);
	cl_assert(git_path_topdir("a") == NULL);
}

/* properly join path components */
nulltoken committed
140
void test_core_path__05_joins(void)
Vicent Marti committed
141 142 143 144 145 146 147 148 149 150 151 152 153
{
	check_joinpath("", "", "");
	check_joinpath("", "a", "a");
	check_joinpath("", "/a", "/a");
	check_joinpath("a", "", "a/");
	check_joinpath("a", "/", "a/");
	check_joinpath("a", "b", "a/b");
	check_joinpath("/", "a", "/a");
	check_joinpath("/", "", "/");
	check_joinpath("/a", "/b", "/a/b");
	check_joinpath("/a", "/b/", "/a/b/");
	check_joinpath("/a/", "b/", "/a/b/");
	check_joinpath("/a/", "/b/", "/a/b/");
154 155 156 157 158 159 160 161 162 163

	check_joinpath("/abcd", "/defg", "/abcd/defg");
	check_joinpath("/abcd", "/defg/", "/abcd/defg/");
	check_joinpath("/abcd/", "defg/", "/abcd/defg/");
	check_joinpath("/abcd/", "/defg/", "/abcd/defg/");

	check_joinpath("/abcdefgh", "/12345678", "/abcdefgh/12345678");
	check_joinpath("/abcdefgh", "/12345678/", "/abcdefgh/12345678/");
	check_joinpath("/abcdefgh/", "12345678/", "/abcdefgh/12345678/");

164 165 166 167
	check_joinpath(REP1024("aaaa"), "", REP1024("aaaa") "/");
	check_joinpath(REP1024("aaaa/"), "", REP1024("aaaa/"));
	check_joinpath(REP1024("/aaaa"), "", REP1024("/aaaa") "/");

168 169 170 171
	check_joinpath(REP1024("aaaa"), REP1024("bbbb"),
				   REP1024("aaaa") "/" REP1024("bbbb"));
	check_joinpath(REP1024("/aaaa"), REP1024("/bbbb"),
				   REP1024("/aaaa") REP1024("/bbbb"));
Vicent Marti committed
172 173 174
}

/* properly join path components for more than one path */
nulltoken committed
175
void test_core_path__06_long_joins(void)
Vicent Marti committed
176 177 178 179 180 181 182
{
	check_joinpath_n("", "", "", "", "");
	check_joinpath_n("", "a", "", "", "a/");
	check_joinpath_n("a", "", "", "", "a/");
	check_joinpath_n("", "", "", "a", "a");
	check_joinpath_n("a", "b", "", "/c/d/", "a/b/c/d/");
	check_joinpath_n("a", "b", "", "/c/d", "a/b/c/d");
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
	check_joinpath_n("abcd", "efgh", "ijkl", "mnop", "abcd/efgh/ijkl/mnop");
	check_joinpath_n("abcd/", "efgh/", "ijkl/", "mnop/", "abcd/efgh/ijkl/mnop/");
	check_joinpath_n("/abcd/", "/efgh/", "/ijkl/", "/mnop/", "/abcd/efgh/ijkl/mnop/");

	check_joinpath_n(REP1024("a"), REP1024("b"), REP1024("c"), REP1024("d"),
					 REP1024("a") "/" REP1024("b") "/"
					 REP1024("c") "/" REP1024("d"));
	check_joinpath_n(REP1024("/a"), REP1024("/b"), REP1024("/c"), REP1024("/d"),
					 REP1024("/a") REP1024("/b")
					 REP1024("/c") REP1024("/d"));
}


static void
check_path_to_dir(
	const char* path,
    const char* expected)
{
	git_buf tgt = GIT_BUF_INIT;

	git_buf_sets(&tgt, path);
	cl_git_pass(git_path_to_dir(&tgt));
205
	cl_assert_equal_s(expected, tgt.ptr);
206 207 208 209 210 211 212

	git_buf_free(&tgt);
}

static void
check_string_to_dir(
	const char* path,
213
	size_t      maxlen,
214 215
    const char* expected)
{
216
	size_t len = strlen(path);
217
	char *buf = git__malloc(len + 2);
218 219
	cl_assert(buf);

220 221 222 223
	strncpy(buf, path, len + 2);

	git_path_string_to_dir(buf, maxlen);

224
	cl_assert_equal_s(expected, buf);
225 226 227 228 229

	git__free(buf);
}

/* convert paths to dirs */
nulltoken committed
230
void test_core_path__07_path_to_dir(void)
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
{
	check_path_to_dir("", "");
	check_path_to_dir(".", "./");
	check_path_to_dir("./", "./");
	check_path_to_dir("a/", "a/");
	check_path_to_dir("ab", "ab/");
	/* make sure we try just under and just over an expansion that will
	 * require a realloc
	 */
	check_path_to_dir("abcdef", "abcdef/");
	check_path_to_dir("abcdefg", "abcdefg/");
	check_path_to_dir("abcdefgh", "abcdefgh/");
	check_path_to_dir("abcdefghi", "abcdefghi/");
	check_path_to_dir(REP1024("abcd") "/", REP1024("abcd") "/");
	check_path_to_dir(REP1024("abcd"), REP1024("abcd") "/");

	check_string_to_dir("", 1, "");
	check_string_to_dir(".", 1, ".");
	check_string_to_dir(".", 2, "./");
	check_string_to_dir(".", 3, "./");
	check_string_to_dir("abcd", 3, "abcd");
	check_string_to_dir("abcd", 4, "abcd");
	check_string_to_dir("abcd", 5, "abcd/");
	check_string_to_dir("abcd", 6, "abcd/");
Vicent Marti committed
255
}
256 257

/* join path to itself */
nulltoken committed
258
void test_core_path__08_self_join(void)
259 260
{
	git_buf path = GIT_BUF_INIT;
261
	size_t asize = 0;
262 263 264

	asize = path.asize;
	cl_git_pass(git_buf_sets(&path, "/foo"));
265
	cl_assert_equal_s(path.ptr, "/foo");
266 267 268 269
	cl_assert(asize < path.asize);

	asize = path.asize;
	cl_git_pass(git_buf_joinpath(&path, path.ptr, "this is a new string"));
270
	cl_assert_equal_s(path.ptr, "/foo/this is a new string");
271 272 273 274
	cl_assert(asize < path.asize);

	asize = path.asize;
	cl_git_pass(git_buf_joinpath(&path, path.ptr, "/grow the buffer, grow the buffer, grow the buffer"));
275
	cl_assert_equal_s(path.ptr, "/foo/this is a new string/grow the buffer, grow the buffer, grow the buffer");
276 277 278 279 280 281
	cl_assert(asize < path.asize);

	git_buf_free(&path);
	cl_git_pass(git_buf_sets(&path, "/foo/bar"));

	cl_git_pass(git_buf_joinpath(&path, path.ptr + 4, "baz"));
282
	cl_assert_equal_s(path.ptr, "/bar/baz");
283 284 285

	asize = path.asize;
	cl_git_pass(git_buf_joinpath(&path, path.ptr + 4, "somethinglongenoughtorealloc"));
286
	cl_assert_equal_s(path.ptr, "/baz/somethinglongenoughtorealloc");
287 288 289 290
	cl_assert(asize < path.asize);
	
	git_buf_free(&path);
}
291 292 293 294 295 296

static void check_percent_decoding(const char *expected_result, const char *input)
{
	git_buf buf = GIT_BUF_INIT;

	cl_git_pass(git__percent_decode(&buf, input));
297
	cl_assert_equal_s(expected_result, git_buf_cstr(&buf));
298 299 300 301

	git_buf_free(&buf);
}

nulltoken committed
302
void test_core_path__09_percent_decode(void)
303 304 305 306 307 308 309 310 311 312 313 314
{
	check_percent_decoding("abcd", "abcd");
	check_percent_decoding("a2%", "a2%");
	check_percent_decoding("a2%3", "a2%3");
	check_percent_decoding("a2%%3", "a2%%3");
	check_percent_decoding("a2%3z", "a2%3z");
	check_percent_decoding("a,", "a%2c");
	check_percent_decoding("a21", "a2%31");
	check_percent_decoding("a2%1", "a2%%31");
	check_percent_decoding("a bc ", "a%20bc%20");
	check_percent_decoding("Vicent Mart" "\355", "Vicent%20Mart%ED");
}
nulltoken committed
315 316 317 318 319 320 321 322 323

static void check_fromurl(const char *expected_result, const char *input, int should_fail)
{
	git_buf buf = GIT_BUF_INIT;

	assert(should_fail || expected_result);

	if (!should_fail) {
		cl_git_pass(git_path_fromurl(&buf, input));
324
		cl_assert_equal_s(expected_result, git_buf_cstr(&buf));
nulltoken committed
325 326 327 328 329 330
	} else
		cl_git_fail(git_path_fromurl(&buf, input));

	git_buf_free(&buf);
}

331
#ifdef GIT_WIN32
nulltoken committed
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
#define ABS_PATH_MARKER ""
#else
#define ABS_PATH_MARKER "/"
#endif

void test_core_path__10_fromurl(void)
{
	/* Failing cases */
	check_fromurl(NULL, "a", 1);
	check_fromurl(NULL, "http:///c:/Temp%20folder/note.txt", 1);
	check_fromurl(NULL, "file://c:/Temp%20folder/note.txt", 1);
	check_fromurl(NULL, "file:////c:/Temp%20folder/note.txt", 1);
	check_fromurl(NULL, "file:///", 1);
	check_fromurl(NULL, "file:////", 1);
	check_fromurl(NULL, "file://servername/c:/Temp%20folder/note.txt", 1);

	/* Passing cases */
	check_fromurl(ABS_PATH_MARKER "c:/Temp folder/note.txt", "file:///c:/Temp%20folder/note.txt", 0);
	check_fromurl(ABS_PATH_MARKER "c:/Temp folder/note.txt", "file://localhost/c:/Temp%20folder/note.txt", 0);
	check_fromurl(ABS_PATH_MARKER "c:/Temp+folder/note.txt", "file:///c:/Temp+folder/note.txt", 0);
	check_fromurl(ABS_PATH_MARKER "a", "file:///a", 0);
}
354

355 356
typedef struct {
	int expect_idx;
357
	int cancel_after;
358 359 360
	char **expect;
} check_walkup_info;

361 362
#define CANCEL_VALUE 1234

363
static int check_one_walkup_step(void *ref, const char *path)
364 365
{
	check_walkup_info *info = (check_walkup_info *)ref;
366 367 368

	if (!info->cancel_after) {
		cl_assert_equal_s(info->expect[info->expect_idx], "[CANCEL]");
369
		return CANCEL_VALUE;
370 371 372
	}
	info->cancel_after--;

373
	cl_assert(info->expect[info->expect_idx] != NULL);
374
	cl_assert_equal_s(info->expect[info->expect_idx], path);
375
	info->expect_idx++;
376

377
	return 0;
378 379
}

380 381
void test_core_path__11_walkup(void)
{
382
	git_buf p = GIT_BUF_INIT;
383

384
	char *expect[] = {
385 386 387 388 389 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
		/*  1 */ "/a/b/c/d/e/", "/a/b/c/d/", "/a/b/c/", "/a/b/", "/a/", "/", NULL,
		/*  2 */ "/a/b/c/d/e", "/a/b/c/d/", "/a/b/c/", "/a/b/", "/a/", "/", NULL,
		/*  3 */ "/a/b/c/d/e", "/a/b/c/d/", "/a/b/c/", "/a/b/", "/a/", "/", NULL,
		/*  4 */ "/a/b/c/d/e", "/a/b/c/d/", "/a/b/c/", "/a/b/", "/a/", "/", NULL,
		/*  5 */ "/a/b/c/d/e", "/a/b/c/d/", "/a/b/c/", "/a/b/", NULL,
		/*  6 */ "/a/b/c/d/e", "/a/b/c/d/", "/a/b/c/", "/a/b/", NULL,
		/*  7 */ "this_is_a_path", "", NULL,
		/*  8 */ "this_is_a_path/", "", NULL,
		/*  9 */ "///a///b///c///d///e///", "///a///b///c///d///", "///a///b///c///", "///a///b///", "///a///", "///", NULL,
		/* 10 */ "a/b/c/", "a/b/", "a/", "", NULL,
		/* 11 */ "a/b/c", "a/b/", "a/", "", NULL,
		/* 12 */ "a/b/c/", "a/b/", "a/", NULL,
		/* 13 */ "", NULL,
		/* 14 */ "/", NULL,
		/* 15 */ NULL
	};

	char *root[] = {
		/*  1 */ NULL,
		/*  2 */ NULL,
		/*  3 */ "/",
		/*  4 */ "",
		/*  5 */ "/a/b",
		/*  6 */ "/a/b/",
		/*  7 */ NULL,
		/*  8 */ NULL,
		/*  9 */ NULL,
		/* 10 */ NULL,
		/* 11 */ NULL,
		/* 12 */ "a/",
		/* 13 */ NULL,
		/* 14 */ NULL,
417
	};
418

419
	int i, j;
420 421 422
	check_walkup_info info;

	info.expect = expect;
423
	info.cancel_after = -1;
424 425 426 427 428

	for (i = 0, j = 0; expect[i] != NULL; i++, j++) {

		git_buf_sets(&p, expect[i]);

429 430 431 432
		info.expect_idx = i;
		cl_git_pass(
			git_path_walk_up(&p, root[j], check_one_walkup_step, &info)
		);
433

434
		cl_assert_equal_s(p.ptr, expect[i]);
435 436
		cl_assert(expect[info.expect_idx] == NULL);
		i = info.expect_idx;
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
	}

	git_buf_free(&p);
}

void test_core_path__11a_walkup_cancel(void)
{
	git_buf p = GIT_BUF_INIT;
	int cancel[] = { 3, 2, 1, 0 };
	char *expect[] = {
		"/a/b/c/d/e/", "/a/b/c/d/", "/a/b/c/", "[CANCEL]", NULL,
		"/a/b/c/d/e", "/a/b/c/d/", "[CANCEL]", NULL,
		"/a/b/c/d/e", "[CANCEL]", NULL,
		"[CANCEL]", NULL,
		NULL
	};
	char *root[] = { NULL, NULL, "/", "", NULL };
	int i, j;
	check_walkup_info info;

	info.expect = expect;

	for (i = 0, j = 0; expect[i] != NULL; i++, j++) {

		git_buf_sets(&p, expect[i]);

		info.cancel_after = cancel[j];
		info.expect_idx = i;

		cl_assert_equal_i(
467
			CANCEL_VALUE,
468 469 470 471 472
			git_path_walk_up(&p, root[j], check_one_walkup_step, &info)
		);

		/* skip to next run of expectations */
		while (expect[i] != NULL) i++;
473 474 475 476
	}

	git_buf_free(&p);
}
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491

void test_core_path__12_offset_to_path_root(void)
{
	cl_assert(git_path_root("non/rooted/path") == -1);
	cl_assert(git_path_root("/rooted/path") == 0);

#ifdef GIT_WIN32
	/* Windows specific tests */
	cl_assert(git_path_root("C:non/rooted/path") == -1);
	cl_assert(git_path_root("C:/rooted/path") == 2);
	cl_assert(git_path_root("//computername/sharefolder/resource") == 14);
	cl_assert(git_path_root("//computername/sharefolder") == 14);
	cl_assert(git_path_root("//computername") == -1);
#endif
}
492 493 494 495 496 497 498

#define NON_EXISTING_FILEPATH "i_hope_i_do_not_exist"

void test_core_path__13_cannot_prettify_a_non_existing_file(void)
{
	git_buf p = GIT_BUF_INIT;

499
	cl_assert_equal_b(git_path_exists(NON_EXISTING_FILEPATH), false);
500 501 502 503 504
	cl_assert_equal_i(GIT_ENOTFOUND, git_path_prettify(&p, NON_EXISTING_FILEPATH, NULL));
	cl_assert_equal_i(GIT_ENOTFOUND, git_path_prettify(&p, NON_EXISTING_FILEPATH "/so-do-i", NULL));

	git_buf_free(&p);
}
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523

void test_core_path__14_apply_relative(void)
{
	git_buf p = GIT_BUF_INIT;

	cl_git_pass(git_buf_sets(&p, "/this/is/a/base"));

	cl_git_pass(git_path_apply_relative(&p, "../test"));
	cl_assert_equal_s("/this/is/a/test", p.ptr);

	cl_git_pass(git_path_apply_relative(&p, "../../the/./end"));
	cl_assert_equal_s("/this/is/the/end", p.ptr);

	cl_git_pass(git_path_apply_relative(&p, "./of/this/../the/string"));
	cl_assert_equal_s("/this/is/the/end/of/the/string", p.ptr);

	cl_git_pass(git_path_apply_relative(&p, "../../../../../.."));
	cl_assert_equal_s("/this/", p.ptr);

524
	cl_git_pass(git_path_apply_relative(&p, "../"));
525 526
	cl_assert_equal_s("/", p.ptr);

527
	cl_git_fail(git_path_apply_relative(&p, "../../.."));
528 529 530 531


	cl_git_pass(git_buf_sets(&p, "d:/another/test"));

532
	cl_git_pass(git_path_apply_relative(&p, "../.."));
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
	cl_assert_equal_s("d:/", p.ptr);

	cl_git_pass(git_path_apply_relative(&p, "from/here/to/../and/./back/."));
	cl_assert_equal_s("d:/from/here/and/back/", p.ptr);


	cl_git_pass(git_buf_sets(&p, "https://my.url.com/test.git"));

	cl_git_pass(git_path_apply_relative(&p, "../another.git"));
	cl_assert_equal_s("https://my.url.com/another.git", p.ptr);

	cl_git_pass(git_path_apply_relative(&p, "../full/path/url.patch"));
	cl_assert_equal_s("https://my.url.com/full/path/url.patch", p.ptr);

	cl_git_pass(git_path_apply_relative(&p, ".."));
	cl_assert_equal_s("https://my.url.com/full/path/", p.ptr);

550
	cl_git_pass(git_path_apply_relative(&p, "../../../"));
551 552
	cl_assert_equal_s("https://", p.ptr);

553 554 555 556 557 558 559 560 561 562 563

	cl_git_pass(git_buf_sets(&p, "../../this/is/relative"));

	cl_git_pass(git_path_apply_relative(&p, "../../preserves/the/prefix"));
	cl_assert_equal_s("../../this/preserves/the/prefix", p.ptr);

	cl_git_pass(git_path_apply_relative(&p, "../../../../that"));
	cl_assert_equal_s("../../that", p.ptr);

	cl_git_pass(git_path_apply_relative(&p, "../there"));
	cl_assert_equal_s("../../there", p.ptr);
564 565
	git_buf_free(&p);
}
566

567 568
static void assert_resolve_relative(
	git_buf *buf, const char *expected, const char *path)
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641
{
	cl_git_pass(git_buf_sets(buf, path));
	cl_git_pass(git_path_resolve_relative(buf, 0));
	cl_assert_equal_s(expected, buf->ptr);
}

void test_core_path__15_resolve_relative(void)
{
	git_buf buf = GIT_BUF_INIT;

	assert_resolve_relative(&buf, "", "");
	assert_resolve_relative(&buf, "", ".");
	assert_resolve_relative(&buf, "", "./");
	assert_resolve_relative(&buf, "..", "..");
	assert_resolve_relative(&buf, "../", "../");
	assert_resolve_relative(&buf, "..", "./..");
	assert_resolve_relative(&buf, "../", "./../");
	assert_resolve_relative(&buf, "../", "../.");
	assert_resolve_relative(&buf, "../", ".././");
	assert_resolve_relative(&buf, "../..", "../..");
	assert_resolve_relative(&buf, "../../", "../../");

	assert_resolve_relative(&buf, "/", "/");
	assert_resolve_relative(&buf, "/", "/.");

	assert_resolve_relative(&buf, "", "a/..");
	assert_resolve_relative(&buf, "", "a/../");
	assert_resolve_relative(&buf, "", "a/../.");

	assert_resolve_relative(&buf, "/a", "/a");
	assert_resolve_relative(&buf, "/a/", "/a/.");
	assert_resolve_relative(&buf, "/", "/a/../");
	assert_resolve_relative(&buf, "/", "/a/../.");
	assert_resolve_relative(&buf, "/", "/a/.././");

	assert_resolve_relative(&buf, "a", "a");
	assert_resolve_relative(&buf, "a/", "a/");
	assert_resolve_relative(&buf, "a/", "a/.");
	assert_resolve_relative(&buf, "a/", "a/./");

	assert_resolve_relative(&buf, "a/b", "a//b");
	assert_resolve_relative(&buf, "a/b/c", "a/b/c");
	assert_resolve_relative(&buf, "b/c", "./b/c");
	assert_resolve_relative(&buf, "a/c", "a/./c");
	assert_resolve_relative(&buf, "a/b/", "a/b/.");

	assert_resolve_relative(&buf, "/a/b/c", "///a/b/c");
	assert_resolve_relative(&buf, "/", "////");
	assert_resolve_relative(&buf, "/a", "///a");
	assert_resolve_relative(&buf, "/", "///.");
	assert_resolve_relative(&buf, "/", "///a/..");

	assert_resolve_relative(&buf, "../../path", "../../test//../././path");
	assert_resolve_relative(&buf, "../d", "a/b/../../../c/../d");

	cl_git_pass(git_buf_sets(&buf, "/.."));
	cl_git_fail(git_path_resolve_relative(&buf, 0));

	cl_git_pass(git_buf_sets(&buf, "/./.."));
	cl_git_fail(git_path_resolve_relative(&buf, 0));

	cl_git_pass(git_buf_sets(&buf, "/.//.."));
	cl_git_fail(git_path_resolve_relative(&buf, 0));

	cl_git_pass(git_buf_sets(&buf, "/../."));
	cl_git_fail(git_path_resolve_relative(&buf, 0));

	cl_git_pass(git_buf_sets(&buf, "/../.././../a"));
	cl_git_fail(git_path_resolve_relative(&buf, 0));

	cl_git_pass(git_buf_sets(&buf, "////.."));
	cl_git_fail(git_path_resolve_relative(&buf, 0));

642 643 644 645 646 647 648 649 650 651 652 653 654 655 656
	/* things that start with Windows network paths */
#ifdef GIT_WIN32
	assert_resolve_relative(&buf, "//a/b/c", "//a/b/c");
	assert_resolve_relative(&buf, "//a/", "//a/b/..");
	assert_resolve_relative(&buf, "//a/b/c", "//a/Q/../b/x/y/../../c");

	cl_git_pass(git_buf_sets(&buf, "//a/b/../.."));
	cl_git_fail(git_path_resolve_relative(&buf, 0));
#else
	assert_resolve_relative(&buf, "/a/b/c", "//a/b/c");
	assert_resolve_relative(&buf, "/a/", "//a/b/..");
	assert_resolve_relative(&buf, "/a/b/c", "//a/Q/../b/x/y/../../c");
	assert_resolve_relative(&buf, "/", "//a/b/../..");
#endif

657 658
	git_buf_free(&buf);
}
659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678

#define assert_common_dirlen(i, p, q) \
	cl_assert_equal_i((i), git_path_common_dirlen((p), (q)));

void test_core_path__16_resolve_relative(void)
{
	assert_common_dirlen(0, "", "");
	assert_common_dirlen(0, "", "bar.txt");
	assert_common_dirlen(0, "foo.txt", "bar.txt");
	assert_common_dirlen(0, "foo.txt", "");
	assert_common_dirlen(0, "foo/bar.txt", "bar/foo.txt");
	assert_common_dirlen(0, "foo/bar.txt", "../foo.txt");

	assert_common_dirlen(1, "/one.txt", "/two.txt");
	assert_common_dirlen(4, "foo/one.txt", "foo/two.txt");
	assert_common_dirlen(5, "/foo/one.txt", "/foo/two.txt");

	assert_common_dirlen(6, "a/b/c/foo.txt", "a/b/c/d/e/bar.txt");
	assert_common_dirlen(7, "/a/b/c/foo.txt", "/a/b/c/d/e/bar.txt");
}