multivar.c 6.89 KB
Newer Older
1 2
#include "clar_libgit2.h"

3
static const char *_name = "remote.ab.url";
4 5 6 7 8 9 10 11 12 13 14

void test_config_multivar__initialize(void)
{
	cl_fixture_sandbox("config");
}

void test_config_multivar__cleanup(void)
{
	cl_fixture_cleanup("config");
}

15
static int mv_read_cb(const git_config_entry *entry, void *data)
16 17 18
{
	int *n = (int *) data;

19
	if (!strcmp(entry->name, _name))
20 21
		(*n)++;

22
	return 0;
23 24 25 26 27 28 29 30 31 32 33 34 35 36
}

void test_config_multivar__foreach(void)
{
	git_config *cfg;
	int n = 0;

	cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config11")));

	cl_git_pass(git_config_foreach(cfg, mv_read_cb, &n));
	cl_assert(n == 2);

	git_config_free(cfg);
}
37

38
static int cb(const git_config_entry *entry, void *data)
39 40 41
{
	int *n = (int *) data;

42
	GIT_UNUSED(entry);
43

44 45
	(*n)++;

46
	return 0;
47 48
}

49
static void check_get_multivar_foreach(
50 51 52 53 54
	git_config *cfg, int expected, int expected_patterned)
{
	int n = 0;

	if (expected > 0) {
55
		cl_git_pass(git_config_get_multivar_foreach(cfg, _name, NULL, cb, &n));
56 57 58
		cl_assert_equal_i(expected, n);
	} else {
		cl_assert_equal_i(GIT_ENOTFOUND,
59
			git_config_get_multivar_foreach(cfg, _name, NULL, cb, &n));
60 61 62 63 64
	}

	n = 0;

	if (expected_patterned > 0) {
65
		cl_git_pass(git_config_get_multivar_foreach(cfg, _name, "example", cb, &n));
66 67 68
		cl_assert_equal_i(expected_patterned, n);
	} else {
		cl_assert_equal_i(GIT_ENOTFOUND,
69
			git_config_get_multivar_foreach(cfg, _name, "example", cb, &n));
70 71 72
	}
}

73 74 75 76 77 78
static void check_get_multivar(git_config *cfg, int expected)
{
	git_config_iterator *iter;
	git_config_entry *entry;
	int n = 0;

79
	cl_git_pass(git_config_multivar_iterator_new(&iter, cfg, _name, NULL));
80 81 82 83 84 85 86 87 88

	while (git_config_next(&entry, iter) == 0)
		n++;

	cl_assert_equal_i(expected, n);
	git_config_iterator_free(iter);

}

89 90 91 92
void test_config_multivar__get(void)
{
	git_config *cfg;

93
	cl_git_pass(git_config_open_ondisk(&cfg, "config/config11"));
94
	check_get_multivar_foreach(cfg, 2, 1);
95

96
	/* add another that has the _name entry */
97
	cl_git_pass(git_config_add_file_ondisk(cfg, "config/config9", GIT_CONFIG_LEVEL_SYSTEM, NULL, 1));
98
	check_get_multivar_foreach(cfg, 3, 2);
99

100
	/* add another that does not have the _name entry */
101
	cl_git_pass(git_config_add_file_ondisk(cfg, "config/config0", GIT_CONFIG_LEVEL_GLOBAL, NULL, 1));
102
	check_get_multivar_foreach(cfg, 3, 2);
103 104

	/* add another that does not have the _name entry at the end */
105
	cl_git_pass(git_config_add_file_ondisk(cfg, "config/config1", GIT_CONFIG_LEVEL_APP, NULL, 1));
106
	check_get_multivar_foreach(cfg, 3, 2);
107 108

	/* drop original file */
109
	cl_git_pass(git_config_add_file_ondisk(cfg, "config/config2", GIT_CONFIG_LEVEL_LOCAL, NULL, 1));
110
	check_get_multivar_foreach(cfg, 1, 1);
111 112

	/* drop other file with match */
113
	cl_git_pass(git_config_add_file_ondisk(cfg, "config/config3", GIT_CONFIG_LEVEL_SYSTEM, NULL, 1));
114
	check_get_multivar_foreach(cfg, 0, 0);
115 116

	/* reload original file (add different place in order) */
117
	cl_git_pass(git_config_add_file_ondisk(cfg, "config/config11", GIT_CONFIG_LEVEL_SYSTEM, NULL, 1));
118
	check_get_multivar_foreach(cfg, 2, 1);
119

120 121
	check_get_multivar(cfg, 2);

122 123
	git_config_free(cfg);
}
124 125 126 127 128 129 130

void test_config_multivar__add(void)
{
	git_config *cfg;
	int n;

	cl_git_pass(git_config_open_ondisk(&cfg, "config/config11"));
131
	cl_git_pass(git_config_set_multivar(cfg, _name, "nonexistant", "git://git.otherplace.org/libgit2"));
132 133

	n = 0;
134
	cl_git_pass(git_config_get_multivar_foreach(cfg, _name, NULL, cb, &n));
135
	cl_assert_equal_i(n, 3);
136 137

	n = 0;
138
	cl_git_pass(git_config_get_multivar_foreach(cfg, _name, "otherplace", cb, &n));
139
	cl_assert_equal_i(n, 1);
140 141 142 143 144 145 146 147

	git_config_free(cfg);

	/* We know it works in memory, let's see if the file is written correctly */

	cl_git_pass(git_config_open_ondisk(&cfg, "config/config11"));

	n = 0;
148
	cl_git_pass(git_config_get_multivar_foreach(cfg, _name, NULL, cb, &n));
149
	cl_assert_equal_i(n, 3);
150 151

	n = 0;
152
	cl_git_pass(git_config_get_multivar_foreach(cfg, _name, "otherplace", cb, &n));
153
	cl_assert_equal_i(n, 1);
154 155 156

	git_config_free(cfg);
}
157

158 159 160 161 162 163 164 165
void test_config_multivar__add_new(void)
{
	const char *var = "a.brand.new";
	git_config *cfg;
	int n;

	cl_git_pass(git_config_open_ondisk(&cfg, "config/config11"));

166
	cl_git_pass(git_config_set_multivar(cfg, var, "$^", "variable"));
167
	n = 0;
168
	cl_git_pass(git_config_get_multivar_foreach(cfg, var, NULL, cb, &n));
169
	cl_assert_equal_i(n, 1);
170 171 172 173

	git_config_free(cfg);
}

174 175 176 177 178 179 180 181
void test_config_multivar__replace(void)
{
	git_config *cfg;
	int n;

	cl_git_pass(git_config_open_ondisk(&cfg, "config/config11"));

	n = 0;
182
	cl_git_pass(git_config_get_multivar_foreach(cfg, _name, NULL, cb, &n));
183 184 185 186 187
	cl_assert(n == 2);

	cl_git_pass(git_config_set_multivar(cfg, _name, "github", "git://git.otherplace.org/libgit2"));

	n = 0;
188
	cl_git_pass(git_config_get_multivar_foreach(cfg, _name, NULL, cb, &n));
189 190 191 192 193 194 195
	cl_assert(n == 2);

	git_config_free(cfg);

	cl_git_pass(git_config_open_ondisk(&cfg, "config/config11"));

	n = 0;
196
	cl_git_pass(git_config_get_multivar_foreach(cfg, _name, NULL, cb, &n));
197
	cl_assert(n == 2);
198 199

	git_config_free(cfg);
200 201 202 203 204 205 206 207 208 209 210
}

void test_config_multivar__replace_multiple(void)
{
	git_config *cfg;
	int n;

	cl_git_pass(git_config_open_ondisk(&cfg, "config/config11"));
	cl_git_pass(git_config_set_multivar(cfg, _name, "git://", "git://git.otherplace.org/libgit2"));

	n = 0;
211
	cl_git_pass(git_config_get_multivar_foreach(cfg, _name, "otherplace", cb, &n));
212
	cl_assert_equal_i(n, 2);
213 214 215 216 217 218

	git_config_free(cfg);

	cl_git_pass(git_config_open_ondisk(&cfg, "config/config11"));

	n = 0;
219
	cl_git_pass(git_config_get_multivar_foreach(cfg, _name, "otherplace", cb, &n));
220
	cl_assert_equal_i(n, 2);
221

222
	git_config_free(cfg);
223
}
224 225 226 227 228 229 230 231 232 233

void test_config_multivar__delete(void)
{
	git_config *cfg;
	int n;

	cl_git_pass(git_config_open_ondisk(&cfg, "config/config11"));

	n = 0;
	cl_git_pass(git_config_get_multivar_foreach(cfg, _name, NULL, cb, &n));
234
	cl_assert_equal_i(2, n);
235 236 237 238 239

	cl_git_pass(git_config_delete_multivar(cfg, _name, "github"));

	n = 0;
	cl_git_pass(git_config_get_multivar_foreach(cfg, _name, NULL, cb, &n));
240
	cl_assert_equal_i(1, n);
241 242 243 244 245 246 247

	git_config_free(cfg);

	cl_git_pass(git_config_open_ondisk(&cfg, "config/config11"));

	n = 0;
	cl_git_pass(git_config_get_multivar_foreach(cfg, _name, NULL, cb, &n));
248
	cl_assert_equal_i(1, n);
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288

	git_config_free(cfg);
}

void test_config_multivar__delete_multiple(void)
{
	git_config *cfg;
	int n;

	cl_git_pass(git_config_open_ondisk(&cfg, "config/config11"));

	n = 0;
	cl_git_pass(git_config_get_multivar_foreach(cfg, _name, NULL, cb, &n));
	cl_assert(n == 2);

	cl_git_pass(git_config_delete_multivar(cfg, _name, "git"));

	n = 0;
	cl_git_fail_with(git_config_get_multivar_foreach(cfg, _name, NULL, cb, &n), GIT_ENOTFOUND);

	git_config_free(cfg);

	cl_git_pass(git_config_open_ondisk(&cfg, "config/config11"));

	n = 0;
	cl_git_fail_with(git_config_get_multivar_foreach(cfg, _name, NULL, cb, &n), GIT_ENOTFOUND);

	git_config_free(cfg);
}

void test_config_multivar__delete_notfound(void)
{
	git_config *cfg;

	cl_git_pass(git_config_open_ondisk(&cfg, "config/config11"));

	cl_git_fail_with(git_config_delete_multivar(cfg, "remote.ab.noturl", "git"), GIT_ENOTFOUND);

	git_config_free(cfg);
}