Commit 780f3e540fa6492b244ef2750a71e39407526c1e

Russell Belfer 2013-09-04T16:13:18

Make tests take umask into account It seems that libgit2 is correctly applying the umask when initializing a repository from a template and when creating new directories during checkout, but the test suite is not accounting for possible variations due to the umask. This updates that so that the test suite will work regardless of the umask.

diff --git a/tests-clar/checkout/index.c b/tests-clar/checkout/index.c
index 982bf9e..a185aec 100644
--- a/tests-clar/checkout/index.c
+++ b/tests-clar/checkout/index.c
@@ -229,6 +229,7 @@ void test_checkout_index__options_dir_modes(void)
 	struct stat st;
 	git_oid oid;
 	git_commit *commit;
+	mode_t um;
 
 	cl_git_pass(git_reference_name_to_id(&oid, g_repo, "refs/heads/dir"));
 	cl_git_pass(git_commit_lookup(&commit, g_repo, &oid));
@@ -240,12 +241,15 @@ void test_checkout_index__options_dir_modes(void)
 
 	cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
 
+	/* umask will influence actual directory creation mode */
+	(void)p_umask(um = p_umask(022));
+
 	cl_git_pass(p_stat("./testrepo/a", &st));
-	cl_assert_equal_i(st.st_mode & 0777, 0701);
+	cl_assert_equal_i_fmt(st.st_mode, GIT_FILEMODE_TREE | 0701 & ~um, "%07o");
 
 	/* File-mode test, since we're on the 'dir' branch */
 	cl_git_pass(p_stat("./testrepo/a/b.txt", &st));
-	cl_assert_equal_i(st.st_mode & 0777, 0755);
+	cl_assert_equal_i_fmt(st.st_mode, GIT_FILEMODE_BLOB_EXECUTABLE, "%07o");
 
 	git_commit_free(commit);
 #endif
@@ -263,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(st.st_mode & 0777, 0700);
+	cl_assert_equal_i_fmt(st.st_mode & 0777, 0700, "%07o");
 #endif
 }
 
diff --git a/tests-clar/clar_libgit2.h b/tests-clar/clar_libgit2.h
index 8c8357e..080d32b 100644
--- a/tests-clar/clar_libgit2.h
+++ b/tests-clar/clar_libgit2.h
@@ -32,7 +32,7 @@ void cl_git_report_failure(int, const char *, int, const char *);
 #define cl_assert_at_line(expr,file,line) \
 	clar__assert((expr) != 0, file, line, "Expression is not true: " #expr, NULL, 1)
 
-#define cl_assert_equal_sz(sz1,sz2) cl_assert_equal_i((int)sz1, (int)(sz2))
+#define cl_assert_equal_sz(sz1,sz2) clar__assert_equal(__FILE__,__LINE__,#sz1 " != " #sz2, 1, PRIuZ, (size_t)(sz1), (size_t)(sz2))
 
 GIT_INLINE(void) clar__assert_in_range(
 	int lo, int val, int hi,
diff --git a/tests-clar/repo/init.c b/tests-clar/repo/init.c
index 5076184..4d93f3d 100644
--- a/tests-clar/repo/init.c
+++ b/tests-clar/repo/init.c
@@ -10,10 +10,17 @@ enum repo_mode {
 };
 
 static git_repository *_repo = NULL;
+static mode_t _umask = 0;
 
 void test_repo_init__initialize(void)
 {
 	_repo = NULL;
+
+	/* load umask if not already loaded */
+	if (!_umask) {
+		_umask = p_umask(022);
+		(void)p_umask(_umask);
+	}
 }
 
 static void cleanup_repository(void *path)
@@ -377,14 +384,18 @@ static void assert_hooks_match(
 	cl_git_pass(git_buf_joinpath(&actual, repo_dir, hook_path));
 	cl_git_pass(git_path_lstat(actual.ptr, &st));
 
-	cl_assert(expected_st.st_size == st.st_size);
+	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) & ~_umask);
 
 	if (!core_filemode) {
 		expected_st.st_mode = expected_st.st_mode & ~0111;
 		st.st_mode = st.st_mode & ~0111;
 	}
 
-	cl_assert_equal_i((int)expected_st.st_mode, (int)st.st_mode);
+	cl_assert_equal_i_fmt(expected_st.st_mode, st.st_mode, "%07o");
 
 	git_buf_free(&expected);
 	git_buf_free(&actual);