3 # Copyright (c) 2008 David Reiss
6 test_description
='Test various path utilities'
8 TEST_PASSES_SANITIZE_LEAK
=true
12 expected
=$
(test-tool path-utils print_path
"$2")
13 test_expect_success
$3 "normalize path: $1 => $2" "
14 echo '$expected' >expect &&
15 test-tool path-utils normalize_path_copy '$1' >actual &&
16 test_cmp expect actual
21 expected
=$
(test-tool path-utils print_path
"$3")
22 test_expect_success
$4 "relative path: $1 $2 => $3" "
23 echo '$expected' >expect &&
24 test-tool path-utils relative_path '$1' '$2' >actual &&
25 test_cmp expect actual
29 test_submodule_relative_url
() {
30 test_expect_success
"test_submodule_relative_url: $1 $2 $3 => $4" "
32 test-tool submodule resolve-relative-url '$1' '$2' '$3' >actual &&
33 test_cmp expect actual
38 test_expect_success
"git-path $1 $2 => $3" "
39 $1 git rev-parse --git-path $2 >actual &&
41 test_cmp expect actual
45 # On Windows, we are using MSYS's bash, which mangles the paths.
46 # Absolute paths are anchored at the MSYS installation directory,
47 # which means that the path / accounts for this many characters:
48 rootoff
=$
(test-tool path-utils normalize_path_copy
/ |
wc -c)
49 # Account for the trailing LF:
50 if test $rootoff = 2; then
51 rootoff
= # we are on Unix
53 rootoff
=$
(($rootoff-1))
54 # In MSYS2, the root directory "/" is translated into a Windows
55 # directory *with* trailing slash. Let's test for that and adjust
56 # our expected longest ancestor length accordingly.
57 case "$(test-tool path-utils print_path /)" in
64 # We do some math with the expected ancestor length.
66 case "$rootoff,$expected,$2" in
67 *,*,//*) ;; # leave UNC paths alone
69 # On Windows, expect MSYS2 pseudo root translation for
70 # Unix-style absolute paths
71 expected
=$
(($expected-$rootslash+$rootoff))
74 test_expect_success
$4 "longest ancestor: $1 $2 => $expected" "
75 echo '$expected' >expect &&
76 test-tool path-utils longest_ancestor_length '$1' '$2' >actual &&
77 test_cmp expect actual
81 # Some absolute path tests should be skipped on Windows due to path mangling
82 # on POSIX-style absolute paths
93 test_expect_success
basename 'test-tool path-utils basename'
94 test_expect_success
dirname 'test-tool path-utils dirname'
100 norm_path .
/..
++failed
++
101 norm_path ..
/.
++failed
++
102 norm_path .
/..
/.
// ++failed
++
104 norm_path dir
/sub
/..
/..
""
105 norm_path dir
/sub
/..
/..
/..
++failed
++
110 norm_path dir
///.
/ dir
/
111 norm_path dir
//sub
/.. dir
/
112 norm_path dir
/sub
/..
/ dir
/
113 norm_path dir
/sub
/..
/. dir
/
114 norm_path dir
/s
1/..
/s
2/ dir
/s
2/
115 norm_path d
1/s
1///s
2/..
//..
/s
3/ d
1/s
3/
116 norm_path d
1/s
1//..
/s
2/..
/..
/d2 d2
117 norm_path d
1/...
/d2 d
1/...
/d2
118 norm_path d
1/...
/.
/..
/d2 d
1/d2
122 norm_path
/// / POSIX
124 norm_path
/.
/ / POSIX
125 norm_path
/.
/..
++failed
++ POSIX
126 norm_path
/..
/.
++failed
++
127 norm_path
/.
/..
/.
// ++failed
++ POSIX
128 norm_path
/dir
/..
/ POSIX
129 norm_path
/dir
/sub
/..
/..
/ POSIX
130 norm_path
/dir
/sub
/..
/..
/..
++failed
++ POSIX
132 norm_path
/dir
// /dir
/
133 norm_path
/.
/dir
/dir
134 norm_path
/dir
/.
/dir
/
135 norm_path
/dir
///.
/ /dir
/
136 norm_path
/dir
//sub
/..
/dir
/
137 norm_path
/dir
/sub
/..
/ /dir
/
138 norm_path
//dir
/sub
/..
/.
/dir
/ POSIX
139 norm_path
/dir
/s
1/..
/s
2/ /dir
/s
2/
140 norm_path
/d
1/s
1///s
2/..
//..
/s
3/ /d
1/s
3/
141 norm_path
/d
1/s
1//..
/s
2/..
/..
/d2
/d2
142 norm_path
/d
1/...
/d2
/d
1/...
/d2
143 norm_path
/d
1/...
/.
/..
/d2
/d
1/d2
148 ancestor
/foo
/foo
-1
149 ancestor
/foo
/bar
-1
150 ancestor
/foo
/foo
/bar
-1
151 ancestor
/foo
/foo
:/bar
-1
152 ancestor
/foo
/:/foo
:/bar
0
153 ancestor
/foo
/foo
:/:/bar
0
154 ancestor
/foo
/:/bar
:/foo
0
155 ancestor
/foo
/bar
/ 0
156 ancestor
/foo
/bar
/fo
-1
157 ancestor
/foo
/bar
/foo
4
158 ancestor
/foo
/bar
/foo
/ba
-1
159 ancestor
/foo
/bar
/:/fo
0
160 ancestor
/foo
/bar
/foo
:/foo
/ba
4
161 ancestor
/foo
/bar
/bar
-1
162 ancestor
/foo
/bar
/fo
-1
163 ancestor
/foo
/bar
/foo
:/bar
4
164 ancestor
/foo
/bar
/:/foo
:/bar
4
165 ancestor
/foo
/bar
/foo
:/:/bar
4
166 ancestor
/foo
/bar
/:/bar
:/fo
0
167 ancestor
/foo
/bar
/:/bar
0
168 ancestor
/foo
/bar
/foo
4
169 ancestor
/foo
/bar
/foo
:/bar
4
170 ancestor
/foo
/bar
/bar
-1
172 # Windows-specific: DOS drives, network shares
173 ancestor C
:/Users
/me C
:/ 2 MINGW
174 ancestor D
:/Users
/me C
:/ -1 MINGW
175 ancestor
//server
/share
/my-directory
//server
/share
/ 14 MINGW
177 test_expect_success
'strip_path_suffix' '
178 echo c:/msysgit >expect &&
179 test-tool path-utils strip_path_suffix \
180 c:/msysgit/libexec//git-core libexec/git-core >actual &&
181 test_cmp expect actual
184 test_expect_success
'absolute path rejects the empty string' '
185 test_must_fail test-tool path-utils absolute_path ""
188 test_expect_success MINGW
'<drive-letter>:\\abc is an absolute path' '
189 for letter in : \" C Z 1 ä
191 path=$letter:\\abc &&
192 absolute="$(test-tool path-utils absolute_path "$path")" &&
193 test "$path" = "$absolute" || return 1
197 test_expect_success
'real path rejects the empty string' '
198 test_must_fail test-tool path-utils real_path ""
201 test_expect_success POSIX
'real path works on absolute paths 1' '
203 test-tool path-utils real_path "/" >actual &&
204 test_cmp expect actual &&
206 nopath="hopefully-absent-path" &&
207 echo "/$nopath" >expect &&
208 test-tool path-utils real_path "/$nopath" >actual &&
209 test_cmp expect actual
212 test_expect_success
'real path works on absolute paths 2' '
213 # Find an existing top-level directory for the remaining tests:
214 d=$(pwd -P | sed -e "s|^\([^/]*/[^/]*\)/.*|\1|") &&
216 test-tool path-utils real_path "$d" >actual &&
217 test_cmp expect actual &&
219 nopath="hopefully-absent-path" &&
220 echo "$d/$nopath" >expect &&
221 test-tool path-utils real_path "$d/$nopath" >actual &&
222 test_cmp expect actual
225 test_expect_success POSIX
'real path removes extra leading slashes' '
227 test-tool path-utils real_path "///" >actual &&
228 test_cmp expect actual &&
230 nopath="hopefully-absent-path" &&
231 echo "/$nopath" >expect &&
232 test-tool path-utils real_path "///$nopath" >actual &&
233 test_cmp expect actual &&
235 # Find an existing top-level directory for the remaining tests:
236 d=$(pwd -P | sed -e "s|^\([^/]*/[^/]*\)/.*|\1|") &&
238 test-tool path-utils real_path "//$d" >actual &&
239 test_cmp expect actual &&
241 echo "$d/$nopath" >expect &&
242 test-tool path-utils real_path "//$d/$nopath" >actual &&
243 test_cmp expect actual
246 test_expect_success
'real path removes other extra slashes' '
247 # Find an existing top-level directory for the remaining tests:
248 d=$(pwd -P | sed -e "s|^\([^/]*/[^/]*\)/.*|\1|") &&
250 test-tool path-utils real_path "$d///" >actual &&
251 test_cmp expect actual &&
253 nopath="hopefully-absent-path" &&
254 echo "$d/$nopath" >expect &&
255 test-tool path-utils real_path "$d///$nopath" >actual &&
256 test_cmp expect actual
259 test_expect_success SYMLINKS
'real path works on symlinks' '
261 ln -s ../.git first/.git &&
263 ln -s ../first second/other &&
265 dir="$(cd .git && pwd -P)" &&
266 dir2=third/../second/other/.git &&
267 echo "$dir" >expect &&
268 test-tool path-utils real_path $dir2 >actual &&
269 test_cmp expect actual &&
271 echo "$file" >expect &&
272 test-tool path-utils real_path $dir2/index >actual &&
273 test_cmp expect actual &&
275 echo "$dir/$basename" >expect &&
276 test-tool -C .git path-utils real_path "$basename" >actual &&
277 test_cmp expect actual &&
278 ln -s ../first/file .git/syml &&
279 sym="$(cd first && pwd -P)"/file &&
280 echo "$sym" >expect &&
281 test-tool path-utils real_path "$dir2/syml" >actual &&
282 test_cmp expect actual
285 test_expect_success SYMLINKS
'prefix_path works with absolute paths to work tree symlinks' '
286 ln -s target symlink &&
287 echo "symlink" >expect &&
288 test-tool path-utils prefix_path prefix "$(pwd)/symlink" >actual &&
289 test_cmp expect actual
292 test_expect_success
'prefix_path works with only absolute path to work tree' '
294 test-tool path-utils prefix_path prefix "$(pwd)" >actual &&
295 test_cmp expected actual
298 test_expect_success
'prefix_path rejects absolute path to dir with same beginning as work tree' '
299 test_must_fail test-tool path-utils prefix_path prefix "$(pwd)a"
302 test_expect_success SYMLINKS
'prefix_path works with absolute path to a symlink to work tree having same beginning as work tree' '
304 ln -s repo repolink &&
306 repo_path="$(cd repo && pwd)" &&
307 test-tool -C repo path-utils prefix_path prefix "$repo_path/../repolink/a" >actual &&
308 test_cmp expect actual
311 relative_path
/foo
/a
/b
/c
/ /foo
/a
/b
/ c
/
312 relative_path
/foo
/a
/b
/c
/ /foo
/a
/b c
/
313 relative_path
/foo
/a
//b
//c
/ ///foo
/a
/b
// c
/ POSIX
314 relative_path
/foo
/a
/b
/foo
/a
/b .
/
315 relative_path
/foo
/a
/b
/ /foo
/a
/b .
/
316 relative_path
/foo
/a
/foo
/a
/b ..
/
317 relative_path
/ /foo
/a
/b
/ ..
/..
/..
/
318 relative_path
/foo
/a
/c
/foo
/a
/b
/ ..
/c
319 relative_path
/foo
/a
/c
/foo
/a
/b ..
/c
320 relative_path
/foo
/x
/y
/foo
/a
/b
/ ..
/..
/x
/y
321 relative_path
/foo
/a
/b
"<empty>" /foo
/a
/b
322 relative_path
/foo
/a
/b
"<null>" /foo
/a
/b
323 relative_path foo
/a
/b
/c
/ foo
/a
/b
/ c
/
324 relative_path foo
/a
/b
/c
/ foo
/a
/b c
/
325 relative_path foo
/a
/b
//c foo
/a
//b c
326 relative_path foo
/a
/b
/ foo
/a
/b
/ .
/
327 relative_path foo
/a
/b
/ foo
/a
/b .
/
328 relative_path foo
/a foo
/a
/b ..
/
329 relative_path foo
/x
/y foo
/a
/b ..
/..
/x
/y
330 relative_path foo
/a
/c foo
/a
/b ..
/c
331 relative_path foo
/a
/b
/foo
/x
/y foo
/a
/b
332 relative_path
/foo
/a
/b foo
/x
/y
/foo
/a
/b
333 relative_path d
:/a
/b D
:/a
/c ..
/b MINGW
334 relative_path C
:/a
/b D
:/a
/c C
:/a
/b MINGW
335 relative_path foo
/a
/b
"<empty>" foo
/a
/b
336 relative_path foo
/a
/b
"<null>" foo
/a
/b
337 relative_path
"<empty>" /foo
/a
/b .
/
338 relative_path
"<empty>" "<empty>" .
/
339 relative_path
"<empty>" "<null>" .
/
340 relative_path
"<null>" "<empty>" .
/
341 relative_path
"<null>" "<null>" .
/
342 relative_path
"<null>" /foo
/a
/b .
/
344 test_git_path A
=B info
/grafts .git
/info
/grafts
345 test_git_path GIT_GRAFT_FILE
=foo info
/grafts foo
346 test_git_path GIT_GRAFT_FILE
=foo info
/////grafts foo
347 test_git_path GIT_INDEX_FILE
=foo index foo
348 test_git_path GIT_INDEX_FILE
=foo index
/foo .git
/index
/foo
349 test_git_path GIT_INDEX_FILE
=foo index2 .git
/index2
350 test_expect_success
'setup fake objects directory foo' 'mkdir foo'
351 test_git_path GIT_OBJECT_DIRECTORY
=foo objects foo
352 test_git_path GIT_OBJECT_DIRECTORY
=foo objects
/foo foo
/foo
353 test_git_path GIT_OBJECT_DIRECTORY
=foo objects2 .git
/objects2
354 test_expect_success
'setup common repository' 'git --git-dir=bar init'
355 test_git_path GIT_COMMON_DIR
=bar index .git
/index
356 test_git_path GIT_COMMON_DIR
=bar index.lock .git
/index.lock
357 test_git_path GIT_COMMON_DIR
=bar HEAD .git
/HEAD
358 test_git_path GIT_COMMON_DIR
=bar logs
/HEAD .git
/logs
/HEAD
359 test_git_path GIT_COMMON_DIR
=bar logs
/HEAD.lock .git
/logs
/HEAD.lock
360 test_git_path GIT_COMMON_DIR
=bar logs
/refs
/bisect
/foo .git
/logs
/refs
/bisect
/foo
361 test_git_path GIT_COMMON_DIR
=bar logs
/refs bar
/logs
/refs
362 test_git_path GIT_COMMON_DIR
=bar logs
/refs
/ bar
/logs
/refs
/
363 test_git_path GIT_COMMON_DIR
=bar logs
/refs
/bisec
/foo bar
/logs
/refs
/bisec
/foo
364 test_git_path GIT_COMMON_DIR
=bar logs
/refs
/bisec bar
/logs
/refs
/bisec
365 test_git_path GIT_COMMON_DIR
=bar logs
/refs
/bisectfoo bar
/logs
/refs
/bisectfoo
366 test_git_path GIT_COMMON_DIR
=bar objects bar
/objects
367 test_git_path GIT_COMMON_DIR
=bar objects
/bar bar
/objects
/bar
368 test_git_path GIT_COMMON_DIR
=bar info
/exclude bar
/info
/exclude
369 test_git_path GIT_COMMON_DIR
=bar info
/grafts bar
/info
/grafts
370 test_git_path GIT_COMMON_DIR
=bar info
/sparse-checkout .git
/info
/sparse-checkout
371 test_git_path GIT_COMMON_DIR
=bar info
//sparse-checkout .git
/info
//sparse-checkout
372 test_git_path GIT_COMMON_DIR
=bar remotes
/bar bar
/remotes
/bar
373 test_git_path GIT_COMMON_DIR
=bar branches
/bar bar
/branches
/bar
374 test_git_path GIT_COMMON_DIR
=bar logs
/refs
/heads
/main bar
/logs
/refs
/heads
/main
375 test_git_path GIT_COMMON_DIR
=bar refs
/heads
/main bar
/refs
/heads
/main
376 test_git_path GIT_COMMON_DIR
=bar refs
/bisect
/foo .git
/refs
/bisect
/foo
377 test_git_path GIT_COMMON_DIR
=bar hooks
/me bar
/hooks
/me
378 test_git_path GIT_COMMON_DIR
=bar config bar
/config
379 test_git_path GIT_COMMON_DIR
=bar packed-refs bar
/packed-refs
380 test_git_path GIT_COMMON_DIR
=bar shallow bar
/shallow
381 test_git_path GIT_COMMON_DIR
=bar common bar
/common
382 test_git_path GIT_COMMON_DIR
=bar common
/file bar
/common
/file
384 # In the tests below, $(pwd) must be used because it is a native path on
385 # Windows and avoids MSYS's path mangling (which simplifies "foo/../bar" and
386 # strips the dot from trailing "/.").
388 test_submodule_relative_url
"../" "../foo" "../submodule" "../../submodule"
389 test_submodule_relative_url
"../" "../foo/bar" "../submodule" "../../foo/submodule"
390 test_submodule_relative_url
"../" "../foo/submodule" "../submodule" "../../foo/submodule"
391 test_submodule_relative_url
"../" "./foo" "../submodule" "../submodule"
392 test_submodule_relative_url
"../" "./foo/bar" "../submodule" "../foo/submodule"
393 test_submodule_relative_url
"../../../" "../foo/bar" "../sub/a/b/c" "../../../../foo/sub/a/b/c"
394 test_submodule_relative_url
"../" "$(pwd)/addtest" "../repo" "$(pwd)/repo"
395 test_submodule_relative_url
"../" "foo/bar" "../submodule" "../foo/submodule"
396 test_submodule_relative_url
"../" "foo" "../submodule" "../submodule"
398 test_submodule_relative_url
"(null)" "../foo/bar" "../sub/a/b/c" "../foo/sub/a/b/c"
399 test_submodule_relative_url
"(null)" "../foo/bar" "../sub/a/b/c/" "../foo/sub/a/b/c"
400 test_submodule_relative_url
"(null)" "../foo/bar/" "../sub/a/b/c" "../foo/sub/a/b/c"
401 test_submodule_relative_url
"(null)" "../foo/bar" "../submodule" "../foo/submodule"
402 test_submodule_relative_url
"(null)" "../foo/submodule" "../submodule" "../foo/submodule"
403 test_submodule_relative_url
"(null)" "../foo" "../submodule" "../submodule"
404 test_submodule_relative_url
"(null)" "./foo/bar" "../submodule" "foo/submodule"
405 test_submodule_relative_url
"(null)" "./foo" "../submodule" "submodule"
406 test_submodule_relative_url
"(null)" "//somewhere else/repo" "../subrepo" "//somewhere else/subrepo"
407 test_submodule_relative_url
"(null)" "//somewhere else/repo" "../../subrepo" "//subrepo"
408 test_submodule_relative_url
"(null)" "//somewhere else/repo" "../../../subrepo" "/subrepo"
409 test_submodule_relative_url
"(null)" "//somewhere else/repo" "../../../../subrepo" "subrepo"
410 test_submodule_relative_url
"(null)" "$(pwd)/subsuper_update_r" "../subsubsuper_update_r" "$(pwd)/subsubsuper_update_r"
411 test_submodule_relative_url
"(null)" "$(pwd)/super_update_r2" "../subsuper_update_r" "$(pwd)/subsuper_update_r"
412 test_submodule_relative_url
"(null)" "$(pwd)/." "../." "$(pwd)/."
413 test_submodule_relative_url
"(null)" "$(pwd)" "./." "$(pwd)/."
414 test_submodule_relative_url
"(null)" "$(pwd)/addtest" "../repo" "$(pwd)/repo"
415 test_submodule_relative_url
"(null)" "$(pwd)" "./å äö" "$(pwd)/å äö"
416 test_submodule_relative_url
"(null)" "$(pwd)/." "../submodule" "$(pwd)/submodule"
417 test_submodule_relative_url
"(null)" "$(pwd)/submodule" "../submodule" "$(pwd)/submodule"
418 test_submodule_relative_url
"(null)" "$(pwd)/home2/../remote" "../bundle1" "$(pwd)/home2/../bundle1"
419 test_submodule_relative_url
"(null)" "$(pwd)/submodule_update_repo" "./." "$(pwd)/submodule_update_repo/."
420 test_submodule_relative_url
"(null)" "file:///tmp/repo" "../subrepo" "file:///tmp/subrepo"
421 test_submodule_relative_url
"(null)" "foo/bar" "../submodule" "foo/submodule"
422 test_submodule_relative_url
"(null)" "foo" "../submodule" "submodule"
423 test_submodule_relative_url
"(null)" "helper:://hostname/repo" "../subrepo" "helper:://hostname/subrepo"
424 test_submodule_relative_url
"(null)" "helper:://hostname/repo" "../../subrepo" "helper:://subrepo"
425 test_submodule_relative_url
"(null)" "helper:://hostname/repo" "../../../subrepo" "helper::/subrepo"
426 test_submodule_relative_url
"(null)" "helper:://hostname/repo" "../../../../subrepo" "helper::subrepo"
427 test_submodule_relative_url
"(null)" "helper:://hostname/repo" "../../../../../subrepo" "helper:subrepo"
428 test_submodule_relative_url
"(null)" "helper:://hostname/repo" "../../../../../../subrepo" ".:subrepo"
429 test_submodule_relative_url
"(null)" "ssh://hostname/repo" "../subrepo" "ssh://hostname/subrepo"
430 test_submodule_relative_url
"(null)" "ssh://hostname/repo" "../../subrepo" "ssh://subrepo"
431 test_submodule_relative_url
"(null)" "ssh://hostname/repo" "../../../subrepo" "ssh:/subrepo"
432 test_submodule_relative_url
"(null)" "ssh://hostname/repo" "../../../../subrepo" "ssh:subrepo"
433 test_submodule_relative_url
"(null)" "ssh://hostname/repo" "../../../../../subrepo" ".:subrepo"
434 test_submodule_relative_url
"(null)" "ssh://hostname:22/repo" "../subrepo" "ssh://hostname:22/subrepo"
435 test_submodule_relative_url
"(null)" "user@host:path/to/repo" "../subrepo" "user@host:path/to/subrepo"
436 test_submodule_relative_url
"(null)" "user@host:repo" "../subrepo" "user@host:subrepo"
437 test_submodule_relative_url
"(null)" "user@host:repo" "../../subrepo" ".:subrepo"
439 test_expect_success
'match .gitmodules' '
440 test-tool path-utils is_dotgitmodules \
443 .git${u200c}modules \
499 "gitmod~4 . :\$DATA" \
530 test_expect_success
'match .gitattributes' '
531 test-tool path-utils is_dotgitattributes \
533 .git${u200c}attributes \
540 test_expect_success
'match .gitignore' '
541 test-tool path-utils is_dotgitignore \
550 test_expect_success
'match .mailmap' '
551 test-tool path-utils is_dotmailmap \
560 test_expect_success MINGW
'is_valid_path() on Windows' '
561 test-tool path-utils is_valid_path \
579 "abc/conOut\$ .xyz/test" \
587 test_lazy_prereq RUNTIME_PREFIX
'
588 test true = "$RUNTIME_PREFIX"
591 test_lazy_prereq CAN_EXEC_IN_PWD
'
592 cp "$GIT_EXEC_PATH"/git$X ./ &&
596 test_expect_success
!VALGRIND
,RUNTIME_PREFIX
,CAN_EXEC_IN_PWD
'RUNTIME_PREFIX works' '
597 mkdir -p pretend/bin pretend/libexec/git-core &&
598 echo "echo HERE" | write_script pretend/libexec/git-core/git-here &&
599 cp "$GIT_EXEC_PATH"/git$X pretend/bin/ &&
600 GIT_EXEC_PATH= ./pretend/bin/git here >actual &&
602 test_cmp expect actual'
604 test_expect_success
!VALGRIND
,RUNTIME_PREFIX
,CAN_EXEC_IN_PWD
'%(prefix)/ works' '
605 mkdir -p pretend/bin &&
606 cp "$GIT_EXEC_PATH"/git$X pretend/bin/ &&
607 git config yes.path "%(prefix)/yes" &&
608 GIT_EXEC_PATH= ./pretend/bin/git config --path yes.path >actual &&
609 echo "$(pwd)/pretend/yes" >expect &&
610 test_cmp expect actual