From 3f1b1a6da5f1a778ccad46b6f340e82ad4c99209 Mon Sep 17 00:00:00 2001 From: Laszlo Csomor Date: Fri, 1 Dec 2017 10:29:34 +0100 Subject: io_win32_unittest: use CWD as last tempdir If the test cannot find a temp directory by checking environment variables, it will fall back to using the current working directory as the temp directory root. This is what the test used to do as of commit https://github.com/google/protobuf/commit/6de51caed52d798815954646b230c5aef3e4d2fc and what was then changed by commit https://github.com/google/protobuf/pull/3978/commits/792d098769d8e000d8d474c8ffd201d2eabc2134 --- src/google/protobuf/stubs/io_win32_unittest.cc | 29 +++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/src/google/protobuf/stubs/io_win32_unittest.cc b/src/google/protobuf/stubs/io_win32_unittest.cc index 7a331872..d277ad6e 100644 --- a/src/google/protobuf/stubs/io_win32_unittest.cc +++ b/src/google/protobuf/stubs/io_win32_unittest.cc @@ -128,6 +128,22 @@ bool GetEnvVarAsUtf8(const WCHAR* name, string* result) { } } +bool GetCwdAsUtf8(string* result) { + DWORD size = ::GetCurrentDirectoryW(0, NULL); + if (size == 0 && GetLastError() == ERROR_INSUFFICIENT_BUFFER) { + scoped_array wcs(new WCHAR[size]); + ::GetCurrentDirectoryW(size, wcs.get()); + // GetCurrentDirectoryA retrieves an Active-Code-Page-encoded text which + // we'd first need to convert to UTF-16 then to UTF-8, because there seems + // to be no API function to do that conversion directly. + // GetCurrentDirectoryW retrieves an UTF-16-encoded text, which we need + // to convert to UTF-8. + return strings::wcs_to_mbs(wcs.get(), result, true); + } else { + return false; + } +} + } // namespace void IoWin32Test::SetUp() { @@ -138,16 +154,23 @@ void IoWin32Test::SetUp() { string tmp; bool ok = false; if (!ok) { + // Bazel sets this environment variable when it runs tests. ok = GetEnvVarAsUtf8(L"TEST_TMPDIR", &tmp); } if (!ok) { + // Bazel 0.8.0 sets this environment for every build and test action. ok = GetEnvVarAsUtf8(L"TEMP", &tmp); } if (!ok) { + // Bazel 0.8.0 sets this environment for every build and test action. ok = GetEnvVarAsUtf8(L"TMP", &tmp); } + if (!ok) { + // Fall back to using the current directory. + ok = GetCwdAsUtf8(&tmp); + } if (!ok || tmp.empty()) { - FAIL(); + FAIL() << "Cannot find a temp directory."; } StripTrailingSlashes(&tmp); @@ -156,8 +179,8 @@ void IoWin32Test::SetUp() { // just deleted the previous temp directory, sometimes we cannot recreate the // same directory. // Use a counter so every test method gets its own temp directory. - static int counter = 0; - result << tmp << "\\io_win32_test" << counter++ << ".tmp"; + static unsigned int counter = 0; + result << tmp << "\\w32tst" << counter++ << ".tmp"; test_tmpdir = result.str(); wtest_tmpdir = testonly_utf8_to_winpath(test_tmpdir.c_str()); ASSERT_FALSE(wtest_tmpdir.empty()); -- cgit v1.2.3