env.c 8.61 KB
Newer Older
1
#include "clar_libgit2.h"
2
#include "futils.h"
3
#include "sysdir.h"
4 5

#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\x9c\x80ke_\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
	for (i = 0; i < NUM_VARS; ++i)
		env_save[i] = cl_getenv(env_vars[i]);
34 35
}

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

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

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

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

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

65
	cl_sandbox_set_search_path_defaults();
66 67
}

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

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

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

80
	git__free(check);
81 82
}

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

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

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

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

105
		cl_git_pass(git_fs_path_prettify(&path, *val, NULL));
106 107 108 109 110

		/* vary testfile name in each directory so accidentally leaving
		 * an environment variable set from a previous iteration won't
		 * accidentally make this test pass...
		 */
111
		testfile[testlen] = tidx++;
112
		cl_git_pass(git_str_joinpath(&path, path.ptr, testfile));
113
		cl_git_mkfile(path.ptr, "find me");
114
		git_str_rtruncate_at_char(&path, '/');
115

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

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

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

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

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

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

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

138
		{
139
			int root = git_fs_path_root(path.ptr);
140
			char old;
141

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

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

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

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

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

164 165
	git_str_dispose(&path);
	git_str_dispose(&found);
166
}
167

168

169 170
void test_core_env__1(void)
{
171
	git_str path = GIT_STR_INIT;
172

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

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

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

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

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

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

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

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

208
	git_str_dispose(&path);
209
}
210

Russell Belfer committed
211
static void check_global_searchpath(
212
	const char *path, int position, const char *file, git_str *temp)
Russell Belfer committed
213
{
214
	git_str out = GIT_STR_INIT;
Russell Belfer committed
215 216 217

	/* build and set new path */
	if (position < 0)
218
		cl_git_pass(git_str_join(temp, GIT_PATH_LIST_SEPARATOR, path, "$PATH"));
Russell Belfer committed
219
	else if (position > 0)
220
		cl_git_pass(git_str_join(temp, GIT_PATH_LIST_SEPARATOR, "$PATH", path));
Russell Belfer committed
221
	else
222
		cl_git_pass(git_str_sets(temp, path));
Russell Belfer committed
223 224 225 226 227 228

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

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

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

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

247
	git_str_dispose(&out);
Russell Belfer committed
248 249
}

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

272
		cl_git_pass(git_fs_path_prettify(&path, *val, NULL));
273

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

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

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

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

297 298
	git_str_dispose(&path);
	git_str_dispose(&found);
299
}
300 301 302

void test_core_env__substitution(void)
{
303
  git_str buf = GIT_STR_INIT, expected = GIT_STR_INIT;
304 305 306 307 308 309

  /* 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);

310 311
  git_str_clear(&buf);
  cl_git_pass(git_str_join(&buf, GIT_PATH_LIST_SEPARATOR, "$PATH", "/tmp/b"));
312 313 314
  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));

315
  cl_git_pass(git_str_join(&expected, GIT_PATH_LIST_SEPARATOR, "/tmp/a", "/tmp/b"));
316 317
  cl_assert_equal_s(expected.ptr, buf.ptr);

318 319
  git_str_dispose(&expected);
  git_str_dispose(&buf);
320
}