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 | |
Johannes Sixt | 2718e85 | 2009-03-11 22:15:10 +0100 | [diff] [blame] | 22 | # On Windows, we are using MSYS's bash, which mangles the paths. |
| 23 | # Absolute paths are anchored at the MSYS installation directory, |
| 24 | # which means that the path / accounts for this many characters: |
| 25 | rootoff=$(test-path-utils normalize_path_copy / | wc -c) |
| 26 | # Account for the trailing LF: |
Jeff King | 28baf82 | 2009-03-23 02:22:29 -0400 | [diff] [blame] | 27 | if test $rootoff = 2; then |
Johannes Sixt | 2718e85 | 2009-03-11 22:15:10 +0100 | [diff] [blame] | 28 | rootoff= # we are on Unix |
| 29 | else |
| 30 | rootoff=$(($rootoff-1)) |
| 31 | fi |
| 32 | |
David Reiss | 0454dd9 | 2008-05-19 23:49:26 -0700 | [diff] [blame] | 33 | ancestor() { |
Johannes Sixt | 2718e85 | 2009-03-11 22:15:10 +0100 | [diff] [blame] | 34 | # We do some math with the expected ancestor length. |
| 35 | expected=$3 |
| 36 | if test -n "$rootoff" && test "x$expected" != x-1; then |
| 37 | expected=$(($expected+$rootoff)) |
| 38 | fi |
| 39 | test_expect_success "longest ancestor: $1 $2 => $expected" \ |
| 40 | "actual=\$(test-path-utils longest_ancestor_length '$1' '$2') && |
| 41 | test \"\$actual\" = '$expected'" |
David Reiss | 0454dd9 | 2008-05-19 23:49:26 -0700 | [diff] [blame] | 42 | } |
| 43 | |
Jiang Xin | abd4284 | 2013-06-25 23:53:57 +0800 | [diff] [blame] | 44 | # Some absolute path tests should be skipped on Windows due to path mangling |
| 45 | # on POSIX-style absolute paths |
Johannes Sixt | 2718e85 | 2009-03-11 22:15:10 +0100 | [diff] [blame] | 46 | case $(uname -s) in |
| 47 | *MINGW*) |
| 48 | ;; |
| 49 | *) |
| 50 | test_set_prereq POSIX |
| 51 | ;; |
| 52 | esac |
| 53 | |
| 54 | norm_path "" "" |
| 55 | norm_path . "" |
| 56 | norm_path ./ "" |
| 57 | norm_path ./. "" |
| 58 | norm_path ./.. ++failed++ |
| 59 | norm_path ../. ++failed++ |
| 60 | norm_path ./../.// ++failed++ |
| 61 | norm_path dir/.. "" |
| 62 | norm_path dir/sub/../.. "" |
| 63 | norm_path dir/sub/../../.. ++failed++ |
| 64 | norm_path dir dir |
| 65 | norm_path dir// dir/ |
| 66 | norm_path ./dir dir |
| 67 | norm_path dir/. dir/ |
| 68 | norm_path dir///./ dir/ |
| 69 | norm_path dir//sub/.. dir/ |
| 70 | norm_path dir/sub/../ dir/ |
| 71 | norm_path dir/sub/../. dir/ |
| 72 | norm_path dir/s1/../s2/ dir/s2/ |
| 73 | norm_path d1/s1///s2/..//../s3/ d1/s3/ |
| 74 | norm_path d1/s1//../s2/../../d2 d2 |
| 75 | norm_path d1/.../d2 d1/.../d2 |
| 76 | norm_path d1/..././../d2 d1/d2 |
| 77 | |
Jiang Xin | abd4284 | 2013-06-25 23:53:57 +0800 | [diff] [blame] | 78 | norm_path / / |
Johannes Sixt | 2718e85 | 2009-03-11 22:15:10 +0100 | [diff] [blame] | 79 | norm_path // / POSIX |
| 80 | norm_path /// / POSIX |
Jiang Xin | abd4284 | 2013-06-25 23:53:57 +0800 | [diff] [blame] | 81 | norm_path /. / |
Johannes Sixt | 2718e85 | 2009-03-11 22:15:10 +0100 | [diff] [blame] | 82 | norm_path /./ / POSIX |
| 83 | norm_path /./.. ++failed++ POSIX |
Jiang Xin | abd4284 | 2013-06-25 23:53:57 +0800 | [diff] [blame] | 84 | norm_path /../. ++failed++ |
Johannes Sixt | 2718e85 | 2009-03-11 22:15:10 +0100 | [diff] [blame] | 85 | norm_path /./../.// ++failed++ POSIX |
| 86 | norm_path /dir/.. / POSIX |
| 87 | norm_path /dir/sub/../.. / POSIX |
| 88 | norm_path /dir/sub/../../.. ++failed++ POSIX |
Jiang Xin | abd4284 | 2013-06-25 23:53:57 +0800 | [diff] [blame] | 89 | norm_path /dir /dir |
| 90 | norm_path /dir// /dir/ |
| 91 | norm_path /./dir /dir |
| 92 | norm_path /dir/. /dir/ |
| 93 | norm_path /dir///./ /dir/ |
| 94 | norm_path /dir//sub/.. /dir/ |
| 95 | norm_path /dir/sub/../ /dir/ |
Johannes Sixt | 2718e85 | 2009-03-11 22:15:10 +0100 | [diff] [blame] | 96 | norm_path //dir/sub/../. /dir/ POSIX |
Jiang Xin | abd4284 | 2013-06-25 23:53:57 +0800 | [diff] [blame] | 97 | norm_path /dir/s1/../s2/ /dir/s2/ |
| 98 | norm_path /d1/s1///s2/..//../s3/ /d1/s3/ |
| 99 | norm_path /d1/s1//../s2/../../d2 /d2 |
| 100 | norm_path /d1/.../d2 /d1/.../d2 |
| 101 | norm_path /d1/..././../d2 /d1/d2 |
David Reiss | ae299be | 2008-05-19 23:48:54 -0700 | [diff] [blame] | 102 | |
David Reiss | 0454dd9 | 2008-05-19 23:49:26 -0700 | [diff] [blame] | 103 | ancestor / / -1 |
David Reiss | 0454dd9 | 2008-05-19 23:49:26 -0700 | [diff] [blame] | 104 | ancestor /foo / 0 |
| 105 | ancestor /foo /fo -1 |
| 106 | ancestor /foo /foo -1 |
David Reiss | 0454dd9 | 2008-05-19 23:49:26 -0700 | [diff] [blame] | 107 | ancestor /foo /bar -1 |
David Reiss | 0454dd9 | 2008-05-19 23:49:26 -0700 | [diff] [blame] | 108 | ancestor /foo /foo/bar -1 |
Michael Haggerty | 9e2326c | 2012-10-28 17:16:25 +0100 | [diff] [blame] | 109 | ancestor /foo /foo:/bar -1 |
| 110 | ancestor /foo /:/foo:/bar 0 |
| 111 | ancestor /foo /foo:/:/bar 0 |
| 112 | ancestor /foo /:/bar:/foo 0 |
David Reiss | 0454dd9 | 2008-05-19 23:49:26 -0700 | [diff] [blame] | 113 | ancestor /foo/bar / 0 |
| 114 | ancestor /foo/bar /fo -1 |
David Reiss | 0454dd9 | 2008-05-19 23:49:26 -0700 | [diff] [blame] | 115 | ancestor /foo/bar /foo 4 |
David Reiss | 0454dd9 | 2008-05-19 23:49:26 -0700 | [diff] [blame] | 116 | ancestor /foo/bar /foo/ba -1 |
| 117 | ancestor /foo/bar /:/fo 0 |
| 118 | ancestor /foo/bar /foo:/foo/ba 4 |
| 119 | ancestor /foo/bar /bar -1 |
Michael Haggerty | 9e2326c | 2012-10-28 17:16:25 +0100 | [diff] [blame] | 120 | ancestor /foo/bar /fo -1 |
| 121 | ancestor /foo/bar /foo:/bar 4 |
| 122 | ancestor /foo/bar /:/foo:/bar 4 |
| 123 | ancestor /foo/bar /foo:/:/bar 4 |
| 124 | ancestor /foo/bar /:/bar:/fo 0 |
| 125 | ancestor /foo/bar /:/bar 0 |
| 126 | ancestor /foo/bar /foo 4 |
| 127 | ancestor /foo/bar /foo:/bar 4 |
| 128 | ancestor /foo/bar /bar -1 |
David Reiss | 0454dd9 | 2008-05-19 23:49:26 -0700 | [diff] [blame] | 129 | |
Johannes Schindelin | 4fcc86b | 2009-02-19 20:10:49 +0100 | [diff] [blame] | 130 | test_expect_success 'strip_path_suffix' ' |
| 131 | test c:/msysgit = $(test-path-utils strip_path_suffix \ |
| 132 | c:/msysgit/libexec//git-core libexec/git-core) |
| 133 | ' |
Michael Haggerty | 8da650b | 2012-09-07 00:40:57 +0200 | [diff] [blame] | 134 | |
Michael Haggerty | a0601dc | 2012-09-07 00:40:59 +0200 | [diff] [blame] | 135 | test_expect_success 'absolute path rejects the empty string' ' |
Michael Haggerty | 17264bc | 2012-09-07 00:40:58 +0200 | [diff] [blame] | 136 | test_must_fail test-path-utils absolute_path "" |
| 137 | ' |
| 138 | |
Michael Haggerty | 3efe5d1 | 2012-09-07 00:41:01 +0200 | [diff] [blame] | 139 | test_expect_success 'real path rejects the empty string' ' |
Michael Haggerty | a5c4521 | 2012-09-07 00:41:00 +0200 | [diff] [blame] | 140 | test_must_fail test-path-utils real_path "" |
| 141 | ' |
| 142 | |
Johannes Sixt | bacca78 | 2012-09-09 17:42:20 +0200 | [diff] [blame] | 143 | test_expect_success POSIX 'real path works on absolute paths 1' ' |
Michael Haggerty | 7bcf48d | 2012-09-07 00:41:02 +0200 | [diff] [blame] | 144 | nopath="hopefully-absent-path" && |
| 145 | test "/" = "$(test-path-utils real_path "/")" && |
Johannes Sixt | bacca78 | 2012-09-09 17:42:20 +0200 | [diff] [blame] | 146 | test "/$nopath" = "$(test-path-utils real_path "/$nopath")" |
| 147 | ' |
| 148 | |
| 149 | test_expect_success 'real path works on absolute paths 2' ' |
| 150 | nopath="hopefully-absent-path" && |
Michael Haggerty | 7bcf48d | 2012-09-07 00:41:02 +0200 | [diff] [blame] | 151 | # Find an existing top-level directory for the remaining tests: |
| 152 | d=$(pwd -P | sed -e "s|^\([^/]*/[^/]*\)/.*|\1|") && |
| 153 | test "$d" = "$(test-path-utils real_path "$d")" && |
| 154 | test "$d/$nopath" = "$(test-path-utils real_path "$d/$nopath")" |
| 155 | ' |
| 156 | |
Michael Haggerty | 379a03a | 2012-09-07 00:41:04 +0200 | [diff] [blame] | 157 | test_expect_success POSIX 'real path removes extra leading slashes' ' |
| 158 | nopath="hopefully-absent-path" && |
| 159 | test "/" = "$(test-path-utils real_path "///")" && |
| 160 | test "/$nopath" = "$(test-path-utils real_path "///$nopath")" && |
| 161 | # Find an existing top-level directory for the remaining tests: |
| 162 | d=$(pwd -P | sed -e "s|^\([^/]*/[^/]*\)/.*|\1|") && |
| 163 | test "$d" = "$(test-path-utils real_path "//$d")" && |
| 164 | test "$d/$nopath" = "$(test-path-utils real_path "//$d/$nopath")" |
| 165 | ' |
| 166 | |
| 167 | test_expect_success 'real path removes other extra slashes' ' |
| 168 | nopath="hopefully-absent-path" && |
| 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 | |
Michael Haggerty | 7bcf48d | 2012-09-07 00:41:02 +0200 | [diff] [blame] | 175 | test_expect_success SYMLINKS 'real path works on symlinks' ' |
Michael Haggerty | 8da650b | 2012-09-07 00:40:57 +0200 | [diff] [blame] | 176 | mkdir first && |
| 177 | ln -s ../.git first/.git && |
| 178 | mkdir second && |
| 179 | ln -s ../first second/other && |
| 180 | mkdir third && |
| 181 | dir="$(cd .git; pwd -P)" && |
| 182 | dir2=third/../second/other/.git && |
| 183 | test "$dir" = "$(test-path-utils real_path $dir2)" && |
| 184 | file="$dir"/index && |
| 185 | test "$file" = "$(test-path-utils real_path $dir2/index)" && |
| 186 | basename=blub && |
| 187 | test "$dir/$basename" = "$(cd .git && test-path-utils real_path "$basename")" && |
| 188 | ln -s ../first/file .git/syml && |
| 189 | sym="$(cd first; pwd -P)"/file && |
| 190 | test "$sym" = "$(test-path-utils real_path "$dir2/syml")" |
| 191 | ' |
| 192 | |
Jiang Xin | 203439b | 2013-06-25 23:53:42 +0800 | [diff] [blame] | 193 | relative_path /a/b/c/ /a/b/ c/ |
| 194 | relative_path /a/b/c/ /a/b c/ |
| 195 | relative_path /a//b//c/ //a/b// c/ POSIX |
Jiang Xin | e02ca72 | 2013-06-25 23:53:43 +0800 | [diff] [blame] | 196 | relative_path /a/b /a/b ./ |
| 197 | relative_path /a/b/ /a/b ./ |
| 198 | relative_path /a /a/b ../ |
| 199 | relative_path / /a/b/ ../../ |
| 200 | relative_path /a/c /a/b/ ../c |
| 201 | relative_path /a/c /a/b ../c |
| 202 | relative_path /x/y /a/b/ ../../x/y |
Jiang Xin | abd4284 | 2013-06-25 23:53:57 +0800 | [diff] [blame] | 203 | relative_path /a/b "<empty>" /a/b |
| 204 | relative_path /a/b "<null>" /a/b |
Jiang Xin | 203439b | 2013-06-25 23:53:42 +0800 | [diff] [blame] | 205 | relative_path a/b/c/ a/b/ c/ |
| 206 | relative_path a/b/c/ a/b c/ |
| 207 | relative_path a/b//c a//b c |
Jiang Xin | e02ca72 | 2013-06-25 23:53:43 +0800 | [diff] [blame] | 208 | relative_path a/b/ a/b/ ./ |
| 209 | relative_path a/b/ a/b ./ |
| 210 | relative_path a a/b ../ |
| 211 | relative_path x/y a/b ../../x/y |
| 212 | relative_path a/c a/b ../c |
Jiang Xin | 203439b | 2013-06-25 23:53:42 +0800 | [diff] [blame] | 213 | relative_path a/b "<empty>" a/b |
| 214 | relative_path a/b "<null>" a/b |
Jiang Xin | e02ca72 | 2013-06-25 23:53:43 +0800 | [diff] [blame] | 215 | relative_path "<empty>" /a/b ./ |
| 216 | relative_path "<empty>" "<empty>" ./ |
| 217 | relative_path "<empty>" "<null>" ./ |
| 218 | relative_path "<null>" "<empty>" ./ |
| 219 | relative_path "<null>" "<null>" ./ |
| 220 | relative_path "<null>" /a/b ./ |
Jiang Xin | 203439b | 2013-06-25 23:53:42 +0800 | [diff] [blame] | 221 | |
David Reiss | ae299be | 2008-05-19 23:48:54 -0700 | [diff] [blame] | 222 | test_done |