Fourth batch
[git/raj.git] / t / t0060-path-utils.sh
blob56db5c8abab62e7ae327bece57768f0f4ae6f0e4
1 #!/bin/sh
3 # Copyright (c) 2008 David Reiss
6 test_description='Test various path utilities'
8 . ./test-lib.sh
10 norm_path() {
11 expected=$(test-tool path-utils print_path "$2")
12 test_expect_success $3 "normalize path: $1 => $2" \
13 "test \"\$(test-tool path-utils normalize_path_copy '$1')\" = '$expected'"
16 relative_path() {
17 expected=$(test-tool path-utils print_path "$3")
18 test_expect_success $4 "relative path: $1 $2 => $3" \
19 "test \"\$(test-tool path-utils relative_path '$1' '$2')\" = '$expected'"
22 test_submodule_relative_url() {
23 test_expect_success "test_submodule_relative_url: $1 $2 $3 => $4" "
24 actual=\$(git submodule--helper resolve-relative-url-test '$1' '$2' '$3') &&
25 test \"\$actual\" = '$4'
29 test_git_path() {
30 test_expect_success "git-path $1 $2 => $3" "
31 $1 git rev-parse --git-path $2 >actual &&
32 echo $3 >expect &&
33 test_cmp expect actual
37 # On Windows, we are using MSYS's bash, which mangles the paths.
38 # Absolute paths are anchored at the MSYS installation directory,
39 # which means that the path / accounts for this many characters:
40 rootoff=$(test-tool path-utils normalize_path_copy / | wc -c)
41 # Account for the trailing LF:
42 if test $rootoff = 2; then
43 rootoff= # we are on Unix
44 else
45 rootoff=$(($rootoff-1))
46 # In MSYS2, the root directory "/" is translated into a Windows
47 # directory *with* trailing slash. Let's test for that and adjust
48 # our expected longest ancestor length accordingly.
49 case "$(test-tool path-utils print_path /)" in
50 */) rootslash=1;;
51 *) rootslash=0;;
52 esac
55 ancestor() {
56 # We do some math with the expected ancestor length.
57 expected=$3
58 if test -n "$rootoff" && test "x$expected" != x-1; then
59 expected=$(($expected-$rootslash))
60 test $expected -lt 0 ||
61 expected=$(($expected+$rootoff))
63 test_expect_success "longest ancestor: $1 $2 => $expected" \
64 "actual=\$(test-tool path-utils longest_ancestor_length '$1' '$2') &&
65 test \"\$actual\" = '$expected'"
68 # Some absolute path tests should be skipped on Windows due to path mangling
69 # on POSIX-style absolute paths
70 case $(uname -s) in
71 *MINGW*)
73 *CYGWIN*)
76 test_set_prereq POSIX
78 esac
80 test_expect_success basename 'test-tool path-utils basename'
81 test_expect_success dirname 'test-tool path-utils dirname'
83 norm_path "" ""
84 norm_path . ""
85 norm_path ./ ""
86 norm_path ./. ""
87 norm_path ./.. ++failed++
88 norm_path ../. ++failed++
89 norm_path ./../.// ++failed++
90 norm_path dir/.. ""
91 norm_path dir/sub/../.. ""
92 norm_path dir/sub/../../.. ++failed++
93 norm_path dir dir
94 norm_path dir// dir/
95 norm_path ./dir dir
96 norm_path dir/. dir/
97 norm_path dir///./ dir/
98 norm_path dir//sub/.. dir/
99 norm_path dir/sub/../ dir/
100 norm_path dir/sub/../. dir/
101 norm_path dir/s1/../s2/ dir/s2/
102 norm_path d1/s1///s2/..//../s3/ d1/s3/
103 norm_path d1/s1//../s2/../../d2 d2
104 norm_path d1/.../d2 d1/.../d2
105 norm_path d1/..././../d2 d1/d2
107 norm_path / /
108 norm_path // / POSIX
109 norm_path /// / POSIX
110 norm_path /. /
111 norm_path /./ / POSIX
112 norm_path /./.. ++failed++ POSIX
113 norm_path /../. ++failed++
114 norm_path /./../.// ++failed++ POSIX
115 norm_path /dir/.. / POSIX
116 norm_path /dir/sub/../.. / POSIX
117 norm_path /dir/sub/../../.. ++failed++ POSIX
118 norm_path /dir /dir
119 norm_path /dir// /dir/
120 norm_path /./dir /dir
121 norm_path /dir/. /dir/
122 norm_path /dir///./ /dir/
123 norm_path /dir//sub/.. /dir/
124 norm_path /dir/sub/../ /dir/
125 norm_path //dir/sub/../. /dir/ POSIX
126 norm_path /dir/s1/../s2/ /dir/s2/
127 norm_path /d1/s1///s2/..//../s3/ /d1/s3/
128 norm_path /d1/s1//../s2/../../d2 /d2
129 norm_path /d1/.../d2 /d1/.../d2
130 norm_path /d1/..././../d2 /d1/d2
132 ancestor / / -1
133 ancestor /foo / 0
134 ancestor /foo /fo -1
135 ancestor /foo /foo -1
136 ancestor /foo /bar -1
137 ancestor /foo /foo/bar -1
138 ancestor /foo /foo:/bar -1
139 ancestor /foo /:/foo:/bar 0
140 ancestor /foo /foo:/:/bar 0
141 ancestor /foo /:/bar:/foo 0
142 ancestor /foo/bar / 0
143 ancestor /foo/bar /fo -1
144 ancestor /foo/bar /foo 4
145 ancestor /foo/bar /foo/ba -1
146 ancestor /foo/bar /:/fo 0
147 ancestor /foo/bar /foo:/foo/ba 4
148 ancestor /foo/bar /bar -1
149 ancestor /foo/bar /fo -1
150 ancestor /foo/bar /foo:/bar 4
151 ancestor /foo/bar /:/foo:/bar 4
152 ancestor /foo/bar /foo:/:/bar 4
153 ancestor /foo/bar /:/bar:/fo 0
154 ancestor /foo/bar /:/bar 0
155 ancestor /foo/bar /foo 4
156 ancestor /foo/bar /foo:/bar 4
157 ancestor /foo/bar /bar -1
159 test_expect_success 'strip_path_suffix' '
160 test c:/msysgit = $(test-tool path-utils strip_path_suffix \
161 c:/msysgit/libexec//git-core libexec/git-core)
164 test_expect_success 'absolute path rejects the empty string' '
165 test_must_fail test-tool path-utils absolute_path ""
168 test_expect_success MINGW '<drive-letter>:\\abc is an absolute path' '
169 for letter in : \" C Z 1 ä
171 path=$letter:\\abc &&
172 absolute="$(test-tool path-utils absolute_path "$path")" &&
173 test "$path" = "$absolute" || return 1
174 done
177 test_expect_success 'real path rejects the empty string' '
178 test_must_fail test-tool path-utils real_path ""
181 test_expect_success POSIX 'real path works on absolute paths 1' '
182 nopath="hopefully-absent-path" &&
183 test "/" = "$(test-tool path-utils real_path "/")" &&
184 test "/$nopath" = "$(test-tool path-utils real_path "/$nopath")"
187 test_expect_success 'real path works on absolute paths 2' '
188 nopath="hopefully-absent-path" &&
189 # Find an existing top-level directory for the remaining tests:
190 d=$(pwd -P | sed -e "s|^\([^/]*/[^/]*\)/.*|\1|") &&
191 test "$d" = "$(test-tool path-utils real_path "$d")" &&
192 test "$d/$nopath" = "$(test-tool path-utils real_path "$d/$nopath")"
195 test_expect_success POSIX 'real path removes extra leading slashes' '
196 nopath="hopefully-absent-path" &&
197 test "/" = "$(test-tool path-utils real_path "///")" &&
198 test "/$nopath" = "$(test-tool path-utils real_path "///$nopath")" &&
199 # Find an existing top-level directory for the remaining tests:
200 d=$(pwd -P | sed -e "s|^\([^/]*/[^/]*\)/.*|\1|") &&
201 test "$d" = "$(test-tool path-utils real_path "//$d")" &&
202 test "$d/$nopath" = "$(test-tool path-utils real_path "//$d/$nopath")"
205 test_expect_success 'real path removes other extra slashes' '
206 nopath="hopefully-absent-path" &&
207 # Find an existing top-level directory for the remaining tests:
208 d=$(pwd -P | sed -e "s|^\([^/]*/[^/]*\)/.*|\1|") &&
209 test "$d" = "$(test-tool path-utils real_path "$d///")" &&
210 test "$d/$nopath" = "$(test-tool path-utils real_path "$d///$nopath")"
213 test_expect_success SYMLINKS 'real path works on symlinks' '
214 mkdir first &&
215 ln -s ../.git first/.git &&
216 mkdir second &&
217 ln -s ../first second/other &&
218 mkdir third &&
219 dir="$(cd .git; pwd -P)" &&
220 dir2=third/../second/other/.git &&
221 test "$dir" = "$(test-tool path-utils real_path $dir2)" &&
222 file="$dir"/index &&
223 test "$file" = "$(test-tool path-utils real_path $dir2/index)" &&
224 basename=blub &&
225 test "$dir/$basename" = "$(cd .git && test-tool path-utils real_path "$basename")" &&
226 ln -s ../first/file .git/syml &&
227 sym="$(cd first; pwd -P)"/file &&
228 test "$sym" = "$(test-tool path-utils real_path "$dir2/syml")"
231 test_expect_success SYMLINKS 'prefix_path works with absolute paths to work tree symlinks' '
232 ln -s target symlink &&
233 test "$(test-tool path-utils prefix_path prefix "$(pwd)/symlink")" = "symlink"
236 test_expect_success 'prefix_path works with only absolute path to work tree' '
237 echo "" >expected &&
238 test-tool path-utils prefix_path prefix "$(pwd)" >actual &&
239 test_cmp expected actual
242 test_expect_success 'prefix_path rejects absolute path to dir with same beginning as work tree' '
243 test_must_fail test-tool path-utils prefix_path prefix "$(pwd)a"
246 test_expect_success SYMLINKS 'prefix_path works with absolute path to a symlink to work tree having same beginning as work tree' '
247 git init repo &&
248 ln -s repo repolink &&
249 test "a" = "$(cd repo && test-tool path-utils prefix_path prefix "$(pwd)/../repolink/a")"
252 relative_path /foo/a/b/c/ /foo/a/b/ c/
253 relative_path /foo/a/b/c/ /foo/a/b c/
254 relative_path /foo/a//b//c/ ///foo/a/b// c/ POSIX
255 relative_path /foo/a/b /foo/a/b ./
256 relative_path /foo/a/b/ /foo/a/b ./
257 relative_path /foo/a /foo/a/b ../
258 relative_path / /foo/a/b/ ../../../
259 relative_path /foo/a/c /foo/a/b/ ../c
260 relative_path /foo/a/c /foo/a/b ../c
261 relative_path /foo/x/y /foo/a/b/ ../../x/y
262 relative_path /foo/a/b "<empty>" /foo/a/b
263 relative_path /foo/a/b "<null>" /foo/a/b
264 relative_path foo/a/b/c/ foo/a/b/ c/
265 relative_path foo/a/b/c/ foo/a/b c/
266 relative_path foo/a/b//c foo/a//b c
267 relative_path foo/a/b/ foo/a/b/ ./
268 relative_path foo/a/b/ foo/a/b ./
269 relative_path foo/a foo/a/b ../
270 relative_path foo/x/y foo/a/b ../../x/y
271 relative_path foo/a/c foo/a/b ../c
272 relative_path foo/a/b /foo/x/y foo/a/b
273 relative_path /foo/a/b foo/x/y /foo/a/b
274 relative_path d:/a/b D:/a/c ../b MINGW
275 relative_path C:/a/b D:/a/c C:/a/b MINGW
276 relative_path foo/a/b "<empty>" foo/a/b
277 relative_path foo/a/b "<null>" foo/a/b
278 relative_path "<empty>" /foo/a/b ./
279 relative_path "<empty>" "<empty>" ./
280 relative_path "<empty>" "<null>" ./
281 relative_path "<null>" "<empty>" ./
282 relative_path "<null>" "<null>" ./
283 relative_path "<null>" /foo/a/b ./
285 test_git_path A=B info/grafts .git/info/grafts
286 test_git_path GIT_GRAFT_FILE=foo info/grafts foo
287 test_git_path GIT_GRAFT_FILE=foo info/////grafts foo
288 test_git_path GIT_INDEX_FILE=foo index foo
289 test_git_path GIT_INDEX_FILE=foo index/foo .git/index/foo
290 test_git_path GIT_INDEX_FILE=foo index2 .git/index2
291 test_expect_success 'setup fake objects directory foo' 'mkdir foo'
292 test_git_path GIT_OBJECT_DIRECTORY=foo objects foo
293 test_git_path GIT_OBJECT_DIRECTORY=foo objects/foo foo/foo
294 test_git_path GIT_OBJECT_DIRECTORY=foo objects2 .git/objects2
295 test_expect_success 'setup common repository' 'git --git-dir=bar init'
296 test_git_path GIT_COMMON_DIR=bar index .git/index
297 test_git_path GIT_COMMON_DIR=bar index.lock .git/index.lock
298 test_git_path GIT_COMMON_DIR=bar HEAD .git/HEAD
299 test_git_path GIT_COMMON_DIR=bar logs/HEAD .git/logs/HEAD
300 test_git_path GIT_COMMON_DIR=bar logs/HEAD.lock .git/logs/HEAD.lock
301 test_git_path GIT_COMMON_DIR=bar logs/refs/bisect/foo .git/logs/refs/bisect/foo
302 test_git_path GIT_COMMON_DIR=bar logs/refs bar/logs/refs
303 test_git_path GIT_COMMON_DIR=bar logs/refs/ bar/logs/refs/
304 test_git_path GIT_COMMON_DIR=bar logs/refs/bisec/foo bar/logs/refs/bisec/foo
305 test_git_path GIT_COMMON_DIR=bar logs/refs/bisec bar/logs/refs/bisec
306 test_git_path GIT_COMMON_DIR=bar logs/refs/bisectfoo bar/logs/refs/bisectfoo
307 test_git_path GIT_COMMON_DIR=bar objects bar/objects
308 test_git_path GIT_COMMON_DIR=bar objects/bar bar/objects/bar
309 test_git_path GIT_COMMON_DIR=bar info/exclude bar/info/exclude
310 test_git_path GIT_COMMON_DIR=bar info/grafts bar/info/grafts
311 test_git_path GIT_COMMON_DIR=bar info/sparse-checkout .git/info/sparse-checkout
312 test_git_path GIT_COMMON_DIR=bar info//sparse-checkout .git/info//sparse-checkout
313 test_git_path GIT_COMMON_DIR=bar remotes/bar bar/remotes/bar
314 test_git_path GIT_COMMON_DIR=bar branches/bar bar/branches/bar
315 test_git_path GIT_COMMON_DIR=bar logs/refs/heads/master bar/logs/refs/heads/master
316 test_git_path GIT_COMMON_DIR=bar refs/heads/master bar/refs/heads/master
317 test_git_path GIT_COMMON_DIR=bar refs/bisect/foo .git/refs/bisect/foo
318 test_git_path GIT_COMMON_DIR=bar hooks/me bar/hooks/me
319 test_git_path GIT_COMMON_DIR=bar config bar/config
320 test_git_path GIT_COMMON_DIR=bar packed-refs bar/packed-refs
321 test_git_path GIT_COMMON_DIR=bar shallow bar/shallow
322 test_git_path GIT_COMMON_DIR=bar common bar/common
323 test_git_path GIT_COMMON_DIR=bar common/file bar/common/file
325 # In the tests below, $(pwd) must be used because it is a native path on
326 # Windows and avoids MSYS's path mangling (which simplifies "foo/../bar" and
327 # strips the dot from trailing "/.").
329 test_submodule_relative_url "../" "../foo" "../submodule" "../../submodule"
330 test_submodule_relative_url "../" "../foo/bar" "../submodule" "../../foo/submodule"
331 test_submodule_relative_url "../" "../foo/submodule" "../submodule" "../../foo/submodule"
332 test_submodule_relative_url "../" "./foo" "../submodule" "../submodule"
333 test_submodule_relative_url "../" "./foo/bar" "../submodule" "../foo/submodule"
334 test_submodule_relative_url "../../../" "../foo/bar" "../sub/a/b/c" "../../../../foo/sub/a/b/c"
335 test_submodule_relative_url "../" "$(pwd)/addtest" "../repo" "$(pwd)/repo"
336 test_submodule_relative_url "../" "foo/bar" "../submodule" "../foo/submodule"
337 test_submodule_relative_url "../" "foo" "../submodule" "../submodule"
339 test_submodule_relative_url "(null)" "../foo/bar" "../sub/a/b/c" "../foo/sub/a/b/c"
340 test_submodule_relative_url "(null)" "../foo/bar" "../sub/a/b/c/" "../foo/sub/a/b/c"
341 test_submodule_relative_url "(null)" "../foo/bar/" "../sub/a/b/c" "../foo/sub/a/b/c"
342 test_submodule_relative_url "(null)" "../foo/bar" "../submodule" "../foo/submodule"
343 test_submodule_relative_url "(null)" "../foo/submodule" "../submodule" "../foo/submodule"
344 test_submodule_relative_url "(null)" "../foo" "../submodule" "../submodule"
345 test_submodule_relative_url "(null)" "./foo/bar" "../submodule" "foo/submodule"
346 test_submodule_relative_url "(null)" "./foo" "../submodule" "submodule"
347 test_submodule_relative_url "(null)" "//somewhere else/repo" "../subrepo" "//somewhere else/subrepo"
348 test_submodule_relative_url "(null)" "//somewhere else/repo" "../../subrepo" "//subrepo"
349 test_submodule_relative_url "(null)" "//somewhere else/repo" "../../../subrepo" "/subrepo"
350 test_submodule_relative_url "(null)" "//somewhere else/repo" "../../../../subrepo" "subrepo"
351 test_submodule_relative_url "(null)" "$(pwd)/subsuper_update_r" "../subsubsuper_update_r" "$(pwd)/subsubsuper_update_r"
352 test_submodule_relative_url "(null)" "$(pwd)/super_update_r2" "../subsuper_update_r" "$(pwd)/subsuper_update_r"
353 test_submodule_relative_url "(null)" "$(pwd)/." "../." "$(pwd)/."
354 test_submodule_relative_url "(null)" "$(pwd)" "./." "$(pwd)/."
355 test_submodule_relative_url "(null)" "$(pwd)/addtest" "../repo" "$(pwd)/repo"
356 test_submodule_relative_url "(null)" "$(pwd)" "./å äö" "$(pwd)/å äö"
357 test_submodule_relative_url "(null)" "$(pwd)/." "../submodule" "$(pwd)/submodule"
358 test_submodule_relative_url "(null)" "$(pwd)/submodule" "../submodule" "$(pwd)/submodule"
359 test_submodule_relative_url "(null)" "$(pwd)/home2/../remote" "../bundle1" "$(pwd)/home2/../bundle1"
360 test_submodule_relative_url "(null)" "$(pwd)/submodule_update_repo" "./." "$(pwd)/submodule_update_repo/."
361 test_submodule_relative_url "(null)" "file:///tmp/repo" "../subrepo" "file:///tmp/subrepo"
362 test_submodule_relative_url "(null)" "foo/bar" "../submodule" "foo/submodule"
363 test_submodule_relative_url "(null)" "foo" "../submodule" "submodule"
364 test_submodule_relative_url "(null)" "helper:://hostname/repo" "../subrepo" "helper:://hostname/subrepo"
365 test_submodule_relative_url "(null)" "helper:://hostname/repo" "../../subrepo" "helper:://subrepo"
366 test_submodule_relative_url "(null)" "helper:://hostname/repo" "../../../subrepo" "helper::/subrepo"
367 test_submodule_relative_url "(null)" "helper:://hostname/repo" "../../../../subrepo" "helper::subrepo"
368 test_submodule_relative_url "(null)" "helper:://hostname/repo" "../../../../../subrepo" "helper:subrepo"
369 test_submodule_relative_url "(null)" "helper:://hostname/repo" "../../../../../../subrepo" ".:subrepo"
370 test_submodule_relative_url "(null)" "ssh://hostname/repo" "../subrepo" "ssh://hostname/subrepo"
371 test_submodule_relative_url "(null)" "ssh://hostname/repo" "../../subrepo" "ssh://subrepo"
372 test_submodule_relative_url "(null)" "ssh://hostname/repo" "../../../subrepo" "ssh:/subrepo"
373 test_submodule_relative_url "(null)" "ssh://hostname/repo" "../../../../subrepo" "ssh:subrepo"
374 test_submodule_relative_url "(null)" "ssh://hostname/repo" "../../../../../subrepo" ".:subrepo"
375 test_submodule_relative_url "(null)" "ssh://hostname:22/repo" "../subrepo" "ssh://hostname:22/subrepo"
376 test_submodule_relative_url "(null)" "user@host:path/to/repo" "../subrepo" "user@host:path/to/subrepo"
377 test_submodule_relative_url "(null)" "user@host:repo" "../subrepo" "user@host:subrepo"
378 test_submodule_relative_url "(null)" "user@host:repo" "../../subrepo" ".:subrepo"
380 test_expect_success 'match .gitmodules' '
381 test-tool path-utils is_dotgitmodules \
382 .gitmodules \
384 .git${u200c}modules \
386 .Gitmodules \
387 .gitmoduleS \
389 ".gitmodules " \
390 ".gitmodules." \
391 ".gitmodules " \
392 ".gitmodules. " \
393 ".gitmodules ." \
394 ".gitmodules.." \
395 ".gitmodules " \
396 ".gitmodules. " \
397 ".gitmodules . " \
398 ".gitmodules ." \
400 ".Gitmodules " \
401 ".Gitmodules." \
402 ".Gitmodules " \
403 ".Gitmodules. " \
404 ".Gitmodules ." \
405 ".Gitmodules.." \
406 ".Gitmodules " \
407 ".Gitmodules. " \
408 ".Gitmodules . " \
409 ".Gitmodules ." \
411 GITMOD~1 \
412 gitmod~1 \
413 GITMOD~2 \
414 gitmod~3 \
415 GITMOD~4 \
417 "GITMOD~1 " \
418 "gitmod~2." \
419 "GITMOD~3 " \
420 "gitmod~4. " \
421 "GITMOD~1 ." \
422 "gitmod~2 " \
423 "GITMOD~3. " \
424 "gitmod~4 . " \
426 GI7EBA~1 \
427 gi7eba~9 \
429 GI7EB~10 \
430 GI7EB~11 \
431 GI7EB~99 \
432 GI7EB~10 \
433 GI7E~100 \
434 GI7E~101 \
435 GI7E~999 \
436 ~1000000 \
437 ~9999999 \
439 .gitmodules:\$DATA \
440 "gitmod~4 . :\$DATA" \
442 --not \
443 ".gitmodules x" \
444 ".gitmodules .x" \
446 " .gitmodules" \
448 ..gitmodules \
450 gitmodules \
452 .gitmodule \
454 ".gitmodules x " \
455 ".gitmodules .x" \
457 GI7EBA~ \
458 GI7EBA~0 \
459 GI7EBA~~1 \
460 GI7EBA~X \
461 Gx7EBA~1 \
462 GI7EBX~1 \
464 GI7EB~1 \
465 GI7EB~01 \
466 GI7EB~1X \
468 .gitmodules,:\$DATA
471 test_expect_success MINGW 'is_valid_path() on Windows' '
472 test-tool path-utils is_valid_path \
473 win32 \
474 "win32 x" \
475 ../hello.txt \
476 C:\\git \
477 comm \
478 conout.c \
479 com0.c \
480 lptN \
482 --not \
483 "win32 " \
484 "win32 /x " \
485 "win32." \
486 "win32 . ." \
487 .../hello.txt \
488 colon:test \
489 "AUX.c" \
490 "abc/conOut\$ .xyz/test" \
491 lpt8 \
492 com9.c \
493 "lpt*" \
494 Nul \
495 "PRN./abc"
498 test_done