]> git.proxmox.com Git - libgit2.git/blob - tests/repo/config.c
b92d8420221c0374ff008a231ae0d8865f7edd03
[libgit2.git] / tests / repo / config.c
1 #include "clar_libgit2.h"
2 #include "sysdir.h"
3 #include "fileops.h"
4 #include <ctype.h>
5
6 static git_buf path = GIT_BUF_INIT;
7
8 void test_repo_config__initialize(void)
9 {
10 cl_fixture_sandbox("empty_standard_repo");
11 cl_git_pass(cl_rename(
12 "empty_standard_repo/.gitted", "empty_standard_repo/.git"));
13
14 git_buf_clear(&path);
15
16 cl_must_pass(p_mkdir("alternate", 0777));
17 cl_git_pass(git_path_prettify(&path, "alternate", NULL));
18 }
19
20 void test_repo_config__cleanup(void)
21 {
22 cl_sandbox_set_search_path_defaults();
23
24 git_buf_dispose(&path);
25
26 cl_git_pass(
27 git_futils_rmdir_r("alternate", NULL, GIT_RMDIR_REMOVE_FILES));
28 cl_assert(!git_path_isdir("alternate"));
29
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 git_repository *repo;
37 git_config *config, *global;
38
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));
45
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));
50
51 cl_git_pass(git_config_set_string(global, "test.set", "42"));
52
53 git_config_free(global);
54 git_config_free(config);
55 git_repository_free(repo);
56 }
57
58 void test_repo_config__can_open_missing_global_with_separators(void)
59 {
60 git_repository *repo;
61 git_config *config, *global;
62
63 cl_git_pass(git_buf_printf(
64 &path, "%c%s", GIT_PATH_LIST_SEPARATOR, "dummy"));
65
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));
72
73 git_buf_dispose(&path);
74
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));
79
80 cl_git_pass(git_config_set_string(global, "test.set", "42"));
81
82 git_config_free(global);
83 git_config_free(config);
84 git_repository_free(repo);
85 }
86
87 #include "repository.h"
88
89 void test_repo_config__read_with_no_configs_at_all(void)
90 {
91 git_repository *repo;
92 int val;
93
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));
100
101 /* with none */
102
103 cl_must_pass(p_unlink("empty_standard_repo/.git/config"));
104 cl_assert(!git_path_isfile("empty_standard_repo/.git/config"));
105
106 cl_git_pass(git_repository_open(&repo, "empty_standard_repo"));
107 git_repository__cvar_cache_clear(repo);
108 val = -1;
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);
112
113 /* with no local config, just system */
114
115 cl_sandbox_set_search_path_defaults();
116
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));
122
123 cl_git_pass(git_repository_open(&repo, "empty_standard_repo"));
124 git_repository__cvar_cache_clear(repo);
125 val = -1;
126 cl_git_pass(git_repository__cvar(&val, repo, GIT_CVAR_ABBREV));
127 cl_assert_equal_i(10, val);
128 git_repository_free(repo);
129
130 /* with just xdg + system */
131
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));
137
138 cl_git_pass(git_repository_open(&repo, "empty_standard_repo"));
139 git_repository__cvar_cache_clear(repo);
140 val = -1;
141 cl_git_pass(git_repository__cvar(&val, repo, GIT_CVAR_ABBREV));
142 cl_assert_equal_i(20, val);
143 git_repository_free(repo);
144
145 /* with global + xdg + system */
146
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));
152
153 cl_git_pass(git_repository_open(&repo, "empty_standard_repo"));
154 git_repository__cvar_cache_clear(repo);
155 val = -1;
156 cl_git_pass(git_repository__cvar(&val, repo, GIT_CVAR_ABBREV));
157 cl_assert_equal_i(30, val);
158 git_repository_free(repo);
159
160 /* with all configs */
161
162 cl_git_rewritefile("empty_standard_repo/.git/config", "[core]\n\tabbrev = 40\n");
163
164 cl_git_pass(git_repository_open(&repo, "empty_standard_repo"));
165 git_repository__cvar_cache_clear(repo);
166 val = -1;
167 cl_git_pass(git_repository__cvar(&val, repo, GIT_CVAR_ABBREV));
168 cl_assert_equal_i(40, val);
169 git_repository_free(repo);
170
171 /* with all configs but delete the files ? */
172
173 cl_git_pass(git_repository_open(&repo, "empty_standard_repo"));
174 git_repository__cvar_cache_clear(repo);
175 val = -1;
176 cl_git_pass(git_repository__cvar(&val, repo, GIT_CVAR_ABBREV));
177 cl_assert_equal_i(40, val);
178
179 cl_must_pass(p_unlink("empty_standard_repo/.git/config"));
180 cl_assert(!git_path_isfile("empty_standard_repo/.git/config"));
181
182 cl_must_pass(p_unlink("alternate/1/gitconfig"));
183 cl_assert(!git_path_isfile("alternate/1/gitconfig"));
184
185 cl_must_pass(p_unlink("alternate/2/config"));
186 cl_assert(!git_path_isfile("alternate/2/config"));
187
188 cl_must_pass(p_unlink("alternate/3/.gitconfig"));
189 cl_assert(!git_path_isfile("alternate/3/.gitconfig"));
190
191 git_repository__cvar_cache_clear(repo);
192 val = -1;
193 cl_git_pass(git_repository__cvar(&val, repo, GIT_CVAR_ABBREV));
194 cl_assert_equal_i(40, val);
195 git_repository_free(repo);
196
197 /* reopen */
198
199 cl_assert(!git_path_isfile("empty_standard_repo/.git/config"));
200 cl_assert(!git_path_isfile("alternate/3/.gitconfig"));
201
202 cl_git_pass(git_repository_open(&repo, "empty_standard_repo"));
203 git_repository__cvar_cache_clear(repo);
204 val = -1;
205 cl_git_pass(git_repository__cvar(&val, repo, GIT_CVAR_ABBREV));
206 cl_assert_equal_i(7, val);
207 git_repository_free(repo);
208
209 cl_assert(!git_path_exists("empty_standard_repo/.git/config"));
210 cl_assert(!git_path_exists("alternate/3/.gitconfig"));
211 }