]> git.proxmox.com Git - libgit2.git/blame - tests/attr/macro.c
New upstream version 1.4.3+dfsg.1
[libgit2.git] / tests / attr / macro.c
CommitLineData
22a2d3d5
UG
1/*
2 * Copyright (C) the libgit2 contributors. All rights reserved.
3 *
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.
6 */
7
8#include "clar_libgit2.h"
9
10#include "git2/sys/repository.h"
11#include "attr.h"
12
13static git_repository *g_repo = NULL;
14
15void test_attr_macro__cleanup(void)
16{
17 cl_git_sandbox_cleanup();
18 g_repo = NULL;
19}
20
21void test_attr_macro__macros(void)
22{
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];
27
28 g_repo = cl_git_sandbox_init("attr");
29
30 cl_git_pass(git_attr_get_many(values, g_repo, 0, "binfile", 7, names));
31
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]));
39
40 cl_git_pass(git_attr_get_many(values, g_repo, 0, "macro_test", 5, names2));
41
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]);
47
48 cl_git_pass(git_attr_get_many(values, g_repo, 0, "macro_test", 3, names3));
49
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]);
53}
54
55void test_attr_macro__bad_macros(void)
56{
57 const char *names[6] = { "rootattr", "positive", "negative",
58 "firstmacro", "secondmacro", "thirdmacro" };
59 const char *values[6];
60
61 g_repo = cl_git_sandbox_init("attr");
62
63 cl_git_pass(git_attr_get_many(values, g_repo, 0, "macro_bad", 6, names));
64
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]));
69
70 /* file contains:
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
76 *
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"
83 *
84 * macro_bad assignment list ends up with:
85 * -thirdmacro -secondmacro firstmacro &&
86 * -thirdmacro -secondmacro -firstmacro secondmacro &&
87 * secondmacro="hahaha" thirdmacro
88 *
89 * so summary results should be:
90 * -firstmacro secondmacro="hahaha" thirdmacro
91 */
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]));
95}
96
97void test_attr_macro__macros_in_root_wd_apply(void)
98{
99 const char *value;
100
101 g_repo = cl_git_sandbox_init("empty_standard_repo");
102
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");
106
107 cl_git_pass(git_attr_get(&value, g_repo, 0, "dir/file", "key"));
108 cl_assert_equal_s(value, "value");
109}
110
111void test_attr_macro__changing_macro_in_root_wd_updates_attributes(void)
112{
113 const char *value;
114
115 g_repo = cl_git_sandbox_init("empty_standard_repo");
116
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");
122
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");
128}
129
130void test_attr_macro__macros_in_subdir_do_not_apply(void)
131{
132 const char *value;
133
134 g_repo = cl_git_sandbox_init("empty_standard_repo");
135
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");
140
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);
144}
145
146void test_attr_macro__adding_macro_succeeds(void)
147{
148 const char *value;
149
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");
153
154 cl_git_pass(git_attr_get(&value, g_repo, 0, "file.txt", "key"));
155 cl_assert_equal_s(value, "value");
156}
157
158void test_attr_macro__adding_boolean_macros_succeeds(void)
159{
160 const char *value;
161
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");
166
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));
171}
172
173void test_attr_macro__redefining_macro_succeeds(void)
174{
175 const char *value;
176
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");
181
182 cl_git_pass(git_attr_get(&value, g_repo, 0, "file.txt", "key"));
183 cl_assert_equal_s(value, "value2");
184}
185
186void test_attr_macro__recursive_macro_resolves(void)
187{
188 const char *value;
189
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");
194
195 cl_git_pass(git_attr_get(&value, g_repo, 0, "file.txt", "key"));
196 cl_assert_equal_s(value, "value");
197}