]> git.proxmox.com Git - libgit2.git/blob - tests-clar/core/env.c
Merge pull request #727 from libgit2/env-expansion
[libgit2.git] / tests-clar / core / env.c
1 #include "clar_libgit2.h"
2 #include "fileops.h"
3 #include "path.h"
4
5 #ifdef GIT_WIN32
6
7 #include "win32/utf-conv.h"
8
9 static char *cl_getenv(const char *name)
10 {
11 wchar_t *name_utf16 = gitwin_to_utf16(name);
12 DWORD value_len, alloc_len;
13 wchar_t *value_utf16;
14 char *value_utf8;
15
16 cl_assert(name_utf16);
17 alloc_len = GetEnvironmentVariableW(name_utf16, NULL, 0);
18 if (alloc_len <= 0)
19 return NULL;
20
21 cl_assert(value_utf16 = git__calloc(alloc_len, sizeof(wchar_t)));
22
23 value_len = GetEnvironmentVariableW(name_utf16, value_utf16, alloc_len);
24 cl_assert_equal_i(value_len, alloc_len - 1);
25
26 cl_assert(value_utf8 = gitwin_from_utf16(value_utf16));
27
28 git__free(value_utf16);
29
30 return value_utf8;
31 }
32
33 static int cl_setenv(const char *name, const char *value)
34 {
35 wchar_t *name_utf16 = gitwin_to_utf16(name);
36 wchar_t *value_utf16 = value ? gitwin_to_utf16(value) : NULL;
37
38 cl_assert(name_utf16);
39 cl_assert(SetEnvironmentVariableW(name_utf16, value_utf16));
40
41 git__free(name_utf16);
42 git__free(value_utf16);
43
44 return 0;
45
46 }
47 #else
48
49 #include <stdlib.h>
50 #define cl_getenv(n) getenv(n)
51 #define cl_setenv(n,v) (v) ? setenv((n),(v),1) : unsetenv(n)
52
53 #endif
54
55 static char *env_home = NULL;
56 static char *env_userprofile = NULL;
57
58 void test_core_env__initialize(void)
59 {
60 #ifdef GIT_WIN32
61 env_userprofile = cl_getenv("USERPROFILE");
62 #else
63 env_home = cl_getenv("HOME");
64 #endif
65 }
66
67 void test_core_env__cleanup(void)
68 {
69 #ifdef GIT_WIN32
70 cl_setenv("USERPROFILE", env_userprofile);
71 git__free(env_userprofile);
72 #else
73 cl_setenv("HOME", env_home);
74 #endif
75 }
76
77 void test_core_env__0(void)
78 {
79 static char *home_values[] = {
80 "fake_home",
81 "fáke_hõme", /* all in latin-1 supplement */
82 "fĀke_Ĥome", /* latin extended */
83 "fακε_hοmέ", /* having fun with greek */
84 "faงe_นome", /* now I have no idea, but thai characters */
85 "f\xe1\x9cx80ke_\xe1\x9c\x91ome", /* tagalog characters */
86 "\xe1\xb8\x9fẢke_hoṁe", /* latin extended additional */
87 "\xf0\x9f\x98\x98\xf0\x9f\x98\x82", /* emoticons */
88 NULL
89 };
90 git_buf path = GIT_BUF_INIT, found = GIT_BUF_INIT;
91 char **val;
92 char *check;
93
94 for (val = home_values; *val != NULL; val++) {
95
96 if (p_mkdir(*val, 0777) == 0) {
97 /* if we can't make the directory, let's just assume
98 * we are on a filesystem that doesn't support the
99 * characters in question and skip this test...
100 */
101 cl_git_pass(git_path_prettify(&path, *val, NULL));
102
103 #ifdef GIT_WIN32
104 cl_git_pass(cl_setenv("USERPROFILE", path.ptr));
105
106 /* do a quick check that it was set correctly */
107 check = cl_getenv("USERPROFILE");
108 cl_assert_equal_s(path.ptr, check);
109 git__free(check);
110 #else
111 cl_git_pass(cl_setenv("HOME", path.ptr));
112
113 /* do a quick check that it was set correctly */
114 check = cl_getenv("HOME");
115 cl_assert_equal_s(path.ptr, check);
116 #endif
117
118 cl_git_pass(git_buf_puts(&path, "/testfile"));
119 cl_git_mkfile(path.ptr, "find me");
120
121 cl_git_pass(git_futils_find_global_file(&found, "testfile"));
122 }
123 }
124
125 git_buf_free(&path);
126 git_buf_free(&found);
127 }