Skip to content

Commit 4a7f704

Browse files
committed
tests: attr: implement tests to verify attribute rewriting behaviour
Implement some tests that verify that we are correctly updating gitattributes when rewriting or unlinking the corresponding files.
1 parent ed854aa commit 4a7f704

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

tests/attr/macro.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,36 @@ void test_attr_macro__bad_macros(void)
8989
cl_assert_equal_s("hahaha", values[4]);
9090
cl_assert(GIT_ATTR_IS_TRUE(values[5]));
9191
}
92+
93+
void test_attr_macro__macros_in_root_wd_apply(void)
94+
{
95+
const char *value;
96+
97+
g_repo = cl_git_sandbox_init("empty_standard_repo");
98+
99+
cl_git_pass(p_mkdir("empty_standard_repo/dir", 0777));
100+
cl_git_rewritefile("empty_standard_repo/.gitattributes", "[attr]customattr key=value\n");
101+
cl_git_rewritefile("empty_standard_repo/dir/.gitattributes", "file customattr\n");
102+
103+
cl_git_pass(git_attr_get(&value, g_repo, 0, "dir/file", "key"));
104+
cl_assert_equal_s(value, "value");
105+
}
106+
107+
void test_attr_macro__changing_macro_in_root_wd_updates_attributes(void)
108+
{
109+
const char *value;
110+
111+
g_repo = cl_git_sandbox_init("empty_standard_repo");
112+
113+
cl_git_rewritefile("empty_standard_repo/.gitattributes",
114+
"[attr]customattr key=first\n"
115+
"file customattr\n");
116+
cl_git_pass(git_attr_get(&value, g_repo, 0, "file", "key"));
117+
cl_assert_equal_s(value, "first");
118+
119+
cl_git_rewritefile("empty_standard_repo/.gitattributes",
120+
"[attr]customattr key=second\n"
121+
"file customattr\n");
122+
cl_git_pass(git_attr_get(&value, g_repo, 0, "file", "key"));
123+
cl_assert_equal_s(value, "second");
124+
}

tests/attr/repo.c

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,3 +351,55 @@ void test_attr_repo__sysdir_with_session(void)
351351
git_buf_dispose(&sysdir);
352352
git_attr_session__free(&session);
353353
}
354+
355+
void test_attr_repo__rewrite(void)
356+
{
357+
const char *value;
358+
359+
cl_git_rewritefile("attr/.gitattributes", "file.txt foo=first\n");
360+
cl_git_pass(git_attr_get(&value, g_repo, 0, "file.txt", "foo"));
361+
cl_assert_equal_s(value, "first");
362+
363+
cl_git_rewritefile("attr/.gitattributes", "file.txt foo=second\n");
364+
cl_git_pass(git_attr_get(&value, g_repo, 0, "file.txt", "foo"));
365+
cl_assert_equal_s(value, "second");
366+
367+
cl_git_rewritefile("attr/.gitattributes", "file.txt other=value\n");
368+
cl_git_pass(git_attr_get(&value, g_repo, 0, "file.txt", "foo"));
369+
cl_assert_equal_p(value, NULL);
370+
}
371+
372+
void test_attr_repo__rewrite_sysdir(void)
373+
{
374+
git_buf sysdir = GIT_BUF_INIT;
375+
const char *value;
376+
377+
cl_git_pass(p_mkdir("system", 0777));
378+
cl_git_pass(git_buf_joinpath(&sysdir, clar_sandbox_path(), "system"));
379+
cl_git_pass(git_sysdir_set(GIT_SYSDIR_SYSTEM, sysdir.ptr));
380+
g_repo = cl_git_sandbox_reopen();
381+
382+
cl_git_rewritefile("system/gitattributes", "file foo=first");
383+
cl_git_pass(git_attr_get(&value, g_repo, 0, "file", "foo"));
384+
cl_assert_equal_s(value, "first");
385+
386+
cl_git_rewritefile("system/gitattributes", "file foo=second");
387+
cl_git_pass(git_attr_get(&value, g_repo, 0, "file", "foo"));
388+
cl_assert_equal_s(value, "second");
389+
390+
git_buf_dispose(&sysdir);
391+
}
392+
393+
void test_attr_repo__unlink(void)
394+
{
395+
const char *value;
396+
397+
cl_git_rewritefile("attr/.gitattributes", "file.txt foo=value1\n");
398+
cl_git_pass(git_attr_get(&value, g_repo, 0, "file.txt", "foo"));
399+
cl_assert_equal_s(value, "value1");
400+
401+
cl_git_pass(p_unlink("attr/.gitattributes"));
402+
403+
cl_git_pass(git_attr_get(&value, g_repo, 0, "file.txt", "foo"));
404+
cl_assert_equal_p(value, NULL);
405+
}

0 commit comments

Comments
 (0)