Commit c97d407d9cc54fc99af0a57e09e04e9e0bc68cb6

Russell Belfer 2013-09-05T11:45:29

Fix tests of file modes This fixes an issue checking file modes in the tests that initialize a repo from a template directory when a symlink is used in the template. Also, this updates some other places where we are examining file modes to use the new macros.

diff --git a/tests-clar/checkout/index.c b/tests-clar/checkout/index.c
index c9352d8..73050d0 100644
--- a/tests-clar/checkout/index.c
+++ b/tests-clar/checkout/index.c
@@ -267,7 +267,7 @@ void test_checkout_index__options_override_file_modes(void)
 	cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
 
 	cl_git_pass(p_stat("./testrepo/new.txt", &st));
-	cl_assert_equal_i_fmt(st.st_mode & 0777, 0700, "%07o");
+	cl_assert_equal_i_fmt(st.st_mode & GIT_MODE_PERMS_MASK, 0700, "%07o");
 #endif
 }
 
diff --git a/tests-clar/index/addall.c b/tests-clar/index/addall.c
index fca6e77..e6ce463 100644
--- a/tests-clar/index/addall.c
+++ b/tests-clar/index/addall.c
@@ -1,6 +1,7 @@
 #include "clar_libgit2.h"
 #include "../status/status_helpers.h"
 #include "posix.h"
+#include "fileops.h"
 
 git_repository *g_repo = NULL;
 
@@ -108,8 +109,10 @@ static void check_stat_data(git_index *index, const char *path, bool match)
 		cl_assert(st.st_size == entry->file_size);
 		cl_assert(st.st_uid  == entry->uid);
 		cl_assert(st.st_gid  == entry->gid);
-		cl_assert_equal_b(st.st_mode & ~0777, entry->mode & ~0777);
-		cl_assert_equal_b(st.st_mode &  0111, entry->mode &  0111);
+		cl_assert_equal_i_fmt(
+			GIT_MODE_TYPE(st.st_mode), GIT_MODE_TYPE(entry->mode), "%07o");
+		cl_assert_equal_b(
+			GIT_PERMS_EXECUTABLE(st.st_mode), GIT_PERMS_EXECUTABLE(entry->mode));
 	} else {
 		/* most things will still match */
 		cl_assert(st.st_size != entry->file_size);
diff --git a/tests-clar/repo/init.c b/tests-clar/repo/init.c
index 62e4ecd..d7f2524 100644
--- a/tests-clar/repo/init.c
+++ b/tests-clar/repo/init.c
@@ -364,6 +364,8 @@ void test_repo_init__extended_1(void)
 	cl_fixture_cleanup("root");
 }
 
+#define CLEAR_FOR_CORE_FILEMODE(M) ((M) &= ~0177)
+
 static void assert_hooks_match(
 	const char *template_dir,
 	const char *repo_dir,
@@ -382,16 +384,18 @@ static void assert_hooks_match(
 
 	cl_assert_equal_sz(expected_st.st_size, st.st_size);
 
-	expected_st.st_mode =
-		(expected_st.st_mode & ~0777) |
-		(((expected_st.st_mode & 0111) ? 0100777 : 0100666) & ~g_umask);
+	if (GIT_MODE_TYPE(expected_st.st_mode) != GIT_FILEMODE_LINK) {
+		mode_t expected_mode =
+			GIT_MODE_TYPE(expected_st.st_mode) |
+			(GIT_PERMS_FOR_WRITE(expected_st.st_mode) & ~g_umask);
 
-	if (!core_filemode) {
-		expected_st.st_mode = expected_st.st_mode & ~0177;
-		st.st_mode = st.st_mode & ~0177;
-	}
+		if (!core_filemode) {
+			CLEAR_FOR_CORE_FILEMODE(expected_mode);
+			CLEAR_FOR_CORE_FILEMODE(st.st_mode);
+		}
 
-	cl_assert_equal_i_fmt(expected_st.st_mode, st.st_mode, "%07o");
+		cl_assert_equal_i_fmt(expected_mode, st.st_mode, "%07o");
+	}
 
 	git_buf_free(&expected);
 	git_buf_free(&actual);
@@ -409,8 +413,8 @@ static void assert_mode_seems_okay(
 	git_buf_free(&full);
 
 	if (!core_filemode) {
-		expect_mode = expect_mode & ~0111;
-		st.st_mode = st.st_mode & ~0111;
+		CLEAR_FOR_CORE_FILEMODE(expect_mode);
+		CLEAR_FOR_CORE_FILEMODE(st.st_mode);
 		expect_setgid = false;
 	}
 
@@ -421,12 +425,11 @@ static void assert_mode_seems_okay(
 			cl_assert((st.st_mode & S_ISGID) == 0);
 	}
 
-	if ((expect_mode & 0111) != 0)
-		cl_assert((st.st_mode & 0111) != 0);
-	else
-		cl_assert((st.st_mode & 0111) == 0);
+	cl_assert_equal_b(
+		GIT_PERMS_EXECUTABLE(expect_mode), GIT_PERMS_EXECUTABLE(st.st_mode));
 
-	cl_assert((expect_mode & 0170000) == (st.st_mode & 0170000));
+	cl_assert_equal_i_fmt(
+		GIT_MODE_TYPE(expect_mode), GIT_MODE_TYPE(st.st_mode), "%07o");
 }
 
 void test_repo_init__extended_with_template(void)