read.c 7.21 KB
Newer Older
1
#include "clar_libgit2.h"
2 3 4 5 6 7 8 9

void test_config_read__simple_read(void)
{
	git_config *cfg;
	int32_t i;

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

10
	cl_git_pass(git_config_get_int32(&i, cfg, "core.repositoryformatversion"));
11
	cl_assert(i == 0);
12
	cl_git_pass(git_config_get_bool(&i, cfg, "core.filemode"));
13
	cl_assert(i == 1);
14
	cl_git_pass(git_config_get_bool(&i, cfg, "core.bare"));
15
	cl_assert(i == 0);
16
	cl_git_pass(git_config_get_bool(&i, cfg, "core.logallrefupdates"));
17 18 19 20 21 22 23 24 25 26 27 28 29
	cl_assert(i == 1);

	git_config_free(cfg);
}

void test_config_read__case_sensitive(void)
{
	git_config *cfg;
	int i;
	const char *str;

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

30
	cl_git_pass(git_config_get_string(&str, cfg, "this.that.other"));
Vicent Martí committed
31
	cl_assert_equal_s(str, "true");
32
	cl_git_pass(git_config_get_string(&str, cfg, "this.That.other"));
Vicent Martí committed
33
	cl_assert_equal_s(str, "yes");
34

35
	cl_git_pass(git_config_get_bool(&i, cfg, "this.that.other"));
36
	cl_assert(i == 1);
37
	cl_git_pass(git_config_get_bool(&i, cfg, "this.That.other"));
38 39 40
	cl_assert(i == 1);

	/* This one doesn't exist */
41
	cl_must_fail(git_config_get_bool(&i, cfg, "this.thaT.other"));
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56

	git_config_free(cfg);
}

/*
 * If \ is the last non-space character on the line, we read the next
 * one, separating each line with SP.
 */
void test_config_read__multiline_value(void)
{
	git_config *cfg;
	const char *str;

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

57
	cl_git_pass(git_config_get_string(&str, cfg, "this.That.and"));
Vicent Martí committed
58
	cl_assert_equal_s(str, "one one one two two three three");
59 60 61 62 63 64 65 66 67 68 69 70 71 72

	git_config_free(cfg);
}

/*
 * This kind of subsection declaration is case-insensitive
 */
void test_config_read__subsection_header(void)
{
	git_config *cfg;
	const char *str;

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

73
	cl_git_pass(git_config_get_string(&str, cfg, "section.subsection.var"));
Vicent Martí committed
74
	cl_assert_equal_s(str, "hello");
75 76

	/* The subsection is transformed to lower-case */
77
	cl_must_fail(git_config_get_string(&str, cfg, "section.subSectIon.var"));
78 79 80 81 82 83 84 85 86 87 88 89

	git_config_free(cfg);
}

void test_config_read__lone_variable(void)
{
	git_config *cfg;
	const char *str;
	int i;

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

90
	cl_git_pass(git_config_get_string(&str, cfg, "some.section.variable"));
91 92
	cl_assert(str == NULL);

93
	cl_git_pass(git_config_get_bool(&i, cfg, "some.section.variable"));
94 95 96 97 98 99 100 101 102 103 104 105
	cl_assert(i == 1);

	git_config_free(cfg);
}

void test_config_read__number_suffixes(void)
{
	git_config *cfg;
	int64_t i;

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

106
	cl_git_pass(git_config_get_int64(&i, cfg, "number.simple"));
107 108
	cl_assert(i == 1);

109
	cl_git_pass(git_config_get_int64(&i, cfg, "number.k"));
110 111
	cl_assert(i == 1 * 1024);

112
	cl_git_pass(git_config_get_int64(&i, cfg, "number.kk"));
113 114
	cl_assert(i == 1 * 1024);

115
	cl_git_pass(git_config_get_int64(&i, cfg, "number.m"));
116 117
	cl_assert(i == 1 * 1024 * 1024);

118
	cl_git_pass(git_config_get_int64(&i, cfg, "number.mm"));
119 120
	cl_assert(i == 1 * 1024 * 1024);

121
	cl_git_pass(git_config_get_int64(&i, cfg, "number.g"));
122 123
	cl_assert(i == 1 * 1024 * 1024 * 1024);

124
	cl_git_pass(git_config_get_int64(&i, cfg, "number.gg"));
125 126 127 128 129 130 131 132 133 134 135 136
	cl_assert(i == 1 * 1024 * 1024 * 1024);

	git_config_free(cfg);
}

void test_config_read__blank_lines(void)
{
	git_config *cfg;
	int i;

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

137
	cl_git_pass(git_config_get_bool(&i, cfg, "valid.subsection.something"));
138 139
	cl_assert(i == 1);

140
	cl_git_pass(git_config_get_bool(&i, cfg, "something.else.something"));
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
	cl_assert(i == 0);

	git_config_free(cfg);
}

void test_config_read__invalid_ext_headers(void)
{
	git_config *cfg;
	cl_must_fail(git_config_open_ondisk(&cfg, cl_fixture("config/config7")));
}

void test_config_read__empty_files(void)
{
	git_config *cfg;
	cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config8")));
	git_config_free(cfg);
}

void test_config_read__header_in_last_line(void)
{
	git_config *cfg;

	cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config10")));
	git_config_free(cfg);
}

void test_config_read__prefixes(void)
{
	git_config *cfg;
	const char *str;

	cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config9")));
173
	cl_git_pass(git_config_get_string(&str, cfg, "remote.ab.url"));
Vicent Martí committed
174
	cl_assert_equal_s(str, "http://example.com/git/ab");
175

176
	cl_git_pass(git_config_get_string(&str, cfg, "remote.abba.url"));
Vicent Martí committed
177
	cl_assert_equal_s(str, "http://example.com/git/abba");
178 179 180 181

	git_config_free(cfg);
}

182 183 184 185 186 187
void test_config_read__escaping_quotes(void)
{
	git_config *cfg;
	const char *str;

	cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config13")));
188
	cl_git_pass(git_config_get_string(&str, cfg, "core.editor"));
189 190 191 192 193
	cl_assert(strcmp(str, "\"C:/Program Files/Nonsense/bah.exe\" \"--some option\"") == 0);

	git_config_free(cfg);
}

194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
static int count_cfg_entries(
	const char *var_name, const char *value, void *payload)
{
	int *count = payload;
	GIT_UNUSED(var_name);
	GIT_UNUSED(value);
	(*count)++;
	return 0;
}

static int cfg_callback_countdown(
	const char *var_name, const char *value, void *payload)
{
	int *count = payload;
	GIT_UNUSED(var_name);
	GIT_UNUSED(value);
	(*count)--;
	if (*count == 0)
		return -100;
	return 0;
}

void test_config_read__foreach(void)
{
	git_config *cfg;
	int count, ret;

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

	count = 0;
	cl_git_pass(git_config_foreach(cfg, count_cfg_entries, &count));
	cl_assert_equal_i(5, count);

	count = 3;
	cl_git_fail(ret = git_config_foreach(cfg, cfg_callback_countdown, &count));
	cl_assert_equal_i(-100, ret);

	git_config_free(cfg);
}

void test_config_read__foreach_match(void)
{
	git_config *cfg;
	int count;

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

	count = 0;
	cl_git_pass(
		git_config_foreach_match(cfg, "core.*", count_cfg_entries, &count));
	cl_assert_equal_i(3, count);

	count = 0;
	cl_git_pass(
		git_config_foreach_match(cfg, "remote\\.ab.*", count_cfg_entries, &count));
	cl_assert_equal_i(2, count);

	count = 0;
	cl_git_pass(
		git_config_foreach_match(cfg, ".*url$", count_cfg_entries, &count));
	cl_assert_equal_i(2, count);

	count = 0;
	cl_git_pass(
		git_config_foreach_match(cfg, ".*dummy.*", count_cfg_entries, &count));
	cl_assert_equal_i(2, count);

	count = 0;
	cl_git_pass(
		git_config_foreach_match(cfg, ".*nomatch.*", count_cfg_entries, &count));
	cl_assert_equal_i(0, count);

	git_config_free(cfg);
}

269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
#if 0

BEGIN_TEST(config10, "a repo's config overrides the global config")
	git_repository *repo;
	git_config *cfg;
	int32_t version;

	cl_git_pass(git_repository_open(&repo, REPOSITORY_FOLDER));
	cl_git_pass(git_repository_config(&cfg, repo, GLOBAL_CONFIG, NULL));
	cl_git_pass(git_config_get_int32(cfg, "core.repositoryformatversion", &version));
	cl_assert(version == 0);
	git_config_free(cfg);
	git_repository_free(repo);
END_TEST

BEGIN_TEST(config11, "fall back to the global config")
	git_repository *repo;
	git_config *cfg;
	int32_t num;

	cl_git_pass(git_repository_open(&repo, REPOSITORY_FOLDER));
	cl_git_pass(git_repository_config(&cfg, repo, GLOBAL_CONFIG, NULL));
	cl_git_pass(git_config_get_int32(cfg, "core.something", &num));
	cl_assert(num == 2);
	git_config_free(cfg);
	git_repository_free(repo);
END_TEST
#endif