]>
Commit | Line | Data |
---|---|---|
3fd1520c | 1 | #include "clar_libgit2.h" |
618b7689 | 2 | #include "fileops.h" |
f1558d9b VM |
3 | |
4 | static void | |
5 | check_dirname(const char *A, const char *B) | |
6 | { | |
97769280 RB |
7 | git_buf dir = GIT_BUF_INIT; |
8 | char *dir2; | |
f1558d9b | 9 | |
97769280 | 10 | cl_assert(git_path_dirname_r(&dir, A) >= 0); |
1a6e8f8a | 11 | cl_assert_equal_s(B, dir.ptr); |
97769280 | 12 | git_buf_free(&dir); |
f1558d9b | 13 | |
97769280 | 14 | cl_assert((dir2 = git_path_dirname(A)) != NULL); |
1a6e8f8a | 15 | cl_assert_equal_s(B, dir2); |
3286c408 | 16 | git__free(dir2); |
f1558d9b VM |
17 | } |
18 | ||
19 | static void | |
20 | check_basename(const char *A, const char *B) | |
21 | { | |
97769280 RB |
22 | git_buf base = GIT_BUF_INIT; |
23 | char *base2; | |
f1558d9b | 24 | |
97769280 | 25 | cl_assert(git_path_basename_r(&base, A) >= 0); |
1a6e8f8a | 26 | cl_assert_equal_s(B, base.ptr); |
97769280 | 27 | git_buf_free(&base); |
f1558d9b | 28 | |
97769280 | 29 | cl_assert((base2 = git_path_basename(A)) != NULL); |
1a6e8f8a | 30 | cl_assert_equal_s(B, base2); |
3286c408 | 31 | git__free(base2); |
f1558d9b VM |
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); | |
1a6e8f8a | 40 | cl_assert_equal_s(B, dir); |
f1558d9b VM |
41 | } |
42 | ||
43 | static void | |
44 | check_joinpath(const char *path_a, const char *path_b, const char *expected_path) | |
45 | { | |
97769280 | 46 | git_buf joined_path = GIT_BUF_INIT; |
f1558d9b | 47 | |
97769280 | 48 | cl_git_pass(git_buf_joinpath(&joined_path, path_a, path_b)); |
1a6e8f8a | 49 | cl_assert_equal_s(expected_path, joined_path.ptr); |
97769280 RB |
50 | |
51 | git_buf_free(&joined_path); | |
f1558d9b VM |
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 | { | |
97769280 RB |
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)); | |
1a6e8f8a | 66 | cl_assert_equal_s(expected_path, joined_path.ptr); |
f1558d9b | 67 | |
97769280 | 68 | git_buf_free(&joined_path); |
f1558d9b VM |
69 | } |
70 | ||
71 | ||
72 | /* get the dirname of a path */ | |
2017a15d | 73 | void test_core_path__00_dirname(void) |
f1558d9b | 74 | { |
f1558d9b VM |
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/", "."); | |
97769280 RB |
88 | |
89 | check_dirname(REP16("/abc"), REP15("/abc")); | |
34b6f05f | 90 | |
91 | #ifdef GIT_WIN32 | |
92 | check_dirname("C:/path/", "C:/"); | |
93 | check_dirname("C:/path", "C:/"); | |
50a762a5 | 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"); | |
34b6f05f | 98 | #endif |
f1558d9b VM |
99 | } |
100 | ||
101 | /* get the base name of a path */ | |
2017a15d | 102 | void test_core_path__01_basename(void) |
f1558d9b VM |
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"); | |
97769280 RB |
113 | |
114 | check_basename(REP16("/abc"), "abc"); | |
115 | check_basename(REP1024("/abc"), "abc"); | |
f1558d9b VM |
116 | } |
117 | ||
118 | /* get the latest component in a path */ | |
2017a15d | 119 | void test_core_path__02_topdir(void) |
f1558d9b VM |
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 */ | |
2017a15d | 136 | void test_core_path__05_joins(void) |
f1558d9b VM |
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/"); | |
97769280 RB |
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 | ||
b5daae68 RB |
160 | check_joinpath(REP1024("aaaa"), "", REP1024("aaaa") "/"); |
161 | check_joinpath(REP1024("aaaa/"), "", REP1024("aaaa/")); | |
162 | check_joinpath(REP1024("/aaaa"), "", REP1024("/aaaa") "/"); | |
163 | ||
97769280 RB |
164 | check_joinpath(REP1024("aaaa"), REP1024("bbbb"), |
165 | REP1024("aaaa") "/" REP1024("bbbb")); | |
166 | check_joinpath(REP1024("/aaaa"), REP1024("/bbbb"), | |
167 | REP1024("/aaaa") REP1024("/bbbb")); | |
f1558d9b VM |
168 | } |
169 | ||
170 | /* properly join path components for more than one path */ | |
2017a15d | 171 | void test_core_path__06_long_joins(void) |
f1558d9b VM |
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"); | |
97769280 RB |
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)); | |
1a6e8f8a | 201 | cl_assert_equal_s(expected, tgt.ptr); |
97769280 RB |
202 | |
203 | git_buf_free(&tgt); | |
204 | } | |
205 | ||
206 | static void | |
207 | check_string_to_dir( | |
208 | const char* path, | |
1a6e8f8a | 209 | size_t maxlen, |
97769280 RB |
210 | const char* expected) |
211 | { | |
1a6e8f8a | 212 | size_t len = strlen(path); |
97769280 | 213 | char *buf = git__malloc(len + 2); |
1a6e8f8a RB |
214 | cl_assert(buf); |
215 | ||
97769280 RB |
216 | strncpy(buf, path, len + 2); |
217 | ||
218 | git_path_string_to_dir(buf, maxlen); | |
219 | ||
1a6e8f8a | 220 | cl_assert_equal_s(expected, buf); |
97769280 RB |
221 | |
222 | git__free(buf); | |
223 | } | |
224 | ||
225 | /* convert paths to dirs */ | |
2017a15d | 226 | void test_core_path__07_path_to_dir(void) |
97769280 RB |
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/"); | |
f1558d9b | 251 | } |
b5daae68 RB |
252 | |
253 | /* join path to itself */ | |
2017a15d | 254 | void test_core_path__08_self_join(void) |
b5daae68 RB |
255 | { |
256 | git_buf path = GIT_BUF_INIT; | |
13224ea4 | 257 | size_t asize = 0; |
b5daae68 RB |
258 | |
259 | asize = path.asize; | |
260 | cl_git_pass(git_buf_sets(&path, "/foo")); | |
1a6e8f8a | 261 | cl_assert_equal_s(path.ptr, "/foo"); |
b5daae68 RB |
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")); | |
1a6e8f8a | 266 | cl_assert_equal_s(path.ptr, "/foo/this is a new string"); |
b5daae68 RB |
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")); | |
1a6e8f8a | 271 | cl_assert_equal_s(path.ptr, "/foo/this is a new string/grow the buffer, grow the buffer, grow the buffer"); |
b5daae68 RB |
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")); | |
1a6e8f8a | 278 | cl_assert_equal_s(path.ptr, "/bar/baz"); |
b5daae68 RB |
279 | |
280 | asize = path.asize; | |
281 | cl_git_pass(git_buf_joinpath(&path, path.ptr + 4, "somethinglongenoughtorealloc")); | |
1a6e8f8a | 282 | cl_assert_equal_s(path.ptr, "/baz/somethinglongenoughtorealloc"); |
b5daae68 RB |
283 | cl_assert(asize < path.asize); |
284 | ||
285 | git_buf_free(&path); | |
286 | } | |
459e2dcd | 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)); | |
1a6e8f8a | 293 | cl_assert_equal_s(expected_result, git_buf_cstr(&buf)); |
459e2dcd | 294 | |
295 | git_buf_free(&buf); | |
296 | } | |
297 | ||
2017a15d | 298 | void test_core_path__09_percent_decode(void) |
459e2dcd | 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 | } | |
2017a15d | 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)); | |
1a6e8f8a | 320 | cl_assert_equal_s(expected_result, git_buf_cstr(&buf)); |
2017a15d | 321 | } else |
322 | cl_git_fail(git_path_fromurl(&buf, input)); | |
323 | ||
324 | git_buf_free(&buf); | |
325 | } | |
326 | ||
8c29dca6 | 327 | #ifdef GIT_WIN32 |
2017a15d | 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 | } | |
df743c7d | 350 | |
0cfcff5d RB |
351 | typedef struct { |
352 | int expect_idx; | |
dab89f9b | 353 | int cancel_after; |
0cfcff5d RB |
354 | char **expect; |
355 | } check_walkup_info; | |
356 | ||
25e0b157 RB |
357 | #define CANCEL_VALUE 1234 |
358 | ||
0cfcff5d RB |
359 | static int check_one_walkup_step(void *ref, git_buf *path) |
360 | { | |
361 | check_walkup_info *info = (check_walkup_info *)ref; | |
dab89f9b RB |
362 | |
363 | if (!info->cancel_after) { | |
364 | cl_assert_equal_s(info->expect[info->expect_idx], "[CANCEL]"); | |
25e0b157 | 365 | return CANCEL_VALUE; |
dab89f9b RB |
366 | } |
367 | info->cancel_after--; | |
368 | ||
0cfcff5d | 369 | cl_assert(info->expect[info->expect_idx] != NULL); |
1a6e8f8a | 370 | cl_assert_equal_s(info->expect[info->expect_idx], path->ptr); |
0cfcff5d | 371 | info->expect_idx++; |
dab89f9b | 372 | |
0d0fa7c3 | 373 | return 0; |
0cfcff5d RB |
374 | } |
375 | ||
df743c7d RB |
376 | void test_core_path__11_walkup(void) |
377 | { | |
0cfcff5d | 378 | git_buf p = GIT_BUF_INIT; |
df743c7d RB |
379 | char *expect[] = { |
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, | |
388 | NULL | |
389 | }; | |
390 | char *root[] = { NULL, NULL, "/", "", "/a/b", "/a/b/", NULL, NULL, NULL }; | |
391 | int i, j; | |
0cfcff5d RB |
392 | check_walkup_info info; |
393 | ||
394 | info.expect = expect; | |
dab89f9b | 395 | info.cancel_after = -1; |
df743c7d RB |
396 | |
397 | for (i = 0, j = 0; expect[i] != NULL; i++, j++) { | |
df743c7d RB |
398 | |
399 | git_buf_sets(&p, expect[i]); | |
400 | ||
0cfcff5d RB |
401 | info.expect_idx = i; |
402 | cl_git_pass( | |
403 | git_path_walk_up(&p, root[j], check_one_walkup_step, &info) | |
404 | ); | |
df743c7d | 405 | |
1a6e8f8a | 406 | cl_assert_equal_s(p.ptr, expect[i]); |
df743c7d | 407 | |
dab89f9b RB |
408 | /* skip to next run of expectations */ |
409 | while (expect[i] != NULL) i++; | |
410 | } | |
411 | ||
412 | git_buf_free(&p); | |
413 | } | |
414 | ||
415 | void test_core_path__11a_walkup_cancel(void) | |
416 | { | |
417 | git_buf p = GIT_BUF_INIT; | |
418 | int cancel[] = { 3, 2, 1, 0 }; | |
419 | char *expect[] = { | |
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, | |
423 | "[CANCEL]", NULL, | |
424 | NULL | |
425 | }; | |
426 | char *root[] = { NULL, NULL, "/", "", NULL }; | |
427 | int i, j; | |
428 | check_walkup_info info; | |
429 | ||
430 | info.expect = expect; | |
431 | ||
432 | for (i = 0, j = 0; expect[i] != NULL; i++, j++) { | |
433 | ||
434 | git_buf_sets(&p, expect[i]); | |
435 | ||
436 | info.cancel_after = cancel[j]; | |
437 | info.expect_idx = i; | |
438 | ||
439 | cl_assert_equal_i( | |
25e0b157 | 440 | CANCEL_VALUE, |
dab89f9b RB |
441 | git_path_walk_up(&p, root[j], check_one_walkup_step, &info) |
442 | ); | |
443 | ||
df743c7d RB |
444 | /* skip to next run of expectations */ |
445 | while (expect[i] != NULL) i++; | |
446 | } | |
447 | ||
448 | git_buf_free(&p); | |
449 | } | |
7b93079b | 450 | |
451 | void test_core_path__12_offset_to_path_root(void) | |
452 | { | |
453 | cl_assert(git_path_root("non/rooted/path") == -1); | |
454 | cl_assert(git_path_root("/rooted/path") == 0); | |
455 | ||
456 | #ifdef GIT_WIN32 | |
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); | |
463 | #endif | |
464 | } | |
9abb5bca | 465 | |
466 | #define NON_EXISTING_FILEPATH "i_hope_i_do_not_exist" | |
467 | ||
468 | void test_core_path__13_cannot_prettify_a_non_existing_file(void) | |
469 | { | |
470 | git_buf p = GIT_BUF_INIT; | |
471 | ||
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)); | |
475 | ||
476 | git_buf_free(&p); | |
477 | } | |
b0fe1129 RB |
478 | |
479 | void test_core_path__14_apply_relative(void) | |
480 | { | |
481 | git_buf p = GIT_BUF_INIT; | |
482 | ||
483 | cl_git_pass(git_buf_sets(&p, "/this/is/a/base")); | |
484 | ||
485 | cl_git_pass(git_path_apply_relative(&p, "../test")); | |
486 | cl_assert_equal_s("/this/is/a/test", p.ptr); | |
487 | ||
488 | cl_git_pass(git_path_apply_relative(&p, "../../the/./end")); | |
489 | cl_assert_equal_s("/this/is/the/end", p.ptr); | |
490 | ||
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); | |
493 | ||
494 | cl_git_pass(git_path_apply_relative(&p, "../../../../../..")); | |
495 | cl_assert_equal_s("/this/", p.ptr); | |
496 | ||
6d9a6c5c | 497 | cl_git_pass(git_path_apply_relative(&p, "../")); |
b0fe1129 RB |
498 | cl_assert_equal_s("/", p.ptr); |
499 | ||
6d9a6c5c | 500 | cl_git_fail(git_path_apply_relative(&p, "../../..")); |
b0fe1129 RB |
501 | |
502 | ||
503 | cl_git_pass(git_buf_sets(&p, "d:/another/test")); | |
504 | ||
6d9a6c5c | 505 | cl_git_pass(git_path_apply_relative(&p, "../..")); |
b0fe1129 RB |
506 | cl_assert_equal_s("d:/", p.ptr); |
507 | ||
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); | |
510 | ||
511 | ||
512 | cl_git_pass(git_buf_sets(&p, "https://my.url.com/test.git")); | |
513 | ||
514 | cl_git_pass(git_path_apply_relative(&p, "../another.git")); | |
515 | cl_assert_equal_s("https://my.url.com/another.git", p.ptr); | |
516 | ||
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); | |
519 | ||
520 | cl_git_pass(git_path_apply_relative(&p, "..")); | |
521 | cl_assert_equal_s("https://my.url.com/full/path/", p.ptr); | |
522 | ||
6d9a6c5c | 523 | cl_git_pass(git_path_apply_relative(&p, "../../../")); |
b0fe1129 RB |
524 | cl_assert_equal_s("https://", p.ptr); |
525 | ||
6d9a6c5c NV |
526 | |
527 | cl_git_pass(git_buf_sets(&p, "../../this/is/relative")); | |
528 | ||
529 | cl_git_pass(git_path_apply_relative(&p, "../../preserves/the/prefix")); | |
530 | cl_assert_equal_s("../../this/preserves/the/prefix", p.ptr); | |
531 | ||
532 | cl_git_pass(git_path_apply_relative(&p, "../../../../that")); | |
533 | cl_assert_equal_s("../../that", p.ptr); | |
534 | ||
535 | cl_git_pass(git_path_apply_relative(&p, "../there")); | |
536 | cl_assert_equal_s("../../there", p.ptr); | |
b0fe1129 RB |
537 | git_buf_free(&p); |
538 | } | |
6d9a6c5c | 539 | |
0d1af399 RB |
540 | static void assert_resolve_relative( |
541 | git_buf *buf, const char *expected, const char *path) | |
6d9a6c5c NV |
542 | { |
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); | |
546 | } | |
547 | ||
548 | void test_core_path__15_resolve_relative(void) | |
549 | { | |
550 | git_buf buf = GIT_BUF_INIT; | |
551 | ||
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, "../../", "../../"); | |
563 | ||
564 | assert_resolve_relative(&buf, "/", "/"); | |
565 | assert_resolve_relative(&buf, "/", "/."); | |
566 | ||
567 | assert_resolve_relative(&buf, "", "a/.."); | |
568 | assert_resolve_relative(&buf, "", "a/../"); | |
569 | assert_resolve_relative(&buf, "", "a/../."); | |
570 | ||
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/.././"); | |
576 | ||
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/./"); | |
581 | ||
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/."); | |
587 | ||
588 | assert_resolve_relative(&buf, "/a/b/c", "///a/b/c"); | |
6d9a6c5c NV |
589 | assert_resolve_relative(&buf, "/", "////"); |
590 | assert_resolve_relative(&buf, "/a", "///a"); | |
591 | assert_resolve_relative(&buf, "/", "///."); | |
592 | assert_resolve_relative(&buf, "/", "///a/.."); | |
593 | ||
594 | assert_resolve_relative(&buf, "../../path", "../../test//../././path"); | |
595 | assert_resolve_relative(&buf, "../d", "a/b/../../../c/../d"); | |
596 | ||
597 | cl_git_pass(git_buf_sets(&buf, "/..")); | |
598 | cl_git_fail(git_path_resolve_relative(&buf, 0)); | |
599 | ||
600 | cl_git_pass(git_buf_sets(&buf, "/./..")); | |
601 | cl_git_fail(git_path_resolve_relative(&buf, 0)); | |
602 | ||
603 | cl_git_pass(git_buf_sets(&buf, "/.//..")); | |
604 | cl_git_fail(git_path_resolve_relative(&buf, 0)); | |
605 | ||
606 | cl_git_pass(git_buf_sets(&buf, "/../.")); | |
607 | cl_git_fail(git_path_resolve_relative(&buf, 0)); | |
608 | ||
609 | cl_git_pass(git_buf_sets(&buf, "/../.././../a")); | |
610 | cl_git_fail(git_path_resolve_relative(&buf, 0)); | |
611 | ||
612 | cl_git_pass(git_buf_sets(&buf, "////..")); | |
613 | cl_git_fail(git_path_resolve_relative(&buf, 0)); | |
614 | ||
cae52938 RB |
615 | /* things that start with Windows network paths */ |
616 | #ifdef GIT_WIN32 | |
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"); | |
620 | ||
621 | cl_git_pass(git_buf_sets(&buf, "//a/b/../..")); | |
622 | cl_git_fail(git_path_resolve_relative(&buf, 0)); | |
623 | #else | |
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/../.."); | |
628 | #endif | |
629 | ||
6d9a6c5c NV |
630 | git_buf_free(&buf); |
631 | } |