3 # Copyright (c) 2008 David Reiss
6 test_description
='Test various path utilities'
11 test_expect_success
$3 "normalize path: $1 => $2" \
12 "test \"\$(test-path-utils normalize_path_copy '$1')\" = '$2'"
15 # On Windows, we are using MSYS's bash, which mangles the paths.
16 # Absolute paths are anchored at the MSYS installation directory,
17 # which means that the path / accounts for this many characters:
18 rootoff
=$
(test-path-utils normalize_path_copy
/ |
wc -c)
19 # Account for the trailing LF:
20 if test $rootoff = 2; then
21 rootoff
= # we are on Unix
23 rootoff
=$
(($rootoff-1))
27 # We do some math with the expected ancestor length.
29 if test -n "$rootoff" && test "x$expected" != x-1
; then
30 expected
=$
(($expected+$rootoff))
32 test_expect_success
"longest ancestor: $1 $2 => $expected" \
33 "actual=\$(test-path-utils longest_ancestor_length '$1' '$2') &&
34 test \"\$actual\" = '$expected'"
37 # Absolute path tests must be skipped on Windows because due to path mangling
38 # the test program never sees a POSIX-style absolute path
51 norm_path .
/..
++failed
++
52 norm_path ..
/.
++failed
++
53 norm_path .
/..
/.
// ++failed
++
55 norm_path dir
/sub
/..
/..
""
56 norm_path dir
/sub
/..
/..
/..
++failed
++
61 norm_path dir
///.
/ dir
/
62 norm_path dir
//sub
/.. dir
/
63 norm_path dir
/sub
/..
/ dir
/
64 norm_path dir
/sub
/..
/. dir
/
65 norm_path dir
/s
1/..
/s
2/ dir
/s
2/
66 norm_path d
1/s
1///s
2/..
//..
/s
3/ d
1/s
3/
67 norm_path d
1/s
1//..
/s
2/..
/..
/d2 d2
68 norm_path d
1/...
/d2 d
1/...
/d2
69 norm_path d
1/...
/.
/..
/d2 d
1/d2
76 norm_path
/.
/..
++failed
++ POSIX
77 norm_path
/..
/.
++failed
++ POSIX
78 norm_path
/.
/..
/.
// ++failed
++ POSIX
79 norm_path
/dir
/..
/ POSIX
80 norm_path
/dir
/sub
/..
/..
/ POSIX
81 norm_path
/dir
/sub
/..
/..
/..
++failed
++ POSIX
82 norm_path
/dir
/dir POSIX
83 norm_path
/dir
// /dir
/ POSIX
84 norm_path
/.
/dir
/dir POSIX
85 norm_path
/dir
/.
/dir
/ POSIX
86 norm_path
/dir
///.
/ /dir
/ POSIX
87 norm_path
/dir
//sub
/..
/dir
/ POSIX
88 norm_path
/dir
/sub
/..
/ /dir
/ POSIX
89 norm_path
//dir
/sub
/..
/.
/dir
/ POSIX
90 norm_path
/dir
/s
1/..
/s
2/ /dir
/s
2/ POSIX
91 norm_path
/d
1/s
1///s
2/..
//..
/s
3/ /d
1/s
3/ POSIX
92 norm_path
/d
1/s
1//..
/s
2/..
/..
/d2
/d2 POSIX
93 norm_path
/d
1/...
/d2
/d
1/...
/d2 POSIX
94 norm_path
/d
1/...
/.
/..
/d2
/d
1/d2 POSIX
100 ancestor
/foo
/bar
-1
101 ancestor
/foo
/foo
/bar
-1
102 ancestor
/foo
/foo
:/bar
-1
103 ancestor
/foo
/:/foo
:/bar
0
104 ancestor
/foo
/foo
:/:/bar
0
105 ancestor
/foo
/:/bar
:/foo
0
106 ancestor
/foo
/bar
/ 0
107 ancestor
/foo
/bar
/fo
-1
108 ancestor
/foo
/bar
/foo
4
109 ancestor
/foo
/bar
/foo
/ba
-1
110 ancestor
/foo
/bar
/:/fo
0
111 ancestor
/foo
/bar
/foo
:/foo
/ba
4
112 ancestor
/foo
/bar
/bar
-1
113 ancestor
/foo
/bar
/fo
-1
114 ancestor
/foo
/bar
/foo
:/bar
4
115 ancestor
/foo
/bar
/:/foo
:/bar
4
116 ancestor
/foo
/bar
/foo
:/:/bar
4
117 ancestor
/foo
/bar
/:/bar
:/fo
0
118 ancestor
/foo
/bar
/:/bar
0
119 ancestor
/foo
/bar
/foo
4
120 ancestor
/foo
/bar
/foo
:/bar
4
121 ancestor
/foo
/bar
/bar
-1
123 test_expect_success
'strip_path_suffix' '
124 test c:/msysgit = $(test-path-utils strip_path_suffix \
125 c:/msysgit/libexec//git-core libexec/git-core)
128 test_expect_success
'absolute path rejects the empty string' '
129 test_must_fail test-path-utils absolute_path ""
132 test_expect_success
'real path rejects the empty string' '
133 test_must_fail test-path-utils real_path ""
136 test_expect_success POSIX
'real path works on absolute paths 1' '
137 nopath="hopefully-absent-path" &&
138 test "/" = "$(test-path-utils real_path "/")" &&
139 test "/$nopath" = "$(test-path-utils real_path "/$nopath")"
142 test_expect_success
'real path works on absolute paths 2' '
143 nopath="hopefully-absent-path" &&
144 # Find an existing top-level directory for the remaining tests:
145 d=$(pwd -P | sed -e "s|^\([^/]*/[^/]*\)/.*|\1|") &&
146 test "$d" = "$(test-path-utils real_path "$d")" &&
147 test "$d/$nopath" = "$(test-path-utils real_path "$d/$nopath")"
150 test_expect_success POSIX
'real path removes extra leading slashes' '
151 nopath="hopefully-absent-path" &&
152 test "/" = "$(test-path-utils real_path "///")" &&
153 test "/$nopath" = "$(test-path-utils real_path "///$nopath")" &&
154 # Find an existing top-level directory for the remaining tests:
155 d=$(pwd -P | sed -e "s|^\([^/]*/[^/]*\)/.*|\1|") &&
156 test "$d" = "$(test-path-utils real_path "//$d")" &&
157 test "$d/$nopath" = "$(test-path-utils real_path "//$d/$nopath")"
160 test_expect_success
'real path removes other extra slashes' '
161 nopath="hopefully-absent-path" &&
162 # Find an existing top-level directory for the remaining tests:
163 d=$(pwd -P | sed -e "s|^\([^/]*/[^/]*\)/.*|\1|") &&
164 test "$d" = "$(test-path-utils real_path "$d///")" &&
165 test "$d/$nopath" = "$(test-path-utils real_path "$d///$nopath")"
168 test_expect_success SYMLINKS
'real path works on symlinks' '
170 ln -s ../.git first/.git &&
172 ln -s ../first second/other &&
174 dir="$(cd .git; pwd -P)" &&
175 dir2=third/../second/other/.git &&
176 test "$dir" = "$(test-path-utils real_path $dir2)" &&
178 test "$file" = "$(test-path-utils real_path $dir2/index)" &&
180 test "$dir/$basename" = "$(cd .git && test-path-utils real_path "$basename")" &&
181 ln -s ../first/file .git/syml &&
182 sym="$(cd first; pwd -P)"/file &&
183 test "$sym" = "$(test-path-utils real_path "$dir2/syml")"