config.c 6.54 KB
Newer Older
1
#include "clar_libgit2.h"
2
#include "sysdir.h"
3
#include "futils.h"
4 5
#include <ctype.h>

6
static git_buf path = GIT_BUF_INIT;
7 8 9 10

void test_repo_config__initialize(void)
{
	cl_fixture_sandbox("empty_standard_repo");
11 12
	cl_git_pass(cl_rename(
		"empty_standard_repo/.gitted", "empty_standard_repo/.git"));
13 14 15 16 17 18 19 20 21

	git_buf_clear(&path);

	cl_must_pass(p_mkdir("alternate", 0777));
	cl_git_pass(git_path_prettify(&path, "alternate", NULL));
}

void test_repo_config__cleanup(void)
{
22 23
	cl_sandbox_set_search_path_defaults();

24
	git_buf_dispose(&path);
25 26 27

	cl_git_pass(
		git_futils_rmdir_r("alternate", NULL, GIT_RMDIR_REMOVE_FILES));
28 29
	cl_assert(!git_path_isdir("alternate"));

30
	cl_fixture_cleanup("empty_standard_repo");
31

32 33
}

34
void test_repo_config__can_open_global_when_there_is_no_file(void)
35 36 37 38 39 40 41 42 43 44 45 46 47
{
	git_repository *repo;
	git_config *config, *global;

	cl_git_pass(git_libgit2_opts(
		GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, path.ptr));
	cl_git_pass(git_libgit2_opts(
		GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_SYSTEM, path.ptr));
	cl_git_pass(git_libgit2_opts(
		GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_XDG, path.ptr));

	cl_git_pass(git_repository_open(&repo, "empty_standard_repo"));
	cl_git_pass(git_repository_config(&config, repo));
48 49
	cl_git_pass(git_config_open_level(
		&global, config, GIT_CONFIG_LEVEL_GLOBAL));
50 51 52 53 54 55 56 57

	cl_git_pass(git_config_set_string(global, "test.set", "42"));

	git_config_free(global);
	git_config_free(config);
	git_repository_free(repo);
}

58
void test_repo_config__can_open_missing_global_with_separators(void)
59 60 61 62
{
	git_repository *repo;
	git_config *config, *global;

63 64
	cl_git_pass(git_buf_printf(
		&path, "%c%s", GIT_PATH_LIST_SEPARATOR, "dummy"));
65 66 67 68 69 70 71 72

	cl_git_pass(git_libgit2_opts(
		GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, path.ptr));
	cl_git_pass(git_libgit2_opts(
		GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_SYSTEM, path.ptr));
	cl_git_pass(git_libgit2_opts(
		GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_XDG, path.ptr));

73
	git_buf_dispose(&path);
74 75 76

	cl_git_pass(git_repository_open(&repo, "empty_standard_repo"));
	cl_git_pass(git_repository_config(&config, repo));
77 78
	cl_git_pass(git_config_open_level(
		&global, config, GIT_CONFIG_LEVEL_GLOBAL));
79 80 81 82 83 84 85

	cl_git_pass(git_config_set_string(global, "test.set", "42"));

	git_config_free(global);
	git_config_free(config);
	git_repository_free(repo);
}
86 87 88

#include "repository.h"

89
void test_repo_config__read_with_no_configs_at_all(void)
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
{
	git_repository *repo;
	int val;

	cl_git_pass(git_libgit2_opts(
		GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, path.ptr));
	cl_git_pass(git_libgit2_opts(
		GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_SYSTEM, path.ptr));
	cl_git_pass(git_libgit2_opts(
		GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_XDG, path.ptr));

	/* with none */

	cl_must_pass(p_unlink("empty_standard_repo/.git/config"));
	cl_assert(!git_path_isfile("empty_standard_repo/.git/config"));

	cl_git_pass(git_repository_open(&repo, "empty_standard_repo"));
107
	git_repository__configmap_lookup_cache_clear(repo);
108
	val = -1;
109
	cl_git_pass(git_repository__configmap_lookup(&val, repo, GIT_CONFIGMAP_ABBREV));
110 111 112
	cl_assert_equal_i(GIT_ABBREV_DEFAULT, val);
	git_repository_free(repo);

113
	/* with no local config, just system */
114

115
	cl_sandbox_set_search_path_defaults();
116 117 118 119 120 121 122 123

	cl_must_pass(p_mkdir("alternate/1", 0777));
	cl_git_pass(git_buf_joinpath(&path, path.ptr, "1"));
	cl_git_rewritefile("alternate/1/gitconfig", "[core]\n\tabbrev = 10\n");
	cl_git_pass(git_libgit2_opts(
		GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_SYSTEM, path.ptr));

	cl_git_pass(git_repository_open(&repo, "empty_standard_repo"));
124
	git_repository__configmap_lookup_cache_clear(repo);
125
	val = -1;
126
	cl_git_pass(git_repository__configmap_lookup(&val, repo, GIT_CONFIGMAP_ABBREV));
127 128 129
	cl_assert_equal_i(10, val);
	git_repository_free(repo);

130
	/* with just xdg + system */
131 132 133 134 135 136 137 138

	cl_must_pass(p_mkdir("alternate/2", 0777));
	path.ptr[path.size - 1] = '2';
	cl_git_rewritefile("alternate/2/config", "[core]\n\tabbrev = 20\n");
	cl_git_pass(git_libgit2_opts(
		GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_XDG, path.ptr));

	cl_git_pass(git_repository_open(&repo, "empty_standard_repo"));
139
	git_repository__configmap_lookup_cache_clear(repo);
140
	val = -1;
141
	cl_git_pass(git_repository__configmap_lookup(&val, repo, GIT_CONFIGMAP_ABBREV));
142 143 144 145 146 147 148 149 150 151 152 153
	cl_assert_equal_i(20, val);
	git_repository_free(repo);

	/* with global + xdg + system */

	cl_must_pass(p_mkdir("alternate/3", 0777));
	path.ptr[path.size - 1] = '3';
	cl_git_rewritefile("alternate/3/.gitconfig", "[core]\n\tabbrev = 30\n");
	cl_git_pass(git_libgit2_opts(
		GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, path.ptr));

	cl_git_pass(git_repository_open(&repo, "empty_standard_repo"));
154
	git_repository__configmap_lookup_cache_clear(repo);
155
	val = -1;
156
	cl_git_pass(git_repository__configmap_lookup(&val, repo, GIT_CONFIGMAP_ABBREV));
157 158 159 160 161 162 163 164
	cl_assert_equal_i(30, val);
	git_repository_free(repo);

	/* with all configs */

	cl_git_rewritefile("empty_standard_repo/.git/config", "[core]\n\tabbrev = 40\n");

	cl_git_pass(git_repository_open(&repo, "empty_standard_repo"));
165
	git_repository__configmap_lookup_cache_clear(repo);
166
	val = -1;
167
	cl_git_pass(git_repository__configmap_lookup(&val, repo, GIT_CONFIGMAP_ABBREV));
168 169 170 171 172 173
	cl_assert_equal_i(40, val);
	git_repository_free(repo);

	/* with all configs but delete the files ? */

	cl_git_pass(git_repository_open(&repo, "empty_standard_repo"));
174
	git_repository__configmap_lookup_cache_clear(repo);
175
	val = -1;
176
	cl_git_pass(git_repository__configmap_lookup(&val, repo, GIT_CONFIGMAP_ABBREV));
177 178 179 180 181 182 183 184 185 186 187 188 189 190
	cl_assert_equal_i(40, val);

	cl_must_pass(p_unlink("empty_standard_repo/.git/config"));
	cl_assert(!git_path_isfile("empty_standard_repo/.git/config"));

	cl_must_pass(p_unlink("alternate/1/gitconfig"));
	cl_assert(!git_path_isfile("alternate/1/gitconfig"));

	cl_must_pass(p_unlink("alternate/2/config"));
	cl_assert(!git_path_isfile("alternate/2/config"));

	cl_must_pass(p_unlink("alternate/3/.gitconfig"));
	cl_assert(!git_path_isfile("alternate/3/.gitconfig"));

191
	git_repository__configmap_lookup_cache_clear(repo);
192
	val = -1;
193
	cl_git_pass(git_repository__configmap_lookup(&val, repo, GIT_CONFIGMAP_ABBREV));
194 195 196 197 198 199 200 201 202
	cl_assert_equal_i(40, val);
	git_repository_free(repo);

	/* reopen */

	cl_assert(!git_path_isfile("empty_standard_repo/.git/config"));
	cl_assert(!git_path_isfile("alternate/3/.gitconfig"));

	cl_git_pass(git_repository_open(&repo, "empty_standard_repo"));
203
	git_repository__configmap_lookup_cache_clear(repo);
204
	val = -1;
205
	cl_git_pass(git_repository__configmap_lookup(&val, repo, GIT_CONFIGMAP_ABBREV));
206 207 208 209 210 211
	cl_assert_equal_i(7, val);
	git_repository_free(repo);

	cl_assert(!git_path_exists("empty_standard_repo/.git/config"));
	cl_assert(!git_path_exists("alternate/3/.gitconfig"));
}