]> git.proxmox.com Git - libgit2.git/blob - tests/repo/config.c
37f6b521d820644f3cb548d299ddc2681b88a11d
[libgit2.git] / tests / repo / config.c
1 #include "clar_libgit2.h"
2 #include "sysdir.h"
3 #include "futils.h"
4 #include <ctype.h>
5
6 static git_str path = GIT_STR_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_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 void test_repo_config__cleanup(void)
21 {
22 cl_sandbox_set_search_path_defaults();
23
24 git_str_dispose(&path);
25
26 cl_git_pass(
27 git_futils_rmdir_r("alternate", NULL, GIT_RMDIR_REMOVE_FILES));
28 cl_assert(!git_fs_path_isdir("alternate"));
29
30 cl_fixture_cleanup("empty_standard_repo");
31 }
32
33 void test_repo_config__can_open_global_when_there_is_no_file(void)
34 {
35 git_repository *repo;
36 git_config *config, *global;
37
38 cl_git_pass(git_libgit2_opts(
39 GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, path.ptr));
40 cl_git_pass(git_libgit2_opts(
41 GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_SYSTEM, path.ptr));
42 cl_git_pass(git_libgit2_opts(
43 GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_XDG, path.ptr));
44
45 cl_git_pass(git_repository_open(&repo, "empty_standard_repo"));
46 cl_git_pass(git_repository_config(&config, repo));
47 cl_git_pass(git_config_open_level(
48 &global, config, GIT_CONFIG_LEVEL_GLOBAL));
49
50 cl_git_pass(git_config_set_string(global, "test.set", "42"));
51
52 git_config_free(global);
53 git_config_free(config);
54 git_repository_free(repo);
55 }
56
57 void test_repo_config__can_open_missing_global_with_separators(void)
58 {
59 git_repository *repo;
60 git_config *config, *global;
61
62 cl_git_pass(git_str_printf(
63 &path, "%c%s", GIT_PATH_LIST_SEPARATOR, "dummy"));
64
65 cl_git_pass(git_libgit2_opts(
66 GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, path.ptr));
67 cl_git_pass(git_libgit2_opts(
68 GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_SYSTEM, path.ptr));
69 cl_git_pass(git_libgit2_opts(
70 GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_XDG, path.ptr));
71
72 git_str_dispose(&path);
73
74 cl_git_pass(git_repository_open(&repo, "empty_standard_repo"));
75 cl_git_pass(git_repository_config(&config, repo));
76 cl_git_pass(git_config_open_level(
77 &global, config, GIT_CONFIG_LEVEL_GLOBAL));
78
79 cl_git_pass(git_config_set_string(global, "test.set", "42"));
80
81 git_config_free(global);
82 git_config_free(config);
83 git_repository_free(repo);
84 }
85
86 #include "repository.h"
87
88 void test_repo_config__read_with_no_configs_at_all(void)
89 {
90 git_repository *repo;
91 int val;
92
93 cl_git_pass(git_libgit2_opts(
94 GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, path.ptr));
95 cl_git_pass(git_libgit2_opts(
96 GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_SYSTEM, path.ptr));
97 cl_git_pass(git_libgit2_opts(
98 GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_XDG, path.ptr));
99
100 /* with none */
101
102 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 cl_assert_equal_i(GIT_ABBREV_DEFAULT, val);
110 git_repository_free(repo);
111
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 cl_git_rewritefile("alternate/1/gitconfig", "[core]\n\tabbrev = 10\n");
119 cl_git_pass(git_libgit2_opts(
120 GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_SYSTEM, path.ptr));
121
122 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 cl_assert_equal_i(10, val);
127 git_repository_free(repo);
128
129 /* with just xdg + system */
130
131 cl_must_pass(p_mkdir("alternate/2", 0777));
132 path.ptr[path.size - 1] = '2';
133 cl_git_rewritefile("alternate/2/config", "[core]\n\tabbrev = 20\n");
134 cl_git_pass(git_libgit2_opts(
135 GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_XDG, path.ptr));
136
137 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 cl_assert_equal_i(20, val);
142 git_repository_free(repo);
143
144 /* with global + xdg + system */
145
146 cl_must_pass(p_mkdir("alternate/3", 0777));
147 path.ptr[path.size - 1] = '3';
148 cl_git_rewritefile("alternate/3/.gitconfig", "[core]\n\tabbrev = 30\n");
149 cl_git_pass(git_libgit2_opts(
150 GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, path.ptr));
151
152 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 cl_assert_equal_i(30, val);
157 git_repository_free(repo);
158
159 /* with all configs */
160
161 cl_git_rewritefile("empty_standard_repo/.git/config", "[core]\n\tabbrev = 40\n");
162
163 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 cl_assert_equal_i(40, val);
168 git_repository_free(repo);
169
170 /* with all configs but delete the files ? */
171
172 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 cl_assert_equal_i(40, val);
177
178 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 cl_assert_equal_i(40, val);
194 git_repository_free(repo);
195
196 /* reopen */
197
198 cl_assert(!git_fs_path_isfile("empty_standard_repo/.git/config"));
199 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 cl_assert_equal_i(7, val);
206 git_repository_free(repo);
207
208 cl_assert(!git_fs_path_exists("empty_standard_repo/.git/config"));
209 cl_assert(!git_fs_path_exists("alternate/3/.gitconfig"));
210 }