]> git.ipfire.org Git - thirdparty/git.git/blame - t/t0060-path-utils.sh
path.c: refactor relative_path(), not only strip prefix
[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
JS
10norm_path() {
11 test_expect_success $3 "normalize path: $1 => $2" \
f42302b4 12 "test \"\$(test-path-utils normalize_path_copy '$1')\" = '$2'"
ae299be0
DR
13}
14
203439b2
JX
15relative_path() {
16 test_expect_success $4 "relative path: $1 $2 => $3" \
17 "test \"\$(test-path-utils relative_path '$1' '$2')\" = '$3'"
18}
19
2718e852
JS
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:
23rootoff=$(test-path-utils normalize_path_copy / | wc -c)
24# Account for the trailing LF:
28baf82e 25if test $rootoff = 2; then
2718e852
JS
26 rootoff= # we are on Unix
27else
28 rootoff=$(($rootoff-1))
29fi
30
0454dd93 31ancestor() {
2718e852
JS
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))
36 fi
37 test_expect_success "longest ancestor: $1 $2 => $expected" \
38 "actual=\$(test-path-utils longest_ancestor_length '$1' '$2') &&
39 test \"\$actual\" = '$expected'"
0454dd93
DR
40}
41
2718e852
JS
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
44case $(uname -s) in
45*MINGW*)
46 ;;
47*)
48 test_set_prereq POSIX
49 ;;
50esac
51
52norm_path "" ""
53norm_path . ""
54norm_path ./ ""
55norm_path ./. ""
56norm_path ./.. ++failed++
57norm_path ../. ++failed++
58norm_path ./../.// ++failed++
59norm_path dir/.. ""
60norm_path dir/sub/../.. ""
61norm_path dir/sub/../../.. ++failed++
62norm_path dir dir
63norm_path dir// dir/
64norm_path ./dir dir
65norm_path dir/. dir/
66norm_path dir///./ dir/
67norm_path dir//sub/.. dir/
68norm_path dir/sub/../ dir/
69norm_path dir/sub/../. dir/
70norm_path dir/s1/../s2/ dir/s2/
71norm_path d1/s1///s2/..//../s3/ d1/s3/
72norm_path d1/s1//../s2/../../d2 d2
73norm_path d1/.../d2 d1/.../d2
74norm_path d1/..././../d2 d1/d2
75
76norm_path / / POSIX
77norm_path // / POSIX
78norm_path /// / POSIX
79norm_path /. / POSIX
80norm_path /./ / POSIX
81norm_path /./.. ++failed++ POSIX
82norm_path /../. ++failed++ POSIX
83norm_path /./../.// ++failed++ POSIX
84norm_path /dir/.. / POSIX
85norm_path /dir/sub/../.. / POSIX
86norm_path /dir/sub/../../.. ++failed++ POSIX
87norm_path /dir /dir POSIX
88norm_path /dir// /dir/ POSIX
89norm_path /./dir /dir POSIX
90norm_path /dir/. /dir/ POSIX
91norm_path /dir///./ /dir/ POSIX
92norm_path /dir//sub/.. /dir/ POSIX
93norm_path /dir/sub/../ /dir/ POSIX
94norm_path //dir/sub/../. /dir/ POSIX
95norm_path /dir/s1/../s2/ /dir/s2/ POSIX
96norm_path /d1/s1///s2/..//../s3/ /d1/s3/ POSIX
97norm_path /d1/s1//../s2/../../d2 /d2 POSIX
98norm_path /d1/.../d2 /d1/.../d2 POSIX
99norm_path /d1/..././../d2 /d1/d2 POSIX
ae299be0 100
0454dd93 101ancestor / / -1
0454dd93
DR
102ancestor /foo / 0
103ancestor /foo /fo -1
104ancestor /foo /foo -1
0454dd93 105ancestor /foo /bar -1
0454dd93 106ancestor /foo /foo/bar -1
9e2326c7
MH
107ancestor /foo /foo:/bar -1
108ancestor /foo /:/foo:/bar 0
109ancestor /foo /foo:/:/bar 0
110ancestor /foo /:/bar:/foo 0
0454dd93
DR
111ancestor /foo/bar / 0
112ancestor /foo/bar /fo -1
0454dd93 113ancestor /foo/bar /foo 4
0454dd93
DR
114ancestor /foo/bar /foo/ba -1
115ancestor /foo/bar /:/fo 0
116ancestor /foo/bar /foo:/foo/ba 4
117ancestor /foo/bar /bar -1
9e2326c7
MH
118ancestor /foo/bar /fo -1
119ancestor /foo/bar /foo:/bar 4
120ancestor /foo/bar /:/foo:/bar 4
121ancestor /foo/bar /foo:/:/bar 4
122ancestor /foo/bar /:/bar:/fo 0
123ancestor /foo/bar /:/bar 0
124ancestor /foo/bar /foo 4
125ancestor /foo/bar /foo:/bar 4
126ancestor /foo/bar /bar -1
0454dd93 127
4fcc86b0
JS
128test_expect_success 'strip_path_suffix' '
129 test c:/msysgit = $(test-path-utils strip_path_suffix \
130 c:/msysgit/libexec//git-core libexec/git-core)
131'
8da650b4 132
a0601dc1 133test_expect_success 'absolute path rejects the empty string' '
17264bcc
MH
134 test_must_fail test-path-utils absolute_path ""
135'
136
3efe5d1d 137test_expect_success 'real path rejects the empty string' '
a5c45218
MH
138 test_must_fail test-path-utils real_path ""
139'
140
bacca785 141test_expect_success POSIX 'real path works on absolute paths 1' '
7bcf48da
MH
142 nopath="hopefully-absent-path" &&
143 test "/" = "$(test-path-utils real_path "/")" &&
bacca785
JS
144 test "/$nopath" = "$(test-path-utils real_path "/$nopath")"
145'
146
147test_expect_success 'real path works on absolute paths 2' '
148 nopath="hopefully-absent-path" &&
7bcf48da
MH
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")"
153'
154
379a03ad
MH
155test_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")"
163'
164
165test_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")"
171'
172
7bcf48da 173test_expect_success SYMLINKS 'real path works on symlinks' '
8da650b4
MH
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")"
189'
190
203439b2
JX
191relative_path /a/b/c/ /a/b/ c/
192relative_path /a/b/c/ /a/b c/
193relative_path /a//b//c/ //a/b// c/ POSIX
e02ca72f
JX
194relative_path /a/b /a/b ./
195relative_path /a/b/ /a/b ./
196relative_path /a /a/b ../
197relative_path / /a/b/ ../../
198relative_path /a/c /a/b/ ../c
199relative_path /a/c /a/b ../c
200relative_path /x/y /a/b/ ../../x/y
203439b2
JX
201relative_path /a/b "<empty>" /a/b POSIX
202relative_path /a/b "<null>" /a/b POSIX
203relative_path a/b/c/ a/b/ c/
204relative_path a/b/c/ a/b c/
205relative_path a/b//c a//b c
e02ca72f
JX
206relative_path a/b/ a/b/ ./
207relative_path a/b/ a/b ./
208relative_path a a/b ../
209relative_path x/y a/b ../../x/y
210relative_path a/c a/b ../c
203439b2
JX
211relative_path a/b "<empty>" a/b
212relative_path a/b "<null>" a/b
e02ca72f
JX
213relative_path "<empty>" /a/b ./
214relative_path "<empty>" "<empty>" ./
215relative_path "<empty>" "<null>" ./
216relative_path "<null>" "<empty>" ./
217relative_path "<null>" "<null>" ./
218relative_path "<null>" /a/b ./
203439b2 219
ae299be0 220test_done