From 8da650b456d8fd744abf401a67535acbdd6c22c7 Mon Sep 17 00:00:00 2001 From: Michael Haggerty Date: Fri, 7 Sep 2012 00:40:57 +0200 Subject: t0060: move tests of real_path() from t0000 to here Suggested by: Johannes Sixt Signed-off-by: Michael Haggerty Signed-off-by: Junio C Hamano diff --git a/t/t0000-basic.sh b/t/t0000-basic.sh index ccb5435..ae6a3f0 100755 --- a/t/t0000-basic.sh +++ b/t/t0000-basic.sh @@ -450,24 +450,6 @@ test_expect_success 'update-index D/F conflict' ' test $numpath0 = 1 ' -test_expect_success SYMLINKS 'real path works as expected' ' - 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")" -' - test_expect_success 'very long name in the index handled sanely' ' a=a && # 1 diff --git a/t/t0060-path-utils.sh b/t/t0060-path-utils.sh index 53cf1f8..c8db144 100755 --- a/t/t0060-path-utils.sh +++ b/t/t0060-path-utils.sh @@ -139,4 +139,23 @@ test_expect_success 'strip_path_suffix' ' test c:/msysgit = $(test-path-utils strip_path_suffix \ c:/msysgit/libexec//git-core libexec/git-core) ' + +test_expect_success SYMLINKS 'real path works as expected' ' + 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")" +' + test_done -- cgit v0.10.2-6-g49f6 From 17264bcc4f580d5e2a94703967236e770f6e236f Mon Sep 17 00:00:00 2001 From: Michael Haggerty Date: Fri, 7 Sep 2012 00:40:58 +0200 Subject: t0060: verify that absolute_path() fails if passed the empty string It doesn't, so mark the test as failing. Signed-off-by: Michael Haggerty Signed-off-by: Junio C Hamano diff --git a/t/t0060-path-utils.sh b/t/t0060-path-utils.sh index c8db144..d91e516 100755 --- a/t/t0060-path-utils.sh +++ b/t/t0060-path-utils.sh @@ -140,6 +140,10 @@ test_expect_success 'strip_path_suffix' ' c:/msysgit/libexec//git-core libexec/git-core) ' +test_expect_failure 'absolute path rejects the empty string' ' + test_must_fail test-path-utils absolute_path "" +' + test_expect_success SYMLINKS 'real path works as expected' ' mkdir first && ln -s ../.git first/.git && -- cgit v0.10.2-6-g49f6 From a0601dc11fab5b4525a348b2ad6c9bb92529a281 Mon Sep 17 00:00:00 2001 From: Michael Haggerty Date: Fri, 7 Sep 2012 00:40:59 +0200 Subject: absolute_path(): reject the empty string Signed-off-by: Michael Haggerty Signed-off-by: Junio C Hamano diff --git a/abspath.c b/abspath.c index f04ac18..5d62430 100644 --- a/abspath.c +++ b/abspath.c @@ -123,7 +123,9 @@ const char *absolute_path(const char *path) { static char buf[PATH_MAX + 1]; - if (is_absolute_path(path)) { + if (!*path) { + die("The empty string is not a valid path"); + } else if (is_absolute_path(path)) { if (strlcpy(buf, path, PATH_MAX) >= PATH_MAX) die("Too long path: %.*s", 60, path); } else { diff --git a/t/t0060-path-utils.sh b/t/t0060-path-utils.sh index d91e516..924aa60 100755 --- a/t/t0060-path-utils.sh +++ b/t/t0060-path-utils.sh @@ -140,7 +140,7 @@ test_expect_success 'strip_path_suffix' ' c:/msysgit/libexec//git-core libexec/git-core) ' -test_expect_failure 'absolute path rejects the empty string' ' +test_expect_success 'absolute path rejects the empty string' ' test_must_fail test-path-utils absolute_path "" ' -- cgit v0.10.2-6-g49f6 From a5c45218b63ce7ac6f5a4a48c4312a1e37e01f3f Mon Sep 17 00:00:00 2001 From: Michael Haggerty Date: Fri, 7 Sep 2012 00:41:00 +0200 Subject: t0060: verify that real_path() fails if passed the empty string It doesn't, so mark the test as failing. Signed-off-by: Michael Haggerty Signed-off-by: Junio C Hamano diff --git a/t/t0060-path-utils.sh b/t/t0060-path-utils.sh index 924aa60..1118056 100755 --- a/t/t0060-path-utils.sh +++ b/t/t0060-path-utils.sh @@ -144,6 +144,10 @@ test_expect_success 'absolute path rejects the empty string' ' test_must_fail test-path-utils absolute_path "" ' +test_expect_failure 'real path rejects the empty string' ' + test_must_fail test-path-utils real_path "" +' + test_expect_success SYMLINKS 'real path works as expected' ' mkdir first && ln -s ../.git first/.git && -- cgit v0.10.2-6-g49f6 From 3efe5d1d32fde899b23ebbb1fde499a0897e1c4e Mon Sep 17 00:00:00 2001 From: Michael Haggerty Date: Fri, 7 Sep 2012 00:41:01 +0200 Subject: real_path(): reject the empty string Signed-off-by: Michael Haggerty Signed-off-by: Junio C Hamano diff --git a/abspath.c b/abspath.c index 5d62430..3e8325c 100644 --- a/abspath.c +++ b/abspath.c @@ -35,6 +35,9 @@ const char *real_path(const char *path) if (path == buf || path == next_buf) return path; + if (!*path) + die("The empty string is not a valid path"); + if (strlcpy(buf, path, PATH_MAX) >= PATH_MAX) die ("Too long path: %.*s", 60, path); diff --git a/t/t0060-path-utils.sh b/t/t0060-path-utils.sh index 1118056..fab5ea2 100755 --- a/t/t0060-path-utils.sh +++ b/t/t0060-path-utils.sh @@ -144,7 +144,7 @@ test_expect_success 'absolute path rejects the empty string' ' test_must_fail test-path-utils absolute_path "" ' -test_expect_failure 'real path rejects the empty string' ' +test_expect_success 'real path rejects the empty string' ' test_must_fail test-path-utils real_path "" ' -- cgit v0.10.2-6-g49f6 From 7bcf48dad83e7a07f27403c2ce9e5c29af9e317d Mon Sep 17 00:00:00 2001 From: Michael Haggerty Date: Fri, 7 Sep 2012 00:41:02 +0200 Subject: t0060: verify that real_path() works correctly with absolute paths There is currently a bug: if passed an absolute top-level path that doesn't exist (e.g., "/foo") it incorrectly interprets the path as a relative path (e.g., returns "$(pwd)/foo"). So mark the test as failing. These tests are skipped on Windows because test-path-utils operates on a DOS-style absolute path even if a POSIX style absolute path is passed as argument. Adjusted for Windows by: Johannes Sixt Signed-off-by: Michael Haggerty Signed-off-by: Junio C Hamano diff --git a/t/t0060-path-utils.sh b/t/t0060-path-utils.sh index fab5ea2..3121691 100755 --- a/t/t0060-path-utils.sh +++ b/t/t0060-path-utils.sh @@ -148,7 +148,17 @@ test_expect_success 'real path rejects the empty string' ' test_must_fail test-path-utils real_path "" ' -test_expect_success SYMLINKS 'real path works as expected' ' +test_expect_failure POSIX 'real path works on absolute paths' ' + 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 SYMLINKS 'real path works on symlinks' ' mkdir first && ln -s ../.git first/.git && mkdir second && -- cgit v0.10.2-6-g49f6 From f4c21e89d7c6b578140e90c02b40eccb51ab8428 Mon Sep 17 00:00:00 2001 From: Michael Haggerty Date: Fri, 7 Sep 2012 00:41:03 +0200 Subject: real_path(): properly handle nonexistent top-level paths The change has two points: 1. Do not strip off a leading slash, because that erroneously turns an absolute path into a relative path. 2. Do not remove slashes from groups of multiple slashes; instead let chdir() handle them. It could be, for example, that it wants to leave leading double-slashes alone. Signed-off-by: Michael Haggerty Signed-off-by: Junio C Hamano diff --git a/abspath.c b/abspath.c index 3e8325c..05f2d79 100644 --- a/abspath.c +++ b/abspath.c @@ -45,8 +45,8 @@ const char *real_path(const char *path) if (!is_directory(buf)) { char *last_slash = find_last_dir_sep(buf); if (last_slash) { - *last_slash = '\0'; last_elem = xstrdup(last_slash + 1); + last_slash[1] = '\0'; } else { last_elem = xstrdup(buf); *buf = '\0'; diff --git a/t/t0060-path-utils.sh b/t/t0060-path-utils.sh index 3121691..30361f9 100755 --- a/t/t0060-path-utils.sh +++ b/t/t0060-path-utils.sh @@ -148,7 +148,7 @@ test_expect_success 'real path rejects the empty string' ' test_must_fail test-path-utils real_path "" ' -test_expect_failure POSIX 'real path works on absolute paths' ' +test_expect_success POSIX 'real path works on absolute paths' ' nopath="hopefully-absent-path" && test "/" = "$(test-path-utils real_path "/")" && test "/$nopath" = "$(test-path-utils real_path "/$nopath")" && -- cgit v0.10.2-6-g49f6 From 379a03ad8194192ab7b2910ec3326a981971fda1 Mon Sep 17 00:00:00 2001 From: Michael Haggerty Date: Fri, 7 Sep 2012 00:41:04 +0200 Subject: t0060: verify that real_path() removes extra slashes Adjusted for Windows by: Johannes Sixt Signed-off-by: Michael Haggerty Signed-off-by: Junio C Hamano diff --git a/t/t0060-path-utils.sh b/t/t0060-path-utils.sh index 30361f9..e40f764 100755 --- a/t/t0060-path-utils.sh +++ b/t/t0060-path-utils.sh @@ -158,6 +158,24 @@ test_expect_success POSIX 'real path works on absolute paths' ' test "$d/$nopath" = "$(test-path-utils real_path "$d/$nopath")" ' +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")" +' + test_expect_success SYMLINKS 'real path works on symlinks' ' mkdir first && ln -s ../.git first/.git && -- cgit v0.10.2-6-g49f6 From bacca7852f2f304ebe3f579de1a6cf0152af3df1 Mon Sep 17 00:00:00 2001 From: Johannes Sixt Date: Sun, 9 Sep 2012 17:42:20 +0200 Subject: t0060: split absolute path test in two to exercise some of it on Windows Only the first half of the test works only on POSIX, the second half passes on Windows as well. A later test "real path removes other extra slashes" looks very similar, but it does not make sense to split it in the same way: When two slashes are prepended in front of an absolute DOS-style path on Windows, the meaning of the path is changed (//server/share style), so that the test cannot pass on Windows. Signed-off-by: Johannes Sixt Acked-by: Michael Haggerty Signed-off-by: Junio C Hamano diff --git a/t/t0060-path-utils.sh b/t/t0060-path-utils.sh index e40f764..4ef2345 100755 --- a/t/t0060-path-utils.sh +++ b/t/t0060-path-utils.sh @@ -148,10 +148,14 @@ test_expect_success 'real path rejects the empty string' ' test_must_fail test-path-utils real_path "" ' -test_expect_success POSIX 'real path works on absolute paths' ' +test_expect_success POSIX 'real path works on absolute paths 1' ' nopath="hopefully-absent-path" && test "/" = "$(test-path-utils real_path "/")" && - test "/$nopath" = "$(test-path-utils real_path "/$nopath")" && + test "/$nopath" = "$(test-path-utils real_path "/$nopath")" +' + +test_expect_success 'real path works on absolute paths 2' ' + 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")" && -- cgit v0.10.2-6-g49f6