]> git.ipfire.org Git - thirdparty/git.git/blame - t/t0060-path-utils.sh
t0060: verify that basename() and dirname() work as expected
[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))
39fi
40
0454dd93 41ancestor() {
2718e852
JS
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))
46 fi
47 test_expect_success "longest ancestor: $1 $2 => $expected" \
48 "actual=\$(test-path-utils longest_ancestor_length '$1' '$2') &&
49 test \"\$actual\" = '$expected'"
0454dd93
DR
50}
51
abd4284b
JX
52# Some absolute path tests should be skipped on Windows due to path mangling
53# on POSIX-style absolute paths
2718e852
JS
54case $(uname -s) in
55*MINGW*)
56 ;;
57*)
58 test_set_prereq POSIX
59 ;;
60esac
61
7d1aaa68
JS
62test_expect_success basename 'test-path-utils basename'
63test_expect_success dirname 'test-path-utils dirname'
64
2718e852
JS
65norm_path "" ""
66norm_path . ""
67norm_path ./ ""
68norm_path ./. ""
69norm_path ./.. ++failed++
70norm_path ../. ++failed++
71norm_path ./../.// ++failed++
72norm_path dir/.. ""
73norm_path dir/sub/../.. ""
74norm_path dir/sub/../../.. ++failed++
75norm_path dir dir
76norm_path dir// dir/
77norm_path ./dir dir
78norm_path dir/. dir/
79norm_path dir///./ dir/
80norm_path dir//sub/.. dir/
81norm_path dir/sub/../ dir/
82norm_path dir/sub/../. dir/
83norm_path dir/s1/../s2/ dir/s2/
84norm_path d1/s1///s2/..//../s3/ d1/s3/
85norm_path d1/s1//../s2/../../d2 d2
86norm_path d1/.../d2 d1/.../d2
87norm_path d1/..././../d2 d1/d2
88
abd4284b 89norm_path / /
2718e852
JS
90norm_path // / POSIX
91norm_path /// / POSIX
abd4284b 92norm_path /. /
2718e852
JS
93norm_path /./ / POSIX
94norm_path /./.. ++failed++ POSIX
abd4284b 95norm_path /../. ++failed++
2718e852
JS
96norm_path /./../.// ++failed++ POSIX
97norm_path /dir/.. / POSIX
98norm_path /dir/sub/../.. / POSIX
99norm_path /dir/sub/../../.. ++failed++ POSIX
abd4284b
JX
100norm_path /dir /dir
101norm_path /dir// /dir/
102norm_path /./dir /dir
103norm_path /dir/. /dir/
104norm_path /dir///./ /dir/
105norm_path /dir//sub/.. /dir/
106norm_path /dir/sub/../ /dir/
2718e852 107norm_path //dir/sub/../. /dir/ POSIX
abd4284b
JX
108norm_path /dir/s1/../s2/ /dir/s2/
109norm_path /d1/s1///s2/..//../s3/ /d1/s3/
110norm_path /d1/s1//../s2/../../d2 /d2
111norm_path /d1/.../d2 /d1/.../d2
112norm_path /d1/..././../d2 /d1/d2
ae299be0 113
0454dd93 114ancestor / / -1
0454dd93
DR
115ancestor /foo / 0
116ancestor /foo /fo -1
117ancestor /foo /foo -1
0454dd93 118ancestor /foo /bar -1
0454dd93 119ancestor /foo /foo/bar -1
9e2326c7
MH
120ancestor /foo /foo:/bar -1
121ancestor /foo /:/foo:/bar 0
122ancestor /foo /foo:/:/bar 0
123ancestor /foo /:/bar:/foo 0
0454dd93
DR
124ancestor /foo/bar / 0
125ancestor /foo/bar /fo -1
0454dd93 126ancestor /foo/bar /foo 4
0454dd93
DR
127ancestor /foo/bar /foo/ba -1
128ancestor /foo/bar /:/fo 0
129ancestor /foo/bar /foo:/foo/ba 4
130ancestor /foo/bar /bar -1
9e2326c7
MH
131ancestor /foo/bar /fo -1
132ancestor /foo/bar /foo:/bar 4
133ancestor /foo/bar /:/foo:/bar 4
134ancestor /foo/bar /foo:/:/bar 4
135ancestor /foo/bar /:/bar:/fo 0
136ancestor /foo/bar /:/bar 0
137ancestor /foo/bar /foo 4
138ancestor /foo/bar /foo:/bar 4
139ancestor /foo/bar /bar -1
0454dd93 140
4fcc86b0
JS
141test_expect_success 'strip_path_suffix' '
142 test c:/msysgit = $(test-path-utils strip_path_suffix \
143 c:/msysgit/libexec//git-core libexec/git-core)
144'
8da650b4 145
a0601dc1 146test_expect_success 'absolute path rejects the empty string' '
17264bcc
MH
147 test_must_fail test-path-utils absolute_path ""
148'
149
3efe5d1d 150test_expect_success 'real path rejects the empty string' '
a5c45218
MH
151 test_must_fail test-path-utils real_path ""
152'
153
bacca785 154test_expect_success POSIX 'real path works on absolute paths 1' '
7bcf48da
MH
155 nopath="hopefully-absent-path" &&
156 test "/" = "$(test-path-utils real_path "/")" &&
bacca785
JS
157 test "/$nopath" = "$(test-path-utils real_path "/$nopath")"
158'
159
160test_expect_success 'real path works on absolute paths 2' '
161 nopath="hopefully-absent-path" &&
7bcf48da
MH
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")"
166'
167
379a03ad
MH
168test_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")"
176'
177
178test_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")"
184'
185
7bcf48da 186test_expect_success SYMLINKS 'real path works on symlinks' '
8da650b4
MH
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")"
202'
203
655ee9ea 204test_expect_success SYMLINKS 'prefix_path works with absolute paths to work tree symlinks' '
74af95d6
MEW
205 ln -s target symlink &&
206 test "$(test-path-utils prefix_path prefix "$(pwd)/symlink")" = "symlink"
207'
208
e5aa1fc4
MEW
209test_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
213'
214
e131daa4
MEW
215test_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"
217'
218
219test_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")"
223'
224
daf19a80
JX
225relative_path /foo/a/b/c/ /foo/a/b/ c/
226relative_path /foo/a/b/c/ /foo/a/b c/
227relative_path /foo/a//b//c/ ///foo/a/b// c/ POSIX
228relative_path /foo/a/b /foo/a/b ./
229relative_path /foo/a/b/ /foo/a/b ./
230relative_path /foo/a /foo/a/b ../
231relative_path / /foo/a/b/ ../../../
232relative_path /foo/a/c /foo/a/b/ ../c
233relative_path /foo/a/c /foo/a/b ../c
234relative_path /foo/x/y /foo/a/b/ ../../x/y
235relative_path /foo/a/b "<empty>" /foo/a/b
236relative_path /foo/a/b "<null>" /foo/a/b
237relative_path foo/a/b/c/ foo/a/b/ c/
238relative_path foo/a/b/c/ foo/a/b c/
239relative_path foo/a/b//c foo/a//b c
240relative_path foo/a/b/ foo/a/b/ ./
241relative_path foo/a/b/ foo/a/b ./
242relative_path foo/a foo/a/b ../
243relative_path foo/x/y foo/a/b ../../x/y
244relative_path foo/a/c foo/a/b ../c
7fbd4221
JX
245relative_path foo/a/b /foo/x/y foo/a/b
246relative_path /foo/a/b foo/x/y /foo/a/b
247relative_path d:/a/b D:/a/c ../b MINGW
248relative_path C:/a/b D:/a/c C:/a/b MINGW
daf19a80
JX
249relative_path foo/a/b "<empty>" foo/a/b
250relative_path foo/a/b "<null>" foo/a/b
251relative_path "<empty>" /foo/a/b ./
252relative_path "<empty>" "<empty>" ./
253relative_path "<empty>" "<null>" ./
254relative_path "<null>" "<empty>" ./
255relative_path "<null>" "<null>" ./
256relative_path "<null>" /foo/a/b ./
203439b2 257
557bd833
NTND
258test_git_path A=B info/grafts .git/info/grafts
259test_git_path GIT_GRAFT_FILE=foo info/grafts foo
260test_git_path GIT_GRAFT_FILE=foo info/////grafts foo
261test_git_path GIT_INDEX_FILE=foo index foo
262test_git_path GIT_INDEX_FILE=foo index/foo .git/index/foo
263test_git_path GIT_INDEX_FILE=foo index2 .git/index2
264test_expect_success 'setup fake objects directory foo' 'mkdir foo'
265test_git_path GIT_OBJECT_DIRECTORY=foo objects foo
266test_git_path GIT_OBJECT_DIRECTORY=foo objects/foo foo/foo
267test_git_path GIT_OBJECT_DIRECTORY=foo objects2 .git/objects2
c7b3a3d2
NTND
268test_expect_success 'setup common repository' 'git --git-dir=bar init'
269test_git_path GIT_COMMON_DIR=bar index .git/index
270test_git_path GIT_COMMON_DIR=bar HEAD .git/HEAD
271test_git_path GIT_COMMON_DIR=bar logs/HEAD .git/logs/HEAD
272test_git_path GIT_COMMON_DIR=bar objects bar/objects
273test_git_path GIT_COMMON_DIR=bar objects/bar bar/objects/bar
274test_git_path GIT_COMMON_DIR=bar info/exclude bar/info/exclude
275test_git_path GIT_COMMON_DIR=bar info/grafts bar/info/grafts
6cfbdcb2 276test_git_path GIT_COMMON_DIR=bar info/sparse-checkout .git/info/sparse-checkout
c7b3a3d2
NTND
277test_git_path GIT_COMMON_DIR=bar remotes/bar bar/remotes/bar
278test_git_path GIT_COMMON_DIR=bar branches/bar bar/branches/bar
279test_git_path GIT_COMMON_DIR=bar logs/refs/heads/master bar/logs/refs/heads/master
280test_git_path GIT_COMMON_DIR=bar refs/heads/master bar/refs/heads/master
281test_git_path GIT_COMMON_DIR=bar hooks/me bar/hooks/me
282test_git_path GIT_COMMON_DIR=bar config bar/config
283test_git_path GIT_COMMON_DIR=bar packed-refs bar/packed-refs
284test_git_path GIT_COMMON_DIR=bar shallow bar/shallow
557bd833 285
ae299be0 286test_done