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

6
static git_str path = GIT_STR_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
	git_str_clear(&path);
15 16

	cl_must_pass(p_mkdir("alternate", 0777));
17
	cl_git_pass(git_fs_path_prettify(&path, "alternate", NULL));
18 19 20 21
}

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

24
	git_str_dispose(&path);
25 26 27

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

30 31 32
	cl_fixture_cleanup("empty_standard_repo");
}

33
void test_repo_config__can_open_global_when_there_is_no_file(void)
34 35 36 37 38 39 40 41 42 43 44 45 46
{
	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));
47 48
	cl_git_pass(git_config_open_level(
		&global, config, GIT_CONFIG_LEVEL_GLOBAL));
49 50 51 52 53 54 55 56

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

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

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

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

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

72
	git_str_dispose(&path);
73 74 75

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

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

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

#include "repository.h"

88
void test_repo_config__read_with_no_configs_at_all(void)
89 90 91 92 93 94 95 96 97 98 99 100 101 102
{
	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"));
103
	cl_assert(!git_fs_path_isfile("empty_standard_repo/.git/config"));
104 105

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

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

114
	cl_sandbox_set_search_path_defaults();
115 116

	cl_must_pass(p_mkdir("alternate/1", 0777));
117
	cl_git_pass(git_str_joinpath(&path, path.ptr, "1"));
118 119 120 121 122
	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"));
123
	git_repository__configmap_lookup_cache_clear(repo);
124
	val = -1;
125
	cl_git_pass(git_repository__configmap_lookup(&val, repo, GIT_CONFIGMAP_ABBREV));
126 127 128
	cl_assert_equal_i(10, val);
	git_repository_free(repo);

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

	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"));
138
	git_repository__configmap_lookup_cache_clear(repo);
139
	val = -1;
140
	cl_git_pass(git_repository__configmap_lookup(&val, repo, GIT_CONFIGMAP_ABBREV));
141 142 143 144 145 146 147 148 149 150 151 152
	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"));
153
	git_repository__configmap_lookup_cache_clear(repo);
154
	val = -1;
155
	cl_git_pass(git_repository__configmap_lookup(&val, repo, GIT_CONFIGMAP_ABBREV));
156 157 158 159 160 161 162 163
	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"));
164
	git_repository__configmap_lookup_cache_clear(repo);
165
	val = -1;
166
	cl_git_pass(git_repository__configmap_lookup(&val, repo, GIT_CONFIGMAP_ABBREV));
167 168 169 170 171 172
	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"));
173
	git_repository__configmap_lookup_cache_clear(repo);
174
	val = -1;
175
	cl_git_pass(git_repository__configmap_lookup(&val, repo, GIT_CONFIGMAP_ABBREV));
176 177 178
	cl_assert_equal_i(40, val);

	cl_must_pass(p_unlink("empty_standard_repo/.git/config"));
179
	cl_assert(!git_fs_path_isfile("empty_standard_repo/.git/config"));
180 181

	cl_must_pass(p_unlink("alternate/1/gitconfig"));
182
	cl_assert(!git_fs_path_isfile("alternate/1/gitconfig"));
183 184

	cl_must_pass(p_unlink("alternate/2/config"));
185
	cl_assert(!git_fs_path_isfile("alternate/2/config"));
186 187

	cl_must_pass(p_unlink("alternate/3/.gitconfig"));
188
	cl_assert(!git_fs_path_isfile("alternate/3/.gitconfig"));
189

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

	/* reopen */

198 199
	cl_assert(!git_fs_path_isfile("empty_standard_repo/.git/config"));
	cl_assert(!git_fs_path_isfile("alternate/3/.gitconfig"));
200 201

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

208 209
	cl_assert(!git_fs_path_exists("empty_standard_repo/.git/config"));
	cl_assert(!git_fs_path_exists("alternate/3/.gitconfig"));
210
}