include.c 7.91 KB
Newer Older
1
#include "clar_libgit2.h"
2
#include "futils.h"
3

4 5 6 7
static git_config *cfg;
static git_buf buf;

void test_config_include__initialize(void)
8
{
9 10
	cfg = NULL;
	memset(&buf, 0, sizeof(git_buf));
11
}
12

13 14
void test_config_include__cleanup(void)
{
15 16
	git_config_free(cfg);
	git_buf_dispose(&buf);
17 18 19 20
}

void test_config_include__relative(void)
{
21 22
	cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config-include")));

23
	cl_git_pass(git_config_get_string_buf(&buf, cfg, "foo.bar.baz"));
24
	cl_assert_equal_s("huzzah", buf.ptr);
25 26 27 28
}

void test_config_include__absolute(void)
{
29 30 31 32 33
	git_str cfgdata = GIT_STR_INIT;
	cl_git_pass(git_str_printf(&cfgdata, "[include]\npath = %s/config-included", cl_fixture("config")));

	cl_git_mkfile("config-include-absolute", cfgdata.ptr);
	git_str_dispose(&cfgdata);
34 35 36

	cl_git_pass(git_config_open_ondisk(&cfg, "config-include-absolute"));

37
	cl_git_pass(git_config_get_string_buf(&buf, cfg, "foo.bar.baz"));
38
	cl_assert_equal_s("huzzah", buf.ptr);
39 40

	cl_git_pass(p_unlink("config-include-absolute"));
41 42 43 44
}

void test_config_include__homedir(void)
{
45 46 47 48 49
	git_str homefile = GIT_STR_INIT;

	cl_fake_homedir(&homefile);
	cl_git_pass(git_str_joinpath(&homefile, homefile.ptr, "config-included"));

50
	cl_git_mkfile("config-include-homedir",  "[include]\npath = ~/config-included");
51
	cl_git_mkfile(homefile.ptr, "[foo \"bar\"]\n\tbaz = huzzah\n");
52 53 54

	cl_git_pass(git_config_open_ondisk(&cfg, "config-include-homedir"));

55
	cl_git_pass(git_config_get_string_buf(&buf, cfg, "foo.bar.baz"));
56
	cl_assert_equal_s("huzzah", buf.ptr);
57

58
	cl_sandbox_set_search_path_defaults();
59 60

	cl_git_pass(p_unlink("config-include-homedir"));
61 62

	git_str_dispose(&homefile);
63
}
64

65 66 67 68 69 70 71 72 73 74 75
/* We need to pretend that the variables were defined where the file was included */
void test_config_include__ordering(void)
{
	cl_git_mkfile("included", "[foo \"bar\"]\nbaz = hurrah\nfrotz = hiya");
	cl_git_mkfile("including",
		      "[foo \"bar\"]\nfrotz = hello\n"
		      "[include]\npath = included\n"
		      "[foo \"bar\"]\nbaz = huzzah\n");

	cl_git_pass(git_config_open_ondisk(&cfg, "including"));

76
	cl_git_pass(git_config_get_string_buf(&buf, cfg, "foo.bar.frotz"));
77 78
	cl_assert_equal_s("hiya", buf.ptr);
	git_buf_dispose(&buf);
79
	cl_git_pass(git_config_get_string_buf(&buf, cfg, "foo.bar.baz"));
80
	cl_assert_equal_s("huzzah", buf.ptr);
81 82 83

	cl_git_pass(p_unlink("included"));
	cl_git_pass(p_unlink("including"));
84
}
85 86 87 88 89 90 91 92 93

/* We need to pretend that the variables were defined where the file was included */
void test_config_include__depth(void)
{
	cl_git_mkfile("a", "[include]\npath = b");
	cl_git_mkfile("b", "[include]\npath = a");

	cl_git_fail(git_config_open_ondisk(&cfg, "a"));

94 95
	cl_git_pass(p_unlink("a"));
	cl_git_pass(p_unlink("b"));
96
}
97

98 99 100 101 102
void test_config_include__empty_path_sanely_handled(void)
{
	cl_git_mkfile("a", "[include]\npath");
	cl_git_pass(git_config_open_ondisk(&cfg, "a"));
	cl_git_pass(git_config_get_string_buf(&buf, cfg, "include.path"));
103
	cl_assert_equal_s("", buf.ptr);
104 105 106 107

	cl_git_pass(p_unlink("a"));
}

108 109
void test_config_include__missing(void)
{
110
	cl_git_mkfile("including", "[include]\npath = nonexistentfile\n[foo]\nbar = baz");
111

112
	git_error_clear();
113
	cl_git_pass(git_config_open_ondisk(&cfg, "including"));
114
	cl_assert_equal_i(GIT_ERROR_NONE, git_error_last()->klass);
115
	cl_git_pass(git_config_get_string_buf(&buf, cfg, "foo.bar"));
116
	cl_assert_equal_s("baz", buf.ptr);
117 118

	cl_git_pass(p_unlink("including"));
119 120 121 122
}

void test_config_include__missing_homedir(void)
{
123 124
	cl_fake_homedir(NULL);

125
	cl_git_mkfile("including", "[include]\npath = ~/.nonexistentfile\n[foo]\nbar = baz");
126

127
	git_error_clear();
128
	cl_git_pass(git_config_open_ondisk(&cfg, "including"));
129
	cl_assert_equal_i(GIT_ERROR_NONE, git_error_last()->klass);
130
	cl_git_pass(git_config_get_string_buf(&buf, cfg, "foo.bar"));
131
	cl_assert_equal_s("baz", buf.ptr);
132

133
	cl_sandbox_set_search_path_defaults();
134
	cl_git_pass(p_unlink("including"));
135
}
136 137 138 139 140 141 142 143 144 145 146 147

#define replicate10(s) s s s s s s s s s s
void test_config_include__depth2(void)
{
	const char *content = "[include]\n" replicate10(replicate10("path=bottom\n"));

	cl_git_mkfile("top-level", "[include]\npath = middle\n[foo]\nbar = baz");
	cl_git_mkfile("middle", content);
	cl_git_mkfile("bottom", "[foo]\nbar2 = baz2");

	cl_git_pass(git_config_open_ondisk(&cfg, "top-level"));

148
	cl_git_pass(git_config_get_string_buf(&buf, cfg, "foo.bar"));
149
	cl_assert_equal_s("baz", buf.ptr);
150

151
	git_buf_dispose(&buf);
152
	cl_git_pass(git_config_get_string_buf(&buf, cfg, "foo.bar2"));
153
	cl_assert_equal_s("baz2", buf.ptr);
154 155 156 157

	cl_git_pass(p_unlink("top-level"));
	cl_git_pass(p_unlink("middle"));
	cl_git_pass(p_unlink("bottom"));
158
}
159 160 161 162 163 164 165 166 167

void test_config_include__removing_include_removes_values(void)
{
	cl_git_mkfile("top-level", "[include]\npath = included");
	cl_git_mkfile("included", "[foo]\nbar = value");

	cl_git_pass(git_config_open_ondisk(&cfg, "top-level"));
	cl_git_mkfile("top-level", "");
	cl_git_fail(git_config_get_string_buf(&buf, cfg, "foo.bar"));
168 169 170

	cl_git_pass(p_unlink("top-level"));
	cl_git_pass(p_unlink("included"));
171
}
172 173 174 175 176 177 178 179 180 181 182 183

void test_config_include__rewriting_include_refreshes_values(void)
{
	cl_git_mkfile("top-level", "[include]\npath = first\n[include]\npath = second");
	cl_git_mkfile("first", "[first]\nfoo = bar");
	cl_git_mkfile("second", "[second]\nfoo = bar");

	cl_git_pass(git_config_open_ondisk(&cfg, "top-level"));
	cl_git_mkfile("first", "[first]\nother = value");
	cl_git_fail(git_config_get_string_buf(&buf, cfg, "foo.bar"));
	cl_git_pass(git_config_get_string_buf(&buf, cfg, "first.other"));
	cl_assert_equal_s(buf.ptr, "value");
184 185 186 187

	cl_git_pass(p_unlink("top-level"));
	cl_git_pass(p_unlink("first"));
	cl_git_pass(p_unlink("second"));
188
}
189

190 191 192 193 194 195 196 197
void test_config_include__rewriting_include_twice_refreshes_values(void)
{
	cl_git_mkfile("top-level", "[include]\npath = included");
	cl_git_mkfile("included", "[foo]\nbar = first-value");

	cl_git_pass(git_config_open_ondisk(&cfg, "top-level"));
	cl_git_pass(git_config_get_string_buf(&buf, cfg, "foo.bar"));

198
	git_buf_dispose(&buf);
199 200 201 202 203
	cl_git_mkfile("included", "[foo]\nother = value2");
	cl_git_fail(git_config_get_string_buf(&buf, cfg, "foo.bar"));
	cl_git_pass(git_config_get_string_buf(&buf, cfg, "foo.other"));
	cl_assert_equal_s(buf.ptr, "value2");

204
	git_buf_dispose(&buf);
205 206 207 208 209 210 211 212 213
	cl_git_mkfile("included", "[foo]\nanother = bar");
	cl_git_fail(git_config_get_string_buf(&buf, cfg, "foo.other"));
	cl_git_pass(git_config_get_string_buf(&buf, cfg, "foo.another"));
	cl_assert_equal_s(buf.ptr, "bar");

	cl_git_pass(p_unlink("top-level"));
	cl_git_pass(p_unlink("included"));
}

214 215 216 217 218 219 220
void test_config_include__included_variables_cannot_be_deleted(void)
{
	cl_git_mkfile("top-level", "[include]\npath = included\n");
	cl_git_mkfile("included", "[foo]\nbar = value");

	cl_git_pass(git_config_open_ondisk(&cfg, "top-level"));
	cl_git_fail(git_config_delete_entry(cfg, "foo.bar"));
221 222 223

	cl_git_pass(p_unlink("top-level"));
	cl_git_pass(p_unlink("included"));
224 225 226 227 228
}

void test_config_include__included_variables_cannot_be_modified(void)
{
	cl_git_mkfile("top-level", "[include]\npath = included\n");
229

230 231 232 233
	cl_git_mkfile("included", "[foo]\nbar = value");

	cl_git_pass(git_config_open_ondisk(&cfg, "top-level"));
	cl_git_fail(git_config_set_string(cfg, "foo.bar", "other-value"));
234 235 236

	cl_git_pass(p_unlink("top-level"));
	cl_git_pass(p_unlink("included"));
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

void test_config_include__variables_in_included_override_including(void)
{
	int i;

	cl_git_mkfile("top-level", "[foo]\nbar = 1\n[include]\npath = included");
	cl_git_mkfile("included", "[foo]\nbar = 2");

	cl_git_pass(git_config_open_ondisk(&cfg, "top-level"));
	cl_git_pass(git_config_get_int32(&i, cfg, "foo.bar"));
	cl_assert_equal_i(i, 2);

	cl_git_pass(p_unlink("top-level"));
	cl_git_pass(p_unlink("included"));
}

void test_config_include__variables_in_including_override_included(void)
{
	int i;

	cl_git_mkfile("top-level", "[include]\npath = included\n[foo]\nbar = 1");
	cl_git_mkfile("included", "[foo]\nbar = 2");

	cl_git_pass(git_config_open_ondisk(&cfg, "top-level"));
	cl_git_pass(git_config_get_int32(&i, cfg, "foo.bar"));
	cl_assert_equal_i(i, 1);

	cl_git_pass(p_unlink("top-level"));
	cl_git_pass(p_unlink("included"));
}