]> git.proxmox.com Git - libgit2.git/blob - tests/core/env.c
Merge pull request #2357 from libgit2/cmn/pack-cache-init
[libgit2.git] / tests / core / env.c
1 #include "clar_libgit2.h"
2 #include "fileops.h"
3 #include "sysdir.h"
4 #include "path.h"
5
6 #ifdef GIT_WIN32
7 #define NUM_VARS 5
8 static const char *env_vars[NUM_VARS] = {
9 "HOME", "HOMEDRIVE", "HOMEPATH", "USERPROFILE", "PROGRAMFILES"
10 };
11 #else
12 #define NUM_VARS 1
13 static const char *env_vars[NUM_VARS] = { "HOME" };
14 #endif
15
16 static char *env_save[NUM_VARS];
17
18 static char *home_values[] = {
19 "fake_home",
20 "f\xc3\xa1ke_h\xc3\xb5me", /* all in latin-1 supplement */
21 "f\xc4\x80ke_\xc4\xa4ome", /* latin extended */
22 "f\xce\xb1\xce\xba\xce\xb5_h\xce\xbfm\xce\xad", /* having fun with greek */
23 "fa\xe0" "\xb8" "\x87" "e_\xe0" "\xb8" "\x99" "ome", /* thai characters */
24 "f\xe1\x9c\x80ke_\xe1\x9c\x91ome", /* tagalog characters */
25 "\xe1\xb8\x9f\xe1\xba\xa2" "ke_ho" "\xe1" "\xb9" "\x81" "e", /* latin extended additional */
26 "\xf0\x9f\x98\x98\xf0\x9f\x98\x82", /* emoticons */
27 NULL
28 };
29
30 void test_core_env__initialize(void)
31 {
32 int i;
33 for (i = 0; i < NUM_VARS; ++i) {
34 const char *original = cl_getenv(env_vars[i]);
35 #ifdef GIT_WIN32
36 env_save[i] = (char *)original;
37 #else
38 env_save[i] = original ? git__strdup(original) : NULL;
39 #endif
40 }
41 }
42
43 static void set_global_search_path_from_env(void)
44 {
45 cl_git_pass(git_sysdir_set(GIT_SYSDIR_GLOBAL, NULL));
46 }
47
48 static void set_system_search_path_from_env(void)
49 {
50 cl_git_pass(git_sysdir_set(GIT_SYSDIR_SYSTEM, NULL));
51 }
52
53 void test_core_env__cleanup(void)
54 {
55 int i;
56 char **val;
57
58 for (i = 0; i < NUM_VARS; ++i) {
59 cl_setenv(env_vars[i], env_save[i]);
60 git__free(env_save[i]);
61 env_save[i] = NULL;
62 }
63
64 /* these will probably have already been cleaned up, but if a test
65 * fails, then it's probably good to try and clear out these dirs
66 */
67 for (val = home_values; *val != NULL; val++) {
68 if (**val != '\0')
69 (void)p_rmdir(*val);
70 }
71
72 cl_sandbox_set_search_path_defaults();
73 }
74
75 static void setenv_and_check(const char *name, const char *value)
76 {
77 char *check;
78
79 cl_git_pass(cl_setenv(name, value));
80
81 check = cl_getenv(name);
82 cl_assert_equal_s(value, check);
83 #ifdef GIT_WIN32
84 git__free(check);
85 #endif
86 }
87
88 void test_core_env__0(void)
89 {
90 git_buf path = GIT_BUF_INIT, found = GIT_BUF_INIT;
91 char testfile[16], tidx = '0';
92 char **val;
93 const char *testname = "testfile";
94 size_t testlen = strlen(testname);
95
96 strncpy(testfile, testname, sizeof(testfile));
97 cl_assert_equal_s(testname, testfile);
98
99 for (val = home_values; *val != NULL; val++) {
100
101 /* if we can't make the directory, let's just assume
102 * we are on a filesystem that doesn't support the
103 * characters in question and skip this test...
104 */
105 if (p_mkdir(*val, 0777) != 0) {
106 *val = ""; /* mark as not created */
107 continue;
108 }
109
110 cl_git_pass(git_path_prettify(&path, *val, NULL));
111
112 /* vary testfile name in each directory so accidentally leaving
113 * an environment variable set from a previous iteration won't
114 * accidentally make this test pass...
115 */
116 testfile[testlen] = tidx++;
117 cl_git_pass(git_buf_joinpath(&path, path.ptr, testfile));
118 cl_git_mkfile(path.ptr, "find me");
119 git_buf_rtruncate_at_char(&path, '/');
120
121 cl_assert_equal_i(
122 GIT_ENOTFOUND, git_sysdir_find_global_file(&found, testfile));
123
124 setenv_and_check("HOME", path.ptr);
125 set_global_search_path_from_env();
126
127 cl_git_pass(git_sysdir_find_global_file(&found, testfile));
128
129 cl_setenv("HOME", env_save[0]);
130 set_global_search_path_from_env();
131
132 cl_assert_equal_i(
133 GIT_ENOTFOUND, git_sysdir_find_global_file(&found, testfile));
134
135 #ifdef GIT_WIN32
136 setenv_and_check("HOMEDRIVE", NULL);
137 setenv_and_check("HOMEPATH", NULL);
138 setenv_and_check("USERPROFILE", path.ptr);
139 set_global_search_path_from_env();
140
141 cl_git_pass(git_sysdir_find_global_file(&found, testfile));
142
143 {
144 int root = git_path_root(path.ptr);
145 char old;
146
147 if (root >= 0) {
148 setenv_and_check("USERPROFILE", NULL);
149 set_global_search_path_from_env();
150
151 cl_assert_equal_i(
152 GIT_ENOTFOUND, git_sysdir_find_global_file(&found, testfile));
153
154 old = path.ptr[root];
155 path.ptr[root] = '\0';
156 setenv_and_check("HOMEDRIVE", path.ptr);
157 path.ptr[root] = old;
158 setenv_and_check("HOMEPATH", &path.ptr[root]);
159 set_global_search_path_from_env();
160
161 cl_git_pass(git_sysdir_find_global_file(&found, testfile));
162 }
163 }
164 #endif
165
166 (void)p_rmdir(*val);
167 }
168
169 git_buf_free(&path);
170 git_buf_free(&found);
171 }
172
173
174 void test_core_env__1(void)
175 {
176 git_buf path = GIT_BUF_INIT;
177
178 cl_assert_equal_i(
179 GIT_ENOTFOUND, git_sysdir_find_global_file(&path, "nonexistentfile"));
180
181 cl_git_pass(cl_setenv("HOME", "doesnotexist"));
182 #ifdef GIT_WIN32
183 cl_git_pass(cl_setenv("HOMEPATH", "doesnotexist"));
184 cl_git_pass(cl_setenv("USERPROFILE", "doesnotexist"));
185 #endif
186 set_global_search_path_from_env();
187
188 cl_assert_equal_i(
189 GIT_ENOTFOUND, git_sysdir_find_global_file(&path, "nonexistentfile"));
190
191 cl_git_pass(cl_setenv("HOME", NULL));
192 #ifdef GIT_WIN32
193 cl_git_pass(cl_setenv("HOMEPATH", NULL));
194 cl_git_pass(cl_setenv("USERPROFILE", NULL));
195 #endif
196 set_global_search_path_from_env();
197 set_system_search_path_from_env();
198
199 cl_assert_equal_i(
200 GIT_ENOTFOUND, git_sysdir_find_global_file(&path, "nonexistentfile"));
201
202 cl_assert_equal_i(
203 GIT_ENOTFOUND, git_sysdir_find_system_file(&path, "nonexistentfile"));
204
205 #ifdef GIT_WIN32
206 cl_git_pass(cl_setenv("PROGRAMFILES", NULL));
207 set_system_search_path_from_env();
208
209 cl_assert_equal_i(
210 GIT_ENOTFOUND, git_sysdir_find_system_file(&path, "nonexistentfile"));
211 #endif
212
213 git_buf_free(&path);
214 }
215
216 static void check_global_searchpath(
217 const char *path, int position, const char *file, git_buf *temp)
218 {
219 git_buf out = GIT_BUF_INIT;
220
221 /* build and set new path */
222 if (position < 0)
223 cl_git_pass(git_buf_join(temp, GIT_PATH_LIST_SEPARATOR, path, "$PATH"));
224 else if (position > 0)
225 cl_git_pass(git_buf_join(temp, GIT_PATH_LIST_SEPARATOR, "$PATH", path));
226 else
227 cl_git_pass(git_buf_sets(temp, path));
228
229 cl_git_pass(git_libgit2_opts(
230 GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, temp->ptr));
231
232 /* get path and make sure $PATH expansion worked */
233 cl_git_pass(git_libgit2_opts(
234 GIT_OPT_GET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, &out));
235
236 if (position < 0)
237 cl_assert(git__prefixcmp(out.ptr, path) == 0);
238 else if (position > 0)
239 cl_assert(git__suffixcmp(out.ptr, path) == 0);
240 else
241 cl_assert_equal_s(out.ptr, path);
242
243 /* find file using new path */
244 cl_git_pass(git_sysdir_find_global_file(temp, file));
245
246 /* reset path and confirm file not found */
247 cl_git_pass(git_libgit2_opts(
248 GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, NULL));
249 cl_assert_equal_i(
250 GIT_ENOTFOUND, git_sysdir_find_global_file(temp, file));
251
252 git_buf_free(&out);
253 }
254
255 void test_core_env__2(void)
256 {
257 git_buf path = GIT_BUF_INIT, found = GIT_BUF_INIT;
258 char testfile[16], tidx = '0';
259 char **val;
260 const char *testname = "alternate";
261 size_t testlen = strlen(testname);
262
263 strncpy(testfile, testname, sizeof(testfile));
264 cl_assert_equal_s(testname, testfile);
265
266 for (val = home_values; *val != NULL; val++) {
267
268 /* if we can't make the directory, let's just assume
269 * we are on a filesystem that doesn't support the
270 * characters in question and skip this test...
271 */
272 if (p_mkdir(*val, 0777) != 0 && errno != EEXIST) {
273 *val = ""; /* mark as not created */
274 continue;
275 }
276
277 cl_git_pass(git_path_prettify(&path, *val, NULL));
278
279 /* vary testfile name so any sloppiness is resetting variables or
280 * deleting files won't accidentally make a test pass.
281 */
282 testfile[testlen] = tidx++;
283 cl_git_pass(git_buf_joinpath(&path, path.ptr, testfile));
284 cl_git_mkfile(path.ptr, "find me");
285 git_buf_rtruncate_at_char(&path, '/');
286
287 /* default should be NOTFOUND */
288 cl_assert_equal_i(
289 GIT_ENOTFOUND, git_sysdir_find_global_file(&found, testfile));
290
291 /* try plain, append $PATH, and prepend $PATH */
292 check_global_searchpath(path.ptr, 0, testfile, &found);
293 check_global_searchpath(path.ptr, -1, testfile, &found);
294 check_global_searchpath(path.ptr, 1, testfile, &found);
295
296 /* cleanup */
297 cl_git_pass(git_buf_joinpath(&path, path.ptr, testfile));
298 (void)p_unlink(path.ptr);
299 (void)p_rmdir(*val);
300 }
301
302 git_buf_free(&path);
303 git_buf_free(&found);
304 }