file.c 7.51 KB
Newer Older
1
#include "clar_libgit2.h"
2
#include "attr_file.h"
3
#include "attr_expect.h"
4 5 6 7 8 9

#define get_rule(X) ((git_attr_rule *)git_vector_get(&file->rules,(X)))
#define get_assign(R,Y) ((git_attr_assignment *)git_vector_get(&(R)->assigns,(Y)))

void test_attr_file__simple_read(void)
{
10
	git_attr_file *file;
11 12
	git_attr_assignment *assign;
	git_attr_rule *rule;
13

14
	cl_git_pass(git_attr_file__load_standalone(&file, cl_fixture("attr/attr0")));
15

16
	cl_assert_equal_s(cl_fixture("attr/attr0"), file->entry->path);
17 18
	cl_assert(file->rules.length == 1);

19
	rule = get_rule(0);
20
	cl_assert(rule != NULL);
21
	cl_assert_equal_s("*", rule->match.pattern);
22
	cl_assert(rule->match.length == 1);
23
	cl_assert((rule->match.flags & GIT_ATTR_FNMATCH_HASWILD) != 0);
24 25

	cl_assert(rule->assigns.length == 1);
26
	assign = get_assign(rule, 0);
27
	cl_assert(assign != NULL);
28
	cl_assert_equal_s("binary", assign->name);
29
	cl_assert(GIT_ATTR_TRUE(assign->value));
30 31 32 33 34 35

	git_attr_file__free(file);
}

void test_attr_file__match_variants(void)
{
36
	git_attr_file *file;
37 38 39
	git_attr_rule *rule;
	git_attr_assignment *assign;

40
	cl_git_pass(git_attr_file__load_standalone(&file, cl_fixture("attr/attr1")));
41

42
	cl_assert_equal_s(cl_fixture("attr/attr1"), file->entry->path);
43 44 45 46 47 48 49
	cl_assert(file->rules.length == 10);

	/* let's do a thorough check of this rule, then just verify
	 * the things that are unique for the later rules
	 */
	rule = get_rule(0);
	cl_assert(rule);
50
	cl_assert_equal_s("pat0", rule->match.pattern);
51 52 53
	cl_assert(rule->match.length == strlen("pat0"));
	cl_assert(rule->assigns.length == 1);
	assign = get_assign(rule,0);
54
	cl_assert_equal_s("attr0", assign->name);
55
	cl_assert(assign->name_hash == git_attr_file__name_hash(assign->name));
56
	cl_assert(GIT_ATTR_TRUE(assign->value));
57 58

	rule = get_rule(1);
59
	cl_assert_equal_s("pat1", rule->match.pattern);
60
	cl_assert(rule->match.length == strlen("pat1"));
61
	cl_assert((rule->match.flags & GIT_ATTR_FNMATCH_NEGATIVE) != 0);
62 63

	rule = get_rule(2);
64
	cl_assert_equal_s("pat2", rule->match.pattern);
65
	cl_assert(rule->match.length == strlen("pat2"));
66
	cl_assert((rule->match.flags & GIT_ATTR_FNMATCH_DIRECTORY) != 0);
67 68

	rule = get_rule(3);
69
	cl_assert_equal_s("pat3dir/pat3file", rule->match.pattern);
70
	cl_assert((rule->match.flags & GIT_ATTR_FNMATCH_FULLPATH) != 0);
71 72

	rule = get_rule(4);
73
	cl_assert_equal_s("pat4.*", rule->match.pattern);
74
	cl_assert((rule->match.flags & GIT_ATTR_FNMATCH_HASWILD) != 0);
75 76

	rule = get_rule(5);
77
	cl_assert_equal_s("*.pat5", rule->match.pattern);
78
	cl_assert((rule->match.flags & GIT_ATTR_FNMATCH_HASWILD) != 0);
79 80

	rule = get_rule(7);
81
	cl_assert_equal_s("pat7[a-e]??[xyz]", rule->match.pattern);
82
	cl_assert(rule->assigns.length == 1);
83
	cl_assert((rule->match.flags & GIT_ATTR_FNMATCH_HASWILD) != 0);
84
	assign = get_assign(rule,0);
85
	cl_assert_equal_s("attr7", assign->name);
86
	cl_assert(GIT_ATTR_TRUE(assign->value));
87 88

	rule = get_rule(8);
89
	cl_assert_equal_s("pat8 with spaces", rule->match.pattern);
90 91 92
	cl_assert(rule->match.length == strlen("pat8 with spaces"));

	rule = get_rule(9);
93
	cl_assert_equal_s("pat9", rule->match.pattern);
94 95 96 97 98 99 100 101 102 103

	git_attr_file__free(file);
}

static void check_one_assign(
	git_attr_file *file,
	int rule_idx,
	int assign_idx,
	const char *pattern,
	const char *name,
104 105
	enum attr_expect_t expected,
	const char *expected_str)
106 107 108 109
{
	git_attr_rule *rule = get_rule(rule_idx);
	git_attr_assignment *assign = get_assign(rule, assign_idx);

110
	cl_assert_equal_s(pattern, rule->match.pattern);
111
	cl_assert(rule->assigns.length == 1);
112
	cl_assert_equal_s(name, assign->name);
113
	cl_assert(assign->name_hash == git_attr_file__name_hash(assign->name));
114

115
	attr_check_expected(expected, expected_str, assign->name, assign->value);
116 117 118 119
}

void test_attr_file__assign_variants(void)
{
120
	git_attr_file *file;
121 122 123
	git_attr_rule *rule;
	git_attr_assignment *assign;

124
	cl_git_pass(git_attr_file__load_standalone(&file, cl_fixture("attr/attr2")));
125

126
	cl_assert_equal_s(cl_fixture("attr/attr2"), file->entry->path);
127 128
	cl_assert(file->rules.length == 11);

129 130 131 132 133 134 135 136
	check_one_assign(file, 0, 0, "pat0", "simple", EXPECT_TRUE, NULL);
	check_one_assign(file, 1, 0, "pat1", "neg", EXPECT_FALSE, NULL);
	check_one_assign(file, 2, 0, "*", "notundef", EXPECT_TRUE, NULL);
	check_one_assign(file, 3, 0, "pat2", "notundef", EXPECT_UNDEFINED, NULL);
	check_one_assign(file, 4, 0, "pat3", "assigned", EXPECT_STRING, "test-value");
	check_one_assign(file, 5, 0, "pat4", "rule-with-more-chars", EXPECT_STRING, "value-with-more-chars");
	check_one_assign(file, 6, 0, "pat5", "empty", EXPECT_TRUE, NULL);
	check_one_assign(file, 7, 0, "pat6", "negempty", EXPECT_FALSE, NULL);
137 138

	rule = get_rule(8);
139
	cl_assert_equal_s("pat7", rule->match.pattern);
140 141 142 143 144 145
	cl_assert(rule->assigns.length == 5);
	/* assignments will be sorted by hash value, so we have to do
	 * lookups by search instead of by position
	 */
	assign = git_attr_rule__lookup_assignment(rule, "multiple");
	cl_assert(assign);
146
	cl_assert_equal_s("multiple", assign->name);
147
	cl_assert(GIT_ATTR_TRUE(assign->value));
148 149
	assign = git_attr_rule__lookup_assignment(rule, "single");
	cl_assert(assign);
150
	cl_assert_equal_s("single", assign->name);
151
	cl_assert(GIT_ATTR_FALSE(assign->value));
152 153
	assign = git_attr_rule__lookup_assignment(rule, "values");
	cl_assert(assign);
154 155
	cl_assert_equal_s("values", assign->name);
	cl_assert_equal_s("1", assign->value);
156 157
	assign = git_attr_rule__lookup_assignment(rule, "also");
	cl_assert(assign);
158 159
	cl_assert_equal_s("also", assign->name);
	cl_assert_equal_s("a-really-long-value/*", assign->value);
160 161
	assign = git_attr_rule__lookup_assignment(rule, "happy");
	cl_assert(assign);
162 163
	cl_assert_equal_s("happy", assign->name);
	cl_assert_equal_s("yes!", assign->value);
164 165 166 167
	assign = git_attr_rule__lookup_assignment(rule, "other");
	cl_assert(!assign);

	rule = get_rule(9);
168
	cl_assert_equal_s("pat8", rule->match.pattern);
169 170 171
	cl_assert(rule->assigns.length == 2);
	assign = git_attr_rule__lookup_assignment(rule, "again");
	cl_assert(assign);
172
	cl_assert_equal_s("again", assign->name);
173
	cl_assert(GIT_ATTR_TRUE(assign->value));
174 175
	assign = git_attr_rule__lookup_assignment(rule, "another");
	cl_assert(assign);
176 177
	cl_assert_equal_s("another", assign->name);
	cl_assert_equal_s("12321", assign->value);
178

179
	check_one_assign(file, 10, 0, "pat9", "at-eof", EXPECT_FALSE, NULL);
180 181 182 183 184 185

	git_attr_file__free(file);
}

void test_attr_file__check_attr_examples(void)
{
186
	git_attr_file *file;
187 188 189
	git_attr_rule *rule;
	git_attr_assignment *assign;

190
	cl_git_pass(git_attr_file__load_standalone(&file, cl_fixture("attr/attr3")));
191
	cl_assert_equal_s(cl_fixture("attr/attr3"), file->entry->path);
192 193 194
	cl_assert(file->rules.length == 3);

	rule = get_rule(0);
195
	cl_assert_equal_s("*.java", rule->match.pattern);
196 197
	cl_assert(rule->assigns.length == 3);
	assign = git_attr_rule__lookup_assignment(rule, "diff");
198 199
	cl_assert_equal_s("diff", assign->name);
	cl_assert_equal_s("java", assign->value);
200
	assign = git_attr_rule__lookup_assignment(rule, "crlf");
201
	cl_assert_equal_s("crlf", assign->name);
202
	cl_assert(GIT_ATTR_FALSE(assign->value));
203
	assign = git_attr_rule__lookup_assignment(rule, "myAttr");
204
	cl_assert_equal_s("myAttr", assign->name);
205
	cl_assert(GIT_ATTR_TRUE(assign->value));
206 207 208 209
	assign = git_attr_rule__lookup_assignment(rule, "missing");
	cl_assert(assign == NULL);

	rule = get_rule(1);
210
	cl_assert_equal_s("NoMyAttr.java", rule->match.pattern);
211 212
	cl_assert(rule->assigns.length == 1);
	assign = get_assign(rule, 0);
213
	cl_assert_equal_s("myAttr", assign->name);
214
	cl_assert(GIT_ATTR_UNSPECIFIED(assign->value));
215 216

	rule = get_rule(2);
217
	cl_assert_equal_s("README", rule->match.pattern);
218 219
	cl_assert(rule->assigns.length == 1);
	assign = get_assign(rule, 0);
220 221
	cl_assert_equal_s("caveat", assign->name);
	cl_assert_equal_s("unspecified", assign->value);
222 223 224

	git_attr_file__free(file);
}