]>
git.proxmox.com Git - libgit2.git/blob - tests/libgit2/attr/macro.c
2 * Copyright (C) the libgit2 contributors. All rights reserved.
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
8 #include "clar_libgit2.h"
10 #include "git2/sys/repository.h"
13 static git_repository
*g_repo
= NULL
;
15 void test_attr_macro__cleanup(void)
17 cl_git_sandbox_cleanup();
21 void test_attr_macro__macros(void)
23 const char *names
[7] = { "rootattr", "binary", "diff", "crlf", "merge", "text", "frotz" };
24 const char *names2
[5] = { "mymacro", "positive", "negative", "rootattr", "another" };
25 const char *names3
[3] = { "macro2", "multi2", "multi3" };
26 const char *values
[7];
28 g_repo
= cl_git_sandbox_init("attr");
30 cl_git_pass(git_attr_get_many(values
, g_repo
, 0, "binfile", 7, names
));
32 cl_assert(GIT_ATTR_IS_TRUE(values
[0]));
33 cl_assert(GIT_ATTR_IS_TRUE(values
[1]));
34 cl_assert(GIT_ATTR_IS_FALSE(values
[2]));
35 cl_assert(GIT_ATTR_IS_FALSE(values
[3]));
36 cl_assert(GIT_ATTR_IS_FALSE(values
[4]));
37 cl_assert(GIT_ATTR_IS_FALSE(values
[5]));
38 cl_assert(GIT_ATTR_IS_UNSPECIFIED(values
[6]));
40 cl_git_pass(git_attr_get_many(values
, g_repo
, 0, "macro_test", 5, names2
));
42 cl_assert(GIT_ATTR_IS_TRUE(values
[0]));
43 cl_assert(GIT_ATTR_IS_TRUE(values
[1]));
44 cl_assert(GIT_ATTR_IS_FALSE(values
[2]));
45 cl_assert(GIT_ATTR_IS_UNSPECIFIED(values
[3]));
46 cl_assert_equal_s("77", values
[4]);
48 cl_git_pass(git_attr_get_many(values
, g_repo
, 0, "macro_test", 3, names3
));
50 cl_assert(GIT_ATTR_IS_TRUE(values
[0]));
51 cl_assert(GIT_ATTR_IS_FALSE(values
[1]));
52 cl_assert_equal_s("answer", values
[2]);
55 void test_attr_macro__bad_macros(void)
57 const char *names
[6] = { "rootattr", "positive", "negative",
58 "firstmacro", "secondmacro", "thirdmacro" };
59 const char *values
[6];
61 g_repo
= cl_git_sandbox_init("attr");
63 cl_git_pass(git_attr_get_many(values
, g_repo
, 0, "macro_bad", 6, names
));
65 /* these three just confirm that the "mymacro" rule ran */
66 cl_assert(GIT_ATTR_IS_UNSPECIFIED(values
[0]));
67 cl_assert(GIT_ATTR_IS_TRUE(values
[1]));
68 cl_assert(GIT_ATTR_IS_FALSE(values
[2]));
71 * # let's try some malicious macro defs
72 * [attr]firstmacro -thirdmacro -secondmacro
73 * [attr]secondmacro firstmacro -firstmacro
74 * [attr]thirdmacro secondmacro=hahaha -firstmacro
75 * macro_bad firstmacro secondmacro thirdmacro
77 * firstmacro assignment list ends up with:
78 * -thirdmacro -secondmacro
79 * secondmacro assignment list expands "firstmacro" and ends up with:
80 * -thirdmacro -secondmacro -firstmacro
81 * thirdmacro assignment don't expand so list ends up with:
82 * secondmacro="hahaha"
84 * macro_bad assignment list ends up with:
85 * -thirdmacro -secondmacro firstmacro &&
86 * -thirdmacro -secondmacro -firstmacro secondmacro &&
87 * secondmacro="hahaha" thirdmacro
89 * so summary results should be:
90 * -firstmacro secondmacro="hahaha" thirdmacro
92 cl_assert(GIT_ATTR_IS_FALSE(values
[3]));
93 cl_assert_equal_s("hahaha", values
[4]);
94 cl_assert(GIT_ATTR_IS_TRUE(values
[5]));
97 void test_attr_macro__macros_in_root_wd_apply(void)
101 g_repo
= cl_git_sandbox_init("empty_standard_repo");
103 cl_git_pass(p_mkdir("empty_standard_repo/dir", 0777));
104 cl_git_rewritefile("empty_standard_repo/.gitattributes", "[attr]customattr key=value\n");
105 cl_git_rewritefile("empty_standard_repo/dir/.gitattributes", "file customattr\n");
107 cl_git_pass(git_attr_get(&value
, g_repo
, 0, "dir/file", "key"));
108 cl_assert_equal_s(value
, "value");
111 void test_attr_macro__changing_macro_in_root_wd_updates_attributes(void)
115 g_repo
= cl_git_sandbox_init("empty_standard_repo");
117 cl_git_rewritefile("empty_standard_repo/.gitattributes",
118 "[attr]customattr key=first\n"
119 "file customattr\n");
120 cl_git_pass(git_attr_get(&value
, g_repo
, 0, "file", "key"));
121 cl_assert_equal_s(value
, "first");
123 cl_git_rewritefile("empty_standard_repo/.gitattributes",
124 "[attr]customattr key=second\n"
125 "file customattr\n");
126 cl_git_pass(git_attr_get(&value
, g_repo
, 0, "file", "key"));
127 cl_assert_equal_s(value
, "second");
130 void test_attr_macro__macros_in_subdir_do_not_apply(void)
134 g_repo
= cl_git_sandbox_init("empty_standard_repo");
136 cl_git_pass(p_mkdir("empty_standard_repo/dir", 0777));
137 cl_git_rewritefile("empty_standard_repo/dir/.gitattributes",
138 "[attr]customattr key=value\n"
139 "file customattr\n");
141 /* This should _not_ pass, as macros in subdirectories shall be ignored */
142 cl_git_pass(git_attr_get(&value
, g_repo
, 0, "dir/file", "key"));
143 cl_assert_equal_p(value
, NULL
);
146 void test_attr_macro__adding_macro_succeeds(void)
150 g_repo
= cl_git_sandbox_init("empty_standard_repo");
151 cl_git_pass(git_attr_add_macro(g_repo
, "macro", "key=value"));
152 cl_git_rewritefile("empty_standard_repo/.gitattributes", "file.txt macro\n");
154 cl_git_pass(git_attr_get(&value
, g_repo
, 0, "file.txt", "key"));
155 cl_assert_equal_s(value
, "value");
158 void test_attr_macro__adding_boolean_macros_succeeds(void)
162 g_repo
= cl_git_sandbox_init("empty_standard_repo");
163 cl_git_pass(git_attr_add_macro(g_repo
, "macro-pos", "positive"));
164 cl_git_pass(git_attr_add_macro(g_repo
, "macro-neg", "-negative"));
165 cl_git_rewritefile("empty_standard_repo/.gitattributes", "file.txt macro-pos macro-neg\n");
167 cl_git_pass(git_attr_get(&value
, g_repo
, 0, "file.txt", "positive"));
168 cl_assert(GIT_ATTR_IS_TRUE(value
));
169 cl_git_pass(git_attr_get(&value
, g_repo
, 0, "file.txt", "negative"));
170 cl_assert(GIT_ATTR_IS_FALSE(value
));
173 void test_attr_macro__redefining_macro_succeeds(void)
177 g_repo
= cl_git_sandbox_init("empty_standard_repo");
178 cl_git_pass(git_attr_add_macro(g_repo
, "macro", "key=value1"));
179 cl_git_pass(git_attr_add_macro(g_repo
, "macro", "key=value2"));
180 cl_git_rewritefile("empty_standard_repo/.gitattributes", "file.txt macro");
182 cl_git_pass(git_attr_get(&value
, g_repo
, 0, "file.txt", "key"));
183 cl_assert_equal_s(value
, "value2");
186 void test_attr_macro__recursive_macro_resolves(void)
190 g_repo
= cl_git_sandbox_init("empty_standard_repo");
191 cl_git_pass(git_attr_add_macro(g_repo
, "expandme", "key=value"));
192 cl_git_pass(git_attr_add_macro(g_repo
, "macro", "expandme"));
193 cl_git_rewritefile("empty_standard_repo/.gitattributes", "file.txt macro");
195 cl_git_pass(git_attr_get(&value
, g_repo
, 0, "file.txt", "key"));
196 cl_assert_equal_s(value
, "value");