repo.c 7.59 KB
Newer Older
1
#include "clar_libgit2.h"
2 3
#include "fileops.h"
#include "git2/attr.h"
4
#include "attr.h"
5

6 7
#include "attr_expect.h"

8 9 10 11
static git_repository *g_repo = NULL;

void test_attr_repo__initialize(void)
{
12
	/* Before each test, instantiate the attr repo from the fixtures and
13
	 * rename the .gitted to .git so it is a repo with a working dir.
14 15
	 * Also rename gitattributes to .gitattributes, because it contains
	 * macro definitions which are only allowed in the root.
16 17 18
	 */
	cl_fixture_sandbox("attr");
	cl_git_pass(p_rename("attr/.gitted", "attr/.git"));
19
	cl_git_pass(p_rename("attr/gitattributes", "attr/.gitattributes"));
20 21 22 23 24 25 26 27 28 29 30 31
	cl_git_pass(git_repository_open(&g_repo, "attr/.git"));
}

void test_attr_repo__cleanup(void)
{
	git_repository_free(g_repo);
	g_repo = NULL;
	cl_fixture_cleanup("attr");
}

void test_attr_repo__get_one(void)
{
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
	struct attr_expected test_cases[] = {
		{ "root_test1", "repoattr", EXPECT_TRUE, NULL },
		{ "root_test1", "rootattr", EXPECT_TRUE, NULL },
		{ "root_test1", "missingattr", EXPECT_UNDEFINED, NULL },
		{ "root_test1", "subattr", EXPECT_UNDEFINED, NULL },
		{ "root_test1", "negattr", EXPECT_UNDEFINED, NULL },
		{ "root_test2", "repoattr", EXPECT_TRUE, NULL },
		{ "root_test2", "rootattr", EXPECT_FALSE, NULL },
		{ "root_test2", "missingattr", EXPECT_UNDEFINED, NULL },
		{ "root_test2", "multiattr", EXPECT_FALSE, NULL },
		{ "root_test3", "repoattr", EXPECT_TRUE, NULL },
		{ "root_test3", "rootattr", EXPECT_UNDEFINED, NULL },
		{ "root_test3", "multiattr", EXPECT_STRING, "3" },
		{ "root_test3", "multi2", EXPECT_UNDEFINED, NULL },
		{ "sub/subdir_test1", "repoattr", EXPECT_TRUE, NULL },
		{ "sub/subdir_test1", "rootattr", EXPECT_TRUE, NULL },
		{ "sub/subdir_test1", "missingattr", EXPECT_UNDEFINED, NULL },
		{ "sub/subdir_test1", "subattr", EXPECT_STRING, "yes" },
		{ "sub/subdir_test1", "negattr", EXPECT_FALSE, NULL },
		{ "sub/subdir_test1", "another", EXPECT_UNDEFINED, NULL },
		{ "sub/subdir_test2.txt", "repoattr", EXPECT_TRUE, NULL },
		{ "sub/subdir_test2.txt", "rootattr", EXPECT_TRUE, NULL },
		{ "sub/subdir_test2.txt", "missingattr", EXPECT_UNDEFINED, NULL },
		{ "sub/subdir_test2.txt", "subattr", EXPECT_STRING, "yes" },
		{ "sub/subdir_test2.txt", "negattr", EXPECT_FALSE, NULL },
		{ "sub/subdir_test2.txt", "another", EXPECT_STRING, "zero" },
		{ "sub/subdir_test2.txt", "reposub", EXPECT_TRUE, NULL },
		{ "sub/sub/subdir.txt", "another", EXPECT_STRING, "one" },
		{ "sub/sub/subdir.txt", "reposubsub", EXPECT_TRUE, NULL },
		{ "sub/sub/subdir.txt", "reposub", EXPECT_UNDEFINED, NULL },
		{ "does-not-exist", "foo", EXPECT_STRING, "yes" },
		{ "sub/deep/file", "deepdeep", EXPECT_TRUE, NULL },
64 65
		{ "sub/sub/d/no", "test", EXPECT_STRING, "a/b/d/*" },
		{ "sub/sub/d/yes", "test", EXPECT_UNDEFINED, NULL },
66 67
		{ NULL, NULL, 0, NULL }
	}, *scan;
68

69
	for (scan = test_cases; scan->path != NULL; scan++) {
70
		const char *value;
71 72
		cl_git_pass(git_attr_get(g_repo, scan->path, scan->attr, &value));
		attr_check_expected(scan->expected, scan->expected_str, value);
73
	}
74

75 76 77
	cl_assert(git_attr_cache__is_cached(g_repo, ".git/info/attributes"));
	cl_assert(git_attr_cache__is_cached(g_repo, ".gitattributes"));
	cl_assert(git_attr_cache__is_cached(g_repo, "sub/.gitattributes"));
78 79 80 81 82 83 84 85 86
}

void test_attr_repo__get_many(void)
{
	const char *names[4] = { "repoattr", "rootattr", "missingattr", "subattr" };
	const char *values[4];

	cl_git_pass(git_attr_get_many(g_repo, "root_test1", 4, names, values));

87 88 89 90
	cl_assert(GIT_ATTR_TRUE(values[0]));
	cl_assert(GIT_ATTR_TRUE(values[1]));
	cl_assert(GIT_ATTR_UNSPECIFIED(values[2]));
	cl_assert(GIT_ATTR_UNSPECIFIED(values[3]));
91 92 93

	cl_git_pass(git_attr_get_many(g_repo, "root_test2", 4, names, values));

94 95 96 97
	cl_assert(GIT_ATTR_TRUE(values[0]));
	cl_assert(GIT_ATTR_FALSE(values[1]));
	cl_assert(GIT_ATTR_UNSPECIFIED(values[2]));
	cl_assert(GIT_ATTR_UNSPECIFIED(values[3]));
98

99
	cl_git_pass(git_attr_get_many(g_repo, "sub/subdir_test1", 4, names, values));
100

101 102 103
	cl_assert(GIT_ATTR_TRUE(values[0]));
	cl_assert(GIT_ATTR_TRUE(values[1]));
	cl_assert(GIT_ATTR_UNSPECIFIED(values[2]));
104 105 106 107
	cl_assert_strequal("yes", values[3]);
}

static int count_attrs(
108 109
	const char *name,
	const char *value,
110 111
	void *payload)
{
112 113
	GIT_UNUSED(name);
	GIT_UNUSED(value);
114 115 116

	*((int *)payload) += 1;

117
	return 0;
118 119 120 121 122 123 124 125 126 127 128
}

void test_attr_repo__foreach(void)
{
	int count;

	count = 0;
	cl_git_pass(git_attr_foreach(g_repo, "root_test1", &count_attrs, &count));
	cl_assert(count == 2);

	count = 0;
129
	cl_git_pass(git_attr_foreach(g_repo, "sub/subdir_test1",
130 131 132 133
		&count_attrs, &count));
	cl_assert(count == 4); /* repoattr, rootattr, subattr, negattr */

	count = 0;
134
	cl_git_pass(git_attr_foreach(g_repo, "sub/subdir_test2.txt",
135
		&count_attrs, &count));
136
	cl_assert(count == 6); /* repoattr, rootattr, subattr, reposub, negattr, another */
137
}
138 139 140 141 142

void test_attr_repo__manpage_example(void)
{
	const char *value;

143
	cl_git_pass(git_attr_get(g_repo, "sub/abc", "foo", &value));
144
	cl_assert(GIT_ATTR_TRUE(value));
145

146
	cl_git_pass(git_attr_get(g_repo, "sub/abc", "bar", &value));
147
	cl_assert(GIT_ATTR_UNSPECIFIED(value));
148

149
	cl_git_pass(git_attr_get(g_repo, "sub/abc", "baz", &value));
150
	cl_assert(GIT_ATTR_FALSE(value));
151

152
	cl_git_pass(git_attr_get(g_repo, "sub/abc", "merge", &value));
153 154
	cl_assert_strequal("filfre", value);

155
	cl_git_pass(git_attr_get(g_repo, "sub/abc", "frotz", &value));
156
	cl_assert(GIT_ATTR_UNSPECIFIED(value));
157 158 159 160 161 162
}

void test_attr_repo__macros(void)
{
	const char *names[5] = { "rootattr", "binary", "diff", "crlf", "frotz" };
	const char *names2[5] = { "mymacro", "positive", "negative", "rootattr", "another" };
163
	const char *names3[3] = { "macro2", "multi2", "multi3" };
164 165 166 167
	const char *values[5];

	cl_git_pass(git_attr_get_many(g_repo, "binfile", 5, names, values));

168 169 170 171 172
	cl_assert(GIT_ATTR_TRUE(values[0]));
	cl_assert(GIT_ATTR_TRUE(values[1]));
	cl_assert(GIT_ATTR_FALSE(values[2]));
	cl_assert(GIT_ATTR_FALSE(values[3]));
	cl_assert(GIT_ATTR_UNSPECIFIED(values[4]));
173 174 175

	cl_git_pass(git_attr_get_many(g_repo, "macro_test", 5, names2, values));

176 177 178 179
	cl_assert(GIT_ATTR_TRUE(values[0]));
	cl_assert(GIT_ATTR_TRUE(values[1]));
	cl_assert(GIT_ATTR_FALSE(values[2]));
	cl_assert(GIT_ATTR_UNSPECIFIED(values[3]));
180
	cl_assert_strequal("77", values[4]);
181 182 183

	cl_git_pass(git_attr_get_many(g_repo, "macro_test", 3, names3, values));

184 185
	cl_assert(GIT_ATTR_TRUE(values[0]));
	cl_assert(GIT_ATTR_FALSE(values[1]));
186 187 188 189 190 191 192 193 194 195 196 197
	cl_assert_strequal("answer", values[2]);
}

void test_attr_repo__bad_macros(void)
{
	const char *names[6] = { "rootattr", "positive", "negative",
		"firstmacro", "secondmacro", "thirdmacro" };
	const char *values[6];

	cl_git_pass(git_attr_get_many(g_repo, "macro_bad", 6, names, values));

	/* these three just confirm that the "mymacro" rule ran */
198 199 200
	cl_assert(GIT_ATTR_UNSPECIFIED(values[0]));
	cl_assert(GIT_ATTR_TRUE(values[1]));
	cl_assert(GIT_ATTR_FALSE(values[2]));
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223

	/* file contains:
	 *     # let's try some malicious macro defs
	 *     [attr]firstmacro -thirdmacro -secondmacro
	 *     [attr]secondmacro firstmacro -firstmacro
	 *     [attr]thirdmacro secondmacro=hahaha -firstmacro
	 *     macro_bad firstmacro secondmacro thirdmacro
	 *
	 * firstmacro assignment list ends up with:
	 *     -thirdmacro -secondmacro
	 * secondmacro assignment list expands "firstmacro" and ends up with:
	 *     -thirdmacro -secondmacro -firstmacro
	 * thirdmacro assignment don't expand so list ends up with:
	 *     secondmacro="hahaha"
	 *
	 * macro_bad assignment list ends up with:
	 *     -thirdmacro -secondmacro firstmacro &&
	 *     -thirdmacro -secondmacro -firstmacro secondmacro &&
	 *     secondmacro="hahaha" thirdmacro
	 *
	 * so summary results should be:
	 *     -firstmacro secondmacro="hahaha" thirdmacro
	 */
224
	cl_assert(GIT_ATTR_FALSE(values[3]));
225
	cl_assert_strequal("hahaha", values[4]);
226
	cl_assert(GIT_ATTR_TRUE(values[5]));
227
}
228