Commit c87abeca9ee68a89fa3a6776aa7d44f9d14de6e7

Patrick Steinhardt 2019-07-04T11:45:02

tests: attr: add tests for system-level attributes There were no tests that verified that system-level gitattributes files get handled correctly. In fact, we have recently introduced a regression that caused us to abort if there was a system-level gitattributes file available. Add two tests that verify that we're able to handle system-level gitattributes files. The test attr::repo::sysdir_with_session would've failed without the fix to the described regression.

diff --git a/tests/attr/repo.c b/tests/attr/repo.c
index 5f50e90..32f7b66 100644
--- a/tests/attr/repo.c
+++ b/tests/attr/repo.c
@@ -2,6 +2,7 @@
 #include "fileops.h"
 #include "git2/attr.h"
 #include "attr.h"
+#include "sysdir.h"
 
 #include "attr_expect.h"
 #include "git2/sys/repository.h"
@@ -17,6 +18,7 @@ void test_attr_repo__cleanup(void)
 {
 	cl_git_sandbox_cleanup();
 	g_repo = NULL;
+	cl_sandbox_set_search_path_defaults();
 }
 
 static struct attr_expected get_one_test_cases[] = {
@@ -376,3 +378,46 @@ void test_attr_repo__bare_repo_with_index(void)
 	cl_assert(GIT_ATTR_IS_FALSE(values[2]));
 	cl_assert(GIT_ATTR_IS_UNSPECIFIED(values[3]));
 }
+
+void test_attr_repo__sysdir(void)
+{
+	git_buf sysdir = GIT_BUF_INIT;
+	const char *value;
+
+	cl_git_pass(p_mkdir("system", 0777));
+	cl_git_rewritefile("system/gitattributes", "file merge=foo");
+	cl_git_pass(git_buf_joinpath(&sysdir, clar_sandbox_path(), "system"));
+	cl_git_pass(git_sysdir_set(GIT_SYSDIR_SYSTEM, sysdir.ptr));
+	g_repo = cl_git_sandbox_reopen();
+
+	cl_git_pass(git_attr_get(&value, g_repo, 0, "file", "merge"));
+	cl_assert_equal_s(value, "foo");
+
+	cl_git_pass(p_unlink("system/gitattributes"));
+	cl_git_pass(p_rmdir("system"));
+	git_buf_dispose(&sysdir);
+}
+
+void test_attr_repo__sysdir_with_session(void)
+{
+	const char *values[2], *attrs[2] = { "foo", "bar" };
+	git_buf sysdir = GIT_BUF_INIT;
+	git_attr_session session;
+
+	cl_git_pass(p_mkdir("system", 0777));
+	cl_git_rewritefile("system/gitattributes", "file foo=1 bar=2");
+	cl_git_pass(git_buf_joinpath(&sysdir, clar_sandbox_path(), "system"));
+	cl_git_pass(git_sysdir_set(GIT_SYSDIR_SYSTEM, sysdir.ptr));
+	g_repo = cl_git_sandbox_reopen();
+
+	cl_git_pass(git_attr_session__init(&session, g_repo));
+	cl_git_pass(git_attr_get_many_with_session(values, g_repo, &session, 0, "file", ARRAY_SIZE(attrs), attrs));
+
+	cl_assert_equal_s(values[0], "1");
+	cl_assert_equal_s(values[1], "2");
+
+	cl_git_pass(p_unlink("system/gitattributes"));
+	cl_git_pass(p_rmdir("system"));
+	git_buf_dispose(&sysdir);
+	git_attr_session__free(&session);
+}