env.c 4.11 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
void test_core_env__initialize(void)
{
19 20 21 22 23 24 25 26 27 28
	int i;
	for (i = 0; i < NUM_VARS; ++i)
		env_save[i] = cl_getenv(env_vars[i]);
}

void test_core_env__cleanup(void)
{
	int i;
	for (i = 0; i < NUM_VARS; ++i) {
		cl_setenv(env_vars[i], env_save[i]);
29
#ifdef GIT_WIN32
30
		git__free(env_save[i]);
31
#endif
32
		env_save[i] = NULL;
33
	}
34 35
}

36
static void setenv_and_check(const char *name, const char *value)
37
{
38 39 40 41
	char *check;
	cl_git_pass(cl_setenv(name, value));
	check = cl_getenv(name);
	cl_assert_equal_s(value, check);
42
#ifdef GIT_WIN32
43
	git__free(check);
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
#endif
}

void test_core_env__0(void)
{
	static char *home_values[] = {
		"fake_home",
		"fáke_hõme", /* all in latin-1 supplement */
		"fĀke_Ĥome", /* latin extended */
		"fακε_hοmέ",  /* having fun with greek */
		"faงe_นome", /* now I have no idea, but thai characters */
		"f\xe1\x9cx80ke_\xe1\x9c\x91ome", /* tagalog characters */
		"\xe1\xb8\x9fẢke_hoṁe", /* latin extended additional */
		"\xf0\x9f\x98\x98\xf0\x9f\x98\x82", /* emoticons */
		NULL
	};
	git_buf path = GIT_BUF_INIT, found = GIT_BUF_INIT;
61
	char testfile[16], tidx = '0';
62
	char **val;
63 64 65 66

	memset(testfile, 0, sizeof(testfile));
	memcpy(testfile, "testfile", 8);
	cl_assert_equal_s("testfile", testfile);
67 68 69

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

70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
		/* 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...
		 */
		if (p_mkdir(*val, 0777) != 0)
			continue;

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

88 89
		cl_assert_equal_i(
			GIT_ENOTFOUND, git_futils_find_global_file(&found, testfile));
90 91 92 93 94

		setenv_and_check("HOME", path.ptr);
		cl_git_pass(git_futils_find_global_file(&found, testfile));

		cl_setenv("HOME", env_save[0]);
95 96
		cl_assert_equal_i(
			GIT_ENOTFOUND, git_futils_find_global_file(&found, testfile));
97 98

#ifdef GIT_WIN32
99 100 101
		setenv_and_check("HOMEDRIVE", NULL);
		setenv_and_check("HOMEPATH", NULL);
		setenv_and_check("USERPROFILE", path.ptr);
102

103
		cl_git_pass(git_futils_find_global_file(&found, testfile));
104

105 106 107
		{
			int root = git_path_root(path.ptr);
			char old;
108

109 110
			if (root >= 0) {
				setenv_and_check("USERPROFILE", NULL);
111

112 113
				cl_assert_equal_i(
					GIT_ENOTFOUND, git_futils_find_global_file(&found, testfile));
114 115 116 117 118 119 120 121 122

				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]);

				cl_git_pass(git_futils_find_global_file(&found, testfile));
			}
123
		}
124
#endif
125 126 127 128 129
	}

	git_buf_free(&path);
	git_buf_free(&found);
}
130 131 132 133 134

void test_core_env__1(void)
{
	git_buf path = GIT_BUF_INIT;

135 136
	cl_assert_equal_i(
		GIT_ENOTFOUND, git_futils_find_global_file(&path, "nonexistentfile"));
137

138
	cl_git_pass(cl_setenv("HOME", "doesnotexist"));
139
#ifdef GIT_WIN32
140
	cl_git_pass(cl_setenv("HOMEPATH", "doesnotexist"));
141 142 143
	cl_git_pass(cl_setenv("USERPROFILE", "doesnotexist"));
#endif

144 145
	cl_assert_equal_i(
		GIT_ENOTFOUND, git_futils_find_global_file(&path, "nonexistentfile"));
146

147
	cl_git_pass(cl_setenv("HOME", NULL));
148
#ifdef GIT_WIN32
149
	cl_git_pass(cl_setenv("HOMEPATH", NULL));
150 151 152
	cl_git_pass(cl_setenv("USERPROFILE", NULL));
#endif

153 154
	cl_assert_equal_i(
		GIT_ENOTFOUND, git_futils_find_global_file(&path, "nonexistentfile"));
155

156 157
	cl_assert_equal_i(
		GIT_ENOTFOUND, git_futils_find_system_file(&path, "nonexistentfile"));
158 159 160

#ifdef GIT_WIN32
	cl_git_pass(cl_setenv("PROGRAMFILES", NULL));
161 162
	cl_assert_equal_i(
		GIT_ENOTFOUND, git_futils_find_system_file(&path, "nonexistentfile"));
163
#endif
Carlos Martín Nieto committed
164 165

	git_buf_free(&path);
166
}