Commit 3f1b1a6d authored by Laszlo Csomor's avatar Laszlo Csomor

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
parent 57a01c7f
......@@ -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<WCHAR> 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());
......
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