blob: 93605f42f27cef9ef3ffe381c84aea9f6f4be426 [file] [log] [blame]
David Reissae299be2008-05-19 23:48:54 -07001#!/bin/sh
2#
3# Copyright (c) 2008 David Reiss
4#
5
6test_description='Test various path utilities'
7
8. ./test-lib.sh
9
Johannes Sixt2718e852009-03-11 22:15:10 +010010norm_path() {
Sebastian Schuberth7ffd18f2013-10-10 22:49:43 +020011 expected=$(test-path-utils print_path "$2")
Johannes Sixt2718e852009-03-11 22:15:10 +010012 test_expect_success $3 "normalize path: $1 => $2" \
Jiang Xinabd42842013-06-25 23:53:57 +080013 "test \"\$(test-path-utils normalize_path_copy '$1')\" = '$expected'"
David Reissae299be2008-05-19 23:48:54 -070014}
15
Jiang Xin203439b2013-06-25 23:53:42 +080016relative_path() {
Sebastian Schuberth7ffd18f2013-10-10 22:49:43 +020017 expected=$(test-path-utils print_path "$3")
Jiang Xin203439b2013-06-25 23:53:42 +080018 test_expect_success $4 "relative path: $1 $2 => $3" \
Jiang Xinabd42842013-06-25 23:53:57 +080019 "test \"\$(test-path-utils relative_path '$1' '$2')\" = '$expected'"
Jiang Xin203439b2013-06-25 23:53:42 +080020}
21
Nguyễn Thái Ngọc Duy557bd832014-11-30 15:24:31 +070022test_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
Johannes Sixt2718e852009-03-11 22:15:10 +010030# 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:
Jeff King28baf822009-03-23 02:22:29 -040035if test $rootoff = 2; then
Johannes Sixt2718e852009-03-11 22:15:10 +010036 rootoff= # we are on Unix
37else
38 rootoff=$(($rootoff-1))
39fi
40
David Reiss0454dd92008-05-19 23:49:26 -070041ancestor() {
Johannes Sixt2718e852009-03-11 22:15:10 +010042 # 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'"
David Reiss0454dd92008-05-19 23:49:26 -070050}
51
Jiang Xinabd42842013-06-25 23:53:57 +080052# Some absolute path tests should be skipped on Windows due to path mangling
53# on POSIX-style absolute paths
Johannes Sixt2718e852009-03-11 22:15:10 +010054case $(uname -s) in
55*MINGW*)
56 ;;
57*)
58 test_set_prereq POSIX
59 ;;
60esac
61
62norm_path "" ""
63norm_path . ""
64norm_path ./ ""
65norm_path ./. ""
66norm_path ./.. ++failed++
67norm_path ../. ++failed++
68norm_path ./../.// ++failed++
69norm_path dir/.. ""
70norm_path dir/sub/../.. ""
71norm_path dir/sub/../../.. ++failed++
72norm_path dir dir
73norm_path dir// dir/
74norm_path ./dir dir
75norm_path dir/. dir/
76norm_path dir///./ dir/
77norm_path dir//sub/.. dir/
78norm_path dir/sub/../ dir/
79norm_path dir/sub/../. dir/
80norm_path dir/s1/../s2/ dir/s2/
81norm_path d1/s1///s2/..//../s3/ d1/s3/
82norm_path d1/s1//../s2/../../d2 d2
83norm_path d1/.../d2 d1/.../d2
84norm_path d1/..././../d2 d1/d2
85
Jiang Xinabd42842013-06-25 23:53:57 +080086norm_path / /
Johannes Sixt2718e852009-03-11 22:15:10 +010087norm_path // / POSIX
88norm_path /// / POSIX
Jiang Xinabd42842013-06-25 23:53:57 +080089norm_path /. /
Johannes Sixt2718e852009-03-11 22:15:10 +010090norm_path /./ / POSIX
91norm_path /./.. ++failed++ POSIX
Jiang Xinabd42842013-06-25 23:53:57 +080092norm_path /../. ++failed++
Johannes Sixt2718e852009-03-11 22:15:10 +010093norm_path /./../.// ++failed++ POSIX
94norm_path /dir/.. / POSIX
95norm_path /dir/sub/../.. / POSIX
96norm_path /dir/sub/../../.. ++failed++ POSIX
Jiang Xinabd42842013-06-25 23:53:57 +080097norm_path /dir /dir
98norm_path /dir// /dir/
99norm_path /./dir /dir
100norm_path /dir/. /dir/
101norm_path /dir///./ /dir/
102norm_path /dir//sub/.. /dir/
103norm_path /dir/sub/../ /dir/
Johannes Sixt2718e852009-03-11 22:15:10 +0100104norm_path //dir/sub/../. /dir/ POSIX
Jiang Xinabd42842013-06-25 23:53:57 +0800105norm_path /dir/s1/../s2/ /dir/s2/
106norm_path /d1/s1///s2/..//../s3/ /d1/s3/
107norm_path /d1/s1//../s2/../../d2 /d2
108norm_path /d1/.../d2 /d1/.../d2
109norm_path /d1/..././../d2 /d1/d2
David Reissae299be2008-05-19 23:48:54 -0700110
David Reiss0454dd92008-05-19 23:49:26 -0700111ancestor / / -1
David Reiss0454dd92008-05-19 23:49:26 -0700112ancestor /foo / 0
113ancestor /foo /fo -1
114ancestor /foo /foo -1
David Reiss0454dd92008-05-19 23:49:26 -0700115ancestor /foo /bar -1
David Reiss0454dd92008-05-19 23:49:26 -0700116ancestor /foo /foo/bar -1
Michael Haggerty9e2326c2012-10-28 17:16:25 +0100117ancestor /foo /foo:/bar -1
118ancestor /foo /:/foo:/bar 0
119ancestor /foo /foo:/:/bar 0
120ancestor /foo /:/bar:/foo 0
David Reiss0454dd92008-05-19 23:49:26 -0700121ancestor /foo/bar / 0
122ancestor /foo/bar /fo -1
David Reiss0454dd92008-05-19 23:49:26 -0700123ancestor /foo/bar /foo 4
David Reiss0454dd92008-05-19 23:49:26 -0700124ancestor /foo/bar /foo/ba -1
125ancestor /foo/bar /:/fo 0
126ancestor /foo/bar /foo:/foo/ba 4
127ancestor /foo/bar /bar -1
Michael Haggerty9e2326c2012-10-28 17:16:25 +0100128ancestor /foo/bar /fo -1
129ancestor /foo/bar /foo:/bar 4
130ancestor /foo/bar /:/foo:/bar 4
131ancestor /foo/bar /foo:/:/bar 4
132ancestor /foo/bar /:/bar:/fo 0
133ancestor /foo/bar /:/bar 0
134ancestor /foo/bar /foo 4
135ancestor /foo/bar /foo:/bar 4
136ancestor /foo/bar /bar -1
David Reiss0454dd92008-05-19 23:49:26 -0700137
Johannes Schindelin4fcc86b2009-02-19 20:10:49 +0100138test_expect_success 'strip_path_suffix' '
139 test c:/msysgit = $(test-path-utils strip_path_suffix \
140 c:/msysgit/libexec//git-core libexec/git-core)
141'
Michael Haggerty8da650b2012-09-07 00:40:57 +0200142
Michael Haggertya0601dc2012-09-07 00:40:59 +0200143test_expect_success 'absolute path rejects the empty string' '
Michael Haggerty17264bc2012-09-07 00:40:58 +0200144 test_must_fail test-path-utils absolute_path ""
145'
146
Michael Haggerty3efe5d12012-09-07 00:41:01 +0200147test_expect_success 'real path rejects the empty string' '
Michael Haggertya5c45212012-09-07 00:41:00 +0200148 test_must_fail test-path-utils real_path ""
149'
150
Johannes Sixtbacca782012-09-09 17:42:20 +0200151test_expect_success POSIX 'real path works on absolute paths 1' '
Michael Haggerty7bcf48d2012-09-07 00:41:02 +0200152 nopath="hopefully-absent-path" &&
153 test "/" = "$(test-path-utils real_path "/")" &&
Johannes Sixtbacca782012-09-09 17:42:20 +0200154 test "/$nopath" = "$(test-path-utils real_path "/$nopath")"
155'
156
157test_expect_success 'real path works on absolute paths 2' '
158 nopath="hopefully-absent-path" &&
Michael Haggerty7bcf48d2012-09-07 00:41:02 +0200159 # 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
Michael Haggerty379a03a2012-09-07 00:41:04 +0200165test_expect_success POSIX 'real path removes extra leading slashes' '
166 nopath="hopefully-absent-path" &&
167 test "/" = "$(test-path-utils real_path "///")" &&
168 test "/$nopath" = "$(test-path-utils real_path "///$nopath")" &&
169 # Find an existing top-level directory for the remaining tests:
170 d=$(pwd -P | sed -e "s|^\([^/]*/[^/]*\)/.*|\1|") &&
171 test "$d" = "$(test-path-utils real_path "//$d")" &&
172 test "$d/$nopath" = "$(test-path-utils real_path "//$d/$nopath")"
173'
174
175test_expect_success 'real path removes other extra slashes' '
176 nopath="hopefully-absent-path" &&
177 # Find an existing top-level directory for the remaining tests:
178 d=$(pwd -P | sed -e "s|^\([^/]*/[^/]*\)/.*|\1|") &&
179 test "$d" = "$(test-path-utils real_path "$d///")" &&
180 test "$d/$nopath" = "$(test-path-utils real_path "$d///$nopath")"
181'
182
Michael Haggerty7bcf48d2012-09-07 00:41:02 +0200183test_expect_success SYMLINKS 'real path works on symlinks' '
Michael Haggerty8da650b2012-09-07 00:40:57 +0200184 mkdir first &&
185 ln -s ../.git first/.git &&
186 mkdir second &&
187 ln -s ../first second/other &&
188 mkdir third &&
189 dir="$(cd .git; pwd -P)" &&
190 dir2=third/../second/other/.git &&
191 test "$dir" = "$(test-path-utils real_path $dir2)" &&
192 file="$dir"/index &&
193 test "$file" = "$(test-path-utils real_path $dir2/index)" &&
194 basename=blub &&
195 test "$dir/$basename" = "$(cd .git && test-path-utils real_path "$basename")" &&
196 ln -s ../first/file .git/syml &&
197 sym="$(cd first; pwd -P)"/file &&
198 test "$sym" = "$(test-path-utils real_path "$dir2/syml")"
199'
200
Martin Erik Werner655ee9e2014-02-04 15:25:20 +0100201test_expect_success SYMLINKS 'prefix_path works with absolute paths to work tree symlinks' '
Martin Erik Werner74af95d2014-02-04 15:25:16 +0100202 ln -s target symlink &&
203 test "$(test-path-utils prefix_path prefix "$(pwd)/symlink")" = "symlink"
204'
205
Martin Erik Wernere5aa1fc2014-02-04 15:25:17 +0100206test_expect_success 'prefix_path works with only absolute path to work tree' '
207 echo "" >expected &&
208 test-path-utils prefix_path prefix "$(pwd)" >actual &&
209 test_cmp expected actual
210'
211
Martin Erik Wernere131daa2014-02-04 15:25:18 +0100212test_expect_success 'prefix_path rejects absolute path to dir with same beginning as work tree' '
213 test_must_fail test-path-utils prefix_path prefix "$(pwd)a"
214'
215
216test_expect_success SYMLINKS 'prefix_path works with absolute path to a symlink to work tree having same beginning as work tree' '
217 git init repo &&
218 ln -s repo repolink &&
219 test "a" = "$(cd repo && test-path-utils prefix_path prefix "$(pwd)/../repolink/a")"
220'
221
Jiang Xindaf19a82013-10-14 10:29:38 +0800222relative_path /foo/a/b/c/ /foo/a/b/ c/
223relative_path /foo/a/b/c/ /foo/a/b c/
224relative_path /foo/a//b//c/ ///foo/a/b// c/ POSIX
225relative_path /foo/a/b /foo/a/b ./
226relative_path /foo/a/b/ /foo/a/b ./
227relative_path /foo/a /foo/a/b ../
228relative_path / /foo/a/b/ ../../../
229relative_path /foo/a/c /foo/a/b/ ../c
230relative_path /foo/a/c /foo/a/b ../c
231relative_path /foo/x/y /foo/a/b/ ../../x/y
232relative_path /foo/a/b "<empty>" /foo/a/b
233relative_path /foo/a/b "<null>" /foo/a/b
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
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/x/y foo/a/b ../../x/y
241relative_path foo/a/c foo/a/b ../c
Jiang Xin7fbd4222013-10-14 10:29:39 +0800242relative_path foo/a/b /foo/x/y foo/a/b
243relative_path /foo/a/b foo/x/y /foo/a/b
244relative_path d:/a/b D:/a/c ../b MINGW
245relative_path C:/a/b D:/a/c C:/a/b MINGW
Jiang Xindaf19a82013-10-14 10:29:38 +0800246relative_path foo/a/b "<empty>" foo/a/b
247relative_path foo/a/b "<null>" foo/a/b
248relative_path "<empty>" /foo/a/b ./
249relative_path "<empty>" "<empty>" ./
250relative_path "<empty>" "<null>" ./
251relative_path "<null>" "<empty>" ./
252relative_path "<null>" "<null>" ./
253relative_path "<null>" /foo/a/b ./
Jiang Xin203439b2013-06-25 23:53:42 +0800254
Nguyễn Thái Ngọc Duy557bd832014-11-30 15:24:31 +0700255test_git_path A=B info/grafts .git/info/grafts
256test_git_path GIT_GRAFT_FILE=foo info/grafts foo
257test_git_path GIT_GRAFT_FILE=foo info/////grafts foo
258test_git_path GIT_INDEX_FILE=foo index foo
259test_git_path GIT_INDEX_FILE=foo index/foo .git/index/foo
260test_git_path GIT_INDEX_FILE=foo index2 .git/index2
261test_expect_success 'setup fake objects directory foo' 'mkdir foo'
262test_git_path GIT_OBJECT_DIRECTORY=foo objects foo
263test_git_path GIT_OBJECT_DIRECTORY=foo objects/foo foo/foo
264test_git_path GIT_OBJECT_DIRECTORY=foo objects2 .git/objects2
Nguyễn Thái Ngọc Duyc7b3a3d2014-11-30 15:24:36 +0700265test_expect_success 'setup common repository' 'git --git-dir=bar init'
266test_git_path GIT_COMMON_DIR=bar index .git/index
267test_git_path GIT_COMMON_DIR=bar HEAD .git/HEAD
268test_git_path GIT_COMMON_DIR=bar logs/HEAD .git/logs/HEAD
269test_git_path GIT_COMMON_DIR=bar objects bar/objects
270test_git_path GIT_COMMON_DIR=bar objects/bar bar/objects/bar
271test_git_path GIT_COMMON_DIR=bar info/exclude bar/info/exclude
272test_git_path GIT_COMMON_DIR=bar info/grafts bar/info/grafts
Nguyễn Thái Ngọc Duy6cfbdcb2014-11-30 15:24:55 +0700273test_git_path GIT_COMMON_DIR=bar info/sparse-checkout .git/info/sparse-checkout
Nguyễn Thái Ngọc Duyc7b3a3d2014-11-30 15:24:36 +0700274test_git_path GIT_COMMON_DIR=bar remotes/bar bar/remotes/bar
275test_git_path GIT_COMMON_DIR=bar branches/bar bar/branches/bar
276test_git_path GIT_COMMON_DIR=bar logs/refs/heads/master bar/logs/refs/heads/master
277test_git_path GIT_COMMON_DIR=bar refs/heads/master bar/refs/heads/master
278test_git_path GIT_COMMON_DIR=bar hooks/me bar/hooks/me
279test_git_path GIT_COMMON_DIR=bar config bar/config
280test_git_path GIT_COMMON_DIR=bar packed-refs bar/packed-refs
281test_git_path GIT_COMMON_DIR=bar shallow bar/shallow
Nguyễn Thái Ngọc Duy557bd832014-11-30 15:24:31 +0700282
David Reissae299be2008-05-19 23:48:54 -0700283test_done