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

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

16 17
static char *env_save[NUM_VARS];

18 19
static char *home_values[] = {
	"fake_home",
20 21 22 23
	"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 */
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 27 28 29
	"\xf0\x9f\x98\x98\xf0\x9f\x98\x82", /* emoticons */
	NULL
};

30 31
void test_core_env__initialize(void)
{
32
	int i;
33 34
	for (i = 0; i < NUM_VARS; ++i)
		env_save[i] = cl_getenv(env_vars[i]);
35 36
}

37
static void set_global_search_path_from_env(void)
38
{
39
	cl_git_pass(git_sysdir_set(GIT_SYSDIR_GLOBAL, NULL));
40 41
}

42
static void set_system_search_path_from_env(void)
43
{
44
	cl_git_pass(git_sysdir_set(GIT_SYSDIR_SYSTEM, NULL));
45 46 47 48 49
}

void test_core_env__cleanup(void)
{
	int i;
50 51
	char **val;

52 53 54
	for (i = 0; i < NUM_VARS; ++i) {
		cl_setenv(env_vars[i], env_save[i]);
		git__free(env_save[i]);
55
		env_save[i] = NULL;
56
	}
57

58 59 60
	/* 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
	 */
61
	for (val = home_values; *val != NULL; val++) {
62 63
		if (**val != '\0')
			(void)p_rmdir(*val);
64
	}
65

66
	cl_sandbox_set_search_path_defaults();
67 68
}

69
static void setenv_and_check(const char *name, const char *value)
70
{
71
	char *check;
72

73 74
	cl_git_pass(cl_setenv(name, value));
	check = cl_getenv(name);
75 76 77 78 79 80

	if (value)
		cl_assert_equal_s(value, check);
	else
		cl_assert(check == NULL);

81
	git__free(check);
82 83
}

84 85 86
void test_core_env__0(void)
{
	git_buf path = GIT_BUF_INIT, found = GIT_BUF_INIT;
87
	char testfile[16], tidx = '0';
88
	char **val;
89 90
	const char *testname = "testfile";
	size_t testlen = strlen(testname);
91

92 93
	strncpy(testfile, testname, sizeof(testfile));
	cl_assert_equal_s(testname, testfile);
94 95 96

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

97 98 99 100
		/* 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...
		 */
101 102
		if (p_mkdir(*val, 0777) != 0) {
			*val = ""; /* mark as not created */
103
			continue;
104
		}
105 106 107 108 109 110 111

		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...
		 */
112
		testfile[testlen] = tidx++;
113 114 115 116
		cl_git_pass(git_buf_joinpath(&path, path.ptr, testfile));
		cl_git_mkfile(path.ptr, "find me");
		git_buf_rtruncate_at_char(&path, '/');

117
		cl_assert_equal_i(
118
			GIT_ENOTFOUND, git_sysdir_find_global_file(&found, testfile));
119 120

		setenv_and_check("HOME", path.ptr);
121
		set_global_search_path_from_env();
122

123
		cl_git_pass(git_sysdir_find_global_file(&found, testfile));
124 125

		cl_setenv("HOME", env_save[0]);
126
		set_global_search_path_from_env();
127

128
		cl_assert_equal_i(
129
			GIT_ENOTFOUND, git_sysdir_find_global_file(&found, testfile));
130 131

#ifdef GIT_WIN32
132 133 134
		setenv_and_check("HOMEDRIVE", NULL);
		setenv_and_check("HOMEPATH", NULL);
		setenv_and_check("USERPROFILE", path.ptr);
135
		set_global_search_path_from_env();
136

137
		cl_git_pass(git_sysdir_find_global_file(&found, testfile));
138

139 140 141
		{
			int root = git_path_root(path.ptr);
			char old;
142

143 144
			if (root >= 0) {
				setenv_and_check("USERPROFILE", NULL);
145
				set_global_search_path_from_env();
146

147
				cl_assert_equal_i(
148
					GIT_ENOTFOUND, git_sysdir_find_global_file(&found, testfile));
149 150 151 152 153 154

				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]);
155
				set_global_search_path_from_env();
156

157
				cl_git_pass(git_sysdir_find_global_file(&found, testfile));
158
			}
159
		}
160
#endif
161 162

		(void)p_rmdir(*val);
163 164
	}

165 166
	git_buf_dispose(&path);
	git_buf_dispose(&found);
167
}
168

169

170 171 172 173
void test_core_env__1(void)
{
	git_buf path = GIT_BUF_INIT;

174
	cl_assert_equal_i(
175
		GIT_ENOTFOUND, git_sysdir_find_global_file(&path, "nonexistentfile"));
176

177
	cl_git_pass(cl_setenv("HOME", "doesnotexist"));
178
#ifdef GIT_WIN32
179
	cl_git_pass(cl_setenv("HOMEPATH", "doesnotexist"));
180 181
	cl_git_pass(cl_setenv("USERPROFILE", "doesnotexist"));
#endif
182
	set_global_search_path_from_env();
183

184
	cl_assert_equal_i(
185
		GIT_ENOTFOUND, git_sysdir_find_global_file(&path, "nonexistentfile"));
186

187
	cl_git_pass(cl_setenv("HOME", NULL));
188
#ifdef GIT_WIN32
189
	cl_git_pass(cl_setenv("HOMEPATH", NULL));
190 191
	cl_git_pass(cl_setenv("USERPROFILE", NULL));
#endif
192 193
	set_global_search_path_from_env();
	set_system_search_path_from_env();
194

195
	cl_assert_equal_i(
196
		GIT_ENOTFOUND, git_sysdir_find_global_file(&path, "nonexistentfile"));
197

198
	cl_assert_equal_i(
199
		GIT_ENOTFOUND, git_sysdir_find_system_file(&path, "nonexistentfile"));
200 201 202

#ifdef GIT_WIN32
	cl_git_pass(cl_setenv("PROGRAMFILES", NULL));
203
	set_system_search_path_from_env();
204

205
	cl_assert_equal_i(
206
		GIT_ENOTFOUND, git_sysdir_find_system_file(&path, "nonexistentfile"));
207
#endif
Carlos Martín Nieto committed
208

209
	git_buf_dispose(&path);
210
}
211

Russell Belfer committed
212 213 214
static void check_global_searchpath(
	const char *path, int position, const char *file, git_buf *temp)
{
215
	git_buf out = GIT_BUF_INIT;
Russell Belfer committed
216 217 218 219 220 221 222 223 224 225 226 227 228 229

	/* 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(
230
		GIT_OPT_GET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, &out));
Russell Belfer committed
231 232

	if (position < 0)
233
		cl_assert(git__prefixcmp(out.ptr, path) == 0);
Russell Belfer committed
234
	else if (position > 0)
235
		cl_assert(git__suffixcmp(out.ptr, path) == 0);
Russell Belfer committed
236
	else
237
		cl_assert_equal_s(out.ptr, path);
Russell Belfer committed
238 239

	/* find file using new path */
240
	cl_git_pass(git_sysdir_find_global_file(temp, file));
Russell Belfer committed
241 242 243 244 245

	/* 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(
246
		GIT_ENOTFOUND, git_sysdir_find_global_file(temp, file));
247

248
	git_buf_dispose(&out);
Russell Belfer committed
249 250
}

251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
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...
		 */
268
		if (p_mkdir(*val, 0777) != 0 && errno != EEXIST) {
269 270 271 272 273 274
			*val = ""; /* mark as not created */
			continue;
		}

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

Russell Belfer committed
275 276
		/* vary testfile name so any sloppiness is resetting variables or
		 * deleting files won't accidentally make a test pass.
277 278 279 280 281 282
		 */
		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, '/');

283
		/* default should be NOTFOUND */
284
		cl_assert_equal_i(
285
			GIT_ENOTFOUND, git_sysdir_find_global_file(&found, testfile));
286

Russell Belfer committed
287 288 289 290
		/* 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);
291

Russell Belfer committed
292
		/* cleanup */
293 294
		cl_git_pass(git_buf_joinpath(&path, path.ptr, testfile));
		(void)p_unlink(path.ptr);
295 296 297
		(void)p_rmdir(*val);
	}

298 299
	git_buf_dispose(&path);
	git_buf_dispose(&found);
300
}
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318

void test_core_env__substitution(void)
{
  git_buf buf = GIT_BUF_INIT, expected = GIT_BUF_INIT;

  /* Set it to something non-default so we have controllable values */
  cl_git_pass(git_libgit2_opts(GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, "/tmp/a"));
  cl_git_pass(git_libgit2_opts(GIT_OPT_GET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, &buf));
  cl_assert_equal_s("/tmp/a", buf.ptr);

  git_buf_clear(&buf);
  cl_git_pass(git_buf_join(&buf, GIT_PATH_LIST_SEPARATOR, "$PATH", "/tmp/b"));
  cl_git_pass(git_libgit2_opts(GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, buf.ptr));
  cl_git_pass(git_libgit2_opts(GIT_OPT_GET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, &buf));

  cl_git_pass(git_buf_join(&expected, GIT_PATH_LIST_SEPARATOR, "/tmp/a", "/tmp/b"));
  cl_assert_equal_s(expected.ptr, buf.ptr);

319 320
  git_buf_dispose(&expected);
  git_buf_dispose(&buf);
321
}