From f42d4247ae1138c6deed50f92dcd1a4f34e07dec Mon Sep 17 00:00:00 2001 From: Rui Ueyama Date: Tue, 10 Sep 2013 19:45:51 +0000 Subject: Add getenv() wrapper that works on multibyte environment variable. On Windows, character encoding of multibyte environment variable varies depending on settings. The only reliable way to handle it I think is to use GetEnvironmentVariableW(). GetEnvironmentVariableW() works on wchar_t string, which is on Windows UTF16 string. That's not ideal because we use UTF-8 as the internal encoding in LLVM. This patch defines a wrapper function which takes and returns UTF-8 string for GetEnvironmentVariableW(). The wrapper function does not do any conversion and just forwards the argument to getenv() on Unix. Differential Revision: http://llvm-reviews.chandlerc.com/D1612 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@190423 91177308-0d34-0410-b5e6-96231b3b80d8 --- unittests/Support/ProcessTest.cpp | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'unittests') diff --git a/unittests/Support/ProcessTest.cpp b/unittests/Support/ProcessTest.cpp index eff9c711a1..ac1b01e88b 100644 --- a/unittests/Support/ProcessTest.cpp +++ b/unittests/Support/ProcessTest.cpp @@ -39,4 +39,32 @@ TEST(ProcessTest, SelfProcess) { EXPECT_GT(TimeValue::MaxTime, process::get_self()->get_wall_time()); } +#ifdef LLVM_ON_WIN32 +#define setenv(name, var, ignore) _putenv_s(name, var) +#endif + +#if HAVE_SETENV || defined(LLVM_ON_WIN32) +TEST(ProcessTest, Basic) { + setenv("__LLVM_TEST_ENVIRON_VAR__", "abc", true); + Optional val(Process::GetEnv("__LLVM_TEST_ENVIRON_VAR__")); + EXPECT_TRUE(val.hasValue()); + EXPECT_STREQ("abc", val->c_str()); +} + +TEST(ProcessTest, None) { + Optional val( + Process::GetEnv("__LLVM_TEST_ENVIRON_NO_SUCH_VAR__")); + EXPECT_FALSE(val.hasValue()); +} +#endif + +#ifdef LLVM_ON_WIN32 +TEST(ProcessTest, Wchar) { + SetEnvironmentVariableW(L"__LLVM_TEST_ENVIRON_VAR__", L"abcdefghijklmnopqrs"); + Optional val(Process::GetEnv("__LLVM_TEST_ENVIRON_VAR__")); + EXPECT_TRUE(val.hasValue()); + EXPECT_STREQ("abcdefghijklmnopqrs", val->c_str()); +} +#endif + } // end anonymous namespace -- cgit v1.2.3