]>
git.proxmox.com Git - libgit2.git/blob - tests/core/env.c
1 #include "clar_libgit2.h"
7 static const char *env_vars
[NUM_VARS
] = {
8 "HOME", "HOMEDRIVE", "HOMEPATH", "USERPROFILE", "PROGRAMFILES"
12 static const char *env_vars
[NUM_VARS
] = { "HOME" };
15 static char *env_save
[NUM_VARS
];
17 static char *home_values
[] = {
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\x9c\x80ke_\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 */
29 void test_core_env__initialize(void)
32 for (i
= 0; i
< NUM_VARS
; ++i
)
33 env_save
[i
] = cl_getenv(env_vars
[i
]);
36 static void set_global_search_path_from_env(void)
38 cl_git_pass(git_sysdir_set(GIT_SYSDIR_GLOBAL
, NULL
));
41 static void set_system_search_path_from_env(void)
43 cl_git_pass(git_sysdir_set(GIT_SYSDIR_SYSTEM
, NULL
));
46 void test_core_env__cleanup(void)
51 for (i
= 0; i
< NUM_VARS
; ++i
) {
52 cl_setenv(env_vars
[i
], env_save
[i
]);
53 git__free(env_save
[i
]);
57 /* these will probably have already been cleaned up, but if a test
58 * fails, then it's probably good to try and clear out these dirs
60 for (val
= home_values
; *val
!= NULL
; val
++) {
65 cl_sandbox_set_search_path_defaults();
68 static void setenv_and_check(const char *name
, const char *value
)
72 cl_git_pass(cl_setenv(name
, value
));
73 check
= cl_getenv(name
);
76 cl_assert_equal_s(value
, check
);
78 cl_assert(check
== NULL
);
83 void test_core_env__0(void)
85 git_str path
= GIT_STR_INIT
, found
= GIT_STR_INIT
;
86 char testfile
[16], tidx
= '0';
88 const char *testname
= "testfile";
89 size_t testlen
= strlen(testname
);
91 strncpy(testfile
, testname
, sizeof(testfile
));
92 cl_assert_equal_s(testname
, testfile
);
94 for (val
= home_values
; *val
!= NULL
; val
++) {
96 /* if we can't make the directory, let's just assume
97 * we are on a filesystem that doesn't support the
98 * characters in question and skip this test...
100 if (p_mkdir(*val
, 0777) != 0) {
101 *val
= ""; /* mark as not created */
105 cl_git_pass(git_fs_path_prettify(&path
, *val
, NULL
));
107 /* vary testfile name in each directory so accidentally leaving
108 * an environment variable set from a previous iteration won't
109 * accidentally make this test pass...
111 testfile
[testlen
] = tidx
++;
112 cl_git_pass(git_str_joinpath(&path
, path
.ptr
, testfile
));
113 cl_git_mkfile(path
.ptr
, "find me");
114 git_str_rtruncate_at_char(&path
, '/');
117 GIT_ENOTFOUND
, git_sysdir_find_global_file(&found
, testfile
));
119 setenv_and_check("HOME", path
.ptr
);
120 set_global_search_path_from_env();
122 cl_git_pass(git_sysdir_find_global_file(&found
, testfile
));
124 cl_setenv("HOME", env_save
[0]);
125 set_global_search_path_from_env();
128 GIT_ENOTFOUND
, git_sysdir_find_global_file(&found
, testfile
));
131 setenv_and_check("HOMEDRIVE", NULL
);
132 setenv_and_check("HOMEPATH", NULL
);
133 setenv_and_check("USERPROFILE", path
.ptr
);
134 set_global_search_path_from_env();
136 cl_git_pass(git_sysdir_find_global_file(&found
, testfile
));
139 int root
= git_fs_path_root(path
.ptr
);
143 setenv_and_check("USERPROFILE", NULL
);
144 set_global_search_path_from_env();
147 GIT_ENOTFOUND
, git_sysdir_find_global_file(&found
, testfile
));
149 old
= path
.ptr
[root
];
150 path
.ptr
[root
] = '\0';
151 setenv_and_check("HOMEDRIVE", path
.ptr
);
152 path
.ptr
[root
] = old
;
153 setenv_and_check("HOMEPATH", &path
.ptr
[root
]);
154 set_global_search_path_from_env();
156 cl_git_pass(git_sysdir_find_global_file(&found
, testfile
));
164 git_str_dispose(&path
);
165 git_str_dispose(&found
);
169 void test_core_env__1(void)
171 git_str path
= GIT_STR_INIT
;
174 GIT_ENOTFOUND
, git_sysdir_find_global_file(&path
, "nonexistentfile"));
176 cl_git_pass(cl_setenv("HOME", "doesnotexist"));
178 cl_git_pass(cl_setenv("HOMEPATH", "doesnotexist"));
179 cl_git_pass(cl_setenv("USERPROFILE", "doesnotexist"));
181 set_global_search_path_from_env();
184 GIT_ENOTFOUND
, git_sysdir_find_global_file(&path
, "nonexistentfile"));
186 cl_git_pass(cl_setenv("HOME", NULL
));
188 cl_git_pass(cl_setenv("HOMEPATH", NULL
));
189 cl_git_pass(cl_setenv("USERPROFILE", NULL
));
191 set_global_search_path_from_env();
192 set_system_search_path_from_env();
195 GIT_ENOTFOUND
, git_sysdir_find_global_file(&path
, "nonexistentfile"));
198 GIT_ENOTFOUND
, git_sysdir_find_system_file(&path
, "nonexistentfile"));
201 cl_git_pass(cl_setenv("PROGRAMFILES", NULL
));
202 set_system_search_path_from_env();
205 GIT_ENOTFOUND
, git_sysdir_find_system_file(&path
, "nonexistentfile"));
208 git_str_dispose(&path
);
211 static void check_global_searchpath(
212 const char *path
, int position
, const char *file
, git_str
*temp
)
214 git_str out
= GIT_STR_INIT
;
216 /* build and set new path */
218 cl_git_pass(git_str_join(temp
, GIT_PATH_LIST_SEPARATOR
, path
, "$PATH"));
219 else if (position
> 0)
220 cl_git_pass(git_str_join(temp
, GIT_PATH_LIST_SEPARATOR
, "$PATH", path
));
222 cl_git_pass(git_str_sets(temp
, path
));
224 cl_git_pass(git_libgit2_opts(
225 GIT_OPT_SET_SEARCH_PATH
, GIT_CONFIG_LEVEL_GLOBAL
, temp
->ptr
));
227 /* get path and make sure $PATH expansion worked */
228 cl_git_pass(git_libgit2_opts(
229 GIT_OPT_GET_SEARCH_PATH
, GIT_CONFIG_LEVEL_GLOBAL
, &out
));
232 cl_assert(git__prefixcmp(out
.ptr
, path
) == 0);
233 else if (position
> 0)
234 cl_assert(git__suffixcmp(out
.ptr
, path
) == 0);
236 cl_assert_equal_s(out
.ptr
, path
);
238 /* find file using new path */
239 cl_git_pass(git_sysdir_find_global_file(temp
, file
));
241 /* reset path and confirm file not found */
242 cl_git_pass(git_libgit2_opts(
243 GIT_OPT_SET_SEARCH_PATH
, GIT_CONFIG_LEVEL_GLOBAL
, NULL
));
245 GIT_ENOTFOUND
, git_sysdir_find_global_file(temp
, file
));
247 git_str_dispose(&out
);
250 void test_core_env__2(void)
252 git_str path
= GIT_STR_INIT
, found
= GIT_STR_INIT
;
253 char testfile
[16], tidx
= '0';
255 const char *testname
= "alternate";
256 size_t testlen
= strlen(testname
);
258 strncpy(testfile
, testname
, sizeof(testfile
));
259 cl_assert_equal_s(testname
, testfile
);
261 for (val
= home_values
; *val
!= NULL
; val
++) {
263 /* if we can't make the directory, let's just assume
264 * we are on a filesystem that doesn't support the
265 * characters in question and skip this test...
267 if (p_mkdir(*val
, 0777) != 0 && errno
!= EEXIST
) {
268 *val
= ""; /* mark as not created */
272 cl_git_pass(git_fs_path_prettify(&path
, *val
, NULL
));
274 /* vary testfile name so any sloppiness is resetting variables or
275 * deleting files won't accidentally make a test pass.
277 testfile
[testlen
] = tidx
++;
278 cl_git_pass(git_str_joinpath(&path
, path
.ptr
, testfile
));
279 cl_git_mkfile(path
.ptr
, "find me");
280 git_str_rtruncate_at_char(&path
, '/');
282 /* default should be NOTFOUND */
284 GIT_ENOTFOUND
, git_sysdir_find_global_file(&found
, testfile
));
286 /* try plain, append $PATH, and prepend $PATH */
287 check_global_searchpath(path
.ptr
, 0, testfile
, &found
);
288 check_global_searchpath(path
.ptr
, -1, testfile
, &found
);
289 check_global_searchpath(path
.ptr
, 1, testfile
, &found
);
292 cl_git_pass(git_str_joinpath(&path
, path
.ptr
, testfile
));
293 (void)p_unlink(path
.ptr
);
297 git_str_dispose(&path
);
298 git_str_dispose(&found
);
301 void test_core_env__substitution(void)
303 git_str buf
= GIT_STR_INIT
, expected
= GIT_STR_INIT
;
305 /* Set it to something non-default so we have controllable values */
306 cl_git_pass(git_libgit2_opts(GIT_OPT_SET_SEARCH_PATH
, GIT_CONFIG_LEVEL_GLOBAL
, "/tmp/a"));
307 cl_git_pass(git_libgit2_opts(GIT_OPT_GET_SEARCH_PATH
, GIT_CONFIG_LEVEL_GLOBAL
, &buf
));
308 cl_assert_equal_s("/tmp/a", buf
.ptr
);
311 cl_git_pass(git_str_join(&buf
, GIT_PATH_LIST_SEPARATOR
, "$PATH", "/tmp/b"));
312 cl_git_pass(git_libgit2_opts(GIT_OPT_SET_SEARCH_PATH
, GIT_CONFIG_LEVEL_GLOBAL
, buf
.ptr
));
313 cl_git_pass(git_libgit2_opts(GIT_OPT_GET_SEARCH_PATH
, GIT_CONFIG_LEVEL_GLOBAL
, &buf
));
315 cl_git_pass(git_str_join(&expected
, GIT_PATH_LIST_SEPARATOR
, "/tmp/a", "/tmp/b"));
316 cl_assert_equal_s(expected
.ptr
, buf
.ptr
);
318 git_str_dispose(&expected
);
319 git_str_dispose(&buf
);