]>
git.proxmox.com Git - libgit2.git/blob - tests/repo/config.c
b92d8420221c0374ff008a231ae0d8865f7edd03
1 #include "clar_libgit2.h"
6 static git_buf path
= GIT_BUF_INIT
;
8 void test_repo_config__initialize(void)
10 cl_fixture_sandbox("empty_standard_repo");
11 cl_git_pass(cl_rename(
12 "empty_standard_repo/.gitted", "empty_standard_repo/.git"));
16 cl_must_pass(p_mkdir("alternate", 0777));
17 cl_git_pass(git_path_prettify(&path
, "alternate", NULL
));
20 void test_repo_config__cleanup(void)
22 cl_sandbox_set_search_path_defaults();
24 git_buf_dispose(&path
);
27 git_futils_rmdir_r("alternate", NULL
, GIT_RMDIR_REMOVE_FILES
));
28 cl_assert(!git_path_isdir("alternate"));
30 cl_fixture_cleanup("empty_standard_repo");
34 void test_repo_config__can_open_global_when_there_is_no_file(void)
37 git_config
*config
, *global
;
39 cl_git_pass(git_libgit2_opts(
40 GIT_OPT_SET_SEARCH_PATH
, GIT_CONFIG_LEVEL_GLOBAL
, path
.ptr
));
41 cl_git_pass(git_libgit2_opts(
42 GIT_OPT_SET_SEARCH_PATH
, GIT_CONFIG_LEVEL_SYSTEM
, path
.ptr
));
43 cl_git_pass(git_libgit2_opts(
44 GIT_OPT_SET_SEARCH_PATH
, GIT_CONFIG_LEVEL_XDG
, path
.ptr
));
46 cl_git_pass(git_repository_open(&repo
, "empty_standard_repo"));
47 cl_git_pass(git_repository_config(&config
, repo
));
48 cl_git_pass(git_config_open_level(
49 &global
, config
, GIT_CONFIG_LEVEL_GLOBAL
));
51 cl_git_pass(git_config_set_string(global
, "test.set", "42"));
53 git_config_free(global
);
54 git_config_free(config
);
55 git_repository_free(repo
);
58 void test_repo_config__can_open_missing_global_with_separators(void)
61 git_config
*config
, *global
;
63 cl_git_pass(git_buf_printf(
64 &path
, "%c%s", GIT_PATH_LIST_SEPARATOR
, "dummy"));
66 cl_git_pass(git_libgit2_opts(
67 GIT_OPT_SET_SEARCH_PATH
, GIT_CONFIG_LEVEL_GLOBAL
, path
.ptr
));
68 cl_git_pass(git_libgit2_opts(
69 GIT_OPT_SET_SEARCH_PATH
, GIT_CONFIG_LEVEL_SYSTEM
, path
.ptr
));
70 cl_git_pass(git_libgit2_opts(
71 GIT_OPT_SET_SEARCH_PATH
, GIT_CONFIG_LEVEL_XDG
, path
.ptr
));
73 git_buf_dispose(&path
);
75 cl_git_pass(git_repository_open(&repo
, "empty_standard_repo"));
76 cl_git_pass(git_repository_config(&config
, repo
));
77 cl_git_pass(git_config_open_level(
78 &global
, config
, GIT_CONFIG_LEVEL_GLOBAL
));
80 cl_git_pass(git_config_set_string(global
, "test.set", "42"));
82 git_config_free(global
);
83 git_config_free(config
);
84 git_repository_free(repo
);
87 #include "repository.h"
89 void test_repo_config__read_with_no_configs_at_all(void)
94 cl_git_pass(git_libgit2_opts(
95 GIT_OPT_SET_SEARCH_PATH
, GIT_CONFIG_LEVEL_GLOBAL
, path
.ptr
));
96 cl_git_pass(git_libgit2_opts(
97 GIT_OPT_SET_SEARCH_PATH
, GIT_CONFIG_LEVEL_SYSTEM
, path
.ptr
));
98 cl_git_pass(git_libgit2_opts(
99 GIT_OPT_SET_SEARCH_PATH
, GIT_CONFIG_LEVEL_XDG
, path
.ptr
));
103 cl_must_pass(p_unlink("empty_standard_repo/.git/config"));
104 cl_assert(!git_path_isfile("empty_standard_repo/.git/config"));
106 cl_git_pass(git_repository_open(&repo
, "empty_standard_repo"));
107 git_repository__cvar_cache_clear(repo
);
109 cl_git_pass(git_repository__cvar(&val
, repo
, GIT_CVAR_ABBREV
));
110 cl_assert_equal_i(GIT_ABBREV_DEFAULT
, val
);
111 git_repository_free(repo
);
113 /* with no local config, just system */
115 cl_sandbox_set_search_path_defaults();
117 cl_must_pass(p_mkdir("alternate/1", 0777));
118 cl_git_pass(git_buf_joinpath(&path
, path
.ptr
, "1"));
119 cl_git_rewritefile("alternate/1/gitconfig", "[core]\n\tabbrev = 10\n");
120 cl_git_pass(git_libgit2_opts(
121 GIT_OPT_SET_SEARCH_PATH
, GIT_CONFIG_LEVEL_SYSTEM
, path
.ptr
));
123 cl_git_pass(git_repository_open(&repo
, "empty_standard_repo"));
124 git_repository__cvar_cache_clear(repo
);
126 cl_git_pass(git_repository__cvar(&val
, repo
, GIT_CVAR_ABBREV
));
127 cl_assert_equal_i(10, val
);
128 git_repository_free(repo
);
130 /* with just xdg + system */
132 cl_must_pass(p_mkdir("alternate/2", 0777));
133 path
.ptr
[path
.size
- 1] = '2';
134 cl_git_rewritefile("alternate/2/config", "[core]\n\tabbrev = 20\n");
135 cl_git_pass(git_libgit2_opts(
136 GIT_OPT_SET_SEARCH_PATH
, GIT_CONFIG_LEVEL_XDG
, path
.ptr
));
138 cl_git_pass(git_repository_open(&repo
, "empty_standard_repo"));
139 git_repository__cvar_cache_clear(repo
);
141 cl_git_pass(git_repository__cvar(&val
, repo
, GIT_CVAR_ABBREV
));
142 cl_assert_equal_i(20, val
);
143 git_repository_free(repo
);
145 /* with global + xdg + system */
147 cl_must_pass(p_mkdir("alternate/3", 0777));
148 path
.ptr
[path
.size
- 1] = '3';
149 cl_git_rewritefile("alternate/3/.gitconfig", "[core]\n\tabbrev = 30\n");
150 cl_git_pass(git_libgit2_opts(
151 GIT_OPT_SET_SEARCH_PATH
, GIT_CONFIG_LEVEL_GLOBAL
, path
.ptr
));
153 cl_git_pass(git_repository_open(&repo
, "empty_standard_repo"));
154 git_repository__cvar_cache_clear(repo
);
156 cl_git_pass(git_repository__cvar(&val
, repo
, GIT_CVAR_ABBREV
));
157 cl_assert_equal_i(30, val
);
158 git_repository_free(repo
);
160 /* with all configs */
162 cl_git_rewritefile("empty_standard_repo/.git/config", "[core]\n\tabbrev = 40\n");
164 cl_git_pass(git_repository_open(&repo
, "empty_standard_repo"));
165 git_repository__cvar_cache_clear(repo
);
167 cl_git_pass(git_repository__cvar(&val
, repo
, GIT_CVAR_ABBREV
));
168 cl_assert_equal_i(40, val
);
169 git_repository_free(repo
);
171 /* with all configs but delete the files ? */
173 cl_git_pass(git_repository_open(&repo
, "empty_standard_repo"));
174 git_repository__cvar_cache_clear(repo
);
176 cl_git_pass(git_repository__cvar(&val
, repo
, GIT_CVAR_ABBREV
));
177 cl_assert_equal_i(40, val
);
179 cl_must_pass(p_unlink("empty_standard_repo/.git/config"));
180 cl_assert(!git_path_isfile("empty_standard_repo/.git/config"));
182 cl_must_pass(p_unlink("alternate/1/gitconfig"));
183 cl_assert(!git_path_isfile("alternate/1/gitconfig"));
185 cl_must_pass(p_unlink("alternate/2/config"));
186 cl_assert(!git_path_isfile("alternate/2/config"));
188 cl_must_pass(p_unlink("alternate/3/.gitconfig"));
189 cl_assert(!git_path_isfile("alternate/3/.gitconfig"));
191 git_repository__cvar_cache_clear(repo
);
193 cl_git_pass(git_repository__cvar(&val
, repo
, GIT_CVAR_ABBREV
));
194 cl_assert_equal_i(40, val
);
195 git_repository_free(repo
);
199 cl_assert(!git_path_isfile("empty_standard_repo/.git/config"));
200 cl_assert(!git_path_isfile("alternate/3/.gitconfig"));
202 cl_git_pass(git_repository_open(&repo
, "empty_standard_repo"));
203 git_repository__cvar_cache_clear(repo
);
205 cl_git_pass(git_repository__cvar(&val
, repo
, GIT_CVAR_ABBREV
));
206 cl_assert_equal_i(7, val
);
207 git_repository_free(repo
);
209 cl_assert(!git_path_exists("empty_standard_repo/.git/config"));
210 cl_assert(!git_path_exists("alternate/3/.gitconfig"));