Commit 953a0253 authored by Laszlo Csomor's avatar Laszlo Csomor

io_win32_unittest: make //:win32_test run again

Do not use "googletest.h", apprently that leads to
linking errors on Windows which I couldn't figure
out how to solve, and decided to just go with
plain gTest instead.

See https://github.com/google/protobuf/issues/3951
parent b140cb31
...@@ -48,7 +48,6 @@ ...@@ -48,7 +48,6 @@
#include <google/protobuf/stubs/io_win32.h> #include <google/protobuf/stubs/io_win32.h>
#include <google/protobuf/stubs/scoped_ptr.h> #include <google/protobuf/stubs/scoped_ptr.h>
#include <google/protobuf/testing/googletest.h>
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <memory> #include <memory>
...@@ -89,62 +88,43 @@ void StripTrailingSlashes(string* str) { ...@@ -89,62 +88,43 @@ void StripTrailingSlashes(string* str) {
for (; i >= 0 && ((*str)[i] == '/' || (*str)[i] == '\\'); --i) {} for (; i >= 0 && ((*str)[i] == '/' || (*str)[i] == '\\'); --i) {}
str->resize(i+1); str->resize(i+1);
} }
} // namespace
void IoWin32Test::SetUp() { bool GetEnvVar(const char* name, string* result) {
test_tmpdir = string(TestTempDir()); DWORD size = ::GetEnvironmentVariableA(name, NULL, 0);
wtest_tmpdir.clear(); if (size > 0 && GetLastError() != ERROR_ENVVAR_NOT_FOUND) {
if (test_tmpdir.empty()) { scoped_array<char> str(new char[size]);
const char* test_tmpdir_env = getenv("TEST_TMPDIR"); ::GetEnvironmentVariableA(name, str.get(), size);
if (test_tmpdir_env != NULL && *test_tmpdir_env) { result->assign(str.get());
test_tmpdir = string(test_tmpdir_env); return true;
} } else {
return false;
}
}
// Only Bazel defines TEST_TMPDIR, CMake does not, so look for other } // namespace
// suitable environment variables.
if (test_tmpdir.empty()) {
static const char* names[] = {"TEMP", "TMP"};
for (int i = 0; i < sizeof(names)/sizeof(names[0]); ++i) {
const char* name = names[i];
test_tmpdir_env = getenv(name);
if (test_tmpdir_env != NULL && *test_tmpdir_env) {
test_tmpdir = string(test_tmpdir_env);
break;
}
}
}
// No other temp directory was found. Use the current director void IoWin32Test::SetUp() {
if (test_tmpdir.empty()) { string tmp;
char buffer[MAX_PATH]; bool ok = false;
// Use GetCurrentDirectoryA instead of GetCurrentDirectoryW, because the if (!ok) {
// current working directory must always be shorter than MAX_PATH, even ok = GetEnvVar("TEST_TMPDIR", &tmp);
// with }
// "\\?\" prefix (except on Windows 10 version 1607 and beyond, after if (!ok) {
// opting in to long paths by default [1]). ok = GetEnvVar("TEMP", &tmp);
// }
// [1] https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx#maxpath if (!ok) {
DWORD result = ::GetCurrentDirectoryA(MAX_PATH, buffer); ok = GetEnvVar("TMP", &tmp);
if (result > 0) { }
test_tmpdir = string(buffer); if (!ok || tmp.empty()) {
} else { FAIL();
// Using assertions in SetUp/TearDown seems to confuse the test
// framework, so just leave the member variables empty in case of
// failure.
GOOGLE_CHECK_OK(false);
return;
}
}
} }
StripTrailingSlashes(&test_tmpdir);
test_tmpdir += "\\io_win32_unittest.tmp";
// CreateDirectoryA's limit is 248 chars, see MSDN. StripTrailingSlashes(&tmp);
// https://msdn.microsoft.com/en-us/library/windows/desktop/aa363855(v=vs.85).aspx test_tmpdir = tmp + "\\io_win32_unittest.tmp";
wtest_tmpdir = testonly_path_to_winpath(test_tmpdir); wtest_tmpdir = testonly_path_to_winpath(test_tmpdir);
if (!DeleteAllUnder(wtest_tmpdir) || !CreateAllUnder(wtest_tmpdir)) { if (!DeleteAllUnder(wtest_tmpdir) || !CreateAllUnder(wtest_tmpdir)) {
GOOGLE_CHECK_OK(false); FAIL();
test_tmpdir.clear(); test_tmpdir.clear();
wtest_tmpdir.clear(); wtest_tmpdir.clear();
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment