git-clean: refactor git-clean into two phases
[git/mingw.git] / t / t0060-path-utils.sh
blob76c779252ca21219e75d1cc9db2f9babd8f18e07
1 #!/bin/sh
3 # Copyright (c) 2008 David Reiss
6 test_description='Test various path utilities'
8 . ./test-lib.sh
10 norm_path() {
11 test_expect_success $3 "normalize path: $1 => $2" \
12 "test \"\$(test-path-utils normalize_path_copy '$1')\" = '$2'"
15 relative_path() {
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
27 else
28 rootoff=$(($rootoff-1))
31 ancestor() {
32 # We do some math with the expected ancestor length.
33 expected=$3
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
44 case $(uname -s) in
45 *MINGW*)
48 test_set_prereq POSIX
50 esac
52 norm_path "" ""
53 norm_path . ""
54 norm_path ./ ""
55 norm_path ./. ""
56 norm_path ./.. ++failed++
57 norm_path ../. ++failed++
58 norm_path ./../.// ++failed++
59 norm_path dir/.. ""
60 norm_path dir/sub/../.. ""
61 norm_path dir/sub/../../.. ++failed++
62 norm_path dir dir
63 norm_path dir// dir/
64 norm_path ./dir dir
65 norm_path dir/. dir/
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/s1/../s2/ dir/s2/
71 norm_path d1/s1///s2/..//../s3/ d1/s3/
72 norm_path d1/s1//../s2/../../d2 d2
73 norm_path d1/.../d2 d1/.../d2
74 norm_path d1/..././../d2 d1/d2
76 norm_path / / POSIX
77 norm_path // / POSIX
78 norm_path /// / POSIX
79 norm_path /. / POSIX
80 norm_path /./ / POSIX
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/s1/../s2/ /dir/s2/ POSIX
96 norm_path /d1/s1///s2/..//../s3/ /d1/s3/ POSIX
97 norm_path /d1/s1//../s2/../../d2 /d2 POSIX
98 norm_path /d1/.../d2 /d1/.../d2 POSIX
99 norm_path /d1/..././../d2 /d1/d2 POSIX
101 ancestor / / -1
102 ancestor /foo / 0
103 ancestor /foo /fo -1
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' '
174 mkdir first &&
175 ln -s ../.git first/.git &&
176 mkdir second &&
177 ln -s ../first second/other &&
178 mkdir third &&
179 dir="$(cd .git; pwd -P)" &&
180 dir2=third/../second/other/.git &&
181 test "$dir" = "$(test-path-utils real_path $dir2)" &&
182 file="$dir"/index &&
183 test "$file" = "$(test-path-utils real_path $dir2/index)" &&
184 basename=blub &&
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 ../
197 relative_path / /a/b/ ../../
198 relative_path /a/c /a/b/ ../c
199 relative_path /a/c /a/b ../c
200 relative_path /x/y /a/b/ ../../x/y
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 ../
209 relative_path x/y a/b ../../x/y
210 relative_path a/c a/b ../c
211 relative_path a/b "<empty>" a/b
212 relative_path a/b "<null>" a/b
213 relative_path "<empty>" /a/b ./
214 relative_path "<empty>" "<empty>" ./
215 relative_path "<empty>" "<null>" ./
216 relative_path "<null>" "<empty>" ./
217 relative_path "<null>" "<null>" ./
218 relative_path "<null>" /a/b ./
220 test_done