lookup.c 9.14 KB
Newer Older
1
#include "clar_libgit2.h"
2
#include "attr_file.h"
3

4 5
#include "attr_expect.h"

6 7
void test_attr_lookup__simple(void)
{
8
	git_attr_file *file;
9 10 11
	git_attr_path path;
	const char *value = NULL;

12 13
	cl_git_pass(git_attr_file__new_and_load(&file, cl_fixture("attr/attr0")));
	cl_assert_equal_s(cl_fixture("attr/attr0"), file->key + 2);
14 15
	cl_assert(file->rules.length == 1);

16
	cl_git_pass(git_attr_path__init(&path, "test", NULL));
17 18
	cl_assert_equal_s("test", path.path);
	cl_assert_equal_s("test", path.basename);
19 20 21
	cl_assert(!path.is_dir);

	cl_git_pass(git_attr_file__lookup_one(file,&path,"binary",&value));
22
	cl_assert(GIT_ATTR_TRUE(value));
23 24 25 26

	cl_git_pass(git_attr_file__lookup_one(file,&path,"missing",&value));
	cl_assert(!value);

27
	git_attr_path__free(&path);
28 29 30
	git_attr_file__free(file);
}

31
static void run_test_cases(git_attr_file *file, struct attr_expected *cases, int force_dir)
32 33 34
{
	git_attr_path path;
	const char *value = NULL;
35
	struct attr_expected *c;
36 37 38
	int error;

	for (c = cases; c->path != NULL; c++) {
39
		cl_git_pass(git_attr_path__init(&path, c->path, NULL));
40

41
		if (force_dir)
42 43 44 45 46
			path.is_dir = 1;

		error = git_attr_file__lookup_one(file,&path,c->attr,&value);
		cl_git_pass(error);

47
		attr_check_expected(c->expected, c->expected_str, c->attr, value);
48 49

		git_attr_path__free(&path);
50 51 52 53 54
	}
}

void test_attr_lookup__match_variants(void)
{
55
	git_attr_file *file;
56
	git_attr_path path;
57 58 59 60 61 62 63 64 65 66 67

	struct attr_expected dir_cases[] = {
		{ "pat2", "attr2", EXPECT_TRUE, NULL },
		{ "/testing/for/pat2", "attr2", EXPECT_TRUE, NULL },
		{ "/not/pat2/yousee", "attr2", EXPECT_UNDEFINED, NULL },
		{ "/fun/fun/fun/pat4.dir", "attr4", EXPECT_TRUE, NULL },
		{ "foo.pat5", "attr5", EXPECT_TRUE, NULL },
		{ NULL, NULL, 0, NULL }
	};

	struct attr_expected cases[] = {
68
		/* pat0 -> simple match */
69 70 71 72 73 74
		{ "pat0", "attr0", EXPECT_TRUE, NULL },
		{ "/testing/for/pat0", "attr0", EXPECT_TRUE, NULL },
		{ "relative/to/pat0", "attr0", EXPECT_TRUE, NULL },
		{ "this-contains-pat0-inside", "attr0", EXPECT_UNDEFINED, NULL },
		{ "this-aint-right", "attr0", EXPECT_UNDEFINED, NULL },
		{ "/this/pat0/dont/match", "attr0", EXPECT_UNDEFINED, NULL },
75
		/* negative match */
76 77 78 79 80 81
		{ "pat0", "attr1", EXPECT_TRUE, NULL },
		{ "pat1", "attr1", EXPECT_UNDEFINED, NULL },
		{ "/testing/for/pat1", "attr1", EXPECT_UNDEFINED, NULL },
		{ "/testing/for/pat0", "attr1", EXPECT_TRUE, NULL },
		{ "/testing/for/pat1/inside", "attr1", EXPECT_TRUE, NULL },
		{ "misc", "attr1", EXPECT_TRUE, NULL },
82
		/* dir match */
83 84 85
		{ "pat2", "attr2", EXPECT_UNDEFINED, NULL },
		{ "/testing/for/pat2", "attr2", EXPECT_UNDEFINED, NULL },
		{ "/not/pat2/yousee", "attr2", EXPECT_UNDEFINED, NULL },
86
		/* path match */
87
		{ "pat3file", "attr3", EXPECT_UNDEFINED, NULL },
88
		{ "/pat3dir/pat3file", "attr3", EXPECT_TRUE, NULL },
89
		{ "pat3dir/pat3file", "attr3", EXPECT_TRUE, NULL },
90
		/* pattern* match */
91 92 93 94
		{ "pat4.txt", "attr4", EXPECT_TRUE, NULL },
		{ "/fun/fun/fun/pat4.c", "attr4", EXPECT_TRUE, NULL },
		{ "pat4.", "attr4", EXPECT_TRUE, NULL },
		{ "pat4", "attr4", EXPECT_UNDEFINED, NULL },
95
		/* *pattern match */
96 97 98 99
		{ "foo.pat5", "attr5", EXPECT_TRUE, NULL },
		{ "/this/is/ok.pat5", "attr5", EXPECT_TRUE, NULL },
		{ "/this/is/bad.pat5/yousee.txt", "attr5", EXPECT_UNDEFINED, NULL },
		{ "foo.pat5", "attr100", EXPECT_UNDEFINED, NULL },
100
		/* glob match with slashes */
101 102 103 104 105
		{ "foo.pat6", "attr6", EXPECT_UNDEFINED, NULL },
		{ "pat6/pat6/foobar.pat6", "attr6", EXPECT_TRUE, NULL },
		{ "pat6/pat6/.pat6", "attr6", EXPECT_TRUE, NULL },
		{ "pat6/pat6/extra/foobar.pat6", "attr6", EXPECT_UNDEFINED, NULL },
		{ "/prefix/pat6/pat6/foobar.pat6", "attr6", EXPECT_UNDEFINED, NULL },
106
		{ "/pat6/pat6/foobar.pat6", "attr6", EXPECT_TRUE, NULL },
107
		/* complex pattern */
108 109 110 111 112 113 114 115 116
		{ "pat7a12z", "attr7", EXPECT_TRUE, NULL },
		{ "pat7e__x", "attr7", EXPECT_TRUE, NULL },
		{ "pat7b/1y", "attr7", EXPECT_UNDEFINED, NULL }, /* ? does not match / */
		{ "pat7e_x", "attr7", EXPECT_UNDEFINED, NULL },
		{ "pat7aaaa", "attr7", EXPECT_UNDEFINED, NULL },
		{ "pat7zzzz", "attr7", EXPECT_UNDEFINED, NULL },
		{ "/this/can/be/anything/pat7a12z", "attr7", EXPECT_TRUE, NULL },
		{ "but/it/still/must/match/pat7aaaa", "attr7", EXPECT_UNDEFINED, NULL },
		{ "pat7aaay.fail", "attr7", EXPECT_UNDEFINED, NULL },
117
		/* pattern with spaces */
118 119 120 121
		{ "pat8 with spaces", "attr8", EXPECT_TRUE, NULL },
		{ "/gotta love/pat8 with spaces", "attr8", EXPECT_TRUE, NULL },
		{ "failing pat8 with spaces", "attr8", EXPECT_UNDEFINED, NULL },
		{ "spaces", "attr8", EXPECT_UNDEFINED, NULL },
122
		/* pattern at eof */
123 124 125 126 127
		{ "pat9", "attr9", EXPECT_TRUE, NULL },
		{ "/eof/pat9", "attr9", EXPECT_TRUE, NULL },
		{ "pat", "attr9", EXPECT_UNDEFINED, NULL },
		{ "at9", "attr9", EXPECT_UNDEFINED, NULL },
		{ "pat9.fail", "attr9", EXPECT_UNDEFINED, NULL },
128
		/* sentinel at end */
129
		{ NULL, NULL, 0, NULL }
130 131
	};

132 133
	cl_git_pass(git_attr_file__new_and_load(&file, cl_fixture("attr/attr1")));
	cl_assert_equal_s(cl_fixture("attr/attr1"), file->key + 2);
134 135
	cl_assert(file->rules.length == 10);

136
	cl_git_pass(git_attr_path__init(&path, "/testing/for/pat0", NULL));
137
	cl_assert_equal_s("pat0", path.basename);
138

139 140
	run_test_cases(file, cases, 0);
	run_test_cases(file, dir_cases, 1);
141 142

	git_attr_file__free(file);
143
	git_attr_path__free(&path);
144 145 146 147
}

void test_attr_lookup__assign_variants(void)
{
148
	git_attr_file *file;
149

150
	struct attr_expected cases[] = {
151
		/* pat0 -> simple assign */
152 153 154 155
		{ "pat0", "simple", EXPECT_TRUE, NULL },
		{ "/testing/pat0", "simple", EXPECT_TRUE, NULL },
		{ "pat0", "fail", EXPECT_UNDEFINED, NULL },
		{ "/testing/pat0", "fail", EXPECT_UNDEFINED, NULL },
156
		/* negative assign */
157 158 159 160
		{ "pat1", "neg", EXPECT_FALSE, NULL },
		{ "/testing/pat1", "neg", EXPECT_FALSE, NULL },
		{ "pat1", "fail", EXPECT_UNDEFINED, NULL },
		{ "/testing/pat1", "fail", EXPECT_UNDEFINED, NULL },
161
		/* forced undef */
162 163 164 165
		{ "pat1", "notundef", EXPECT_TRUE, NULL },
		{ "pat2", "notundef", EXPECT_UNDEFINED, NULL },
		{ "/lead/in/pat1", "notundef", EXPECT_TRUE, NULL },
		{ "/lead/in/pat2", "notundef", EXPECT_UNDEFINED, NULL },
166
		/* assign value */
167 168
		{ "pat3", "assigned", EXPECT_STRING, "test-value" },
		{ "pat3", "notassigned", EXPECT_UNDEFINED, NULL },
169
		/* assign value */
170 171
		{ "pat4", "rule-with-more-chars", EXPECT_STRING, "value-with-more-chars" },
		{ "pat4", "notassigned-rule-with-more-chars", EXPECT_UNDEFINED, NULL },
172
		/* empty assignments */
173 174
		{ "pat5", "empty", EXPECT_TRUE, NULL },
		{ "pat6", "negempty", EXPECT_FALSE, NULL },
175
		/* multiple assignment */
176 177 178 179 180 181 182
		{ "pat7", "multiple", EXPECT_TRUE, NULL },
		{ "pat7", "single", EXPECT_FALSE, NULL },
		{ "pat7", "values", EXPECT_STRING, "1" },
		{ "pat7", "also", EXPECT_STRING, "a-really-long-value/*" },
		{ "pat7", "happy", EXPECT_STRING, "yes!" },
		{ "pat8", "again", EXPECT_TRUE, NULL },
		{ "pat8", "another", EXPECT_STRING, "12321" },
183
		/* bad assignment */
184 185 186
		{ "patbad0", "simple", EXPECT_UNDEFINED, NULL },
		{ "patbad0", "notundef", EXPECT_TRUE, NULL },
		{ "patbad1", "simple", EXPECT_UNDEFINED, NULL },
187
		/* eof assignment */
188
		{ "pat9", "at-eof", EXPECT_FALSE, NULL },
189
		/* sentinel at end */
190
		{ NULL, NULL, 0, NULL }
191 192
	};

193
	cl_git_pass(git_attr_file__new_and_load(&file, cl_fixture("attr/attr2")));
194 195
	cl_assert(file->rules.length == 11);

196
	run_test_cases(file, cases, 0);
197 198 199 200 201 202

	git_attr_file__free(file);
}

void test_attr_lookup__check_attr_examples(void)
{
203
	git_attr_file *file;
204

205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
	struct attr_expected cases[] = {
		{ "foo.java", "diff", EXPECT_STRING, "java" },
		{ "foo.java", "crlf", EXPECT_FALSE, NULL },
		{ "foo.java", "myAttr", EXPECT_TRUE, NULL },
		{ "foo.java", "other", EXPECT_UNDEFINED, NULL },
		{ "/prefix/dir/foo.java", "diff", EXPECT_STRING, "java" },
		{ "/prefix/dir/foo.java", "crlf", EXPECT_FALSE, NULL },
		{ "/prefix/dir/foo.java", "myAttr", EXPECT_TRUE, NULL },
		{ "/prefix/dir/foo.java", "other", EXPECT_UNDEFINED, NULL },
		{ "NoMyAttr.java", "crlf", EXPECT_FALSE, NULL },
		{ "NoMyAttr.java", "myAttr", EXPECT_UNDEFINED, NULL },
		{ "NoMyAttr.java", "other", EXPECT_UNDEFINED, NULL },
		{ "/prefix/dir/NoMyAttr.java", "crlf", EXPECT_FALSE, NULL },
		{ "/prefix/dir/NoMyAttr.java", "myAttr", EXPECT_UNDEFINED, NULL },
		{ "/prefix/dir/NoMyAttr.java", "other", EXPECT_UNDEFINED, NULL },
		{ "README", "caveat", EXPECT_STRING, "unspecified" },
		{ "/specific/path/README", "caveat", EXPECT_STRING, "unspecified" },
		{ "README", "missing", EXPECT_UNDEFINED, NULL },
		{ "/specific/path/README", "missing", EXPECT_UNDEFINED, NULL },
224
		/* sentinel at end */
225
		{ NULL, NULL, 0, NULL }
226 227
	};

228
	cl_git_pass(git_attr_file__new_and_load(&file, cl_fixture("attr/attr3")));
229 230
	cl_assert(file->rules.length == 3);

231
	run_test_cases(file, cases, 0);
232 233 234 235 236 237

	git_attr_file__free(file);
}

void test_attr_lookup__from_buffer(void)
{
238
	git_attr_file *file;
239

240 241 242 243 244 245 246 247 248 249 250
	struct attr_expected cases[] = {
		{ "abc", "foo", EXPECT_TRUE, NULL },
		{ "abc", "bar", EXPECT_TRUE, NULL },
		{ "abc", "baz", EXPECT_TRUE, NULL },
		{ "aaa", "foo", EXPECT_TRUE, NULL },
		{ "aaa", "bar", EXPECT_UNDEFINED, NULL },
		{ "aaa", "baz", EXPECT_TRUE, NULL },
		{ "qqq", "foo", EXPECT_UNDEFINED, NULL },
		{ "qqq", "bar", EXPECT_UNDEFINED, NULL },
		{ "qqq", "baz", EXPECT_TRUE, NULL },
		{ NULL, NULL, 0, NULL }
251 252
	};

253 254
	cl_git_pass(git_attr_file__new(&file, 0, NULL, NULL));

255
	cl_git_pass(git_attr_file__parse_buffer(NULL, NULL, "a* foo\nabc bar\n* baz", file));
256

257 258
	cl_assert(file->rules.length == 3);

259
	run_test_cases(file, cases, 0);
260 261 262

	git_attr_file__free(file);
}