]>
git.proxmox.com Git - libgit2.git/blob - tests/core/path.c
1 #include "clar_libgit2.h"
5 check_dirname(const char *A
, const char *B
)
7 git_buf dir
= GIT_BUF_INIT
;
10 cl_assert(git_path_dirname_r(&dir
, A
) >= 0);
11 cl_assert_equal_s(B
, dir
.ptr
);
14 cl_assert((dir2
= git_path_dirname(A
)) != NULL
);
15 cl_assert_equal_s(B
, dir2
);
20 check_basename(const char *A
, const char *B
)
22 git_buf base
= GIT_BUF_INIT
;
25 cl_assert(git_path_basename_r(&base
, A
) >= 0);
26 cl_assert_equal_s(B
, base
.ptr
);
29 cl_assert((base2
= git_path_basename(A
)) != NULL
);
30 cl_assert_equal_s(B
, base2
);
35 check_topdir(const char *A
, const char *B
)
39 cl_assert((dir
= git_path_topdir(A
)) != NULL
);
40 cl_assert_equal_s(B
, dir
);
44 check_joinpath(const char *path_a
, const char *path_b
, const char *expected_path
)
46 git_buf joined_path
= GIT_BUF_INIT
;
48 cl_git_pass(git_buf_joinpath(&joined_path
, path_a
, path_b
));
49 cl_assert_equal_s(expected_path
, joined_path
.ptr
);
51 git_buf_free(&joined_path
);
60 const char *expected_path
)
62 git_buf joined_path
= GIT_BUF_INIT
;
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
);
68 git_buf_free(&joined_path
);
72 /* get the dirname of a path */
73 void test_core_path__00_dirname(void)
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/", ".");
89 check_dirname(REP16("/abc"), REP15("/abc"));
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");
101 /* get the base name of a path */
102 void test_core_path__01_basename(void)
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");
114 check_basename(REP16("/abc"), "abc");
115 check_basename(REP1024("/abc"), "abc");
118 /* get the latest component in a path */
119 void test_core_path__02_topdir(void)
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/");
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
);
135 /* properly join path components */
136 void test_core_path__05_joins(void)
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/");
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/");
156 check_joinpath("/abcdefgh", "/12345678", "/abcdefgh/12345678");
157 check_joinpath("/abcdefgh", "/12345678/", "/abcdefgh/12345678/");
158 check_joinpath("/abcdefgh/", "12345678/", "/abcdefgh/12345678/");
160 check_joinpath(REP1024("aaaa"), "", REP1024("aaaa") "/");
161 check_joinpath(REP1024("aaaa/"), "", REP1024("aaaa/"));
162 check_joinpath(REP1024("/aaaa"), "", REP1024("/aaaa") "/");
164 check_joinpath(REP1024("aaaa"), REP1024("bbbb"),
165 REP1024("aaaa") "/" REP1024("bbbb"));
166 check_joinpath(REP1024("/aaaa"), REP1024("/bbbb"),
167 REP1024("/aaaa") REP1024("/bbbb"));
170 /* properly join path components for more than one path */
171 void test_core_path__06_long_joins(void)
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/");
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"));
195 const char* expected
)
197 git_buf tgt
= GIT_BUF_INIT
;
199 git_buf_sets(&tgt
, path
);
200 cl_git_pass(git_path_to_dir(&tgt
));
201 cl_assert_equal_s(expected
, tgt
.ptr
);
210 const char* expected
)
212 size_t len
= strlen(path
);
213 char *buf
= git__malloc(len
+ 2);
216 strncpy(buf
, path
, len
+ 2);
218 git_path_string_to_dir(buf
, maxlen
);
220 cl_assert_equal_s(expected
, buf
);
225 /* convert paths to dirs */
226 void test_core_path__07_path_to_dir(void)
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
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") "/");
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/");
253 /* join path to itself */
254 void test_core_path__08_self_join(void)
256 git_buf path
= GIT_BUF_INIT
;
260 cl_git_pass(git_buf_sets(&path
, "/foo"));
261 cl_assert_equal_s(path
.ptr
, "/foo");
262 cl_assert(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
);
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
);
275 cl_git_pass(git_buf_sets(&path
, "/foo/bar"));
277 cl_git_pass(git_buf_joinpath(&path
, path
.ptr
+ 4, "baz"));
278 cl_assert_equal_s(path
.ptr
, "/bar/baz");
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
);
288 static void check_percent_decoding(const char *expected_result
, const char *input
)
290 git_buf buf
= GIT_BUF_INIT
;
292 cl_git_pass(git__percent_decode(&buf
, input
));
293 cl_assert_equal_s(expected_result
, git_buf_cstr(&buf
));
298 void test_core_path__09_percent_decode(void)
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");
312 static void check_fromurl(const char *expected_result
, const char *input
, int should_fail
)
314 git_buf buf
= GIT_BUF_INIT
;
316 assert(should_fail
|| expected_result
);
319 cl_git_pass(git_path_fromurl(&buf
, input
));
320 cl_assert_equal_s(expected_result
, git_buf_cstr(&buf
));
322 cl_git_fail(git_path_fromurl(&buf
, input
));
328 #define ABS_PATH_MARKER ""
330 #define ABS_PATH_MARKER "/"
333 void test_core_path__10_fromurl(void)
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);
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);
357 #define CANCEL_VALUE 1234
359 static int check_one_walkup_step(void *ref
, git_buf
*path
)
361 check_walkup_info
*info
= (check_walkup_info
*)ref
;
363 if (!info
->cancel_after
) {
364 cl_assert_equal_s(info
->expect
[info
->expect_idx
], "[CANCEL]");
367 info
->cancel_after
--;
369 cl_assert(info
->expect
[info
->expect_idx
] != NULL
);
370 cl_assert_equal_s(info
->expect
[info
->expect_idx
], path
->ptr
);
376 void test_core_path__11_walkup(void)
378 git_buf p
= GIT_BUF_INIT
;
380 "/a/b/c/d/e/", "/a/b/c/d/", "/a/b/c/", "/a/b/", "/a/", "/", NULL
,
381 "/a/b/c/d/e", "/a/b/c/d/", "/a/b/c/", "/a/b/", "/a/", "/", NULL
,
382 "/a/b/c/d/e", "/a/b/c/d/", "/a/b/c/", "/a/b/", "/a/", "/", NULL
,
383 "/a/b/c/d/e", "/a/b/c/d/", "/a/b/c/", "/a/b/", "/a/", "/", NULL
,
384 "/a/b/c/d/e", "/a/b/c/d/", "/a/b/c/", "/a/b/", NULL
,
385 "/a/b/c/d/e", "/a/b/c/d/", "/a/b/c/", "/a/b/", NULL
,
386 "this is a path", NULL
,
387 "///a///b///c///d///e///", "///a///b///c///d///", "///a///b///c///", "///a///b///", "///a///", "///", NULL
,
390 char *root
[] = { NULL
, NULL
, "/", "", "/a/b", "/a/b/", NULL
, NULL
, NULL
};
392 check_walkup_info info
;
394 info
.expect
= expect
;
395 info
.cancel_after
= -1;
397 for (i
= 0, j
= 0; expect
[i
] != NULL
; i
++, j
++) {
399 git_buf_sets(&p
, expect
[i
]);
403 git_path_walk_up(&p
, root
[j
], check_one_walkup_step
, &info
)
406 cl_assert_equal_s(p
.ptr
, expect
[i
]);
408 /* skip to next run of expectations */
409 while (expect
[i
] != NULL
) i
++;
415 void test_core_path__11a_walkup_cancel(void)
417 git_buf p
= GIT_BUF_INIT
;
418 int cancel
[] = { 3, 2, 1, 0 };
420 "/a/b/c/d/e/", "/a/b/c/d/", "/a/b/c/", "[CANCEL]", NULL
,
421 "/a/b/c/d/e", "/a/b/c/d/", "[CANCEL]", NULL
,
422 "/a/b/c/d/e", "[CANCEL]", NULL
,
426 char *root
[] = { NULL
, NULL
, "/", "", NULL
};
428 check_walkup_info info
;
430 info
.expect
= expect
;
432 for (i
= 0, j
= 0; expect
[i
] != NULL
; i
++, j
++) {
434 git_buf_sets(&p
, expect
[i
]);
436 info
.cancel_after
= cancel
[j
];
441 git_path_walk_up(&p
, root
[j
], check_one_walkup_step
, &info
)
444 /* skip to next run of expectations */
445 while (expect
[i
] != NULL
) i
++;
451 void test_core_path__12_offset_to_path_root(void)
453 cl_assert(git_path_root("non/rooted/path") == -1);
454 cl_assert(git_path_root("/rooted/path") == 0);
457 /* Windows specific tests */
458 cl_assert(git_path_root("C:non/rooted/path") == -1);
459 cl_assert(git_path_root("C:/rooted/path") == 2);
460 cl_assert(git_path_root("//computername/sharefolder/resource") == 14);
461 cl_assert(git_path_root("//computername/sharefolder") == 14);
462 cl_assert(git_path_root("//computername") == -1);
466 #define NON_EXISTING_FILEPATH "i_hope_i_do_not_exist"
468 void test_core_path__13_cannot_prettify_a_non_existing_file(void)
470 git_buf p
= GIT_BUF_INIT
;
472 cl_must_pass(git_path_exists(NON_EXISTING_FILEPATH
) == false);
473 cl_assert_equal_i(GIT_ENOTFOUND
, git_path_prettify(&p
, NON_EXISTING_FILEPATH
, NULL
));
474 cl_assert_equal_i(GIT_ENOTFOUND
, git_path_prettify(&p
, NON_EXISTING_FILEPATH
"/so-do-i", NULL
));
479 void test_core_path__14_apply_relative(void)
481 git_buf p
= GIT_BUF_INIT
;
483 cl_git_pass(git_buf_sets(&p
, "/this/is/a/base"));
485 cl_git_pass(git_path_apply_relative(&p
, "../test"));
486 cl_assert_equal_s("/this/is/a/test", p
.ptr
);
488 cl_git_pass(git_path_apply_relative(&p
, "../../the/./end"));
489 cl_assert_equal_s("/this/is/the/end", p
.ptr
);
491 cl_git_pass(git_path_apply_relative(&p
, "./of/this/../the/string"));
492 cl_assert_equal_s("/this/is/the/end/of/the/string", p
.ptr
);
494 cl_git_pass(git_path_apply_relative(&p
, "../../../../../.."));
495 cl_assert_equal_s("/this/", p
.ptr
);
497 cl_git_pass(git_path_apply_relative(&p
, "../"));
498 cl_assert_equal_s("/", p
.ptr
);
500 cl_git_fail(git_path_apply_relative(&p
, "../../.."));
503 cl_git_pass(git_buf_sets(&p
, "d:/another/test"));
505 cl_git_pass(git_path_apply_relative(&p
, "../.."));
506 cl_assert_equal_s("d:/", p
.ptr
);
508 cl_git_pass(git_path_apply_relative(&p
, "from/here/to/../and/./back/."));
509 cl_assert_equal_s("d:/from/here/and/back/", p
.ptr
);
512 cl_git_pass(git_buf_sets(&p
, "https://my.url.com/test.git"));
514 cl_git_pass(git_path_apply_relative(&p
, "../another.git"));
515 cl_assert_equal_s("https://my.url.com/another.git", p
.ptr
);
517 cl_git_pass(git_path_apply_relative(&p
, "../full/path/url.patch"));
518 cl_assert_equal_s("https://my.url.com/full/path/url.patch", p
.ptr
);
520 cl_git_pass(git_path_apply_relative(&p
, ".."));
521 cl_assert_equal_s("https://my.url.com/full/path/", p
.ptr
);
523 cl_git_pass(git_path_apply_relative(&p
, "../../../"));
524 cl_assert_equal_s("https://", p
.ptr
);
527 cl_git_pass(git_buf_sets(&p
, "../../this/is/relative"));
529 cl_git_pass(git_path_apply_relative(&p
, "../../preserves/the/prefix"));
530 cl_assert_equal_s("../../this/preserves/the/prefix", p
.ptr
);
532 cl_git_pass(git_path_apply_relative(&p
, "../../../../that"));
533 cl_assert_equal_s("../../that", p
.ptr
);
535 cl_git_pass(git_path_apply_relative(&p
, "../there"));
536 cl_assert_equal_s("../../there", p
.ptr
);
540 static void assert_resolve_relative(
541 git_buf
*buf
, const char *expected
, const char *path
)
543 cl_git_pass(git_buf_sets(buf
, path
));
544 cl_git_pass(git_path_resolve_relative(buf
, 0));
545 cl_assert_equal_s(expected
, buf
->ptr
);
548 void test_core_path__15_resolve_relative(void)
550 git_buf buf
= GIT_BUF_INIT
;
552 assert_resolve_relative(&buf
, "", "");
553 assert_resolve_relative(&buf
, "", ".");
554 assert_resolve_relative(&buf
, "", "./");
555 assert_resolve_relative(&buf
, "..", "..");
556 assert_resolve_relative(&buf
, "../", "../");
557 assert_resolve_relative(&buf
, "..", "./..");
558 assert_resolve_relative(&buf
, "../", "./../");
559 assert_resolve_relative(&buf
, "../", "../.");
560 assert_resolve_relative(&buf
, "../", ".././");
561 assert_resolve_relative(&buf
, "../..", "../..");
562 assert_resolve_relative(&buf
, "../../", "../../");
564 assert_resolve_relative(&buf
, "/", "/");
565 assert_resolve_relative(&buf
, "/", "/.");
567 assert_resolve_relative(&buf
, "", "a/..");
568 assert_resolve_relative(&buf
, "", "a/../");
569 assert_resolve_relative(&buf
, "", "a/../.");
571 assert_resolve_relative(&buf
, "/a", "/a");
572 assert_resolve_relative(&buf
, "/a/", "/a/.");
573 assert_resolve_relative(&buf
, "/", "/a/../");
574 assert_resolve_relative(&buf
, "/", "/a/../.");
575 assert_resolve_relative(&buf
, "/", "/a/.././");
577 assert_resolve_relative(&buf
, "a", "a");
578 assert_resolve_relative(&buf
, "a/", "a/");
579 assert_resolve_relative(&buf
, "a/", "a/.");
580 assert_resolve_relative(&buf
, "a/", "a/./");
582 assert_resolve_relative(&buf
, "a/b", "a//b");
583 assert_resolve_relative(&buf
, "a/b/c", "a/b/c");
584 assert_resolve_relative(&buf
, "b/c", "./b/c");
585 assert_resolve_relative(&buf
, "a/c", "a/./c");
586 assert_resolve_relative(&buf
, "a/b/", "a/b/.");
588 assert_resolve_relative(&buf
, "/a/b/c", "///a/b/c");
589 assert_resolve_relative(&buf
, "/", "////");
590 assert_resolve_relative(&buf
, "/a", "///a");
591 assert_resolve_relative(&buf
, "/", "///.");
592 assert_resolve_relative(&buf
, "/", "///a/..");
594 assert_resolve_relative(&buf
, "../../path", "../../test//../././path");
595 assert_resolve_relative(&buf
, "../d", "a/b/../../../c/../d");
597 cl_git_pass(git_buf_sets(&buf
, "/.."));
598 cl_git_fail(git_path_resolve_relative(&buf
, 0));
600 cl_git_pass(git_buf_sets(&buf
, "/./.."));
601 cl_git_fail(git_path_resolve_relative(&buf
, 0));
603 cl_git_pass(git_buf_sets(&buf
, "/.//.."));
604 cl_git_fail(git_path_resolve_relative(&buf
, 0));
606 cl_git_pass(git_buf_sets(&buf
, "/../."));
607 cl_git_fail(git_path_resolve_relative(&buf
, 0));
609 cl_git_pass(git_buf_sets(&buf
, "/../.././../a"));
610 cl_git_fail(git_path_resolve_relative(&buf
, 0));
612 cl_git_pass(git_buf_sets(&buf
, "////.."));
613 cl_git_fail(git_path_resolve_relative(&buf
, 0));
615 /* things that start with Windows network paths */
617 assert_resolve_relative(&buf
, "//a/b/c", "//a/b/c");
618 assert_resolve_relative(&buf
, "//a/", "//a/b/..");
619 assert_resolve_relative(&buf
, "//a/b/c", "//a/Q/../b/x/y/../../c");
621 cl_git_pass(git_buf_sets(&buf
, "//a/b/../.."));
622 cl_git_fail(git_path_resolve_relative(&buf
, 0));
624 assert_resolve_relative(&buf
, "/a/b/c", "//a/b/c");
625 assert_resolve_relative(&buf
, "/a/", "//a/b/..");
626 assert_resolve_relative(&buf
, "/a/b/c", "//a/Q/../b/x/y/../../c");
627 assert_resolve_relative(&buf
, "/", "//a/b/../..");