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