Skip to content

Commit ed854aa

Browse files
committed
tests: attr: extract macro tests into their own suite
As macros are a specific functionality in the gitattributes code, it makes sense to extract them into their own test suite, too. This makes finding macro-related tests easier.
1 parent dacac9e commit ed854aa

File tree

2 files changed

+91
-70
lines changed

2 files changed

+91
-70
lines changed

tests/attr/macro.c

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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+
#include "attr.h"
10+
11+
static git_repository *g_repo = NULL;
12+
13+
void test_attr_macro__cleanup(void)
14+
{
15+
cl_git_sandbox_cleanup();
16+
g_repo = NULL;
17+
}
18+
19+
void test_attr_macro__macros(void)
20+
{
21+
const char *names[5] = { "rootattr", "binary", "diff", "crlf", "frotz" };
22+
const char *names2[5] = { "mymacro", "positive", "negative", "rootattr", "another" };
23+
const char *names3[3] = { "macro2", "multi2", "multi3" };
24+
const char *values[5];
25+
26+
g_repo = cl_git_sandbox_init("attr");
27+
28+
cl_git_pass(git_attr_get_many(values, g_repo, 0, "binfile", 5, names));
29+
30+
cl_assert(GIT_ATTR_IS_TRUE(values[0]));
31+
cl_assert(GIT_ATTR_IS_TRUE(values[1]));
32+
cl_assert(GIT_ATTR_IS_FALSE(values[2]));
33+
cl_assert(GIT_ATTR_IS_FALSE(values[3]));
34+
cl_assert(GIT_ATTR_IS_UNSPECIFIED(values[4]));
35+
36+
cl_git_pass(git_attr_get_many(values, g_repo, 0, "macro_test", 5, names2));
37+
38+
cl_assert(GIT_ATTR_IS_TRUE(values[0]));
39+
cl_assert(GIT_ATTR_IS_TRUE(values[1]));
40+
cl_assert(GIT_ATTR_IS_FALSE(values[2]));
41+
cl_assert(GIT_ATTR_IS_UNSPECIFIED(values[3]));
42+
cl_assert_equal_s("77", values[4]);
43+
44+
cl_git_pass(git_attr_get_many(values, g_repo, 0, "macro_test", 3, names3));
45+
46+
cl_assert(GIT_ATTR_IS_TRUE(values[0]));
47+
cl_assert(GIT_ATTR_IS_FALSE(values[1]));
48+
cl_assert_equal_s("answer", values[2]);
49+
}
50+
51+
void test_attr_macro__bad_macros(void)
52+
{
53+
const char *names[6] = { "rootattr", "positive", "negative",
54+
"firstmacro", "secondmacro", "thirdmacro" };
55+
const char *values[6];
56+
57+
g_repo = cl_git_sandbox_init("attr");
58+
59+
cl_git_pass(git_attr_get_many(values, g_repo, 0, "macro_bad", 6, names));
60+
61+
/* these three just confirm that the "mymacro" rule ran */
62+
cl_assert(GIT_ATTR_IS_UNSPECIFIED(values[0]));
63+
cl_assert(GIT_ATTR_IS_TRUE(values[1]));
64+
cl_assert(GIT_ATTR_IS_FALSE(values[2]));
65+
66+
/* file contains:
67+
* # let's try some malicious macro defs
68+
* [attr]firstmacro -thirdmacro -secondmacro
69+
* [attr]secondmacro firstmacro -firstmacro
70+
* [attr]thirdmacro secondmacro=hahaha -firstmacro
71+
* macro_bad firstmacro secondmacro thirdmacro
72+
*
73+
* firstmacro assignment list ends up with:
74+
* -thirdmacro -secondmacro
75+
* secondmacro assignment list expands "firstmacro" and ends up with:
76+
* -thirdmacro -secondmacro -firstmacro
77+
* thirdmacro assignment don't expand so list ends up with:
78+
* secondmacro="hahaha"
79+
*
80+
* macro_bad assignment list ends up with:
81+
* -thirdmacro -secondmacro firstmacro &&
82+
* -thirdmacro -secondmacro -firstmacro secondmacro &&
83+
* secondmacro="hahaha" thirdmacro
84+
*
85+
* so summary results should be:
86+
* -firstmacro secondmacro="hahaha" thirdmacro
87+
*/
88+
cl_assert(GIT_ATTR_IS_FALSE(values[3]));
89+
cl_assert_equal_s("hahaha", values[4]);
90+
cl_assert(GIT_ATTR_IS_TRUE(values[5]));
91+
}

tests/attr/repo.c

Lines changed: 0 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -219,76 +219,6 @@ void test_attr_repo__manpage_example(void)
219219
cl_assert(GIT_ATTR_IS_UNSPECIFIED(value));
220220
}
221221

222-
void test_attr_repo__macros(void)
223-
{
224-
const char *names[5] = { "rootattr", "binary", "diff", "crlf", "frotz" };
225-
const char *names2[5] = { "mymacro", "positive", "negative", "rootattr", "another" };
226-
const char *names3[3] = { "macro2", "multi2", "multi3" };
227-
const char *values[5];
228-
229-
cl_git_pass(git_attr_get_many(values, g_repo, 0, "binfile", 5, names));
230-
231-
cl_assert(GIT_ATTR_IS_TRUE(values[0]));
232-
cl_assert(GIT_ATTR_IS_TRUE(values[1]));
233-
cl_assert(GIT_ATTR_IS_FALSE(values[2]));
234-
cl_assert(GIT_ATTR_IS_FALSE(values[3]));
235-
cl_assert(GIT_ATTR_IS_UNSPECIFIED(values[4]));
236-
237-
cl_git_pass(git_attr_get_many(values, g_repo, 0, "macro_test", 5, names2));
238-
239-
cl_assert(GIT_ATTR_IS_TRUE(values[0]));
240-
cl_assert(GIT_ATTR_IS_TRUE(values[1]));
241-
cl_assert(GIT_ATTR_IS_FALSE(values[2]));
242-
cl_assert(GIT_ATTR_IS_UNSPECIFIED(values[3]));
243-
cl_assert_equal_s("77", values[4]);
244-
245-
cl_git_pass(git_attr_get_many(values, g_repo, 0, "macro_test", 3, names3));
246-
247-
cl_assert(GIT_ATTR_IS_TRUE(values[0]));
248-
cl_assert(GIT_ATTR_IS_FALSE(values[1]));
249-
cl_assert_equal_s("answer", values[2]);
250-
}
251-
252-
void test_attr_repo__bad_macros(void)
253-
{
254-
const char *names[6] = { "rootattr", "positive", "negative",
255-
"firstmacro", "secondmacro", "thirdmacro" };
256-
const char *values[6];
257-
258-
cl_git_pass(git_attr_get_many(values, g_repo, 0, "macro_bad", 6, names));
259-
260-
/* these three just confirm that the "mymacro" rule ran */
261-
cl_assert(GIT_ATTR_IS_UNSPECIFIED(values[0]));
262-
cl_assert(GIT_ATTR_IS_TRUE(values[1]));
263-
cl_assert(GIT_ATTR_IS_FALSE(values[2]));
264-
265-
/* file contains:
266-
* # let's try some malicious macro defs
267-
* [attr]firstmacro -thirdmacro -secondmacro
268-
* [attr]secondmacro firstmacro -firstmacro
269-
* [attr]thirdmacro secondmacro=hahaha -firstmacro
270-
* macro_bad firstmacro secondmacro thirdmacro
271-
*
272-
* firstmacro assignment list ends up with:
273-
* -thirdmacro -secondmacro
274-
* secondmacro assignment list expands "firstmacro" and ends up with:
275-
* -thirdmacro -secondmacro -firstmacro
276-
* thirdmacro assignment don't expand so list ends up with:
277-
* secondmacro="hahaha"
278-
*
279-
* macro_bad assignment list ends up with:
280-
* -thirdmacro -secondmacro firstmacro &&
281-
* -thirdmacro -secondmacro -firstmacro secondmacro &&
282-
* secondmacro="hahaha" thirdmacro
283-
*
284-
* so summary results should be:
285-
* -firstmacro secondmacro="hahaha" thirdmacro
286-
*/
287-
cl_assert(GIT_ATTR_IS_FALSE(values[3]));
288-
cl_assert_equal_s("hahaha", values[4]);
289-
cl_assert(GIT_ATTR_IS_TRUE(values[5]));
290-
}
291-
292222
#define CONTENT "I'm going to be dynamically processed\r\n" \
293223
"And my line endings...\r\n" \
294224
"...are going to be\n" \

0 commit comments

Comments
 (0)