Imported Upstream version 2.0.1
[platform/upstream/git.git] / t / t0060-path-utils.sh
index 09a42a4..c0143a0 100755 (executable)
@@ -8,8 +8,15 @@ test_description='Test various path utilities'
 . ./test-lib.sh
 
 norm_path() {
+       expected=$(test-path-utils print_path "$2")
        test_expect_success $3 "normalize path: $1 => $2" \
-       "test \"\$(test-path-utils normalize_path_copy '$1')\" = '$2'"
+       "test \"\$(test-path-utils normalize_path_copy '$1')\" = '$expected'"
+}
+
+relative_path() {
+       expected=$(test-path-utils print_path "$3")
+       test_expect_success $4 "relative path: $1 $2 => $3" \
+       "test \"\$(test-path-utils relative_path '$1' '$2')\" = '$expected'"
 }
 
 # On Windows, we are using MSYS's bash, which mangles the paths.
@@ -34,8 +41,8 @@ ancestor() {
         test \"\$actual\" = '$expected'"
 }
 
-# Absolute path tests must be skipped on Windows because due to path mangling
-# the test program never sees a POSIX-style absolute path
+# Some absolute path tests should be skipped on Windows due to path mangling
+# on POSIX-style absolute paths
 case $(uname -s) in
 *MINGW*)
        ;;
@@ -68,30 +75,30 @@ norm_path d1/s1//../s2/../../d2 d2
 norm_path d1/.../d2 d1/.../d2
 norm_path d1/..././../d2 d1/d2
 
-norm_path / / POSIX
+norm_path / /
 norm_path // / POSIX
 norm_path /// / POSIX
-norm_path /. / POSIX
+norm_path /. /
 norm_path /./ / POSIX
 norm_path /./.. ++failed++ POSIX
-norm_path /../. ++failed++ POSIX
+norm_path /../. ++failed++
 norm_path /./../.// ++failed++ POSIX
 norm_path /dir/.. / POSIX
 norm_path /dir/sub/../.. / POSIX
 norm_path /dir/sub/../../.. ++failed++ POSIX
-norm_path /dir /dir POSIX
-norm_path /dir// /dir/ POSIX
-norm_path /./dir /dir POSIX
-norm_path /dir/. /dir/ POSIX
-norm_path /dir///./ /dir/ POSIX
-norm_path /dir//sub/.. /dir/ POSIX
-norm_path /dir/sub/../ /dir/ POSIX
+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/ POSIX
-norm_path /dir/s1/../s2/ /dir/s2/ POSIX
-norm_path /d1/s1///s2/..//../s3/ /d1/s3/ POSIX
-norm_path /d1/s1//../s2/../../d2 /d2 POSIX
-norm_path /d1/.../d2 /d1/.../d2 POSIX
-norm_path /d1/..././../d2 /d1/d2 POSIX
+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
 
 ancestor / / -1
 ancestor /foo / 0
@@ -183,4 +190,58 @@ test_expect_success SYMLINKS 'real path works on symlinks' '
        test "$sym" = "$(test-path-utils real_path "$dir2/syml")"
 '
 
+test_expect_success SYMLINKS 'prefix_path works with absolute paths to work tree symlinks' '
+       ln -s target symlink &&
+       test "$(test-path-utils prefix_path prefix "$(pwd)/symlink")" = "symlink"
+'
+
+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
+'
+
+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")"
+'
+
+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
+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
+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        ./
+
 test_done