]> git.ipfire.org Git - thirdparty/git.git/blame - t/t0060-path-utils.sh
Merge branch 'sb/submodule-path-misc-bugs' into sb/submodule-init
[thirdparty/git.git] / t / t0060-path-utils.sh
CommitLineData
ae299be0
DR
1#!/bin/sh
2#
3# Copyright (c) 2008 David Reiss
4#
5
6test_description='Test various path utilities'
7
8. ./test-lib.sh
9
2718e852 10norm_path() {
7ffd18fc 11 expected=$(test-path-utils print_path "$2")
2718e852 12 test_expect_success $3 "normalize path: $1 => $2" \
abd4284b 13 "test \"\$(test-path-utils normalize_path_copy '$1')\" = '$expected'"
ae299be0
DR
14}
15
203439b2 16relative_path() {
7ffd18fc 17 expected=$(test-path-utils print_path "$3")
203439b2 18 test_expect_success $4 "relative path: $1 $2 => $3" \
abd4284b 19 "test \"\$(test-path-utils relative_path '$1' '$2')\" = '$expected'"
203439b2
JX
20}
21
557bd833
NTND
22test_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
2718e852
JS
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:
33rootoff=$(test-path-utils normalize_path_copy / | wc -c)
34# Account for the trailing LF:
28baf82e 35if test $rootoff = 2; then
2718e852
JS
36 rootoff= # we are on Unix
37else
38 rootoff=$(($rootoff-1))
fc56c7b3
JS
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
2718e852
JS
46fi
47
0454dd93 48ancestor() {
2718e852
JS
49 # We do some math with the expected ancestor length.
50 expected=$3
51 if test -n "$rootoff" && test "x$expected" != x-1; then
fc56c7b3
JS
52 expected=$(($expected-$rootslash))
53 test $expected -lt 0 ||
2718e852
JS
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'"
0454dd93
DR
59}
60
abd4284b
JX
61# Some absolute path tests should be skipped on Windows due to path mangling
62# on POSIX-style absolute paths
2718e852
JS
63case $(uname -s) in
64*MINGW*)
65 ;;
66*)
67 test_set_prereq POSIX
68 ;;
69esac
70
7d1aaa68
JS
71test_expect_success basename 'test-path-utils basename'
72test_expect_success dirname 'test-path-utils dirname'
73
2718e852
JS
74norm_path "" ""
75norm_path . ""
76norm_path ./ ""
77norm_path ./. ""
78norm_path ./.. ++failed++
79norm_path ../. ++failed++
80norm_path ./../.// ++failed++
81norm_path dir/.. ""
82norm_path dir/sub/../.. ""
83norm_path dir/sub/../../.. ++failed++
84norm_path dir dir
85norm_path dir// dir/
86norm_path ./dir dir
87norm_path dir/. dir/
88norm_path dir///./ dir/
89norm_path dir//sub/.. dir/
90norm_path dir/sub/../ dir/
91norm_path dir/sub/../. dir/
92norm_path dir/s1/../s2/ dir/s2/
93norm_path d1/s1///s2/..//../s3/ d1/s3/
94norm_path d1/s1//../s2/../../d2 d2
95norm_path d1/.../d2 d1/.../d2
96norm_path d1/..././../d2 d1/d2
97
abd4284b 98norm_path / /
2718e852
JS
99norm_path // / POSIX
100norm_path /// / POSIX
abd4284b 101norm_path /. /
2718e852
JS
102norm_path /./ / POSIX
103norm_path /./.. ++failed++ POSIX
abd4284b 104norm_path /../. ++failed++
2718e852
JS
105norm_path /./../.// ++failed++ POSIX
106norm_path /dir/.. / POSIX
107norm_path /dir/sub/../.. / POSIX
108norm_path /dir/sub/../../.. ++failed++ POSIX
abd4284b
JX
109norm_path /dir /dir
110norm_path /dir// /dir/
111norm_path /./dir /dir
112norm_path /dir/. /dir/
113norm_path /dir///./ /dir/
114norm_path /dir//sub/.. /dir/
115norm_path /dir/sub/../ /dir/
2718e852 116norm_path //dir/sub/../. /dir/ POSIX
abd4284b
JX
117norm_path /dir/s1/../s2/ /dir/s2/
118norm_path /d1/s1///s2/..//../s3/ /d1/s3/
119norm_path /d1/s1//../s2/../../d2 /d2
120norm_path /d1/.../d2 /d1/.../d2
121norm_path /d1/..././../d2 /d1/d2
ae299be0 122
0454dd93 123ancestor / / -1
0454dd93
DR
124ancestor /foo / 0
125ancestor /foo /fo -1
126ancestor /foo /foo -1
0454dd93 127ancestor /foo /bar -1
0454dd93 128ancestor /foo /foo/bar -1
9e2326c7
MH
129ancestor /foo /foo:/bar -1
130ancestor /foo /:/foo:/bar 0
131ancestor /foo /foo:/:/bar 0
132ancestor /foo /:/bar:/foo 0
0454dd93
DR
133ancestor /foo/bar / 0
134ancestor /foo/bar /fo -1
0454dd93 135ancestor /foo/bar /foo 4
0454dd93
DR
136ancestor /foo/bar /foo/ba -1
137ancestor /foo/bar /:/fo 0
138ancestor /foo/bar /foo:/foo/ba 4
139ancestor /foo/bar /bar -1
9e2326c7
MH
140ancestor /foo/bar /fo -1
141ancestor /foo/bar /foo:/bar 4
142ancestor /foo/bar /:/foo:/bar 4
143ancestor /foo/bar /foo:/:/bar 4
144ancestor /foo/bar /:/bar:/fo 0
145ancestor /foo/bar /:/bar 0
146ancestor /foo/bar /foo 4
147ancestor /foo/bar /foo:/bar 4
148ancestor /foo/bar /bar -1
0454dd93 149
4fcc86b0
JS
150test_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'
8da650b4 154
a0601dc1 155test_expect_success 'absolute path rejects the empty string' '
17264bcc
MH
156 test_must_fail test-path-utils absolute_path ""
157'
158
3efe5d1d 159test_expect_success 'real path rejects the empty string' '
a5c45218
MH
160 test_must_fail test-path-utils real_path ""
161'
162
bacca785 163test_expect_success POSIX 'real path works on absolute paths 1' '
7bcf48da
MH
164 nopath="hopefully-absent-path" &&
165 test "/" = "$(test-path-utils real_path "/")" &&
bacca785
JS
166 test "/$nopath" = "$(test-path-utils real_path "/$nopath")"
167'
168
169test_expect_success 'real path works on absolute paths 2' '
170 nopath="hopefully-absent-path" &&
7bcf48da
MH
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
379a03ad
MH
177test_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
187test_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
7bcf48da 195test_expect_success SYMLINKS 'real path works on symlinks' '
8da650b4
MH
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
655ee9ea 213test_expect_success SYMLINKS 'prefix_path works with absolute paths to work tree symlinks' '
74af95d6
MEW
214 ln -s target symlink &&
215 test "$(test-path-utils prefix_path prefix "$(pwd)/symlink")" = "symlink"
216'
217
e5aa1fc4
MEW
218test_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
e131daa4
MEW
224test_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
228test_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
daf19a80
JX
234relative_path /foo/a/b/c/ /foo/a/b/ c/
235relative_path /foo/a/b/c/ /foo/a/b c/
236relative_path /foo/a//b//c/ ///foo/a/b// c/ POSIX
237relative_path /foo/a/b /foo/a/b ./
238relative_path /foo/a/b/ /foo/a/b ./
239relative_path /foo/a /foo/a/b ../
240relative_path / /foo/a/b/ ../../../
241relative_path /foo/a/c /foo/a/b/ ../c
242relative_path /foo/a/c /foo/a/b ../c
243relative_path /foo/x/y /foo/a/b/ ../../x/y
244relative_path /foo/a/b "<empty>" /foo/a/b
245relative_path /foo/a/b "<null>" /foo/a/b
246relative_path foo/a/b/c/ foo/a/b/ c/
247relative_path foo/a/b/c/ foo/a/b c/
248relative_path foo/a/b//c foo/a//b c
249relative_path foo/a/b/ foo/a/b/ ./
250relative_path foo/a/b/ foo/a/b ./
251relative_path foo/a foo/a/b ../
252relative_path foo/x/y foo/a/b ../../x/y
253relative_path foo/a/c foo/a/b ../c
7fbd4221
JX
254relative_path foo/a/b /foo/x/y foo/a/b
255relative_path /foo/a/b foo/x/y /foo/a/b
256relative_path d:/a/b D:/a/c ../b MINGW
257relative_path C:/a/b D:/a/c C:/a/b MINGW
daf19a80
JX
258relative_path foo/a/b "<empty>" foo/a/b
259relative_path foo/a/b "<null>" foo/a/b
260relative_path "<empty>" /foo/a/b ./
261relative_path "<empty>" "<empty>" ./
262relative_path "<empty>" "<null>" ./
263relative_path "<null>" "<empty>" ./
264relative_path "<null>" "<null>" ./
265relative_path "<null>" /foo/a/b ./
203439b2 266
557bd833
NTND
267test_git_path A=B info/grafts .git/info/grafts
268test_git_path GIT_GRAFT_FILE=foo info/grafts foo
269test_git_path GIT_GRAFT_FILE=foo info/////grafts foo
270test_git_path GIT_INDEX_FILE=foo index foo
271test_git_path GIT_INDEX_FILE=foo index/foo .git/index/foo
272test_git_path GIT_INDEX_FILE=foo index2 .git/index2
273test_expect_success 'setup fake objects directory foo' 'mkdir foo'
274test_git_path GIT_OBJECT_DIRECTORY=foo objects foo
275test_git_path GIT_OBJECT_DIRECTORY=foo objects/foo foo/foo
276test_git_path GIT_OBJECT_DIRECTORY=foo objects2 .git/objects2
c7b3a3d2
NTND
277test_expect_success 'setup common repository' 'git --git-dir=bar init'
278test_git_path GIT_COMMON_DIR=bar index .git/index
279test_git_path GIT_COMMON_DIR=bar HEAD .git/HEAD
280test_git_path GIT_COMMON_DIR=bar logs/HEAD .git/logs/HEAD
ce414b33
DT
281test_git_path GIT_COMMON_DIR=bar logs/refs/bisect/foo .git/logs/refs/bisect/foo
282test_git_path GIT_COMMON_DIR=bar logs/refs/bisec/foo bar/logs/refs/bisec/foo
283test_git_path GIT_COMMON_DIR=bar logs/refs/bisec bar/logs/refs/bisec
284test_git_path GIT_COMMON_DIR=bar logs/refs/bisectfoo bar/logs/refs/bisectfoo
c7b3a3d2
NTND
285test_git_path GIT_COMMON_DIR=bar objects bar/objects
286test_git_path GIT_COMMON_DIR=bar objects/bar bar/objects/bar
287test_git_path GIT_COMMON_DIR=bar info/exclude bar/info/exclude
288test_git_path GIT_COMMON_DIR=bar info/grafts bar/info/grafts
6cfbdcb2 289test_git_path GIT_COMMON_DIR=bar info/sparse-checkout .git/info/sparse-checkout
4e09cf2a 290test_git_path GIT_COMMON_DIR=bar info//sparse-checkout .git/info//sparse-checkout
c7b3a3d2
NTND
291test_git_path GIT_COMMON_DIR=bar remotes/bar bar/remotes/bar
292test_git_path GIT_COMMON_DIR=bar branches/bar bar/branches/bar
293test_git_path GIT_COMMON_DIR=bar logs/refs/heads/master bar/logs/refs/heads/master
294test_git_path GIT_COMMON_DIR=bar refs/heads/master bar/refs/heads/master
ce414b33 295test_git_path GIT_COMMON_DIR=bar refs/bisect/foo .git/refs/bisect/foo
c7b3a3d2
NTND
296test_git_path GIT_COMMON_DIR=bar hooks/me bar/hooks/me
297test_git_path GIT_COMMON_DIR=bar config bar/config
298test_git_path GIT_COMMON_DIR=bar packed-refs bar/packed-refs
299test_git_path GIT_COMMON_DIR=bar shallow bar/shallow
557bd833 300
ae299be0 301test_done