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