t0060-path-utils.sh 10.0 KB
Newer Older
D
David Reiss 已提交
1 2 3 4 5 6 7 8 9
#!/bin/sh
#
# Copyright (c) 2008 David Reiss
#

test_description='Test various path utilities'

. ./test-lib.sh

J
Johannes Sixt 已提交
10
norm_path() {
11
	expected=$(test-path-utils print_path "$2")
J
Johannes Sixt 已提交
12
	test_expect_success $3 "normalize path: $1 => $2" \
13
	"test \"\$(test-path-utils normalize_path_copy '$1')\" = '$expected'"
D
David Reiss 已提交
14 15
}

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'"
20 21
}

22 23 24 25 26 27 28 29
test_git_path() {
	test_expect_success "git-path $1 $2 => $3" "
		$1 git rev-parse --git-path $2 >actual &&
		echo $3 >expect &&
		test_cmp expect actual
	"
}

J
Johannes Sixt 已提交
30 31 32 33 34
# On Windows, we are using MSYS's bash, which mangles the paths.
# Absolute paths are anchored at the MSYS installation directory,
# which means that the path / accounts for this many characters:
rootoff=$(test-path-utils normalize_path_copy / | wc -c)
# Account for the trailing LF:
35
if test $rootoff = 2; then
J
Johannes Sixt 已提交
36 37 38 39 40
	rootoff=	# we are on Unix
else
	rootoff=$(($rootoff-1))
fi

41
ancestor() {
J
Johannes Sixt 已提交
42 43 44 45 46 47 48 49
	# We do some math with the expected ancestor length.
	expected=$3
	if test -n "$rootoff" && test "x$expected" != x-1; then
		expected=$(($expected+$rootoff))
	fi
	test_expect_success "longest ancestor: $1 $2 => $expected" \
	"actual=\$(test-path-utils longest_ancestor_length '$1' '$2') &&
	 test \"\$actual\" = '$expected'"
50 51
}

52 53
# Some absolute path tests should be skipped on Windows due to path mangling
# on POSIX-style absolute paths
J
Johannes Sixt 已提交
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
case $(uname -s) in
*MINGW*)
	;;
*)
	test_set_prereq POSIX
	;;
esac

norm_path "" ""
norm_path . ""
norm_path ./ ""
norm_path ./. ""
norm_path ./.. ++failed++
norm_path ../. ++failed++
norm_path ./../.// ++failed++
norm_path dir/.. ""
norm_path dir/sub/../.. ""
norm_path dir/sub/../../.. ++failed++
norm_path dir dir
norm_path dir// dir/
norm_path ./dir dir
norm_path dir/. dir/
norm_path dir///./ dir/
norm_path dir//sub/.. dir/
norm_path dir/sub/../ dir/
norm_path dir/sub/../. dir/
norm_path dir/s1/../s2/ dir/s2/
norm_path d1/s1///s2/..//../s3/ d1/s3/
norm_path d1/s1//../s2/../../d2 d2
norm_path d1/.../d2 d1/.../d2
norm_path d1/..././../d2 d1/d2

86
norm_path / /
J
Johannes Sixt 已提交
87 88
norm_path // / POSIX
norm_path /// / POSIX
89
norm_path /. /
J
Johannes Sixt 已提交
90 91
norm_path /./ / POSIX
norm_path /./.. ++failed++ POSIX
92
norm_path /../. ++failed++
J
Johannes Sixt 已提交
93 94 95 96
norm_path /./../.// ++failed++ POSIX
norm_path /dir/.. / POSIX
norm_path /dir/sub/../.. / POSIX
norm_path /dir/sub/../../.. ++failed++ POSIX
97 98 99 100 101 102 103
norm_path /dir /dir
norm_path /dir// /dir/
norm_path /./dir /dir
norm_path /dir/. /dir/
norm_path /dir///./ /dir/
norm_path /dir//sub/.. /dir/
norm_path /dir/sub/../ /dir/
J
Johannes Sixt 已提交
104
norm_path //dir/sub/../. /dir/ POSIX
105 106 107 108 109
norm_path /dir/s1/../s2/ /dir/s2/
norm_path /d1/s1///s2/..//../s3/ /d1/s3/
norm_path /d1/s1//../s2/../../d2 /d2
norm_path /d1/.../d2 /d1/.../d2
norm_path /d1/..././../d2 /d1/d2
D
David Reiss 已提交
110

111 112 113 114 115 116
ancestor / / -1
ancestor /foo / 0
ancestor /foo /fo -1
ancestor /foo /foo -1
ancestor /foo /bar -1
ancestor /foo /foo/bar -1
117 118 119 120
ancestor /foo /foo:/bar -1
ancestor /foo /:/foo:/bar 0
ancestor /foo /foo:/:/bar 0
ancestor /foo /:/bar:/foo 0
121 122 123 124 125 126 127
ancestor /foo/bar / 0
ancestor /foo/bar /fo -1
ancestor /foo/bar /foo 4
ancestor /foo/bar /foo/ba -1
ancestor /foo/bar /:/fo 0
ancestor /foo/bar /foo:/foo/ba 4
ancestor /foo/bar /bar -1
128 129 130 131 132 133 134 135 136
ancestor /foo/bar /fo -1
ancestor /foo/bar /foo:/bar 4
ancestor /foo/bar /:/foo:/bar 4
ancestor /foo/bar /foo:/:/bar 4
ancestor /foo/bar /:/bar:/fo 0
ancestor /foo/bar /:/bar 0
ancestor /foo/bar /foo 4
ancestor /foo/bar /foo:/bar 4
ancestor /foo/bar /bar -1
137

138 139 140 141
test_expect_success 'strip_path_suffix' '
	test c:/msysgit = $(test-path-utils strip_path_suffix \
		c:/msysgit/libexec//git-core libexec/git-core)
'
142

143
test_expect_success 'absolute path rejects the empty string' '
144 145 146
	test_must_fail test-path-utils absolute_path ""
'

147
test_expect_success 'real path rejects the empty string' '
148 149 150
	test_must_fail test-path-utils real_path ""
'

151
test_expect_success POSIX 'real path works on absolute paths 1' '
152 153
	nopath="hopefully-absent-path" &&
	test "/" = "$(test-path-utils real_path "/")" &&
154 155 156 157 158
	test "/$nopath" = "$(test-path-utils real_path "/$nopath")"
'

test_expect_success 'real path works on absolute paths 2' '
	nopath="hopefully-absent-path" &&
159 160 161 162 163 164
	# Find an existing top-level directory for the remaining tests:
	d=$(pwd -P | sed -e "s|^\([^/]*/[^/]*\)/.*|\1|") &&
	test "$d" = "$(test-path-utils real_path "$d")" &&
	test "$d/$nopath" = "$(test-path-utils real_path "$d/$nopath")"
'

165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
test_expect_success POSIX 'real path removes extra leading slashes' '
	nopath="hopefully-absent-path" &&
	test "/" = "$(test-path-utils real_path "///")" &&
	test "/$nopath" = "$(test-path-utils real_path "///$nopath")" &&
	# Find an existing top-level directory for the remaining tests:
	d=$(pwd -P | sed -e "s|^\([^/]*/[^/]*\)/.*|\1|") &&
	test "$d" = "$(test-path-utils real_path "//$d")" &&
	test "$d/$nopath" = "$(test-path-utils real_path "//$d/$nopath")"
'

test_expect_success 'real path removes other extra slashes' '
	nopath="hopefully-absent-path" &&
	# Find an existing top-level directory for the remaining tests:
	d=$(pwd -P | sed -e "s|^\([^/]*/[^/]*\)/.*|\1|") &&
	test "$d" = "$(test-path-utils real_path "$d///")" &&
	test "$d/$nopath" = "$(test-path-utils real_path "$d///$nopath")"
'

183
test_expect_success SYMLINKS 'real path works on symlinks' '
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
	mkdir first &&
	ln -s ../.git first/.git &&
	mkdir second &&
	ln -s ../first second/other &&
	mkdir third &&
	dir="$(cd .git; pwd -P)" &&
	dir2=third/../second/other/.git &&
	test "$dir" = "$(test-path-utils real_path $dir2)" &&
	file="$dir"/index &&
	test "$file" = "$(test-path-utils real_path $dir2/index)" &&
	basename=blub &&
	test "$dir/$basename" = "$(cd .git && test-path-utils real_path "$basename")" &&
	ln -s ../first/file .git/syml &&
	sym="$(cd first; pwd -P)"/file &&
	test "$sym" = "$(test-path-utils real_path "$dir2/syml")"
'

201
test_expect_success SYMLINKS 'prefix_path works with absolute paths to work tree symlinks' '
202 203 204 205
	ln -s target symlink &&
	test "$(test-path-utils prefix_path prefix "$(pwd)/symlink")" = "symlink"
'

206 207 208 209 210 211
test_expect_success 'prefix_path works with only absolute path to work tree' '
	echo "" >expected &&
	test-path-utils prefix_path prefix "$(pwd)" >actual &&
	test_cmp expected actual
'

212 213 214 215 216 217 218 219 220 221
test_expect_success 'prefix_path rejects absolute path to dir with same beginning as work tree' '
	test_must_fail test-path-utils prefix_path prefix "$(pwd)a"
'

test_expect_success SYMLINKS 'prefix_path works with absolute path to a symlink to work tree having  same beginning as work tree' '
	git init repo &&
	ln -s repo repolink &&
	test "a" = "$(cd repo && test-path-utils prefix_path prefix "$(pwd)/../repolink/a")"
'

222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
relative_path /foo/a/b/c/	/foo/a/b/	c/
relative_path /foo/a/b/c/	/foo/a/b	c/
relative_path /foo/a//b//c/	///foo/a/b//	c/		POSIX
relative_path /foo/a/b		/foo/a/b	./
relative_path /foo/a/b/		/foo/a/b	./
relative_path /foo/a		/foo/a/b	../
relative_path /			/foo/a/b/	../../../
relative_path /foo/a/c		/foo/a/b/	../c
relative_path /foo/a/c		/foo/a/b	../c
relative_path /foo/x/y		/foo/a/b/	../../x/y
relative_path /foo/a/b		"<empty>"	/foo/a/b
relative_path /foo/a/b 		"<null>"	/foo/a/b
relative_path foo/a/b/c/	foo/a/b/	c/
relative_path foo/a/b/c/	foo/a/b		c/
relative_path foo/a/b//c	foo/a//b	c
relative_path foo/a/b/		foo/a/b/	./
relative_path foo/a/b/		foo/a/b		./
relative_path foo/a		foo/a/b		../
relative_path foo/x/y		foo/a/b		../../x/y
relative_path foo/a/c		foo/a/b		../c
242 243 244 245
relative_path foo/a/b		/foo/x/y	foo/a/b
relative_path /foo/a/b		foo/x/y		/foo/a/b
relative_path d:/a/b		D:/a/c		../b		MINGW
relative_path C:/a/b		D:/a/c		C:/a/b		MINGW
246 247 248 249 250 251 252 253
relative_path foo/a/b		"<empty>"	foo/a/b
relative_path foo/a/b 		"<null>"	foo/a/b
relative_path "<empty>"		/foo/a/b	./
relative_path "<empty>"		"<empty>"	./
relative_path "<empty>"		"<null>"	./
relative_path "<null>"		"<empty>"	./
relative_path "<null>"		"<null>"	./
relative_path "<null>"		/foo/a/b	./
254

255 256 257 258 259 260 261 262 263 264
test_git_path A=B                info/grafts .git/info/grafts
test_git_path GIT_GRAFT_FILE=foo info/grafts foo
test_git_path GIT_GRAFT_FILE=foo info/////grafts foo
test_git_path GIT_INDEX_FILE=foo index foo
test_git_path GIT_INDEX_FILE=foo index/foo .git/index/foo
test_git_path GIT_INDEX_FILE=foo index2 .git/index2
test_expect_success 'setup fake objects directory foo' 'mkdir foo'
test_git_path GIT_OBJECT_DIRECTORY=foo objects foo
test_git_path GIT_OBJECT_DIRECTORY=foo objects/foo foo/foo
test_git_path GIT_OBJECT_DIRECTORY=foo objects2 .git/objects2
265 266 267 268
test_expect_success 'setup common repository' 'git --git-dir=bar init'
test_git_path GIT_COMMON_DIR=bar index                    .git/index
test_git_path GIT_COMMON_DIR=bar HEAD                     .git/HEAD
test_git_path GIT_COMMON_DIR=bar logs/HEAD                .git/logs/HEAD
269 270 271 272
test_git_path GIT_COMMON_DIR=bar logs/refs/bisect/foo     .git/logs/refs/bisect/foo
test_git_path GIT_COMMON_DIR=bar logs/refs/bisec/foo      bar/logs/refs/bisec/foo
test_git_path GIT_COMMON_DIR=bar logs/refs/bisec          bar/logs/refs/bisec
test_git_path GIT_COMMON_DIR=bar logs/refs/bisectfoo      bar/logs/refs/bisectfoo
273 274 275 276
test_git_path GIT_COMMON_DIR=bar objects                  bar/objects
test_git_path GIT_COMMON_DIR=bar objects/bar              bar/objects/bar
test_git_path GIT_COMMON_DIR=bar info/exclude             bar/info/exclude
test_git_path GIT_COMMON_DIR=bar info/grafts              bar/info/grafts
277
test_git_path GIT_COMMON_DIR=bar info/sparse-checkout     .git/info/sparse-checkout
278
test_git_path GIT_COMMON_DIR=bar info//sparse-checkout    .git/info//sparse-checkout
279 280 281 282
test_git_path GIT_COMMON_DIR=bar remotes/bar              bar/remotes/bar
test_git_path GIT_COMMON_DIR=bar branches/bar             bar/branches/bar
test_git_path GIT_COMMON_DIR=bar logs/refs/heads/master   bar/logs/refs/heads/master
test_git_path GIT_COMMON_DIR=bar refs/heads/master        bar/refs/heads/master
283
test_git_path GIT_COMMON_DIR=bar refs/bisect/foo          .git/refs/bisect/foo
284 285 286 287
test_git_path GIT_COMMON_DIR=bar hooks/me                 bar/hooks/me
test_git_path GIT_COMMON_DIR=bar config                   bar/config
test_git_path GIT_COMMON_DIR=bar packed-refs              bar/packed-refs
test_git_path GIT_COMMON_DIR=bar shallow                  bar/shallow
288

D
David Reiss 已提交
289
test_done