include.c 3.63 KB
Newer Older
1 2 3 4 5 6 7
#include "clar_libgit2.h"
#include "buffer.h"
#include "fileops.h"

void test_config_include__relative(void)
{
	git_config *cfg;
8
	git_buf buf = GIT_BUF_INIT;
9 10 11

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

12 13
	cl_git_pass(git_config_get_string_buf(&buf, cfg, "foo.bar.baz"));
	cl_assert_equal_s("huzzah", git_buf_cstr(&buf));
14

15
	git_buf_free(&buf);
16 17 18 19 20 21 22 23 24 25 26 27 28 29
	git_config_free(cfg);
}

void test_config_include__absolute(void)
{
	git_config *cfg;
	git_buf buf = GIT_BUF_INIT;

	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));
	git_buf_free(&buf);
	cl_git_pass(git_config_open_ondisk(&cfg, "config-include-absolute"));

30 31
	cl_git_pass(git_config_get_string_buf(&buf, cfg, "foo.bar.baz"));
	cl_assert_equal_s("huzzah", git_buf_cstr(&buf));
32

33
	git_buf_free(&buf);
34 35 36 37 38 39
	git_config_free(cfg);
}

void test_config_include__homedir(void)
{
	git_config *cfg;
40
	git_buf buf = GIT_BUF_INIT;
41 42 43 44 45 46

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

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

50
	git_buf_free(&buf);
51
	git_config_free(cfg);
52 53

	cl_sandbox_set_search_path_defaults();
54
}
55

56 57 58 59
/* We need to pretend that the variables were defined where the file was included */
void test_config_include__ordering(void)
{
	git_config *cfg;
60
	git_buf buf = GIT_BUF_INIT;
61 62 63 64 65 66 67 68 69

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

70 71 72 73 74
	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));
75

76
	git_buf_free(&buf);
77 78
	git_config_free(cfg);
}
79 80 81 82 83 84 85 86 87 88 89

/* We need to pretend that the variables were defined where the file was included */
void test_config_include__depth(void)
{
	git_config *cfg;

	cl_git_mkfile("a", "[include]\npath = b");
	cl_git_mkfile("b", "[include]\npath = a");

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

90 91
	p_unlink("a");
	p_unlink("b");
92
}
93 94 95 96

void test_config_include__missing(void)
{
	git_config *cfg;
97
	git_buf buf = GIT_BUF_INIT;
98

99
	cl_git_mkfile("including", "[include]\npath = nonexistentfile\n[foo]\nbar = baz");
100

101 102 103
	giterr_clear();
	cl_git_pass(git_config_open_ondisk(&cfg, "including"));
	cl_assert(giterr_last() == NULL);
104 105
	cl_git_pass(git_config_get_string_buf(&buf, cfg, "foo.bar"));
	cl_assert_equal_s("baz", git_buf_cstr(&buf));
106

107
	git_buf_free(&buf);
108 109
	git_config_free(cfg);
}
110 111 112 113 114

#define replicate10(s) s s s s s s s s s s
void test_config_include__depth2(void)
{
	git_config *cfg;
115
	git_buf buf = GIT_BUF_INIT;
116 117 118 119 120 121 122 123
	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"));

124 125
	cl_git_pass(git_config_get_string_buf(&buf, cfg, "foo.bar"));
	cl_assert_equal_s("baz", git_buf_cstr(&buf));
126

127 128 129
	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));
130

131
	git_buf_free(&buf);
132 133
	git_config_free(cfg);
}