]> git.proxmox.com Git - libgit2.git/blob - tests/core/path.c
Merge pull request #3703 from libgit2/cmn/multivar-set-locked
[libgit2.git] / tests / core / path.c
1 #include "clar_libgit2.h"
2 #include "fileops.h"
3
4 static void
5 check_dirname(const char *A, const char *B)
6 {
7 git_buf dir = GIT_BUF_INIT;
8 char *dir2;
9
10 cl_assert(git_path_dirname_r(&dir, A) >= 0);
11 cl_assert_equal_s(B, dir.ptr);
12 git_buf_free(&dir);
13
14 cl_assert((dir2 = git_path_dirname(A)) != NULL);
15 cl_assert_equal_s(B, dir2);
16 git__free(dir2);
17 }
18
19 static void
20 check_basename(const char *A, const char *B)
21 {
22 git_buf base = GIT_BUF_INIT;
23 char *base2;
24
25 cl_assert(git_path_basename_r(&base, A) >= 0);
26 cl_assert_equal_s(B, base.ptr);
27 git_buf_free(&base);
28
29 cl_assert((base2 = git_path_basename(A)) != NULL);
30 cl_assert_equal_s(B, base2);
31 git__free(base2);
32 }
33
34 static void
35 check_topdir(const char *A, const char *B)
36 {
37 const char *dir;
38
39 cl_assert((dir = git_path_topdir(A)) != NULL);
40 cl_assert_equal_s(B, dir);
41 }
42
43 static void
44 check_joinpath(const char *path_a, const char *path_b, const char *expected_path)
45 {
46 git_buf joined_path = GIT_BUF_INIT;
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);
52 }
53
54 static void
55 check_joinpath_n(
56 const char *path_a,
57 const char *path_b,
58 const char *path_c,
59 const char *path_d,
60 const char *expected_path)
61 {
62 git_buf joined_path = GIT_BUF_INIT;
63
64 cl_git_pass(git_buf_join_n(&joined_path, '/', 4,
65 path_a, path_b, path_c, path_d));
66 cl_assert_equal_s(expected_path, joined_path.ptr);
67
68 git_buf_free(&joined_path);
69 }
70
71
72 /* get the dirname of a path */
73 void test_core_path__00_dirname(void)
74 {
75 check_dirname(NULL, ".");
76 check_dirname("", ".");
77 check_dirname("a", ".");
78 check_dirname("/", "/");
79 check_dirname("/usr", "/");
80 check_dirname("/usr/", "/");
81 check_dirname("/usr/lib", "/usr");
82 check_dirname("/usr/lib/", "/usr");
83 check_dirname("/usr/lib//", "/usr");
84 check_dirname("usr/lib", "usr");
85 check_dirname("usr/lib/", "usr");
86 check_dirname("usr/lib//", "usr");
87 check_dirname(".git/", ".");
88
89 check_dirname(REP16("/abc"), REP15("/abc"));
90
91 #ifdef GIT_WIN32
92 check_dirname("C:/path/", "C:/");
93 check_dirname("C:/path", "C:/");
94 check_dirname("//computername/path/", "//computername/");
95 check_dirname("//computername/path", "//computername/");
96 check_dirname("//computername/sub/path/", "//computername/sub");
97 check_dirname("//computername/sub/path", "//computername/sub");
98 #endif
99 }
100
101 /* get the base name of a path */
102 void test_core_path__01_basename(void)
103 {
104 check_basename(NULL, ".");
105 check_basename("", ".");
106 check_basename("a", "a");
107 check_basename("/", "/");
108 check_basename("/usr", "usr");
109 check_basename("/usr/", "usr");
110 check_basename("/usr/lib", "lib");
111 check_basename("/usr/lib//", "lib");
112 check_basename("usr/lib", "lib");
113
114 check_basename(REP16("/abc"), "abc");
115 check_basename(REP1024("/abc"), "abc");
116 }
117
118 /* get the latest component in a path */
119 void test_core_path__02_topdir(void)
120 {
121 check_topdir(".git/", ".git/");
122 check_topdir("/.git/", ".git/");
123 check_topdir("usr/local/.git/", ".git/");
124 check_topdir("./.git/", ".git/");
125 check_topdir("/usr/.git/", ".git/");
126 check_topdir("/", "/");
127 check_topdir("a/", "a/");
128
129 cl_assert(git_path_topdir("/usr/.git") == NULL);
130 cl_assert(git_path_topdir(".") == NULL);
131 cl_assert(git_path_topdir("") == NULL);
132 cl_assert(git_path_topdir("a") == NULL);
133 }
134
135 /* properly join path components */
136 void test_core_path__05_joins(void)
137 {
138 check_joinpath("", "", "");
139 check_joinpath("", "a", "a");
140 check_joinpath("", "/a", "/a");
141 check_joinpath("a", "", "a/");
142 check_joinpath("a", "/", "a/");
143 check_joinpath("a", "b", "a/b");
144 check_joinpath("/", "a", "/a");
145 check_joinpath("/", "", "/");
146 check_joinpath("/a", "/b", "/a/b");
147 check_joinpath("/a", "/b/", "/a/b/");
148 check_joinpath("/a/", "b/", "/a/b/");
149 check_joinpath("/a/", "/b/", "/a/b/");
150
151 check_joinpath("/abcd", "/defg", "/abcd/defg");
152 check_joinpath("/abcd", "/defg/", "/abcd/defg/");
153 check_joinpath("/abcd/", "defg/", "/abcd/defg/");
154 check_joinpath("/abcd/", "/defg/", "/abcd/defg/");
155
156 check_joinpath("/abcdefgh", "/12345678", "/abcdefgh/12345678");
157 check_joinpath("/abcdefgh", "/12345678/", "/abcdefgh/12345678/");
158 check_joinpath("/abcdefgh/", "12345678/", "/abcdefgh/12345678/");
159
160 check_joinpath(REP1024("aaaa"), "", REP1024("aaaa") "/");
161 check_joinpath(REP1024("aaaa/"), "", REP1024("aaaa/"));
162 check_joinpath(REP1024("/aaaa"), "", REP1024("/aaaa") "/");
163
164 check_joinpath(REP1024("aaaa"), REP1024("bbbb"),
165 REP1024("aaaa") "/" REP1024("bbbb"));
166 check_joinpath(REP1024("/aaaa"), REP1024("/bbbb"),
167 REP1024("/aaaa") REP1024("/bbbb"));
168 }
169
170 /* properly join path components for more than one path */
171 void test_core_path__06_long_joins(void)
172 {
173 check_joinpath_n("", "", "", "", "");
174 check_joinpath_n("", "a", "", "", "a/");
175 check_joinpath_n("a", "", "", "", "a/");
176 check_joinpath_n("", "", "", "a", "a");
177 check_joinpath_n("a", "b", "", "/c/d/", "a/b/c/d/");
178 check_joinpath_n("a", "b", "", "/c/d", "a/b/c/d");
179 check_joinpath_n("abcd", "efgh", "ijkl", "mnop", "abcd/efgh/ijkl/mnop");
180 check_joinpath_n("abcd/", "efgh/", "ijkl/", "mnop/", "abcd/efgh/ijkl/mnop/");
181 check_joinpath_n("/abcd/", "/efgh/", "/ijkl/", "/mnop/", "/abcd/efgh/ijkl/mnop/");
182
183 check_joinpath_n(REP1024("a"), REP1024("b"), REP1024("c"), REP1024("d"),
184 REP1024("a") "/" REP1024("b") "/"
185 REP1024("c") "/" REP1024("d"));
186 check_joinpath_n(REP1024("/a"), REP1024("/b"), REP1024("/c"), REP1024("/d"),
187 REP1024("/a") REP1024("/b")
188 REP1024("/c") REP1024("/d"));
189 }
190
191
192 static void
193 check_path_to_dir(
194 const char* path,
195 const char* expected)
196 {
197 git_buf tgt = GIT_BUF_INIT;
198
199 git_buf_sets(&tgt, path);
200 cl_git_pass(git_path_to_dir(&tgt));
201 cl_assert_equal_s(expected, tgt.ptr);
202
203 git_buf_free(&tgt);
204 }
205
206 static void
207 check_string_to_dir(
208 const char* path,
209 size_t maxlen,
210 const char* expected)
211 {
212 size_t len = strlen(path);
213 char *buf = git__malloc(len + 2);
214 cl_assert(buf);
215
216 strncpy(buf, path, len + 2);
217
218 git_path_string_to_dir(buf, maxlen);
219
220 cl_assert_equal_s(expected, buf);
221
222 git__free(buf);
223 }
224
225 /* convert paths to dirs */
226 void test_core_path__07_path_to_dir(void)
227 {
228 check_path_to_dir("", "");
229 check_path_to_dir(".", "./");
230 check_path_to_dir("./", "./");
231 check_path_to_dir("a/", "a/");
232 check_path_to_dir("ab", "ab/");
233 /* make sure we try just under and just over an expansion that will
234 * require a realloc
235 */
236 check_path_to_dir("abcdef", "abcdef/");
237 check_path_to_dir("abcdefg", "abcdefg/");
238 check_path_to_dir("abcdefgh", "abcdefgh/");
239 check_path_to_dir("abcdefghi", "abcdefghi/");
240 check_path_to_dir(REP1024("abcd") "/", REP1024("abcd") "/");
241 check_path_to_dir(REP1024("abcd"), REP1024("abcd") "/");
242
243 check_string_to_dir("", 1, "");
244 check_string_to_dir(".", 1, ".");
245 check_string_to_dir(".", 2, "./");
246 check_string_to_dir(".", 3, "./");
247 check_string_to_dir("abcd", 3, "abcd");
248 check_string_to_dir("abcd", 4, "abcd");
249 check_string_to_dir("abcd", 5, "abcd/");
250 check_string_to_dir("abcd", 6, "abcd/");
251 }
252
253 /* join path to itself */
254 void test_core_path__08_self_join(void)
255 {
256 git_buf path = GIT_BUF_INIT;
257 size_t asize = 0;
258
259 asize = path.asize;
260 cl_git_pass(git_buf_sets(&path, "/foo"));
261 cl_assert_equal_s(path.ptr, "/foo");
262 cl_assert(asize < path.asize);
263
264 asize = path.asize;
265 cl_git_pass(git_buf_joinpath(&path, path.ptr, "this is a new string"));
266 cl_assert_equal_s(path.ptr, "/foo/this is a new string");
267 cl_assert(asize < path.asize);
268
269 asize = path.asize;
270 cl_git_pass(git_buf_joinpath(&path, path.ptr, "/grow the buffer, grow the buffer, grow the buffer"));
271 cl_assert_equal_s(path.ptr, "/foo/this is a new string/grow the buffer, grow the buffer, grow the buffer");
272 cl_assert(asize < path.asize);
273
274 git_buf_free(&path);
275 cl_git_pass(git_buf_sets(&path, "/foo/bar"));
276
277 cl_git_pass(git_buf_joinpath(&path, path.ptr + 4, "baz"));
278 cl_assert_equal_s(path.ptr, "/bar/baz");
279
280 asize = path.asize;
281 cl_git_pass(git_buf_joinpath(&path, path.ptr + 4, "somethinglongenoughtorealloc"));
282 cl_assert_equal_s(path.ptr, "/baz/somethinglongenoughtorealloc");
283 cl_assert(asize < path.asize);
284
285 git_buf_free(&path);
286 }
287
288 static void check_percent_decoding(const char *expected_result, const char *input)
289 {
290 git_buf buf = GIT_BUF_INIT;
291
292 cl_git_pass(git__percent_decode(&buf, input));
293 cl_assert_equal_s(expected_result, git_buf_cstr(&buf));
294
295 git_buf_free(&buf);
296 }
297
298 void test_core_path__09_percent_decode(void)
299 {
300 check_percent_decoding("abcd", "abcd");
301 check_percent_decoding("a2%", "a2%");
302 check_percent_decoding("a2%3", "a2%3");
303 check_percent_decoding("a2%%3", "a2%%3");
304 check_percent_decoding("a2%3z", "a2%3z");
305 check_percent_decoding("a,", "a%2c");
306 check_percent_decoding("a21", "a2%31");
307 check_percent_decoding("a2%1", "a2%%31");
308 check_percent_decoding("a bc ", "a%20bc%20");
309 check_percent_decoding("Vicent Mart" "\355", "Vicent%20Mart%ED");
310 }
311
312 static void check_fromurl(const char *expected_result, const char *input, int should_fail)
313 {
314 git_buf buf = GIT_BUF_INIT;
315
316 assert(should_fail || expected_result);
317
318 if (!should_fail) {
319 cl_git_pass(git_path_fromurl(&buf, input));
320 cl_assert_equal_s(expected_result, git_buf_cstr(&buf));
321 } else
322 cl_git_fail(git_path_fromurl(&buf, input));
323
324 git_buf_free(&buf);
325 }
326
327 #ifdef GIT_WIN32
328 #define ABS_PATH_MARKER ""
329 #else
330 #define ABS_PATH_MARKER "/"
331 #endif
332
333 void test_core_path__10_fromurl(void)
334 {
335 /* Failing cases */
336 check_fromurl(NULL, "a", 1);
337 check_fromurl(NULL, "http:///c:/Temp%20folder/note.txt", 1);
338 check_fromurl(NULL, "file://c:/Temp%20folder/note.txt", 1);
339 check_fromurl(NULL, "file:////c:/Temp%20folder/note.txt", 1);
340 check_fromurl(NULL, "file:///", 1);
341 check_fromurl(NULL, "file:////", 1);
342 check_fromurl(NULL, "file://servername/c:/Temp%20folder/note.txt", 1);
343
344 /* Passing cases */
345 check_fromurl(ABS_PATH_MARKER "c:/Temp folder/note.txt", "file:///c:/Temp%20folder/note.txt", 0);
346 check_fromurl(ABS_PATH_MARKER "c:/Temp folder/note.txt", "file://localhost/c:/Temp%20folder/note.txt", 0);
347 check_fromurl(ABS_PATH_MARKER "c:/Temp+folder/note.txt", "file:///c:/Temp+folder/note.txt", 0);
348 check_fromurl(ABS_PATH_MARKER "a", "file:///a", 0);
349 }
350
351 typedef struct {
352 int expect_idx;
353 int cancel_after;
354 char **expect;
355 } check_walkup_info;
356
357 #define CANCEL_VALUE 1234
358
359 static int check_one_walkup_step(void *ref, const char *path)
360 {
361 check_walkup_info *info = (check_walkup_info *)ref;
362
363 if (!info->cancel_after) {
364 cl_assert_equal_s(info->expect[info->expect_idx], "[CANCEL]");
365 return CANCEL_VALUE;
366 }
367 info->cancel_after--;
368
369 cl_assert(info->expect[info->expect_idx] != NULL);
370 cl_assert_equal_s(info->expect[info->expect_idx], path);
371 info->expect_idx++;
372
373 return 0;
374 }
375
376 void test_core_path__11_walkup(void)
377 {
378 git_buf p = GIT_BUF_INIT;
379
380 char *expect[] = {
381 /* 1 */ "/a/b/c/d/e/", "/a/b/c/d/", "/a/b/c/", "/a/b/", "/a/", "/", NULL,
382 /* 2 */ "/a/b/c/d/e", "/a/b/c/d/", "/a/b/c/", "/a/b/", "/a/", "/", NULL,
383 /* 3 */ "/a/b/c/d/e", "/a/b/c/d/", "/a/b/c/", "/a/b/", "/a/", "/", NULL,
384 /* 4 */ "/a/b/c/d/e", "/a/b/c/d/", "/a/b/c/", "/a/b/", "/a/", "/", NULL,
385 /* 5 */ "/a/b/c/d/e", "/a/b/c/d/", "/a/b/c/", "/a/b/", NULL,
386 /* 6 */ "/a/b/c/d/e", "/a/b/c/d/", "/a/b/c/", "/a/b/", NULL,
387 /* 7 */ "this_is_a_path", "", NULL,
388 /* 8 */ "this_is_a_path/", "", NULL,
389 /* 9 */ "///a///b///c///d///e///", "///a///b///c///d///", "///a///b///c///", "///a///b///", "///a///", "///", NULL,
390 /* 10 */ "a/b/c/", "a/b/", "a/", "", NULL,
391 /* 11 */ "a/b/c", "a/b/", "a/", "", NULL,
392 /* 12 */ "a/b/c/", "a/b/", "a/", NULL,
393 /* 13 */ "", NULL,
394 /* 14 */ "/", NULL,
395 /* 15 */ NULL
396 };
397
398 char *root[] = {
399 /* 1 */ NULL,
400 /* 2 */ NULL,
401 /* 3 */ "/",
402 /* 4 */ "",
403 /* 5 */ "/a/b",
404 /* 6 */ "/a/b/",
405 /* 7 */ NULL,
406 /* 8 */ NULL,
407 /* 9 */ NULL,
408 /* 10 */ NULL,
409 /* 11 */ NULL,
410 /* 12 */ "a/",
411 /* 13 */ NULL,
412 /* 14 */ NULL,
413 };
414
415 int i, j;
416 check_walkup_info info;
417
418 info.expect = expect;
419 info.cancel_after = -1;
420
421 for (i = 0, j = 0; expect[i] != NULL; i++, j++) {
422
423 git_buf_sets(&p, expect[i]);
424
425 info.expect_idx = i;
426 cl_git_pass(
427 git_path_walk_up(&p, root[j], check_one_walkup_step, &info)
428 );
429
430 cl_assert_equal_s(p.ptr, expect[i]);
431 cl_assert(expect[info.expect_idx] == NULL);
432 i = info.expect_idx;
433 }
434
435 git_buf_free(&p);
436 }
437
438 void test_core_path__11a_walkup_cancel(void)
439 {
440 git_buf p = GIT_BUF_INIT;
441 int cancel[] = { 3, 2, 1, 0 };
442 char *expect[] = {
443 "/a/b/c/d/e/", "/a/b/c/d/", "/a/b/c/", "[CANCEL]", NULL,
444 "/a/b/c/d/e", "/a/b/c/d/", "[CANCEL]", NULL,
445 "/a/b/c/d/e", "[CANCEL]", NULL,
446 "[CANCEL]", NULL,
447 NULL
448 };
449 char *root[] = { NULL, NULL, "/", "", NULL };
450 int i, j;
451 check_walkup_info info;
452
453 info.expect = expect;
454
455 for (i = 0, j = 0; expect[i] != NULL; i++, j++) {
456
457 git_buf_sets(&p, expect[i]);
458
459 info.cancel_after = cancel[j];
460 info.expect_idx = i;
461
462 cl_assert_equal_i(
463 CANCEL_VALUE,
464 git_path_walk_up(&p, root[j], check_one_walkup_step, &info)
465 );
466
467 /* skip to next run of expectations */
468 while (expect[i] != NULL) i++;
469 }
470
471 git_buf_free(&p);
472 }
473
474 void test_core_path__12_offset_to_path_root(void)
475 {
476 cl_assert(git_path_root("non/rooted/path") == -1);
477 cl_assert(git_path_root("/rooted/path") == 0);
478
479 #ifdef GIT_WIN32
480 /* Windows specific tests */
481 cl_assert(git_path_root("C:non/rooted/path") == -1);
482 cl_assert(git_path_root("C:/rooted/path") == 2);
483 cl_assert(git_path_root("//computername/sharefolder/resource") == 14);
484 cl_assert(git_path_root("//computername/sharefolder") == 14);
485 cl_assert(git_path_root("//computername") == -1);
486 #endif
487 }
488
489 #define NON_EXISTING_FILEPATH "i_hope_i_do_not_exist"
490
491 void test_core_path__13_cannot_prettify_a_non_existing_file(void)
492 {
493 git_buf p = GIT_BUF_INIT;
494
495 cl_assert_equal_b(git_path_exists(NON_EXISTING_FILEPATH), false);
496 cl_assert_equal_i(GIT_ENOTFOUND, git_path_prettify(&p, NON_EXISTING_FILEPATH, NULL));
497 cl_assert_equal_i(GIT_ENOTFOUND, git_path_prettify(&p, NON_EXISTING_FILEPATH "/so-do-i", NULL));
498
499 git_buf_free(&p);
500 }
501
502 void test_core_path__14_apply_relative(void)
503 {
504 git_buf p = GIT_BUF_INIT;
505
506 cl_git_pass(git_buf_sets(&p, "/this/is/a/base"));
507
508 cl_git_pass(git_path_apply_relative(&p, "../test"));
509 cl_assert_equal_s("/this/is/a/test", p.ptr);
510
511 cl_git_pass(git_path_apply_relative(&p, "../../the/./end"));
512 cl_assert_equal_s("/this/is/the/end", p.ptr);
513
514 cl_git_pass(git_path_apply_relative(&p, "./of/this/../the/string"));
515 cl_assert_equal_s("/this/is/the/end/of/the/string", p.ptr);
516
517 cl_git_pass(git_path_apply_relative(&p, "../../../../../.."));
518 cl_assert_equal_s("/this/", p.ptr);
519
520 cl_git_pass(git_path_apply_relative(&p, "../"));
521 cl_assert_equal_s("/", p.ptr);
522
523 cl_git_fail(git_path_apply_relative(&p, "../../.."));
524
525
526 cl_git_pass(git_buf_sets(&p, "d:/another/test"));
527
528 cl_git_pass(git_path_apply_relative(&p, "../.."));
529 cl_assert_equal_s("d:/", p.ptr);
530
531 cl_git_pass(git_path_apply_relative(&p, "from/here/to/../and/./back/."));
532 cl_assert_equal_s("d:/from/here/and/back/", p.ptr);
533
534
535 cl_git_pass(git_buf_sets(&p, "https://my.url.com/test.git"));
536
537 cl_git_pass(git_path_apply_relative(&p, "../another.git"));
538 cl_assert_equal_s("https://my.url.com/another.git", p.ptr);
539
540 cl_git_pass(git_path_apply_relative(&p, "../full/path/url.patch"));
541 cl_assert_equal_s("https://my.url.com/full/path/url.patch", p.ptr);
542
543 cl_git_pass(git_path_apply_relative(&p, ".."));
544 cl_assert_equal_s("https://my.url.com/full/path/", p.ptr);
545
546 cl_git_pass(git_path_apply_relative(&p, "../../../"));
547 cl_assert_equal_s("https://", p.ptr);
548
549
550 cl_git_pass(git_buf_sets(&p, "../../this/is/relative"));
551
552 cl_git_pass(git_path_apply_relative(&p, "../../preserves/the/prefix"));
553 cl_assert_equal_s("../../this/preserves/the/prefix", p.ptr);
554
555 cl_git_pass(git_path_apply_relative(&p, "../../../../that"));
556 cl_assert_equal_s("../../that", p.ptr);
557
558 cl_git_pass(git_path_apply_relative(&p, "../there"));
559 cl_assert_equal_s("../../there", p.ptr);
560 git_buf_free(&p);
561 }
562
563 static void assert_resolve_relative(
564 git_buf *buf, const char *expected, const char *path)
565 {
566 cl_git_pass(git_buf_sets(buf, path));
567 cl_git_pass(git_path_resolve_relative(buf, 0));
568 cl_assert_equal_s(expected, buf->ptr);
569 }
570
571 void test_core_path__15_resolve_relative(void)
572 {
573 git_buf buf = GIT_BUF_INIT;
574
575 assert_resolve_relative(&buf, "", "");
576 assert_resolve_relative(&buf, "", ".");
577 assert_resolve_relative(&buf, "", "./");
578 assert_resolve_relative(&buf, "..", "..");
579 assert_resolve_relative(&buf, "../", "../");
580 assert_resolve_relative(&buf, "..", "./..");
581 assert_resolve_relative(&buf, "../", "./../");
582 assert_resolve_relative(&buf, "../", "../.");
583 assert_resolve_relative(&buf, "../", ".././");
584 assert_resolve_relative(&buf, "../..", "../..");
585 assert_resolve_relative(&buf, "../../", "../../");
586
587 assert_resolve_relative(&buf, "/", "/");
588 assert_resolve_relative(&buf, "/", "/.");
589
590 assert_resolve_relative(&buf, "", "a/..");
591 assert_resolve_relative(&buf, "", "a/../");
592 assert_resolve_relative(&buf, "", "a/../.");
593
594 assert_resolve_relative(&buf, "/a", "/a");
595 assert_resolve_relative(&buf, "/a/", "/a/.");
596 assert_resolve_relative(&buf, "/", "/a/../");
597 assert_resolve_relative(&buf, "/", "/a/../.");
598 assert_resolve_relative(&buf, "/", "/a/.././");
599
600 assert_resolve_relative(&buf, "a", "a");
601 assert_resolve_relative(&buf, "a/", "a/");
602 assert_resolve_relative(&buf, "a/", "a/.");
603 assert_resolve_relative(&buf, "a/", "a/./");
604
605 assert_resolve_relative(&buf, "a/b", "a//b");
606 assert_resolve_relative(&buf, "a/b/c", "a/b/c");
607 assert_resolve_relative(&buf, "b/c", "./b/c");
608 assert_resolve_relative(&buf, "a/c", "a/./c");
609 assert_resolve_relative(&buf, "a/b/", "a/b/.");
610
611 assert_resolve_relative(&buf, "/a/b/c", "///a/b/c");
612 assert_resolve_relative(&buf, "/", "////");
613 assert_resolve_relative(&buf, "/a", "///a");
614 assert_resolve_relative(&buf, "/", "///.");
615 assert_resolve_relative(&buf, "/", "///a/..");
616
617 assert_resolve_relative(&buf, "../../path", "../../test//../././path");
618 assert_resolve_relative(&buf, "../d", "a/b/../../../c/../d");
619
620 cl_git_pass(git_buf_sets(&buf, "/.."));
621 cl_git_fail(git_path_resolve_relative(&buf, 0));
622
623 cl_git_pass(git_buf_sets(&buf, "/./.."));
624 cl_git_fail(git_path_resolve_relative(&buf, 0));
625
626 cl_git_pass(git_buf_sets(&buf, "/.//.."));
627 cl_git_fail(git_path_resolve_relative(&buf, 0));
628
629 cl_git_pass(git_buf_sets(&buf, "/../."));
630 cl_git_fail(git_path_resolve_relative(&buf, 0));
631
632 cl_git_pass(git_buf_sets(&buf, "/../.././../a"));
633 cl_git_fail(git_path_resolve_relative(&buf, 0));
634
635 cl_git_pass(git_buf_sets(&buf, "////.."));
636 cl_git_fail(git_path_resolve_relative(&buf, 0));
637
638 /* things that start with Windows network paths */
639 #ifdef GIT_WIN32
640 assert_resolve_relative(&buf, "//a/b/c", "//a/b/c");
641 assert_resolve_relative(&buf, "//a/", "//a/b/..");
642 assert_resolve_relative(&buf, "//a/b/c", "//a/Q/../b/x/y/../../c");
643
644 cl_git_pass(git_buf_sets(&buf, "//a/b/../.."));
645 cl_git_fail(git_path_resolve_relative(&buf, 0));
646 #else
647 assert_resolve_relative(&buf, "/a/b/c", "//a/b/c");
648 assert_resolve_relative(&buf, "/a/", "//a/b/..");
649 assert_resolve_relative(&buf, "/a/b/c", "//a/Q/../b/x/y/../../c");
650 assert_resolve_relative(&buf, "/", "//a/b/../..");
651 #endif
652
653 git_buf_free(&buf);
654 }
655
656 #define assert_common_dirlen(i, p, q) \
657 cl_assert_equal_i((i), git_path_common_dirlen((p), (q)));
658
659 void test_core_path__16_resolve_relative(void)
660 {
661 assert_common_dirlen(0, "", "");
662 assert_common_dirlen(0, "", "bar.txt");
663 assert_common_dirlen(0, "foo.txt", "bar.txt");
664 assert_common_dirlen(0, "foo.txt", "");
665 assert_common_dirlen(0, "foo/bar.txt", "bar/foo.txt");
666 assert_common_dirlen(0, "foo/bar.txt", "../foo.txt");
667
668 assert_common_dirlen(1, "/one.txt", "/two.txt");
669 assert_common_dirlen(4, "foo/one.txt", "foo/two.txt");
670 assert_common_dirlen(5, "/foo/one.txt", "/foo/two.txt");
671
672 assert_common_dirlen(6, "a/b/c/foo.txt", "a/b/c/d/e/bar.txt");
673 assert_common_dirlen(7, "/a/b/c/foo.txt", "/a/b/c/d/e/bar.txt");
674 }