David Reiss | ae299be | 2008-05-19 23:48:54 -0700 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | # |
| 3 | # Copyright (c) 2008 David Reiss |
| 4 | # |
| 5 | |
| 6 | test_description='Test various path utilities' |
| 7 | |
| 8 | . ./test-lib.sh |
| 9 | |
Johannes Sixt | 2718e85 | 2009-03-11 22:15:10 +0100 | [diff] [blame] | 10 | norm_path() { |
Sebastian Schuberth | 7ffd18f | 2013-10-10 22:49:43 +0200 | [diff] [blame] | 11 | expected=$(test-path-utils print_path "$2") |
Johannes Sixt | 2718e85 | 2009-03-11 22:15:10 +0100 | [diff] [blame] | 12 | test_expect_success $3 "normalize path: $1 => $2" \ |
Jiang Xin | abd4284 | 2013-06-25 23:53:57 +0800 | [diff] [blame] | 13 | "test \"\$(test-path-utils normalize_path_copy '$1')\" = '$expected'" |
David Reiss | ae299be | 2008-05-19 23:48:54 -0700 | [diff] [blame] | 14 | } |
| 15 | |
Jiang Xin | 203439b | 2013-06-25 23:53:42 +0800 | [diff] [blame] | 16 | relative_path() { |
Sebastian Schuberth | 7ffd18f | 2013-10-10 22:49:43 +0200 | [diff] [blame] | 17 | expected=$(test-path-utils print_path "$3") |
Jiang Xin | 203439b | 2013-06-25 23:53:42 +0800 | [diff] [blame] | 18 | test_expect_success $4 "relative path: $1 $2 => $3" \ |
Jiang Xin | abd4284 | 2013-06-25 23:53:57 +0800 | [diff] [blame] | 19 | "test \"\$(test-path-utils relative_path '$1' '$2')\" = '$expected'" |
Jiang Xin | 203439b | 2013-06-25 23:53:42 +0800 | [diff] [blame] | 20 | } |
| 21 | |
Nguyễn Thái Ngọc Duy | 557bd83 | 2014-11-30 15:24:31 +0700 | [diff] [blame] | 22 | test_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 Sixt | 2718e85 | 2009-03-11 22:15:10 +0100 | [diff] [blame] | 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: |
| 33 | rootoff=$(test-path-utils normalize_path_copy / | wc -c) |
| 34 | # Account for the trailing LF: |
Jeff King | 28baf82 | 2009-03-23 02:22:29 -0400 | [diff] [blame] | 35 | if test $rootoff = 2; then |
Johannes Sixt | 2718e85 | 2009-03-11 22:15:10 +0100 | [diff] [blame] | 36 | rootoff= # we are on Unix |
| 37 | else |
| 38 | rootoff=$(($rootoff-1)) |
| 39 | fi |
| 40 | |
David Reiss | 0454dd9 | 2008-05-19 23:49:26 -0700 | [diff] [blame] | 41 | ancestor() { |
Johannes Sixt | 2718e85 | 2009-03-11 22:15:10 +0100 | [diff] [blame] | 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'" |
David Reiss | 0454dd9 | 2008-05-19 23:49:26 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Jiang Xin | abd4284 | 2013-06-25 23:53:57 +0800 | [diff] [blame] | 52 | # Some absolute path tests should be skipped on Windows due to path mangling |
| 53 | # on POSIX-style absolute paths |
Johannes Sixt | 2718e85 | 2009-03-11 22:15:10 +0100 | [diff] [blame] | 54 | case $(uname -s) in |
| 55 | *MINGW*) |
| 56 | ;; |
| 57 | *) |
| 58 | test_set_prereq POSIX |
| 59 | ;; |
| 60 | esac |
| 61 | |
| 62 | norm_path "" "" |
| 63 | norm_path . "" |
| 64 | norm_path ./ "" |
| 65 | norm_path ./. "" |
| 66 | norm_path ./.. ++failed++ |
| 67 | norm_path ../. ++failed++ |
| 68 | norm_path ./../.// ++failed++ |
| 69 | norm_path dir/.. "" |
| 70 | norm_path dir/sub/../.. "" |
| 71 | norm_path dir/sub/../../.. ++failed++ |
| 72 | norm_path dir dir |
| 73 | norm_path dir// dir/ |
| 74 | norm_path ./dir dir |
| 75 | norm_path dir/. dir/ |
| 76 | norm_path dir///./ dir/ |
| 77 | norm_path dir//sub/.. dir/ |
| 78 | norm_path dir/sub/../ dir/ |
| 79 | norm_path dir/sub/../. dir/ |
| 80 | norm_path dir/s1/../s2/ dir/s2/ |
| 81 | norm_path d1/s1///s2/..//../s3/ d1/s3/ |
| 82 | norm_path d1/s1//../s2/../../d2 d2 |
| 83 | norm_path d1/.../d2 d1/.../d2 |
| 84 | norm_path d1/..././../d2 d1/d2 |
| 85 | |
Jiang Xin | abd4284 | 2013-06-25 23:53:57 +0800 | [diff] [blame] | 86 | norm_path / / |
Johannes Sixt | 2718e85 | 2009-03-11 22:15:10 +0100 | [diff] [blame] | 87 | norm_path // / POSIX |
| 88 | norm_path /// / POSIX |
Jiang Xin | abd4284 | 2013-06-25 23:53:57 +0800 | [diff] [blame] | 89 | norm_path /. / |
Johannes Sixt | 2718e85 | 2009-03-11 22:15:10 +0100 | [diff] [blame] | 90 | norm_path /./ / POSIX |
| 91 | norm_path /./.. ++failed++ POSIX |
Jiang Xin | abd4284 | 2013-06-25 23:53:57 +0800 | [diff] [blame] | 92 | norm_path /../. ++failed++ |
Johannes Sixt | 2718e85 | 2009-03-11 22:15:10 +0100 | [diff] [blame] | 93 | norm_path /./../.// ++failed++ POSIX |
| 94 | norm_path /dir/.. / POSIX |
| 95 | norm_path /dir/sub/../.. / POSIX |
| 96 | norm_path /dir/sub/../../.. ++failed++ POSIX |
Jiang Xin | abd4284 | 2013-06-25 23:53:57 +0800 | [diff] [blame] | 97 | norm_path /dir /dir |
| 98 | norm_path /dir// /dir/ |
| 99 | norm_path /./dir /dir |
| 100 | norm_path /dir/. /dir/ |
| 101 | norm_path /dir///./ /dir/ |
| 102 | norm_path /dir//sub/.. /dir/ |
| 103 | norm_path /dir/sub/../ /dir/ |
Johannes Sixt | 2718e85 | 2009-03-11 22:15:10 +0100 | [diff] [blame] | 104 | norm_path //dir/sub/../. /dir/ POSIX |
Jiang Xin | abd4284 | 2013-06-25 23:53:57 +0800 | [diff] [blame] | 105 | norm_path /dir/s1/../s2/ /dir/s2/ |
| 106 | norm_path /d1/s1///s2/..//../s3/ /d1/s3/ |
| 107 | norm_path /d1/s1//../s2/../../d2 /d2 |
| 108 | norm_path /d1/.../d2 /d1/.../d2 |
| 109 | norm_path /d1/..././../d2 /d1/d2 |
David Reiss | ae299be | 2008-05-19 23:48:54 -0700 | [diff] [blame] | 110 | |
David Reiss | 0454dd9 | 2008-05-19 23:49:26 -0700 | [diff] [blame] | 111 | ancestor / / -1 |
David Reiss | 0454dd9 | 2008-05-19 23:49:26 -0700 | [diff] [blame] | 112 | ancestor /foo / 0 |
| 113 | ancestor /foo /fo -1 |
| 114 | ancestor /foo /foo -1 |
David Reiss | 0454dd9 | 2008-05-19 23:49:26 -0700 | [diff] [blame] | 115 | ancestor /foo /bar -1 |
David Reiss | 0454dd9 | 2008-05-19 23:49:26 -0700 | [diff] [blame] | 116 | ancestor /foo /foo/bar -1 |
Michael Haggerty | 9e2326c | 2012-10-28 17:16:25 +0100 | [diff] [blame] | 117 | ancestor /foo /foo:/bar -1 |
| 118 | ancestor /foo /:/foo:/bar 0 |
| 119 | ancestor /foo /foo:/:/bar 0 |
| 120 | ancestor /foo /:/bar:/foo 0 |
David Reiss | 0454dd9 | 2008-05-19 23:49:26 -0700 | [diff] [blame] | 121 | ancestor /foo/bar / 0 |
| 122 | ancestor /foo/bar /fo -1 |
David Reiss | 0454dd9 | 2008-05-19 23:49:26 -0700 | [diff] [blame] | 123 | ancestor /foo/bar /foo 4 |
David Reiss | 0454dd9 | 2008-05-19 23:49:26 -0700 | [diff] [blame] | 124 | ancestor /foo/bar /foo/ba -1 |
| 125 | ancestor /foo/bar /:/fo 0 |
| 126 | ancestor /foo/bar /foo:/foo/ba 4 |
| 127 | ancestor /foo/bar /bar -1 |
Michael Haggerty | 9e2326c | 2012-10-28 17:16:25 +0100 | [diff] [blame] | 128 | ancestor /foo/bar /fo -1 |
| 129 | ancestor /foo/bar /foo:/bar 4 |
| 130 | ancestor /foo/bar /:/foo:/bar 4 |
| 131 | ancestor /foo/bar /foo:/:/bar 4 |
| 132 | ancestor /foo/bar /:/bar:/fo 0 |
| 133 | ancestor /foo/bar /:/bar 0 |
| 134 | ancestor /foo/bar /foo 4 |
| 135 | ancestor /foo/bar /foo:/bar 4 |
| 136 | ancestor /foo/bar /bar -1 |
David Reiss | 0454dd9 | 2008-05-19 23:49:26 -0700 | [diff] [blame] | 137 | |
Johannes Schindelin | 4fcc86b | 2009-02-19 20:10:49 +0100 | [diff] [blame] | 138 | test_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 Haggerty | 8da650b | 2012-09-07 00:40:57 +0200 | [diff] [blame] | 142 | |
Michael Haggerty | a0601dc | 2012-09-07 00:40:59 +0200 | [diff] [blame] | 143 | test_expect_success 'absolute path rejects the empty string' ' |
Michael Haggerty | 17264bc | 2012-09-07 00:40:58 +0200 | [diff] [blame] | 144 | test_must_fail test-path-utils absolute_path "" |
| 145 | ' |
| 146 | |
Michael Haggerty | 3efe5d1 | 2012-09-07 00:41:01 +0200 | [diff] [blame] | 147 | test_expect_success 'real path rejects the empty string' ' |
Michael Haggerty | a5c4521 | 2012-09-07 00:41:00 +0200 | [diff] [blame] | 148 | test_must_fail test-path-utils real_path "" |
| 149 | ' |
| 150 | |
Johannes Sixt | bacca78 | 2012-09-09 17:42:20 +0200 | [diff] [blame] | 151 | test_expect_success POSIX 'real path works on absolute paths 1' ' |
Michael Haggerty | 7bcf48d | 2012-09-07 00:41:02 +0200 | [diff] [blame] | 152 | nopath="hopefully-absent-path" && |
| 153 | test "/" = "$(test-path-utils real_path "/")" && |
Johannes Sixt | bacca78 | 2012-09-09 17:42:20 +0200 | [diff] [blame] | 154 | test "/$nopath" = "$(test-path-utils real_path "/$nopath")" |
| 155 | ' |
| 156 | |
| 157 | test_expect_success 'real path works on absolute paths 2' ' |
| 158 | nopath="hopefully-absent-path" && |
Michael Haggerty | 7bcf48d | 2012-09-07 00:41:02 +0200 | [diff] [blame] | 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 | |
Michael Haggerty | 379a03a | 2012-09-07 00:41:04 +0200 | [diff] [blame] | 165 | test_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 | |
| 175 | test_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 Haggerty | 7bcf48d | 2012-09-07 00:41:02 +0200 | [diff] [blame] | 183 | test_expect_success SYMLINKS 'real path works on symlinks' ' |
Michael Haggerty | 8da650b | 2012-09-07 00:40:57 +0200 | [diff] [blame] | 184 | 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 Werner | 655ee9e | 2014-02-04 15:25:20 +0100 | [diff] [blame] | 201 | test_expect_success SYMLINKS 'prefix_path works with absolute paths to work tree symlinks' ' |
Martin Erik Werner | 74af95d | 2014-02-04 15:25:16 +0100 | [diff] [blame] | 202 | ln -s target symlink && |
| 203 | test "$(test-path-utils prefix_path prefix "$(pwd)/symlink")" = "symlink" |
| 204 | ' |
| 205 | |
Martin Erik Werner | e5aa1fc | 2014-02-04 15:25:17 +0100 | [diff] [blame] | 206 | test_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 Werner | e131daa | 2014-02-04 15:25:18 +0100 | [diff] [blame] | 212 | test_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 | |
| 216 | test_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 Xin | daf19a8 | 2013-10-14 10:29:38 +0800 | [diff] [blame] | 222 | relative_path /foo/a/b/c/ /foo/a/b/ c/ |
| 223 | relative_path /foo/a/b/c/ /foo/a/b c/ |
| 224 | relative_path /foo/a//b//c/ ///foo/a/b// c/ POSIX |
| 225 | relative_path /foo/a/b /foo/a/b ./ |
| 226 | relative_path /foo/a/b/ /foo/a/b ./ |
| 227 | relative_path /foo/a /foo/a/b ../ |
| 228 | relative_path / /foo/a/b/ ../../../ |
| 229 | relative_path /foo/a/c /foo/a/b/ ../c |
| 230 | relative_path /foo/a/c /foo/a/b ../c |
| 231 | relative_path /foo/x/y /foo/a/b/ ../../x/y |
| 232 | relative_path /foo/a/b "<empty>" /foo/a/b |
| 233 | relative_path /foo/a/b "<null>" /foo/a/b |
| 234 | relative_path foo/a/b/c/ foo/a/b/ c/ |
| 235 | relative_path foo/a/b/c/ foo/a/b c/ |
| 236 | relative_path foo/a/b//c foo/a//b c |
| 237 | relative_path foo/a/b/ foo/a/b/ ./ |
| 238 | relative_path foo/a/b/ foo/a/b ./ |
| 239 | relative_path foo/a foo/a/b ../ |
| 240 | relative_path foo/x/y foo/a/b ../../x/y |
| 241 | relative_path foo/a/c foo/a/b ../c |
Jiang Xin | 7fbd422 | 2013-10-14 10:29:39 +0800 | [diff] [blame] | 242 | relative_path foo/a/b /foo/x/y foo/a/b |
| 243 | relative_path /foo/a/b foo/x/y /foo/a/b |
| 244 | relative_path d:/a/b D:/a/c ../b MINGW |
| 245 | relative_path C:/a/b D:/a/c C:/a/b MINGW |
Jiang Xin | daf19a8 | 2013-10-14 10:29:38 +0800 | [diff] [blame] | 246 | relative_path foo/a/b "<empty>" foo/a/b |
| 247 | relative_path foo/a/b "<null>" foo/a/b |
| 248 | relative_path "<empty>" /foo/a/b ./ |
| 249 | relative_path "<empty>" "<empty>" ./ |
| 250 | relative_path "<empty>" "<null>" ./ |
| 251 | relative_path "<null>" "<empty>" ./ |
| 252 | relative_path "<null>" "<null>" ./ |
| 253 | relative_path "<null>" /foo/a/b ./ |
Jiang Xin | 203439b | 2013-06-25 23:53:42 +0800 | [diff] [blame] | 254 | |
Nguyễn Thái Ngọc Duy | 557bd83 | 2014-11-30 15:24:31 +0700 | [diff] [blame] | 255 | test_git_path A=B info/grafts .git/info/grafts |
| 256 | test_git_path GIT_GRAFT_FILE=foo info/grafts foo |
| 257 | test_git_path GIT_GRAFT_FILE=foo info/////grafts foo |
| 258 | test_git_path GIT_INDEX_FILE=foo index foo |
| 259 | test_git_path GIT_INDEX_FILE=foo index/foo .git/index/foo |
| 260 | test_git_path GIT_INDEX_FILE=foo index2 .git/index2 |
| 261 | test_expect_success 'setup fake objects directory foo' 'mkdir foo' |
| 262 | test_git_path GIT_OBJECT_DIRECTORY=foo objects foo |
| 263 | test_git_path GIT_OBJECT_DIRECTORY=foo objects/foo foo/foo |
| 264 | test_git_path GIT_OBJECT_DIRECTORY=foo objects2 .git/objects2 |
Nguyễn Thái Ngọc Duy | c7b3a3d | 2014-11-30 15:24:36 +0700 | [diff] [blame] | 265 | test_expect_success 'setup common repository' 'git --git-dir=bar init' |
| 266 | test_git_path GIT_COMMON_DIR=bar index .git/index |
| 267 | test_git_path GIT_COMMON_DIR=bar HEAD .git/HEAD |
| 268 | test_git_path GIT_COMMON_DIR=bar logs/HEAD .git/logs/HEAD |
| 269 | test_git_path GIT_COMMON_DIR=bar objects bar/objects |
| 270 | test_git_path GIT_COMMON_DIR=bar objects/bar bar/objects/bar |
| 271 | test_git_path GIT_COMMON_DIR=bar info/exclude bar/info/exclude |
| 272 | test_git_path GIT_COMMON_DIR=bar info/grafts bar/info/grafts |
Nguyễn Thái Ngọc Duy | 6cfbdcb | 2014-11-30 15:24:55 +0700 | [diff] [blame^] | 273 | test_git_path GIT_COMMON_DIR=bar info/sparse-checkout .git/info/sparse-checkout |
Nguyễn Thái Ngọc Duy | c7b3a3d | 2014-11-30 15:24:36 +0700 | [diff] [blame] | 274 | test_git_path GIT_COMMON_DIR=bar remotes/bar bar/remotes/bar |
| 275 | test_git_path GIT_COMMON_DIR=bar branches/bar bar/branches/bar |
| 276 | test_git_path GIT_COMMON_DIR=bar logs/refs/heads/master bar/logs/refs/heads/master |
| 277 | test_git_path GIT_COMMON_DIR=bar refs/heads/master bar/refs/heads/master |
| 278 | test_git_path GIT_COMMON_DIR=bar hooks/me bar/hooks/me |
| 279 | test_git_path GIT_COMMON_DIR=bar config bar/config |
| 280 | test_git_path GIT_COMMON_DIR=bar packed-refs bar/packed-refs |
| 281 | test_git_path GIT_COMMON_DIR=bar shallow bar/shallow |
Nguyễn Thái Ngọc Duy | 557bd83 | 2014-11-30 15:24:31 +0700 | [diff] [blame] | 282 | |
David Reiss | ae299be | 2008-05-19 23:48:54 -0700 | [diff] [blame] | 283 | test_done |