Commit 93d37a1d5295b77731f2de238fffc7e01b9866b0

Patrick Steinhardt 2019-06-29T09:59:36

tests: core: improve symlink test coverage Add two more tests to verify that we're not deleting symlink targets, but the symlinks themselves. Furthermore, convert several `cl_skip`s on Win32 to conditional skips depending on whether the clar sandbox supports symlinks or not. Windows is grown up now and may allow unprivileged symlinks if the machine has been configured accordingly.

diff --git a/tests/core/filebuf.c b/tests/core/filebuf.c
index 8d1952f..e527ce9 100644
--- a/tests/core/filebuf.c
+++ b/tests/core/filebuf.c
@@ -157,9 +157,8 @@ void test_core_filebuf__symlink_follow(void)
 	git_filebuf file = GIT_FILEBUF_INIT;
 	const char *dir = "linkdir", *source = "linkdir/link";
 
-#ifdef GIT_WIN32
-	cl_skip();
-#endif
+	if (!git_path_supports_symlinks(clar_sandbox_path()))
+		cl_skip();
 
 	cl_git_pass(p_mkdir(dir, 0777));
 	cl_git_pass(p_symlink("target", source));
@@ -192,9 +191,8 @@ void test_core_filebuf__symlink_follow_absolute_paths(void)
 	git_filebuf file = GIT_FILEBUF_INIT;
 	git_buf source = GIT_BUF_INIT, target = GIT_BUF_INIT;
 
-#ifdef GIT_WIN32
-	cl_skip();
-#endif
+	if (!git_path_supports_symlinks(clar_sandbox_path()))
+		cl_skip();
 
 	cl_git_pass(git_buf_joinpath(&source, clar_sandbox_path(), "linkdir/link"));
 	cl_git_pass(git_buf_joinpath(&target, clar_sandbox_path(), "linkdir/target"));
@@ -221,9 +219,8 @@ void test_core_filebuf__symlink_depth(void)
 	git_filebuf file = GIT_FILEBUF_INIT;
 	const char *dir = "linkdir", *source = "linkdir/link";
 
-#ifdef GIT_WIN32
-	cl_skip();
-#endif
+	if (!git_path_supports_symlinks(clar_sandbox_path()))
+		cl_skip();
 
 	cl_git_pass(p_mkdir(dir, 0777));
 	/* Endless loop */
diff --git a/tests/core/futils.c b/tests/core/futils.c
index 82c9b24..2c8294c 100644
--- a/tests/core/futils.c
+++ b/tests/core/futils.c
@@ -66,3 +66,24 @@ void test_core_futils__write_hidden_file(void)
 #endif
 }
 
+void test_core_futils__recursive_rmdir_keeps_symlink_targets(void)
+{
+	if (!git_path_supports_symlinks(clar_sandbox_path()))
+		cl_skip();
+
+	cl_git_pass(git_futils_mkdir_r("a/b", 0777));
+	cl_git_pass(git_futils_mkdir_r("dir-target", 0777));
+	cl_git_mkfile("dir-target/file", "Contents");
+	cl_git_mkfile("file-target", "Contents");
+	cl_must_pass(p_symlink("dir-target", "a/symlink"));
+	cl_must_pass(p_symlink("file-target", "a/b/symlink"));
+
+	cl_git_pass(git_futils_rmdir_r("a", NULL, GIT_RMDIR_REMOVE_FILES));
+
+	cl_assert(git_path_exists("dir-target"));
+	cl_assert(git_path_exists("file-target"));
+
+	cl_must_pass(p_unlink("dir-target/file"));
+	cl_must_pass(p_rmdir("dir-target"));
+	cl_must_pass(p_unlink("file-target"));
+}
diff --git a/tests/core/posix.c b/tests/core/posix.c
index 795be31..10d689a 100644
--- a/tests/core/posix.c
+++ b/tests/core/posix.c
@@ -12,6 +12,7 @@
 #include <locale.h>
 
 #include "clar_libgit2.h"
+#include "futils.h"
 #include "posix.h"
 #include "userdiff.h"
 
@@ -263,3 +264,24 @@ void test_core_posix__p_regcomp_compile_userdiff_regexps(void)
 		cl_assert(!error);
 	}
 }
+
+void test_core_posix__unlink_removes_symlink(void)
+{
+	if (!git_path_supports_symlinks(clar_sandbox_path()))
+		clar__skip();
+
+	cl_git_mkfile("file", "Dummy file.");
+	cl_git_pass(git_futils_mkdir("dir", 0777, 0));
+
+	cl_must_pass(p_symlink("file", "file-symlink"));
+	cl_must_pass(p_symlink("dir", "dir-symlink"));
+
+	cl_must_pass(p_unlink("file-symlink"));
+	cl_must_pass(p_unlink("dir-symlink"));
+
+	cl_assert(git_path_exists("file"));
+	cl_assert(git_path_exists("dir"));
+
+	cl_must_pass(p_unlink("file"));
+	cl_must_pass(p_rmdir("dir"));
+}