]> git.proxmox.com Git - libgit2.git/blame - tests/win32/systemdir.c
New upstream version 1.4.3+dfsg.1
[libgit2.git] / tests / win32 / systemdir.c
CommitLineData
e579e0f7
MB
1#include "clar_libgit2.h"
2#include "futils.h"
3#include "sysdir.h"
4#include "win32/findfile.h"
5
6#ifdef GIT_WIN32
7static char *path_save;
8static git_str gfw_path_root = GIT_STR_INIT;
9static git_str gfw_registry_root = GIT_STR_INIT;
10#endif
11
12void test_win32_systemdir__initialize(void)
13{
14#ifdef GIT_WIN32
15 git_str path_env = GIT_STR_INIT;
16
17 path_save = cl_getenv("PATH");
18 git_win32__set_registry_system_dir(L"");
19
20 cl_git_pass(git_str_puts(&path_env, "C:\\GitTempTest\\Foo;\"c:\\program files\\doesnotexisttesttemp\";C:\\fakefakedoesnotexist"));
21 cl_setenv("PATH", path_env.ptr);
22
23 cl_git_pass(git_str_puts(&gfw_path_root, clar_sandbox_path()));
24 cl_git_pass(git_str_puts(&gfw_path_root, "/fake_gfw_path_install"));
25
26 cl_git_pass(git_str_puts(&gfw_registry_root, clar_sandbox_path()));
27 cl_git_pass(git_str_puts(&gfw_registry_root, "/fake_gfw_registry_install"));
28
29 git_str_dispose(&path_env);
30#endif
31}
32
33void test_win32_systemdir__cleanup(void)
34{
35#ifdef GIT_WIN32
36 cl_fixture_cleanup("fake_gfw_path_install");
37 cl_fixture_cleanup("fake_gfw_registry_install");
38 git_str_dispose(&gfw_path_root);
39 git_str_dispose(&gfw_registry_root);
40
41 cl_setenv("PATH", path_save);
42 git__free(path_save);
43 path_save = NULL;
44
45 git_win32__set_registry_system_dir(NULL);
46 cl_sandbox_set_search_path_defaults();
47#endif
48}
49
50#ifdef GIT_WIN32
51static void fix_path(git_str *s)
52{
53 char *c;
54
55 for (c = s->ptr; *c; c++) {
56 if (*c == '/')
57 *c = '\\';
58 }
59}
60
61static void populate_fake_gfw(
62 git_str *expected_etc_dir,
63 const char *root,
64 const char *token,
65 bool create_gitconfig,
66 bool create_mingw64_gitconfig,
67 bool add_to_path,
68 bool add_to_registry)
69{
70 git_str bin_path = GIT_STR_INIT, exe_path = GIT_STR_INIT,
71 etc_path = GIT_STR_INIT, mingw64_path = GIT_STR_INIT,
72 config_path = GIT_STR_INIT, path_env = GIT_STR_INIT,
73 config_data = GIT_STR_INIT;
74
75 cl_git_pass(git_str_puts(&bin_path, root));
76 cl_git_pass(git_str_puts(&bin_path, "/cmd"));
77 cl_git_pass(git_futils_mkdir_r(bin_path.ptr, 0755));
78
79 cl_git_pass(git_str_puts(&exe_path, bin_path.ptr));
80 cl_git_pass(git_str_puts(&exe_path, "/git.cmd"));
81 cl_git_mkfile(exe_path.ptr, "This is a fake executable.");
82
83 cl_git_pass(git_str_puts(&etc_path, root));
84 cl_git_pass(git_str_puts(&etc_path, "/etc"));
85 cl_git_pass(git_futils_mkdir_r(etc_path.ptr, 0755));
86
87 cl_git_pass(git_str_puts(&mingw64_path, root));
88 cl_git_pass(git_str_puts(&mingw64_path, "/mingw64/etc"));
89 cl_git_pass(git_futils_mkdir_r(mingw64_path.ptr, 0755));
90
91 if (create_gitconfig) {
92 git_str_clear(&config_data);
93 git_str_printf(&config_data, "[gfw]\n\ttest = etc %s\n", token);
94
95 cl_git_pass(git_str_puts(&config_path, etc_path.ptr));
96 cl_git_pass(git_str_puts(&config_path, "/gitconfig"));
97 cl_git_mkfile(config_path.ptr, config_data.ptr);
98 }
99
100 if (create_mingw64_gitconfig) {
101 git_str_clear(&config_data);
102 git_str_printf(&config_data, "[gfw]\n\ttest = mingw64 %s\n", token);
103
104 git_str_clear(&config_path);
105 cl_git_pass(git_str_puts(&config_path, mingw64_path.ptr));
106 cl_git_pass(git_str_puts(&config_path, "/gitconfig"));
107 cl_git_mkfile(config_path.ptr, config_data.ptr);
108 }
109
110 if (add_to_path) {
111 fix_path(&bin_path);
112 cl_git_pass(git_str_puts(&path_env, "C:\\GitTempTest\\Foo;\"c:\\program files\\doesnotexisttesttemp\";"));
113 cl_git_pass(git_str_puts(&path_env, bin_path.ptr));
114 cl_git_pass(git_str_puts(&path_env, ";C:\\fakefakedoesnotexist"));
115 cl_setenv("PATH", path_env.ptr);
116 }
117
118 if (add_to_registry) {
119 git_win32_path registry_path;
120 size_t offset = 0;
121
122 cl_assert(git_win32_path_from_utf8(registry_path, root) >= 0);
123 if (wcsncmp(registry_path, L"\\\\?\\", CONST_STRLEN("\\\\?\\")) == 0)
124 offset = CONST_STRLEN("\\\\?\\");
125 git_win32__set_registry_system_dir(registry_path + offset);
126 }
127
128 cl_git_pass(git_str_join(expected_etc_dir, GIT_PATH_LIST_SEPARATOR, expected_etc_dir->ptr, etc_path.ptr));
129 cl_git_pass(git_str_join(expected_etc_dir, GIT_PATH_LIST_SEPARATOR, expected_etc_dir->ptr, mingw64_path.ptr));
130
131 git_str_dispose(&bin_path);
132 git_str_dispose(&exe_path);
133 git_str_dispose(&etc_path);
134 git_str_dispose(&mingw64_path);
135 git_str_dispose(&config_path);
136 git_str_dispose(&path_env);
137 git_str_dispose(&config_data);
138}
139
140static void populate_fake_ecosystem(
141 git_str *expected_etc_dir,
142 bool create_gitconfig,
143 bool create_mingw64_gitconfig,
144 bool path,
145 bool registry)
146{
147 if (path)
148 populate_fake_gfw(expected_etc_dir, gfw_path_root.ptr, "path", create_gitconfig, create_mingw64_gitconfig, true, false);
149
150 if (registry)
151 populate_fake_gfw(expected_etc_dir, gfw_registry_root.ptr, "registry", create_gitconfig, create_mingw64_gitconfig, false, true);
152}
153#endif
154
155void test_win32_systemdir__finds_etc_in_path(void)
156{
157#ifdef GIT_WIN32
158 git_str expected = GIT_STR_INIT, out = GIT_STR_INIT;
159 git_config *cfg;
160 git_buf value = GIT_BUF_INIT;
161
162 populate_fake_ecosystem(&expected, true, false, true, false);
163
164 cl_git_pass(git_win32__find_system_dirs(&out, "etc"));
165 cl_assert_equal_s(out.ptr, expected.ptr);
166
167 git_sysdir_reset();
168
169 cl_git_pass(git_config_open_default(&cfg));
170 cl_git_pass(git_config_get_string_buf(&value, cfg, "gfw.test"));
171 cl_assert_equal_s("etc path", value.ptr);
172
173 git_buf_dispose(&value);
174 git_str_dispose(&expected);
175 git_str_dispose(&out);
176 git_config_free(cfg);
177#endif
178}
179
180void test_win32_systemdir__finds_mingw64_etc_in_path(void)
181{
182#ifdef GIT_WIN32
183 git_str expected = GIT_STR_INIT, out = GIT_STR_INIT;
184 git_config* cfg;
185 git_buf value = GIT_BUF_INIT;
186
187 populate_fake_ecosystem(&expected, false, true, true, false);
188
189 cl_git_pass(git_win32__find_system_dirs(&out, "etc"));
190 cl_assert_equal_s(out.ptr, expected.ptr);
191
192 git_sysdir_reset();
193
194 cl_git_pass(git_config_open_default(&cfg));
195 cl_git_pass(git_config_get_string_buf(&value, cfg, "gfw.test"));
196 cl_assert_equal_s("mingw64 path", value.ptr);
197
198 git_buf_dispose(&value);
199 git_str_dispose(&expected);
200 git_str_dispose(&out);
201 git_config_free(cfg);
202#endif
203}
204
205void test_win32_systemdir__prefers_etc_to_mingw64_in_path(void)
206{
207#ifdef GIT_WIN32
208 git_str expected = GIT_STR_INIT, out = GIT_STR_INIT;
209 git_config* cfg;
210 git_buf value = GIT_BUF_INIT;
211
212 populate_fake_ecosystem(&expected, true, true, true, false);
213
214 cl_git_pass(git_win32__find_system_dirs(&out, "etc"));
215 cl_assert_equal_s(out.ptr, expected.ptr);
216
217 git_sysdir_reset();
218
219 cl_git_pass(git_config_open_default(&cfg));
220 cl_git_pass(git_config_get_string_buf(&value, cfg, "gfw.test"));
221 cl_assert_equal_s("etc path", value.ptr);
222
223 git_buf_dispose(&value);
224 git_str_dispose(&expected);
225 git_str_dispose(&out);
226 git_config_free(cfg);
227#endif
228}
229
230void test_win32_systemdir__finds_etc_in_registry(void)
231{
232#ifdef GIT_WIN32
233 git_str expected = GIT_STR_INIT, out = GIT_STR_INIT;
234 git_config* cfg;
235 git_buf value = GIT_BUF_INIT;
236
237 populate_fake_ecosystem(&expected, true, false, false, true);
238
239 cl_git_pass(git_win32__find_system_dirs(&out, "etc"));
240 cl_assert_equal_s(out.ptr, expected.ptr);
241
242 git_sysdir_reset();
243
244 cl_git_pass(git_config_open_default(&cfg));
245 cl_git_pass(git_config_get_string_buf(&value, cfg, "gfw.test"));
246 cl_assert_equal_s("etc registry", value.ptr);
247
248 git_buf_dispose(&value);
249 git_str_dispose(&expected);
250 git_str_dispose(&out);
251 git_config_free(cfg);
252#endif
253}
254
255void test_win32_systemdir__finds_mingw64_etc_in_registry(void)
256{
257#ifdef GIT_WIN32
258 git_str expected = GIT_STR_INIT, out = GIT_STR_INIT;
259 git_config* cfg;
260 git_buf value = GIT_BUF_INIT;
261
262 populate_fake_ecosystem(&expected, false, true, false, true);
263
264 cl_git_pass(git_win32__find_system_dirs(&out, "etc"));
265 cl_assert_equal_s(out.ptr, expected.ptr);
266
267 git_sysdir_reset();
268
269 cl_git_pass(git_config_open_default(&cfg));
270 cl_git_pass(git_config_get_string_buf(&value, cfg, "gfw.test"));
271 cl_assert_equal_s("mingw64 registry", value.ptr);
272
273 git_buf_dispose(&value);
274 git_str_dispose(&expected);
275 git_str_dispose(&out);
276 git_config_free(cfg);
277#endif
278}
279
280void test_win32_systemdir__prefers_etc_to_mingw64_in_registry(void)
281{
282#ifdef GIT_WIN32
283 git_str expected = GIT_STR_INIT, out = GIT_STR_INIT;
284 git_config* cfg;
285 git_buf value = GIT_BUF_INIT;
286
287 populate_fake_ecosystem(&expected, true, true, false, true);
288
289 cl_git_pass(git_win32__find_system_dirs(&out, "etc"));
290 cl_assert_equal_s(out.ptr, expected.ptr);
291
292 git_sysdir_reset();
293
294 cl_git_pass(git_config_open_default(&cfg));
295 cl_git_pass(git_config_get_string_buf(&value, cfg, "gfw.test"));
296 cl_assert_equal_s("etc registry", value.ptr);
297
298 git_buf_dispose(&value);
299 git_str_dispose(&expected);
300 git_str_dispose(&out);
301 git_config_free(cfg);
302#endif
303}
304
305void test_win32_systemdir__prefers_path_to_registry(void)
306{
307#ifdef GIT_WIN32
308 git_str expected = GIT_STR_INIT, out = GIT_STR_INIT;
309 git_config* cfg;
310 git_buf value = GIT_BUF_INIT;
311
312 populate_fake_ecosystem(&expected, true, true, true, true);
313
314 cl_git_pass(git_win32__find_system_dirs(&out, "etc"));
315 cl_assert_equal_s(out.ptr, expected.ptr);
316
317 git_sysdir_reset();
318
319 cl_git_pass(git_config_open_default(&cfg));
320 cl_git_pass(git_config_get_string_buf(&value, cfg, "gfw.test"));
321 cl_assert_equal_s("etc path", value.ptr);
322
323 git_buf_dispose(&value);
324 git_str_dispose(&expected);
325 git_str_dispose(&out);
326 git_config_free(cfg);
327#endif
328}
329
330void test_win32_systemdir__no_git_installed(void)
331{
332#ifdef GIT_WIN32
333 git_str out = GIT_STR_INIT;
334
335 cl_git_pass(git_win32__find_system_dirs(&out, "etc"));
336 cl_assert_equal_s(out.ptr, "");
337#endif
338}