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'"
16 test_expect_success
$4 "relative path: $1 $2 => $3" \
17 "test \"\$(test-path-utils relative_path '$1' '$2')\" = '$3'"
20 # On Windows, we are using MSYS's bash, which mangles the paths.
21 # Absolute paths are anchored at the MSYS installation directory,
22 # which means that the path / accounts for this many characters:
23 rootoff
=$
(test-path-utils normalize_path_copy
/ |
wc -c)
24 # Account for the trailing LF:
25 if test $rootoff = 2; then
26 rootoff
= # we are on Unix
28 rootoff
=$
(($rootoff-1))
32 # We do some math with the expected ancestor length.
34 if test -n "$rootoff" && test "x$expected" != x-1
; then
35 expected
=$
(($expected+$rootoff))
37 test_expect_success
"longest ancestor: $1 $2 => $expected" \
38 "actual=\$(test-path-utils longest_ancestor_length '$1' '$2') &&
39 test \"\$actual\" = '$expected'"
42 # Absolute path tests must be skipped on Windows because due to path mangling
43 # the test program never sees a POSIX-style absolute path
56 norm_path .
/..
++failed
++
57 norm_path ..
/.
++failed
++
58 norm_path .
/..
/.
// ++failed
++
60 norm_path dir
/sub
/..
/..
""
61 norm_path dir
/sub
/..
/..
/..
++failed
++
66 norm_path dir
///.
/ dir
/
67 norm_path dir
//sub
/.. dir
/
68 norm_path dir
/sub
/..
/ dir
/
69 norm_path dir
/sub
/..
/. dir
/
70 norm_path dir
/s
1/..
/s
2/ dir
/s
2/
71 norm_path d
1/s
1///s
2/..
//..
/s
3/ d
1/s
3/
72 norm_path d
1/s
1//..
/s
2/..
/..
/d2 d2
73 norm_path d
1/...
/d2 d
1/...
/d2
74 norm_path d
1/...
/.
/..
/d2 d
1/d2
81 norm_path
/.
/..
++failed
++ POSIX
82 norm_path
/..
/.
++failed
++ POSIX
83 norm_path
/.
/..
/.
// ++failed
++ POSIX
84 norm_path
/dir
/..
/ POSIX
85 norm_path
/dir
/sub
/..
/..
/ POSIX
86 norm_path
/dir
/sub
/..
/..
/..
++failed
++ POSIX
87 norm_path
/dir
/dir POSIX
88 norm_path
/dir
// /dir
/ POSIX
89 norm_path
/.
/dir
/dir POSIX
90 norm_path
/dir
/.
/dir
/ POSIX
91 norm_path
/dir
///.
/ /dir
/ POSIX
92 norm_path
/dir
//sub
/..
/dir
/ POSIX
93 norm_path
/dir
/sub
/..
/ /dir
/ POSIX
94 norm_path
//dir
/sub
/..
/.
/dir
/ POSIX
95 norm_path
/dir
/s
1/..
/s
2/ /dir
/s
2/ POSIX
96 norm_path
/d
1/s
1///s
2/..
//..
/s
3/ /d
1/s
3/ POSIX
97 norm_path
/d
1/s
1//..
/s
2/..
/..
/d2
/d2 POSIX
98 norm_path
/d
1/...
/d2
/d
1/...
/d2 POSIX
99 norm_path
/d
1/...
/.
/..
/d2
/d
1/d2 POSIX
104 ancestor
/foo
/foo
-1
105 ancestor
/foo
/bar
-1
106 ancestor
/foo
/foo
/bar
-1
107 ancestor
/foo
/foo
:/bar
-1
108 ancestor
/foo
/:/foo
:/bar
0
109 ancestor
/foo
/foo
:/:/bar
0
110 ancestor
/foo
/:/bar
:/foo
0
111 ancestor
/foo
/bar
/ 0
112 ancestor
/foo
/bar
/fo
-1
113 ancestor
/foo
/bar
/foo
4
114 ancestor
/foo
/bar
/foo
/ba
-1
115 ancestor
/foo
/bar
/:/fo
0
116 ancestor
/foo
/bar
/foo
:/foo
/ba
4
117 ancestor
/foo
/bar
/bar
-1
118 ancestor
/foo
/bar
/fo
-1
119 ancestor
/foo
/bar
/foo
:/bar
4
120 ancestor
/foo
/bar
/:/foo
:/bar
4
121 ancestor
/foo
/bar
/foo
:/:/bar
4
122 ancestor
/foo
/bar
/:/bar
:/fo
0
123 ancestor
/foo
/bar
/:/bar
0
124 ancestor
/foo
/bar
/foo
4
125 ancestor
/foo
/bar
/foo
:/bar
4
126 ancestor
/foo
/bar
/bar
-1
128 test_expect_success
'strip_path_suffix' '
129 test c:/msysgit = $(test-path-utils strip_path_suffix \
130 c:/msysgit/libexec//git-core libexec/git-core)
133 test_expect_success
'absolute path rejects the empty string' '
134 test_must_fail test-path-utils absolute_path ""
137 test_expect_success
'real path rejects the empty string' '
138 test_must_fail test-path-utils real_path ""
141 test_expect_success POSIX
'real path works on absolute paths 1' '
142 nopath="hopefully-absent-path" &&
143 test "/" = "$(test-path-utils real_path "/")" &&
144 test "/$nopath" = "$(test-path-utils real_path "/$nopath")"
147 test_expect_success
'real path works on absolute paths 2' '
148 nopath="hopefully-absent-path" &&
149 # Find an existing top-level directory for the remaining tests:
150 d=$(pwd -P | sed -e "s|^\([^/]*/[^/]*\)/.*|\1|") &&
151 test "$d" = "$(test-path-utils real_path "$d")" &&
152 test "$d/$nopath" = "$(test-path-utils real_path "$d/$nopath")"
155 test_expect_success POSIX
'real path removes extra leading slashes' '
156 nopath="hopefully-absent-path" &&
157 test "/" = "$(test-path-utils real_path "///")" &&
158 test "/$nopath" = "$(test-path-utils real_path "///$nopath")" &&
159 # Find an existing top-level directory for the remaining tests:
160 d=$(pwd -P | sed -e "s|^\([^/]*/[^/]*\)/.*|\1|") &&
161 test "$d" = "$(test-path-utils real_path "//$d")" &&
162 test "$d/$nopath" = "$(test-path-utils real_path "//$d/$nopath")"
165 test_expect_success
'real path removes other extra slashes' '
166 nopath="hopefully-absent-path" &&
167 # Find an existing top-level directory for the remaining tests:
168 d=$(pwd -P | sed -e "s|^\([^/]*/[^/]*\)/.*|\1|") &&
169 test "$d" = "$(test-path-utils real_path "$d///")" &&
170 test "$d/$nopath" = "$(test-path-utils real_path "$d///$nopath")"
173 test_expect_success SYMLINKS
'real path works on symlinks' '
175 ln -s ../.git first/.git &&
177 ln -s ../first second/other &&
179 dir="$(cd .git; pwd -P)" &&
180 dir2=third/../second/other/.git &&
181 test "$dir" = "$(test-path-utils real_path $dir2)" &&
183 test "$file" = "$(test-path-utils real_path $dir2/index)" &&
185 test "$dir/$basename" = "$(cd .git && test-path-utils real_path "$basename")" &&
186 ln -s ../first/file .git/syml &&
187 sym="$(cd first; pwd -P)"/file &&
188 test "$sym" = "$(test-path-utils real_path "$dir2/syml")"
191 relative_path
/a
/b
/c
/ /a
/b
/ c
/
192 relative_path
/a
/b
/c
/ /a
/b c
/
193 relative_path
/a
//b
//c
/ //a
/b
// c
/ POSIX
194 relative_path
/a
/b
/a
/b .
195 relative_path
/a
/b
/ /a
/b .
196 relative_path
/a
/a
/b
/a POSIX
197 relative_path
/ /a
/b
/ / POSIX
198 relative_path
/a
/c
/a
/b
/ /a
/c POSIX
199 relative_path
/a
/c
/a
/b
/a
/c POSIX
200 relative_path
/x
/y
/a
/b
/ /x
/y POSIX
201 relative_path
/a
/b
"<empty>" /a
/b POSIX
202 relative_path
/a
/b
"<null>" /a
/b POSIX
203 relative_path a
/b
/c
/ a
/b
/ c
/
204 relative_path a
/b
/c
/ a
/b c
/
205 relative_path a
/b
//c a
//b c
206 relative_path a
/b
/ a
/b
/ .
207 relative_path a
/b
/ a
/b .
208 relative_path a a
/b a
# TODO: should be: ..
209 relative_path x
/y a
/b x
/y
# TODO: should be: ../../x/y
210 relative_path a
/c a
/b a
/c
# TODO: should be: ../c
211 relative_path a
/b
"<empty>" a
/b
212 relative_path a
/b
"<null>" a
/b
213 relative_path
"<empty>" /a
/b
"(empty)"
214 relative_path
"<empty>" "<empty>" "(empty)"
215 relative_path
"<empty>" "<null>" "(empty)"
216 relative_path
"<null>" "<empty>" "(null)"
217 relative_path
"<null>" "<null>" "(null)"
219 test_expect_failure
'relative path: <null> /a/b => segfault' '
220 test-path-utils relative_path "<null>" "/a/b"