]>
git.proxmox.com Git - libgit2.git/blob - tests/core/env.c
1 #include "clar_libgit2.h"
8 static const char *env_vars
[NUM_VARS
] = {
9 "HOME", "HOMEDRIVE", "HOMEPATH", "USERPROFILE", "PROGRAMFILES"
13 static const char *env_vars
[NUM_VARS
] = { "HOME" };
16 static char *env_save
[NUM_VARS
];
18 static char *home_values
[] = {
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 */
30 void test_core_env__initialize(void)
33 for (i
= 0; i
< NUM_VARS
; ++i
)
34 env_save
[i
] = cl_getenv(env_vars
[i
]);
37 static void set_global_search_path_from_env(void)
39 cl_git_pass(git_sysdir_set(GIT_SYSDIR_GLOBAL
, NULL
));
42 static void set_system_search_path_from_env(void)
44 cl_git_pass(git_sysdir_set(GIT_SYSDIR_SYSTEM
, NULL
));
47 void test_core_env__cleanup(void)
52 for (i
= 0; i
< NUM_VARS
; ++i
) {
53 cl_setenv(env_vars
[i
], env_save
[i
]);
54 git__free(env_save
[i
]);
58 /* these will probably have already been cleaned up, but if a test
59 * fails, then it's probably good to try and clear out these dirs
61 for (val
= home_values
; *val
!= NULL
; val
++) {
66 cl_sandbox_set_search_path_defaults();
69 static void setenv_and_check(const char *name
, const char *value
)
73 cl_git_pass(cl_setenv(name
, value
));
74 check
= cl_getenv(name
);
77 cl_assert_equal_s(value
, check
);
79 cl_assert(check
== NULL
);
84 void test_core_env__0(void)
86 git_buf path
= GIT_BUF_INIT
, found
= GIT_BUF_INIT
;
87 char testfile
[16], tidx
= '0';
89 const char *testname
= "testfile";
90 size_t testlen
= strlen(testname
);
92 strncpy(testfile
, testname
, sizeof(testfile
));
93 cl_assert_equal_s(testname
, testfile
);
95 for (val
= home_values
; *val
!= NULL
; val
++) {
97 /* if we can't make the directory, let's just assume
98 * we are on a filesystem that doesn't support the
99 * characters in question and skip this test...
101 if (p_mkdir(*val
, 0777) != 0) {
102 *val
= ""; /* mark as not created */
106 cl_git_pass(git_path_prettify(&path
, *val
, NULL
));
108 /* vary testfile name in each directory so accidentally leaving
109 * an environment variable set from a previous iteration won't
110 * accidentally make this test pass...
112 testfile
[testlen
] = tidx
++;
113 cl_git_pass(git_buf_joinpath(&path
, path
.ptr
, testfile
));
114 cl_git_mkfile(path
.ptr
, "find me");
115 git_buf_rtruncate_at_char(&path
, '/');
118 GIT_ENOTFOUND
, git_sysdir_find_global_file(&found
, testfile
));
120 setenv_and_check("HOME", path
.ptr
);
121 set_global_search_path_from_env();
123 cl_git_pass(git_sysdir_find_global_file(&found
, testfile
));
125 cl_setenv("HOME", env_save
[0]);
126 set_global_search_path_from_env();
129 GIT_ENOTFOUND
, git_sysdir_find_global_file(&found
, testfile
));
132 setenv_and_check("HOMEDRIVE", NULL
);
133 setenv_and_check("HOMEPATH", NULL
);
134 setenv_and_check("USERPROFILE", path
.ptr
);
135 set_global_search_path_from_env();
137 cl_git_pass(git_sysdir_find_global_file(&found
, testfile
));
140 int root
= git_path_root(path
.ptr
);
144 setenv_and_check("USERPROFILE", NULL
);
145 set_global_search_path_from_env();
148 GIT_ENOTFOUND
, git_sysdir_find_global_file(&found
, testfile
));
150 old
= path
.ptr
[root
];
151 path
.ptr
[root
] = '\0';
152 setenv_and_check("HOMEDRIVE", path
.ptr
);
153 path
.ptr
[root
] = old
;
154 setenv_and_check("HOMEPATH", &path
.ptr
[root
]);
155 set_global_search_path_from_env();
157 cl_git_pass(git_sysdir_find_global_file(&found
, testfile
));
166 git_buf_free(&found
);
170 void test_core_env__1(void)
172 git_buf path
= GIT_BUF_INIT
;
175 GIT_ENOTFOUND
, git_sysdir_find_global_file(&path
, "nonexistentfile"));
177 cl_git_pass(cl_setenv("HOME", "doesnotexist"));
179 cl_git_pass(cl_setenv("HOMEPATH", "doesnotexist"));
180 cl_git_pass(cl_setenv("USERPROFILE", "doesnotexist"));
182 set_global_search_path_from_env();
185 GIT_ENOTFOUND
, git_sysdir_find_global_file(&path
, "nonexistentfile"));
187 cl_git_pass(cl_setenv("HOME", NULL
));
189 cl_git_pass(cl_setenv("HOMEPATH", NULL
));
190 cl_git_pass(cl_setenv("USERPROFILE", NULL
));
192 set_global_search_path_from_env();
193 set_system_search_path_from_env();
196 GIT_ENOTFOUND
, git_sysdir_find_global_file(&path
, "nonexistentfile"));
199 GIT_ENOTFOUND
, git_sysdir_find_system_file(&path
, "nonexistentfile"));
202 cl_git_pass(cl_setenv("PROGRAMFILES", NULL
));
203 set_system_search_path_from_env();
206 GIT_ENOTFOUND
, git_sysdir_find_system_file(&path
, "nonexistentfile"));
212 static void check_global_searchpath(
213 const char *path
, int position
, const char *file
, git_buf
*temp
)
215 git_buf out
= GIT_BUF_INIT
;
217 /* build and set new path */
219 cl_git_pass(git_buf_join(temp
, GIT_PATH_LIST_SEPARATOR
, path
, "$PATH"));
220 else if (position
> 0)
221 cl_git_pass(git_buf_join(temp
, GIT_PATH_LIST_SEPARATOR
, "$PATH", path
));
223 cl_git_pass(git_buf_sets(temp
, path
));
225 cl_git_pass(git_libgit2_opts(
226 GIT_OPT_SET_SEARCH_PATH
, GIT_CONFIG_LEVEL_GLOBAL
, temp
->ptr
));
228 /* get path and make sure $PATH expansion worked */
229 cl_git_pass(git_libgit2_opts(
230 GIT_OPT_GET_SEARCH_PATH
, GIT_CONFIG_LEVEL_GLOBAL
, &out
));
233 cl_assert(git__prefixcmp(out
.ptr
, path
) == 0);
234 else if (position
> 0)
235 cl_assert(git__suffixcmp(out
.ptr
, path
) == 0);
237 cl_assert_equal_s(out
.ptr
, path
);
239 /* find file using new path */
240 cl_git_pass(git_sysdir_find_global_file(temp
, file
));
242 /* reset path and confirm file not found */
243 cl_git_pass(git_libgit2_opts(
244 GIT_OPT_SET_SEARCH_PATH
, GIT_CONFIG_LEVEL_GLOBAL
, NULL
));
246 GIT_ENOTFOUND
, git_sysdir_find_global_file(temp
, file
));
251 void test_core_env__2(void)
253 git_buf path
= GIT_BUF_INIT
, found
= GIT_BUF_INIT
;
254 char testfile
[16], tidx
= '0';
256 const char *testname
= "alternate";
257 size_t testlen
= strlen(testname
);
259 strncpy(testfile
, testname
, sizeof(testfile
));
260 cl_assert_equal_s(testname
, testfile
);
262 for (val
= home_values
; *val
!= NULL
; val
++) {
264 /* if we can't make the directory, let's just assume
265 * we are on a filesystem that doesn't support the
266 * characters in question and skip this test...
268 if (p_mkdir(*val
, 0777) != 0 && errno
!= EEXIST
) {
269 *val
= ""; /* mark as not created */
273 cl_git_pass(git_path_prettify(&path
, *val
, NULL
));
275 /* vary testfile name so any sloppiness is resetting variables or
276 * deleting files won't accidentally make a test pass.
278 testfile
[testlen
] = tidx
++;
279 cl_git_pass(git_buf_joinpath(&path
, path
.ptr
, testfile
));
280 cl_git_mkfile(path
.ptr
, "find me");
281 git_buf_rtruncate_at_char(&path
, '/');
283 /* default should be NOTFOUND */
285 GIT_ENOTFOUND
, git_sysdir_find_global_file(&found
, testfile
));
287 /* try plain, append $PATH, and prepend $PATH */
288 check_global_searchpath(path
.ptr
, 0, testfile
, &found
);
289 check_global_searchpath(path
.ptr
, -1, testfile
, &found
);
290 check_global_searchpath(path
.ptr
, 1, testfile
, &found
);
293 cl_git_pass(git_buf_joinpath(&path
, path
.ptr
, testfile
));
294 (void)p_unlink(path
.ptr
);
299 git_buf_free(&found
);
302 void test_core_env__substitution(void)
304 git_buf buf
= GIT_BUF_INIT
, expected
= GIT_BUF_INIT
;
306 /* Set it to something non-default so we have controllable values */
307 cl_git_pass(git_libgit2_opts(GIT_OPT_SET_SEARCH_PATH
, GIT_CONFIG_LEVEL_GLOBAL
, "/tmp/a"));
308 cl_git_pass(git_libgit2_opts(GIT_OPT_GET_SEARCH_PATH
, GIT_CONFIG_LEVEL_GLOBAL
, &buf
));
309 cl_assert_equal_s("/tmp/a", buf
.ptr
);
312 cl_git_pass(git_buf_join(&buf
, GIT_PATH_LIST_SEPARATOR
, "$PATH", "/tmp/b"));
313 cl_git_pass(git_libgit2_opts(GIT_OPT_SET_SEARCH_PATH
, GIT_CONFIG_LEVEL_GLOBAL
, buf
.ptr
));
314 cl_git_pass(git_libgit2_opts(GIT_OPT_GET_SEARCH_PATH
, GIT_CONFIG_LEVEL_GLOBAL
, &buf
));
316 cl_git_pass(git_buf_join(&expected
, GIT_PATH_LIST_SEPARATOR
, "/tmp/a", "/tmp/b"));
317 cl_assert_equal_s(expected
.ptr
, buf
.ptr
);
319 git_buf_free(&expected
);