include.c 6.22 KB
Newer Older
1 2
#include "clar_libgit2.h"
#include "buffer.h"
3
#include "futils.h"
4

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

void test_config_include__initialize(void)
9
{
10 11 12
    cfg = NULL;
    git_buf_init(&buf, 0);
}
13

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

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

24 25
	cl_git_pass(git_config_get_string_buf(&buf, cfg, "foo.bar.baz"));
	cl_assert_equal_s("huzzah", git_buf_cstr(&buf));
26 27 28 29 30 31 32
}

void test_config_include__absolute(void)
{
	cl_git_pass(git_buf_printf(&buf, "[include]\npath = %s/config-included", cl_fixture("config")));

	cl_git_mkfile("config-include-absolute", git_buf_cstr(&buf));
33
	git_buf_dispose(&buf);
34 35
	cl_git_pass(git_config_open_ondisk(&cfg, "config-include-absolute"));

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

	cl_git_pass(p_unlink("config-include-absolute"));
40 41 42 43 44 45 46 47 48
}

void test_config_include__homedir(void)
{
	cl_git_pass(git_libgit2_opts(GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, cl_fixture("config")));
	cl_git_mkfile("config-include-homedir",  "[include]\npath = ~/config-included");

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

49 50
	cl_git_pass(git_config_get_string_buf(&buf, cfg, "foo.bar.baz"));
	cl_assert_equal_s("huzzah", git_buf_cstr(&buf));
51

52
	cl_sandbox_set_search_path_defaults();
53 54

	cl_git_pass(p_unlink("config-include-homedir"));
55
}
56

57 58 59 60 61 62 63 64 65 66 67
/* 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"));

68 69 70 71 72
	cl_git_pass(git_config_get_string_buf(&buf, cfg, "foo.bar.frotz"));
	cl_assert_equal_s("hiya", git_buf_cstr(&buf));
	git_buf_clear(&buf);
	cl_git_pass(git_config_get_string_buf(&buf, cfg, "foo.bar.baz"));
	cl_assert_equal_s("huzzah", git_buf_cstr(&buf));
73 74 75

	cl_git_pass(p_unlink("included"));
	cl_git_pass(p_unlink("including"));
76
}
77 78 79 80 81 82 83 84 85

/* 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"));

86 87
	cl_git_pass(p_unlink("a"));
	cl_git_pass(p_unlink("b"));
88
}
89

90 91 92 93 94 95 96 97 98 99
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"));
	cl_assert_equal_s("", git_buf_cstr(&buf));

	cl_git_pass(p_unlink("a"));
}

100 101
void test_config_include__missing(void)
{
102
	cl_git_mkfile("including", "[include]\npath = nonexistentfile\n[foo]\nbar = baz");
103

104
	git_error_clear();
105
	cl_git_pass(git_config_open_ondisk(&cfg, "including"));
106
	cl_assert(git_error_last() == NULL);
107 108
	cl_git_pass(git_config_get_string_buf(&buf, cfg, "foo.bar"));
	cl_assert_equal_s("baz", git_buf_cstr(&buf));
109 110

	cl_git_pass(p_unlink("including"));
111 112 113 114
}

void test_config_include__missing_homedir(void)
{
115
	cl_git_pass(git_libgit2_opts(GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, cl_fixture("config")));
116
	cl_git_mkfile("including", "[include]\npath = ~/.nonexistentfile\n[foo]\nbar = baz");
117

118
	git_error_clear();
119
	cl_git_pass(git_config_open_ondisk(&cfg, "including"));
120
	cl_assert(git_error_last() == NULL);
121 122
	cl_git_pass(git_config_get_string_buf(&buf, cfg, "foo.bar"));
	cl_assert_equal_s("baz", git_buf_cstr(&buf));
123

124
	cl_sandbox_set_search_path_defaults();
125
	cl_git_pass(p_unlink("including"));
126
}
127 128 129 130 131 132 133 134 135 136 137 138

#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"));

139 140
	cl_git_pass(git_config_get_string_buf(&buf, cfg, "foo.bar"));
	cl_assert_equal_s("baz", git_buf_cstr(&buf));
141

142 143 144
	git_buf_clear(&buf);
	cl_git_pass(git_config_get_string_buf(&buf, cfg, "foo.bar2"));
	cl_assert_equal_s("baz2", git_buf_cstr(&buf));
145 146 147 148

	cl_git_pass(p_unlink("top-level"));
	cl_git_pass(p_unlink("middle"));
	cl_git_pass(p_unlink("bottom"));
149
}
150 151 152 153 154 155 156 157 158

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"));
159 160 161

	cl_git_pass(p_unlink("top-level"));
	cl_git_pass(p_unlink("included"));
162
}
163 164 165 166 167 168 169 170 171 172 173 174

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");
175 176 177 178

	cl_git_pass(p_unlink("top-level"));
	cl_git_pass(p_unlink("first"));
	cl_git_pass(p_unlink("second"));
179
}
180 181 182 183 184 185 186 187

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"));
188 189 190

	cl_git_pass(p_unlink("top-level"));
	cl_git_pass(p_unlink("included"));
191 192 193 194 195
}

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

197 198 199 200
	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"));
201 202 203

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