prepare_{git,shell}_cmd: use argv_array
[alt-git.git] / t / t0060-path-utils.sh
blobf0152a7ab4aabd4f47f546c038efcd8503adc9a4
1 #!/bin/sh
3 # Copyright (c) 2008 David Reiss
6 test_description='Test various path utilities'
8 . ./test-lib.sh
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'"
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'"
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
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))
41 ancestor() {
42 # We do some math with the expected ancestor length.
43 expected=$3
44 if test -n "$rootoff" && test "x$expected" != x-1; then
45 expected=$(($expected+$rootoff))
47 test_expect_success "longest ancestor: $1 $2 => $expected" \
48 "actual=\$(test-path-utils longest_ancestor_length '$1' '$2') &&
49 test \"\$actual\" = '$expected'"
52 # Some absolute path tests should be skipped on Windows due to path mangling
53 # on POSIX-style absolute paths
54 case $(uname -s) in
55 *MINGW*)
58 test_set_prereq POSIX
60 esac
62 test_expect_success basename 'test-path-utils basename'
63 test_expect_success dirname 'test-path-utils dirname'
65 norm_path "" ""
66 norm_path . ""
67 norm_path ./ ""
68 norm_path ./. ""
69 norm_path ./.. ++failed++
70 norm_path ../. ++failed++
71 norm_path ./../.// ++failed++
72 norm_path dir/.. ""
73 norm_path dir/sub/../.. ""
74 norm_path dir/sub/../../.. ++failed++
75 norm_path dir dir
76 norm_path dir// dir/
77 norm_path ./dir dir
78 norm_path dir/. dir/
79 norm_path dir///./ dir/
80 norm_path dir//sub/.. dir/
81 norm_path dir/sub/../ dir/
82 norm_path dir/sub/../. dir/
83 norm_path dir/s1/../s2/ dir/s2/
84 norm_path d1/s1///s2/..//../s3/ d1/s3/
85 norm_path d1/s1//../s2/../../d2 d2
86 norm_path d1/.../d2 d1/.../d2
87 norm_path d1/..././../d2 d1/d2
89 norm_path / /
90 norm_path // / POSIX
91 norm_path /// / POSIX
92 norm_path /. /
93 norm_path /./ / POSIX
94 norm_path /./.. ++failed++ POSIX
95 norm_path /../. ++failed++
96 norm_path /./../.// ++failed++ POSIX
97 norm_path /dir/.. / POSIX
98 norm_path /dir/sub/../.. / POSIX
99 norm_path /dir/sub/../../.. ++failed++ POSIX
100 norm_path /dir /dir
101 norm_path /dir// /dir/
102 norm_path /./dir /dir
103 norm_path /dir/. /dir/
104 norm_path /dir///./ /dir/
105 norm_path /dir//sub/.. /dir/
106 norm_path /dir/sub/../ /dir/
107 norm_path //dir/sub/../. /dir/ POSIX
108 norm_path /dir/s1/../s2/ /dir/s2/
109 norm_path /d1/s1///s2/..//../s3/ /d1/s3/
110 norm_path /d1/s1//../s2/../../d2 /d2
111 norm_path /d1/.../d2 /d1/.../d2
112 norm_path /d1/..././../d2 /d1/d2
114 ancestor / / -1
115 ancestor /foo / 0
116 ancestor /foo /fo -1
117 ancestor /foo /foo -1
118 ancestor /foo /bar -1
119 ancestor /foo /foo/bar -1
120 ancestor /foo /foo:/bar -1
121 ancestor /foo /:/foo:/bar 0
122 ancestor /foo /foo:/:/bar 0
123 ancestor /foo /:/bar:/foo 0
124 ancestor /foo/bar / 0
125 ancestor /foo/bar /fo -1
126 ancestor /foo/bar /foo 4
127 ancestor /foo/bar /foo/ba -1
128 ancestor /foo/bar /:/fo 0
129 ancestor /foo/bar /foo:/foo/ba 4
130 ancestor /foo/bar /bar -1
131 ancestor /foo/bar /fo -1
132 ancestor /foo/bar /foo:/bar 4
133 ancestor /foo/bar /:/foo:/bar 4
134 ancestor /foo/bar /foo:/:/bar 4
135 ancestor /foo/bar /:/bar:/fo 0
136 ancestor /foo/bar /:/bar 0
137 ancestor /foo/bar /foo 4
138 ancestor /foo/bar /foo:/bar 4
139 ancestor /foo/bar /bar -1
141 test_expect_success 'strip_path_suffix' '
142 test c:/msysgit = $(test-path-utils strip_path_suffix \
143 c:/msysgit/libexec//git-core libexec/git-core)
146 test_expect_success 'absolute path rejects the empty string' '
147 test_must_fail test-path-utils absolute_path ""
150 test_expect_success 'real path rejects the empty string' '
151 test_must_fail test-path-utils real_path ""
154 test_expect_success POSIX 'real path works on absolute paths 1' '
155 nopath="hopefully-absent-path" &&
156 test "/" = "$(test-path-utils real_path "/")" &&
157 test "/$nopath" = "$(test-path-utils real_path "/$nopath")"
160 test_expect_success 'real path works on absolute paths 2' '
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 POSIX 'real path removes extra leading slashes' '
169 nopath="hopefully-absent-path" &&
170 test "/" = "$(test-path-utils real_path "///")" &&
171 test "/$nopath" = "$(test-path-utils real_path "///$nopath")" &&
172 # Find an existing top-level directory for the remaining tests:
173 d=$(pwd -P | sed -e "s|^\([^/]*/[^/]*\)/.*|\1|") &&
174 test "$d" = "$(test-path-utils real_path "//$d")" &&
175 test "$d/$nopath" = "$(test-path-utils real_path "//$d/$nopath")"
178 test_expect_success 'real path removes other extra slashes' '
179 nopath="hopefully-absent-path" &&
180 # Find an existing top-level directory for the remaining tests:
181 d=$(pwd -P | sed -e "s|^\([^/]*/[^/]*\)/.*|\1|") &&
182 test "$d" = "$(test-path-utils real_path "$d///")" &&
183 test "$d/$nopath" = "$(test-path-utils real_path "$d///$nopath")"
186 test_expect_success SYMLINKS 'real path works on symlinks' '
187 mkdir first &&
188 ln -s ../.git first/.git &&
189 mkdir second &&
190 ln -s ../first second/other &&
191 mkdir third &&
192 dir="$(cd .git; pwd -P)" &&
193 dir2=third/../second/other/.git &&
194 test "$dir" = "$(test-path-utils real_path $dir2)" &&
195 file="$dir"/index &&
196 test "$file" = "$(test-path-utils real_path $dir2/index)" &&
197 basename=blub &&
198 test "$dir/$basename" = "$(cd .git && test-path-utils real_path "$basename")" &&
199 ln -s ../first/file .git/syml &&
200 sym="$(cd first; pwd -P)"/file &&
201 test "$sym" = "$(test-path-utils real_path "$dir2/syml")"
204 test_expect_success SYMLINKS 'prefix_path works with absolute paths to work tree symlinks' '
205 ln -s target symlink &&
206 test "$(test-path-utils prefix_path prefix "$(pwd)/symlink")" = "symlink"
209 test_expect_success 'prefix_path works with only absolute path to work tree' '
210 echo "" >expected &&
211 test-path-utils prefix_path prefix "$(pwd)" >actual &&
212 test_cmp expected actual
215 test_expect_success 'prefix_path rejects absolute path to dir with same beginning as work tree' '
216 test_must_fail test-path-utils prefix_path prefix "$(pwd)a"
219 test_expect_success SYMLINKS 'prefix_path works with absolute path to a symlink to work tree having same beginning as work tree' '
220 git init repo &&
221 ln -s repo repolink &&
222 test "a" = "$(cd repo && test-path-utils prefix_path prefix "$(pwd)/../repolink/a")"
225 relative_path /foo/a/b/c/ /foo/a/b/ c/
226 relative_path /foo/a/b/c/ /foo/a/b c/
227 relative_path /foo/a//b//c/ ///foo/a/b// c/ POSIX
228 relative_path /foo/a/b /foo/a/b ./
229 relative_path /foo/a/b/ /foo/a/b ./
230 relative_path /foo/a /foo/a/b ../
231 relative_path / /foo/a/b/ ../../../
232 relative_path /foo/a/c /foo/a/b/ ../c
233 relative_path /foo/a/c /foo/a/b ../c
234 relative_path /foo/x/y /foo/a/b/ ../../x/y
235 relative_path /foo/a/b "<empty>" /foo/a/b
236 relative_path /foo/a/b "<null>" /foo/a/b
237 relative_path foo/a/b/c/ foo/a/b/ c/
238 relative_path foo/a/b/c/ foo/a/b c/
239 relative_path foo/a/b//c foo/a//b c
240 relative_path foo/a/b/ foo/a/b/ ./
241 relative_path foo/a/b/ foo/a/b ./
242 relative_path foo/a foo/a/b ../
243 relative_path foo/x/y foo/a/b ../../x/y
244 relative_path foo/a/c foo/a/b ../c
245 relative_path foo/a/b /foo/x/y foo/a/b
246 relative_path /foo/a/b foo/x/y /foo/a/b
247 relative_path d:/a/b D:/a/c ../b MINGW
248 relative_path C:/a/b D:/a/c C:/a/b MINGW
249 relative_path foo/a/b "<empty>" foo/a/b
250 relative_path foo/a/b "<null>" foo/a/b
251 relative_path "<empty>" /foo/a/b ./
252 relative_path "<empty>" "<empty>" ./
253 relative_path "<empty>" "<null>" ./
254 relative_path "<null>" "<empty>" ./
255 relative_path "<null>" "<null>" ./
256 relative_path "<null>" /foo/a/b ./
258 test_git_path A=B info/grafts .git/info/grafts
259 test_git_path GIT_GRAFT_FILE=foo info/grafts foo
260 test_git_path GIT_GRAFT_FILE=foo info/////grafts foo
261 test_git_path GIT_INDEX_FILE=foo index foo
262 test_git_path GIT_INDEX_FILE=foo index/foo .git/index/foo
263 test_git_path GIT_INDEX_FILE=foo index2 .git/index2
264 test_expect_success 'setup fake objects directory foo' 'mkdir foo'
265 test_git_path GIT_OBJECT_DIRECTORY=foo objects foo
266 test_git_path GIT_OBJECT_DIRECTORY=foo objects/foo foo/foo
267 test_git_path GIT_OBJECT_DIRECTORY=foo objects2 .git/objects2
268 test_expect_success 'setup common repository' 'git --git-dir=bar init'
269 test_git_path GIT_COMMON_DIR=bar index .git/index
270 test_git_path GIT_COMMON_DIR=bar HEAD .git/HEAD
271 test_git_path GIT_COMMON_DIR=bar logs/HEAD .git/logs/HEAD
272 test_git_path GIT_COMMON_DIR=bar logs/refs/bisect/foo .git/logs/refs/bisect/foo
273 test_git_path GIT_COMMON_DIR=bar logs/refs/bisec/foo bar/logs/refs/bisec/foo
274 test_git_path GIT_COMMON_DIR=bar logs/refs/bisec bar/logs/refs/bisec
275 test_git_path GIT_COMMON_DIR=bar logs/refs/bisectfoo bar/logs/refs/bisectfoo
276 test_git_path GIT_COMMON_DIR=bar objects bar/objects
277 test_git_path GIT_COMMON_DIR=bar objects/bar bar/objects/bar
278 test_git_path GIT_COMMON_DIR=bar info/exclude bar/info/exclude
279 test_git_path GIT_COMMON_DIR=bar info/grafts bar/info/grafts
280 test_git_path GIT_COMMON_DIR=bar info/sparse-checkout .git/info/sparse-checkout
281 test_git_path GIT_COMMON_DIR=bar info//sparse-checkout .git/info//sparse-checkout
282 test_git_path GIT_COMMON_DIR=bar remotes/bar bar/remotes/bar
283 test_git_path GIT_COMMON_DIR=bar branches/bar bar/branches/bar
284 test_git_path GIT_COMMON_DIR=bar logs/refs/heads/master bar/logs/refs/heads/master
285 test_git_path GIT_COMMON_DIR=bar refs/heads/master bar/refs/heads/master
286 test_git_path GIT_COMMON_DIR=bar refs/bisect/foo .git/refs/bisect/foo
287 test_git_path GIT_COMMON_DIR=bar hooks/me bar/hooks/me
288 test_git_path GIT_COMMON_DIR=bar config bar/config
289 test_git_path GIT_COMMON_DIR=bar packed-refs bar/packed-refs
290 test_git_path GIT_COMMON_DIR=bar shallow bar/shallow
292 test_done