]>
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 const char *original
= cl_getenv(env_vars
[i
]);
36 env_save
[i
] = (char *)original
;
38 env_save
[i
] = original
? git__strdup(original
) : NULL
;
43 static void set_global_search_path_from_env(void)
45 cl_git_pass(git_sysdir_set(GIT_SYSDIR_GLOBAL
, NULL
));
48 static void set_system_search_path_from_env(void)
50 cl_git_pass(git_sysdir_set(GIT_SYSDIR_SYSTEM
, NULL
));
53 void test_core_env__cleanup(void)
58 for (i
= 0; i
< NUM_VARS
; ++i
) {
59 cl_setenv(env_vars
[i
], env_save
[i
]);
60 git__free(env_save
[i
]);
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
67 for (val
= home_values
; *val
!= NULL
; val
++) {
72 cl_sandbox_set_search_path_defaults();
75 static void setenv_and_check(const char *name
, const char *value
)
79 cl_git_pass(cl_setenv(name
, value
));
81 check
= cl_getenv(name
);
82 cl_assert_equal_s(value
, check
);
88 void test_core_env__0(void)
90 git_buf path
= GIT_BUF_INIT
, found
= GIT_BUF_INIT
;
91 char testfile
[16], tidx
= '0';
93 const char *testname
= "testfile";
94 size_t testlen
= strlen(testname
);
96 strncpy(testfile
, testname
, sizeof(testfile
));
97 cl_assert_equal_s(testname
, testfile
);
99 for (val
= home_values
; *val
!= NULL
; val
++) {
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...
105 if (p_mkdir(*val
, 0777) != 0) {
106 *val
= ""; /* mark as not created */
110 cl_git_pass(git_path_prettify(&path
, *val
, NULL
));
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...
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
, '/');
122 GIT_ENOTFOUND
, git_sysdir_find_global_file(&found
, testfile
));
124 setenv_and_check("HOME", path
.ptr
);
125 set_global_search_path_from_env();
127 cl_git_pass(git_sysdir_find_global_file(&found
, testfile
));
129 cl_setenv("HOME", env_save
[0]);
130 set_global_search_path_from_env();
133 GIT_ENOTFOUND
, git_sysdir_find_global_file(&found
, testfile
));
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();
141 cl_git_pass(git_sysdir_find_global_file(&found
, testfile
));
144 int root
= git_path_root(path
.ptr
);
148 setenv_and_check("USERPROFILE", NULL
);
149 set_global_search_path_from_env();
152 GIT_ENOTFOUND
, git_sysdir_find_global_file(&found
, testfile
));
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();
161 cl_git_pass(git_sysdir_find_global_file(&found
, testfile
));
170 git_buf_free(&found
);
174 void test_core_env__1(void)
176 git_buf path
= GIT_BUF_INIT
;
179 GIT_ENOTFOUND
, git_sysdir_find_global_file(&path
, "nonexistentfile"));
181 cl_git_pass(cl_setenv("HOME", "doesnotexist"));
183 cl_git_pass(cl_setenv("HOMEPATH", "doesnotexist"));
184 cl_git_pass(cl_setenv("USERPROFILE", "doesnotexist"));
186 set_global_search_path_from_env();
189 GIT_ENOTFOUND
, git_sysdir_find_global_file(&path
, "nonexistentfile"));
191 cl_git_pass(cl_setenv("HOME", NULL
));
193 cl_git_pass(cl_setenv("HOMEPATH", NULL
));
194 cl_git_pass(cl_setenv("USERPROFILE", NULL
));
196 set_global_search_path_from_env();
197 set_system_search_path_from_env();
200 GIT_ENOTFOUND
, git_sysdir_find_global_file(&path
, "nonexistentfile"));
203 GIT_ENOTFOUND
, git_sysdir_find_system_file(&path
, "nonexistentfile"));
206 cl_git_pass(cl_setenv("PROGRAMFILES", NULL
));
207 set_system_search_path_from_env();
210 GIT_ENOTFOUND
, git_sysdir_find_system_file(&path
, "nonexistentfile"));
216 static void check_global_searchpath(
217 const char *path
, int position
, const char *file
, git_buf
*temp
)
219 git_buf out
= GIT_BUF_INIT
;
221 /* build and set new path */
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
));
227 cl_git_pass(git_buf_sets(temp
, path
));
229 cl_git_pass(git_libgit2_opts(
230 GIT_OPT_SET_SEARCH_PATH
, GIT_CONFIG_LEVEL_GLOBAL
, temp
->ptr
));
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
));
237 cl_assert(git__prefixcmp(out
.ptr
, path
) == 0);
238 else if (position
> 0)
239 cl_assert(git__suffixcmp(out
.ptr
, path
) == 0);
241 cl_assert_equal_s(out
.ptr
, path
);
243 /* find file using new path */
244 cl_git_pass(git_sysdir_find_global_file(temp
, file
));
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
));
250 GIT_ENOTFOUND
, git_sysdir_find_global_file(temp
, file
));
255 void test_core_env__2(void)
257 git_buf path
= GIT_BUF_INIT
, found
= GIT_BUF_INIT
;
258 char testfile
[16], tidx
= '0';
260 const char *testname
= "alternate";
261 size_t testlen
= strlen(testname
);
263 strncpy(testfile
, testname
, sizeof(testfile
));
264 cl_assert_equal_s(testname
, testfile
);
266 for (val
= home_values
; *val
!= NULL
; val
++) {
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...
272 if (p_mkdir(*val
, 0777) != 0 && errno
!= EEXIST
) {
273 *val
= ""; /* mark as not created */
277 cl_git_pass(git_path_prettify(&path
, *val
, NULL
));
279 /* vary testfile name so any sloppiness is resetting variables or
280 * deleting files won't accidentally make a test pass.
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
, '/');
287 /* default should be NOTFOUND */
289 GIT_ENOTFOUND
, git_sysdir_find_global_file(&found
, testfile
));
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
);
297 cl_git_pass(git_buf_joinpath(&path
, path
.ptr
, testfile
));
298 (void)p_unlink(path
.ptr
);
303 git_buf_free(&found
);