env.c 7.82 KB
Newer Older
1 2 3 4 5
#include "clar_libgit2.h"
#include "fileops.h"
#include "path.h"

#ifdef GIT_WIN32
6 7 8 9
#define NUM_VARS 5
static const char *env_vars[NUM_VARS] = {
	"HOME", "HOMEDRIVE", "HOMEPATH", "USERPROFILE", "PROGRAMFILES"
};
10
#else
11 12
#define NUM_VARS 1
static const char *env_vars[NUM_VARS] = { "HOME" };
13
#endif
14

15 16
static char *env_save[NUM_VARS];

17 18
static char *home_values[] = {
	"fake_home",
19 20 21 22
	"f\xc3\xa1ke_h\xc3\xb5me", /* all in latin-1 supplement */
	"f\xc4\x80ke_\xc4\xa4ome", /* latin extended */
	"f\xce\xb1\xce\xba\xce\xb5_h\xce\xbfm\xce\xad",  /* having fun with greek */
	"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 26 27 28
	"\xf0\x9f\x98\x98\xf0\x9f\x98\x82", /* emoticons */
	NULL
};

29 30
void test_core_env__initialize(void)
{
31
	int i;
32 33 34
	for (i = 0; i < NUM_VARS; ++i) {
		const char *original = cl_getenv(env_vars[i]);
#ifdef GIT_WIN32
Russell Belfer committed
35
		env_save[i] = (char *)original;
36 37 38 39 40 41 42 43 44 45 46 47 48 49
#else
		env_save[i] = original ? git__strdup(original) : NULL;
#endif
	}
}

static void reset_global_search_path(void)
{
	cl_git_pass(git_futils_dirs_set(GIT_FUTILS_DIR_GLOBAL, NULL));
}

static void reset_system_search_path(void)
{
	cl_git_pass(git_futils_dirs_set(GIT_FUTILS_DIR_SYSTEM, NULL));
50 51 52 53 54
}

void test_core_env__cleanup(void)
{
	int i;
55 56
	char **val;

57 58 59
	for (i = 0; i < NUM_VARS; ++i) {
		cl_setenv(env_vars[i], env_save[i]);
		git__free(env_save[i]);
60
		env_save[i] = NULL;
61
	}
62

63 64 65
	/* these will probably have already been cleaned up, but if a test
	 * fails, then it's probably good to try and clear out these dirs
	 */
66
	for (val = home_values; *val != NULL; val++) {
67 68
		if (**val != '\0')
			(void)p_rmdir(*val);
69
	}
70 71

	/* reset search paths to default */
72 73
	reset_global_search_path();
	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 83
	check = cl_getenv(name);
	cl_assert_equal_s(value, check);
Russell Belfer committed
84
#ifdef GIT_WIN32
85
	git__free(check);
Russell Belfer committed
86
#endif
87 88
}

89 90 91
void test_core_env__0(void)
{
	git_buf path = GIT_BUF_INIT, found = GIT_BUF_INIT;
92
	char testfile[16], tidx = '0';
93
	char **val;
94 95
	const char *testname = "testfile";
	size_t testlen = strlen(testname);
96

97 98
	strncpy(testfile, testname, sizeof(testfile));
	cl_assert_equal_s(testname, testfile);
99 100 101

	for (val = home_values; *val != NULL; val++) {

102 103 104 105
		/* if we can't make the directory, let's just assume
		 * we are on a filesystem that doesn't support the
		 * characters in question and skip this test...
		 */
106 107
		if (p_mkdir(*val, 0777) != 0) {
			*val = ""; /* mark as not created */
108
			continue;
109
		}
110 111 112 113 114 115 116

		cl_git_pass(git_path_prettify(&path, *val, NULL));

		/* vary testfile name in each directory so accidentally leaving
		 * an environment variable set from a previous iteration won't
		 * accidentally make this test pass...
		 */
117
		testfile[testlen] = tidx++;
118 119 120 121
		cl_git_pass(git_buf_joinpath(&path, path.ptr, testfile));
		cl_git_mkfile(path.ptr, "find me");
		git_buf_rtruncate_at_char(&path, '/');

122 123
		cl_assert_equal_i(
			GIT_ENOTFOUND, git_futils_find_global_file(&found, testfile));
124 125

		setenv_and_check("HOME", path.ptr);
126 127
		reset_global_search_path();

128 129 130
		cl_git_pass(git_futils_find_global_file(&found, testfile));

		cl_setenv("HOME", env_save[0]);
131 132
		reset_global_search_path();

133 134
		cl_assert_equal_i(
			GIT_ENOTFOUND, git_futils_find_global_file(&found, testfile));
135 136

#ifdef GIT_WIN32
137 138 139
		setenv_and_check("HOMEDRIVE", NULL);
		setenv_and_check("HOMEPATH", NULL);
		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 146
		{
			int root = git_path_root(path.ptr);
			char old;
147

148 149
			if (root >= 0) {
				setenv_and_check("USERPROFILE", NULL);
150
				reset_global_search_path();
151

152 153
				cl_assert_equal_i(
					GIT_ENOTFOUND, git_futils_find_global_file(&found, testfile));
154 155 156 157 158 159

				old = path.ptr[root];
				path.ptr[root] = '\0';
				setenv_and_check("HOMEDRIVE", path.ptr);
				path.ptr[root] = old;
				setenv_and_check("HOMEPATH", &path.ptr[root]);
160
				reset_global_search_path();
161 162 163

				cl_git_pass(git_futils_find_global_file(&found, testfile));
			}
164
		}
165
#endif
166 167

		(void)p_rmdir(*val);
168 169 170 171 172
	}

	git_buf_free(&path);
	git_buf_free(&found);
}
173

174

175 176 177 178
void test_core_env__1(void)
{
	git_buf path = GIT_BUF_INIT;

179 180
	cl_assert_equal_i(
		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 186
	cl_git_pass(cl_setenv("USERPROFILE", "doesnotexist"));
#endif
187
	reset_global_search_path();
188

189 190
	cl_assert_equal_i(
		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 196
	cl_git_pass(cl_setenv("USERPROFILE", NULL));
#endif
197 198
	reset_global_search_path();
	reset_system_search_path();
199

200 201
	cl_assert_equal_i(
		GIT_ENOTFOUND, git_futils_find_global_file(&path, "nonexistentfile"));
202

203 204
	cl_assert_equal_i(
		GIT_ENOTFOUND, git_futils_find_system_file(&path, "nonexistentfile"));
205 206 207

#ifdef GIT_WIN32
	cl_git_pass(cl_setenv("PROGRAMFILES", NULL));
208 209
	reset_system_search_path();

210 211
	cl_assert_equal_i(
		GIT_ENOTFOUND, git_futils_find_system_file(&path, "nonexistentfile"));
212
#endif
Carlos Martín Nieto committed
213 214

	git_buf_free(&path);
215
}
216

Russell Belfer committed
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
static void check_global_searchpath(
	const char *path, int position, const char *file, git_buf *temp)
{
	char out[GIT_PATH_MAX];

	/* build and set new path */
	if (position < 0)
		cl_git_pass(git_buf_join(temp, GIT_PATH_LIST_SEPARATOR, path, "$PATH"));
	else if (position > 0)
		cl_git_pass(git_buf_join(temp, GIT_PATH_LIST_SEPARATOR, "$PATH", path));
	else
		cl_git_pass(git_buf_sets(temp, path));

	cl_git_pass(git_libgit2_opts(
		GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, temp->ptr));

	/* get path and make sure $PATH expansion worked */
	cl_git_pass(git_libgit2_opts(
		GIT_OPT_GET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, out, sizeof(out)));

	if (position < 0)
		cl_assert(git__prefixcmp(out, path) == 0);
	else if (position > 0)
		cl_assert(git__suffixcmp(out, path) == 0);
	else
		cl_assert_equal_s(out, path);

	/* find file using new path */
	cl_git_pass(git_futils_find_global_file(temp, file));

	/* reset path and confirm file not found */
	cl_git_pass(git_libgit2_opts(
		GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, NULL));
	cl_assert_equal_i(
		GIT_ENOTFOUND, git_futils_find_global_file(temp, file));
}

254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
void test_core_env__2(void)
{
	git_buf path = GIT_BUF_INIT, found = GIT_BUF_INIT;
	char testfile[16], tidx = '0';
	char **val;
	const char *testname = "alternate";
	size_t testlen = strlen(testname);

	strncpy(testfile, testname, sizeof(testfile));
	cl_assert_equal_s(testname, testfile);

	for (val = home_values; *val != NULL; val++) {

		/* if we can't make the directory, let's just assume
		 * we are on a filesystem that doesn't support the
		 * characters in question and skip this test...
		 */
271
		if (p_mkdir(*val, 0777) != 0 && errno != EEXIST) {
272 273 274 275 276 277
			*val = ""; /* mark as not created */
			continue;
		}

		cl_git_pass(git_path_prettify(&path, *val, NULL));

Russell Belfer committed
278 279
		/* vary testfile name so any sloppiness is resetting variables or
		 * deleting files won't accidentally make a test pass.
280 281 282 283 284 285
		 */
		testfile[testlen] = tidx++;
		cl_git_pass(git_buf_joinpath(&path, path.ptr, testfile));
		cl_git_mkfile(path.ptr, "find me");
		git_buf_rtruncate_at_char(&path, '/');

286
		/* default should be NOTFOUND */
287 288 289
		cl_assert_equal_i(
			GIT_ENOTFOUND, git_futils_find_global_file(&found, testfile));

Russell Belfer committed
290 291 292 293
		/* try plain, append $PATH, and prepend $PATH */
		check_global_searchpath(path.ptr,  0, testfile, &found);
		check_global_searchpath(path.ptr, -1, testfile, &found);
		check_global_searchpath(path.ptr,  1, testfile, &found);
294

Russell Belfer committed
295
		/* cleanup */
296 297
		cl_git_pass(git_buf_joinpath(&path, path.ptr, testfile));
		(void)p_unlink(path.ptr);
298 299 300 301 302 303
		(void)p_rmdir(*val);
	}

	git_buf_free(&path);
	git_buf_free(&found);
}