]>
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\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 */
29 void test_core_env__initialize(void)
32 for (i
= 0; i
< NUM_VARS
; ++i
) {
33 const char *original
= cl_getenv(env_vars
[i
]);
35 env_save
[i
] = (char *)original
;
37 env_save
[i
] = original
? git__strdup(original
) : NULL
;
42 static void reset_global_search_path(void)
44 cl_git_pass(git_futils_dirs_set(GIT_FUTILS_DIR_GLOBAL
, NULL
));
47 static void reset_system_search_path(void)
49 cl_git_pass(git_futils_dirs_set(GIT_FUTILS_DIR_SYSTEM
, NULL
));
52 void test_core_env__cleanup(void)
57 for (i
= 0; i
< NUM_VARS
; ++i
) {
58 cl_setenv(env_vars
[i
], env_save
[i
]);
59 git__free(env_save
[i
]);
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
66 for (val
= home_values
; *val
!= NULL
; val
++) {
71 /* reset search paths to default */
72 reset_global_search_path();
73 reset_system_search_path();
76 static void setenv_and_check(const char *name
, const char *value
)
80 cl_git_pass(cl_setenv(name
, value
));
82 check
= cl_getenv(name
);
83 cl_assert_equal_s(value
, check
);
89 void test_core_env__0(void)
91 git_buf path
= GIT_BUF_INIT
, found
= GIT_BUF_INIT
;
92 char testfile
[16], tidx
= '0';
94 const char *testname
= "testfile";
95 size_t testlen
= strlen(testname
);
97 strncpy(testfile
, testname
, sizeof(testfile
));
98 cl_assert_equal_s(testname
, testfile
);
100 for (val
= home_values
; *val
!= NULL
; val
++) {
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...
106 if (p_mkdir(*val
, 0777) != 0) {
107 *val
= ""; /* mark as not created */
111 cl_git_pass(git_path_prettify(&path
, *val
, NULL
));
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...
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
, '/');
123 GIT_ENOTFOUND
, git_futils_find_global_file(&found
, testfile
));
125 setenv_and_check("HOME", path
.ptr
);
126 reset_global_search_path();
128 cl_git_pass(git_futils_find_global_file(&found
, testfile
));
130 cl_setenv("HOME", env_save
[0]);
131 reset_global_search_path();
134 GIT_ENOTFOUND
, git_futils_find_global_file(&found
, testfile
));
137 setenv_and_check("HOMEDRIVE", NULL
);
138 setenv_and_check("HOMEPATH", NULL
);
139 setenv_and_check("USERPROFILE", path
.ptr
);
140 reset_global_search_path();
142 cl_git_pass(git_futils_find_global_file(&found
, testfile
));
145 int root
= git_path_root(path
.ptr
);
149 setenv_and_check("USERPROFILE", NULL
);
150 reset_global_search_path();
153 GIT_ENOTFOUND
, git_futils_find_global_file(&found
, testfile
));
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();
162 cl_git_pass(git_futils_find_global_file(&found
, testfile
));
171 git_buf_free(&found
);
175 void test_core_env__1(void)
177 git_buf path
= GIT_BUF_INIT
;
180 GIT_ENOTFOUND
, git_futils_find_global_file(&path
, "nonexistentfile"));
182 cl_git_pass(cl_setenv("HOME", "doesnotexist"));
184 cl_git_pass(cl_setenv("HOMEPATH", "doesnotexist"));
185 cl_git_pass(cl_setenv("USERPROFILE", "doesnotexist"));
187 reset_global_search_path();
190 GIT_ENOTFOUND
, git_futils_find_global_file(&path
, "nonexistentfile"));
192 cl_git_pass(cl_setenv("HOME", NULL
));
194 cl_git_pass(cl_setenv("HOMEPATH", NULL
));
195 cl_git_pass(cl_setenv("USERPROFILE", NULL
));
197 reset_global_search_path();
198 reset_system_search_path();
201 GIT_ENOTFOUND
, git_futils_find_global_file(&path
, "nonexistentfile"));
204 GIT_ENOTFOUND
, git_futils_find_system_file(&path
, "nonexistentfile"));
207 cl_git_pass(cl_setenv("PROGRAMFILES", NULL
));
208 reset_system_search_path();
211 GIT_ENOTFOUND
, git_futils_find_system_file(&path
, "nonexistentfile"));
217 static void check_global_searchpath(
218 const char *path
, int position
, const char *file
, git_buf
*temp
)
220 char out
[GIT_PATH_MAX
];
222 /* build and set new path */
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
));
228 cl_git_pass(git_buf_sets(temp
, path
));
230 cl_git_pass(git_libgit2_opts(
231 GIT_OPT_SET_SEARCH_PATH
, GIT_CONFIG_LEVEL_GLOBAL
, temp
->ptr
));
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
)));
238 cl_assert(git__prefixcmp(out
, path
) == 0);
239 else if (position
> 0)
240 cl_assert(git__suffixcmp(out
, path
) == 0);
242 cl_assert_equal_s(out
, path
);
244 /* find file using new path */
245 cl_git_pass(git_futils_find_global_file(temp
, file
));
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
));
251 GIT_ENOTFOUND
, git_futils_find_global_file(temp
, file
));
254 void test_core_env__2(void)
256 git_buf path
= GIT_BUF_INIT
, found
= GIT_BUF_INIT
;
257 char testfile
[16], tidx
= '0';
259 const char *testname
= "alternate";
260 size_t testlen
= strlen(testname
);
262 strncpy(testfile
, testname
, sizeof(testfile
));
263 cl_assert_equal_s(testname
, testfile
);
265 for (val
= home_values
; *val
!= NULL
; val
++) {
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...
271 if (p_mkdir(*val
, 0777) != 0 && errno
!= EEXIST
) {
272 *val
= ""; /* mark as not created */
276 cl_git_pass(git_path_prettify(&path
, *val
, NULL
));
278 /* vary testfile name so any sloppiness is resetting variables or
279 * deleting files won't accidentally make a test pass.
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
, '/');
286 /* default should be NOTFOUND */
288 GIT_ENOTFOUND
, git_futils_find_global_file(&found
, testfile
));
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
);
296 cl_git_pass(git_buf_joinpath(&path
, path
.ptr
, testfile
));
297 (void)p_unlink(path
.ptr
);
302 git_buf_free(&found
);