Merge branch 'jk/fix-attribute-macro-in-2.5'
[git] / t / t0060-path-utils.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2008 David Reiss
4 #
5
6 test_description='Test various path utilities'
7
8 . ./test-lib.sh
9
10 norm_path() {
11         expected=$(test-path-utils print_path "$2")
12         test_expect_success $3 "normalize path: $1 => $2" \
13         "test \"\$(test-path-utils normalize_path_copy '$1')\" = '$expected'"
14 }
15
16 relative_path() {
17         expected=$(test-path-utils print_path "$3")
18         test_expect_success $4 "relative path: $1 $2 => $3" \
19         "test \"\$(test-path-utils relative_path '$1' '$2')\" = '$expected'"
20 }
21
22 test_git_path() {
23         test_expect_success "git-path $1 $2 => $3" "
24                 $1 git rev-parse --git-path $2 >actual &&
25                 echo $3 >expect &&
26                 test_cmp expect actual
27         "
28 }
29
30 # On Windows, we are using MSYS's bash, which mangles the paths.
31 # Absolute paths are anchored at the MSYS installation directory,
32 # which means that the path / accounts for this many characters:
33 rootoff=$(test-path-utils normalize_path_copy / | wc -c)
34 # Account for the trailing LF:
35 if test $rootoff = 2; then
36         rootoff=        # we are on Unix
37 else
38         rootoff=$(($rootoff-1))
39         # In MSYS2, the root directory "/" is translated into a Windows
40         # directory *with* trailing slash. Let's test for that and adjust
41         # our expected longest ancestor length accordingly.
42         case "$(test-path-utils print_path /)" in
43         */) rootslash=1;;
44         *) rootslash=0;;
45         esac
46 fi
47
48 ancestor() {
49         # We do some math with the expected ancestor length.
50         expected=$3
51         if test -n "$rootoff" && test "x$expected" != x-1; then
52                 expected=$(($expected-$rootslash))
53                 test $expected -lt 0 ||
54                 expected=$(($expected+$rootoff))
55         fi
56         test_expect_success "longest ancestor: $1 $2 => $expected" \
57         "actual=\$(test-path-utils longest_ancestor_length '$1' '$2') &&
58          test \"\$actual\" = '$expected'"
59 }
60
61 # Some absolute path tests should be skipped on Windows due to path mangling
62 # on POSIX-style absolute paths
63 case $(uname -s) in
64 *MINGW*)
65         ;;
66 *)
67         test_set_prereq POSIX
68         ;;
69 esac
70
71 test_expect_success basename 'test-path-utils basename'
72 test_expect_success dirname 'test-path-utils dirname'
73
74 norm_path "" ""
75 norm_path . ""
76 norm_path ./ ""
77 norm_path ./. ""
78 norm_path ./.. ++failed++
79 norm_path ../. ++failed++
80 norm_path ./../.// ++failed++
81 norm_path dir/.. ""
82 norm_path dir/sub/../.. ""
83 norm_path dir/sub/../../.. ++failed++
84 norm_path dir dir
85 norm_path dir// dir/
86 norm_path ./dir dir
87 norm_path dir/. dir/
88 norm_path dir///./ dir/
89 norm_path dir//sub/.. dir/
90 norm_path dir/sub/../ dir/
91 norm_path dir/sub/../. dir/
92 norm_path dir/s1/../s2/ dir/s2/
93 norm_path d1/s1///s2/..//../s3/ d1/s3/
94 norm_path d1/s1//../s2/../../d2 d2
95 norm_path d1/.../d2 d1/.../d2
96 norm_path d1/..././../d2 d1/d2
97
98 norm_path / /
99 norm_path // / POSIX
100 norm_path /// / POSIX
101 norm_path /. /
102 norm_path /./ / POSIX
103 norm_path /./.. ++failed++ POSIX
104 norm_path /../. ++failed++
105 norm_path /./../.// ++failed++ POSIX
106 norm_path /dir/.. / POSIX
107 norm_path /dir/sub/../.. / POSIX
108 norm_path /dir/sub/../../.. ++failed++ POSIX
109 norm_path /dir /dir
110 norm_path /dir// /dir/
111 norm_path /./dir /dir
112 norm_path /dir/. /dir/
113 norm_path /dir///./ /dir/
114 norm_path /dir//sub/.. /dir/
115 norm_path /dir/sub/../ /dir/
116 norm_path //dir/sub/../. /dir/ POSIX
117 norm_path /dir/s1/../s2/ /dir/s2/
118 norm_path /d1/s1///s2/..//../s3/ /d1/s3/
119 norm_path /d1/s1//../s2/../../d2 /d2
120 norm_path /d1/.../d2 /d1/.../d2
121 norm_path /d1/..././../d2 /d1/d2
122
123 ancestor / / -1
124 ancestor /foo / 0
125 ancestor /foo /fo -1
126 ancestor /foo /foo -1
127 ancestor /foo /bar -1
128 ancestor /foo /foo/bar -1
129 ancestor /foo /foo:/bar -1
130 ancestor /foo /:/foo:/bar 0
131 ancestor /foo /foo:/:/bar 0
132 ancestor /foo /:/bar:/foo 0
133 ancestor /foo/bar / 0
134 ancestor /foo/bar /fo -1
135 ancestor /foo/bar /foo 4
136 ancestor /foo/bar /foo/ba -1
137 ancestor /foo/bar /:/fo 0
138 ancestor /foo/bar /foo:/foo/ba 4
139 ancestor /foo/bar /bar -1
140 ancestor /foo/bar /fo -1
141 ancestor /foo/bar /foo:/bar 4
142 ancestor /foo/bar /:/foo:/bar 4
143 ancestor /foo/bar /foo:/:/bar 4
144 ancestor /foo/bar /:/bar:/fo 0
145 ancestor /foo/bar /:/bar 0
146 ancestor /foo/bar /foo 4
147 ancestor /foo/bar /foo:/bar 4
148 ancestor /foo/bar /bar -1
149
150 test_expect_success 'strip_path_suffix' '
151         test c:/msysgit = $(test-path-utils strip_path_suffix \
152                 c:/msysgit/libexec//git-core libexec/git-core)
153 '
154
155 test_expect_success 'absolute path rejects the empty string' '
156         test_must_fail test-path-utils absolute_path ""
157 '
158
159 test_expect_success 'real path rejects the empty string' '
160         test_must_fail test-path-utils real_path ""
161 '
162
163 test_expect_success POSIX 'real path works on absolute paths 1' '
164         nopath="hopefully-absent-path" &&
165         test "/" = "$(test-path-utils real_path "/")" &&
166         test "/$nopath" = "$(test-path-utils real_path "/$nopath")"
167 '
168
169 test_expect_success 'real path works on absolute paths 2' '
170         nopath="hopefully-absent-path" &&
171         # Find an existing top-level directory for the remaining tests:
172         d=$(pwd -P | sed -e "s|^\([^/]*/[^/]*\)/.*|\1|") &&
173         test "$d" = "$(test-path-utils real_path "$d")" &&
174         test "$d/$nopath" = "$(test-path-utils real_path "$d/$nopath")"
175 '
176
177 test_expect_success POSIX 'real path removes extra leading slashes' '
178         nopath="hopefully-absent-path" &&
179         test "/" = "$(test-path-utils real_path "///")" &&
180         test "/$nopath" = "$(test-path-utils real_path "///$nopath")" &&
181         # Find an existing top-level directory for the remaining tests:
182         d=$(pwd -P | sed -e "s|^\([^/]*/[^/]*\)/.*|\1|") &&
183         test "$d" = "$(test-path-utils real_path "//$d")" &&
184         test "$d/$nopath" = "$(test-path-utils real_path "//$d/$nopath")"
185 '
186
187 test_expect_success 'real path removes other extra slashes' '
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-path-utils real_path "$d///")" &&
192         test "$d/$nopath" = "$(test-path-utils real_path "$d///$nopath")"
193 '
194
195 test_expect_success SYMLINKS 'real path works on symlinks' '
196         mkdir first &&
197         ln -s ../.git first/.git &&
198         mkdir second &&
199         ln -s ../first second/other &&
200         mkdir third &&
201         dir="$(cd .git; pwd -P)" &&
202         dir2=third/../second/other/.git &&
203         test "$dir" = "$(test-path-utils real_path $dir2)" &&
204         file="$dir"/index &&
205         test "$file" = "$(test-path-utils real_path $dir2/index)" &&
206         basename=blub &&
207         test "$dir/$basename" = "$(cd .git && test-path-utils real_path "$basename")" &&
208         ln -s ../first/file .git/syml &&
209         sym="$(cd first; pwd -P)"/file &&
210         test "$sym" = "$(test-path-utils real_path "$dir2/syml")"
211 '
212
213 test_expect_success SYMLINKS 'prefix_path works with absolute paths to work tree symlinks' '
214         ln -s target symlink &&
215         test "$(test-path-utils prefix_path prefix "$(pwd)/symlink")" = "symlink"
216 '
217
218 test_expect_success 'prefix_path works with only absolute path to work tree' '
219         echo "" >expected &&
220         test-path-utils prefix_path prefix "$(pwd)" >actual &&
221         test_cmp expected actual
222 '
223
224 test_expect_success 'prefix_path rejects absolute path to dir with same beginning as work tree' '
225         test_must_fail test-path-utils prefix_path prefix "$(pwd)a"
226 '
227
228 test_expect_success SYMLINKS 'prefix_path works with absolute path to a symlink to work tree having  same beginning as work tree' '
229         git init repo &&
230         ln -s repo repolink &&
231         test "a" = "$(cd repo && test-path-utils prefix_path prefix "$(pwd)/../repolink/a")"
232 '
233
234 relative_path /foo/a/b/c/       /foo/a/b/       c/
235 relative_path /foo/a/b/c/       /foo/a/b        c/
236 relative_path /foo/a//b//c/     ///foo/a/b//    c/              POSIX
237 relative_path /foo/a/b          /foo/a/b        ./
238 relative_path /foo/a/b/         /foo/a/b        ./
239 relative_path /foo/a            /foo/a/b        ../
240 relative_path /                 /foo/a/b/       ../../../
241 relative_path /foo/a/c          /foo/a/b/       ../c
242 relative_path /foo/a/c          /foo/a/b        ../c
243 relative_path /foo/x/y          /foo/a/b/       ../../x/y
244 relative_path /foo/a/b          "<empty>"       /foo/a/b
245 relative_path /foo/a/b          "<null>"        /foo/a/b
246 relative_path foo/a/b/c/        foo/a/b/        c/
247 relative_path foo/a/b/c/        foo/a/b         c/
248 relative_path foo/a/b//c        foo/a//b        c
249 relative_path foo/a/b/          foo/a/b/        ./
250 relative_path foo/a/b/          foo/a/b         ./
251 relative_path foo/a             foo/a/b         ../
252 relative_path foo/x/y           foo/a/b         ../../x/y
253 relative_path foo/a/c           foo/a/b         ../c
254 relative_path foo/a/b           /foo/x/y        foo/a/b
255 relative_path /foo/a/b          foo/x/y         /foo/a/b
256 relative_path d:/a/b            D:/a/c          ../b            MINGW
257 relative_path C:/a/b            D:/a/c          C:/a/b          MINGW
258 relative_path foo/a/b           "<empty>"       foo/a/b
259 relative_path foo/a/b           "<null>"        foo/a/b
260 relative_path "<empty>"         /foo/a/b        ./
261 relative_path "<empty>"         "<empty>"       ./
262 relative_path "<empty>"         "<null>"        ./
263 relative_path "<null>"          "<empty>"       ./
264 relative_path "<null>"          "<null>"        ./
265 relative_path "<null>"          /foo/a/b        ./
266
267 test_git_path A=B                info/grafts .git/info/grafts
268 test_git_path GIT_GRAFT_FILE=foo info/grafts foo
269 test_git_path GIT_GRAFT_FILE=foo info/////grafts foo
270 test_git_path GIT_INDEX_FILE=foo index foo
271 test_git_path GIT_INDEX_FILE=foo index/foo .git/index/foo
272 test_git_path GIT_INDEX_FILE=foo index2 .git/index2
273 test_expect_success 'setup fake objects directory foo' 'mkdir foo'
274 test_git_path GIT_OBJECT_DIRECTORY=foo objects foo
275 test_git_path GIT_OBJECT_DIRECTORY=foo objects/foo foo/foo
276 test_git_path GIT_OBJECT_DIRECTORY=foo objects2 .git/objects2
277 test_expect_success 'setup common repository' 'git --git-dir=bar init'
278 test_git_path GIT_COMMON_DIR=bar index                    .git/index
279 test_git_path GIT_COMMON_DIR=bar HEAD                     .git/HEAD
280 test_git_path GIT_COMMON_DIR=bar logs/HEAD                .git/logs/HEAD
281 test_git_path GIT_COMMON_DIR=bar logs/refs/bisect/foo     .git/logs/refs/bisect/foo
282 test_git_path GIT_COMMON_DIR=bar logs/refs/bisec/foo      bar/logs/refs/bisec/foo
283 test_git_path GIT_COMMON_DIR=bar logs/refs/bisec          bar/logs/refs/bisec
284 test_git_path GIT_COMMON_DIR=bar logs/refs/bisectfoo      bar/logs/refs/bisectfoo
285 test_git_path GIT_COMMON_DIR=bar objects                  bar/objects
286 test_git_path GIT_COMMON_DIR=bar objects/bar              bar/objects/bar
287 test_git_path GIT_COMMON_DIR=bar info/exclude             bar/info/exclude
288 test_git_path GIT_COMMON_DIR=bar info/grafts              bar/info/grafts
289 test_git_path GIT_COMMON_DIR=bar info/sparse-checkout     .git/info/sparse-checkout
290 test_git_path GIT_COMMON_DIR=bar info//sparse-checkout    .git/info//sparse-checkout
291 test_git_path GIT_COMMON_DIR=bar remotes/bar              bar/remotes/bar
292 test_git_path GIT_COMMON_DIR=bar branches/bar             bar/branches/bar
293 test_git_path GIT_COMMON_DIR=bar logs/refs/heads/master   bar/logs/refs/heads/master
294 test_git_path GIT_COMMON_DIR=bar refs/heads/master        bar/refs/heads/master
295 test_git_path GIT_COMMON_DIR=bar refs/bisect/foo          .git/refs/bisect/foo
296 test_git_path GIT_COMMON_DIR=bar hooks/me                 bar/hooks/me
297 test_git_path GIT_COMMON_DIR=bar config                   bar/config
298 test_git_path GIT_COMMON_DIR=bar packed-refs              bar/packed-refs
299 test_git_path GIT_COMMON_DIR=bar shallow                  bar/shallow
300
301 test_done