|
@@ -34,7 +34,7 @@
|
|
//
|
|
//
|
|
// This file is only used on Windows, it's empty on other platforms.
|
|
// This file is only used on Windows, it's empty on other platforms.
|
|
|
|
|
|
-#if defined(_WIN32)
|
|
|
|
|
|
+#if defined(_MSC_VER)
|
|
|
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
#define WIN32_LEAN_AND_MEAN
|
|
#include <errno.h>
|
|
#include <errno.h>
|
|
@@ -47,6 +47,7 @@
|
|
#include <windows.h>
|
|
#include <windows.h>
|
|
|
|
|
|
#include <google/protobuf/stubs/io_win32.h>
|
|
#include <google/protobuf/stubs/io_win32.h>
|
|
|
|
+#include <google/protobuf/stubs/scoped_ptr.h>
|
|
#include <google/protobuf/testing/googletest.h>
|
|
#include <google/protobuf/testing/googletest.h>
|
|
#include <gtest/gtest.h>
|
|
#include <gtest/gtest.h>
|
|
|
|
|
|
@@ -61,13 +62,12 @@ namespace win32 {
|
|
namespace {
|
|
namespace {
|
|
|
|
|
|
using std::string;
|
|
using std::string;
|
|
-using std::unique_ptr;
|
|
|
|
using std::wstring;
|
|
using std::wstring;
|
|
|
|
|
|
class IoWin32Test : public ::testing::Test {
|
|
class IoWin32Test : public ::testing::Test {
|
|
public:
|
|
public:
|
|
- void SetUp() override;
|
|
|
|
- void TearDown() override;
|
|
|
|
|
|
+ void SetUp();
|
|
|
|
+ void TearDown();
|
|
|
|
|
|
protected:
|
|
protected:
|
|
bool CreateAllUnder(wstring path);
|
|
bool CreateAllUnder(wstring path);
|
|
@@ -83,21 +83,31 @@ class IoWin32Test : public ::testing::Test {
|
|
EXPECT_FALSE(wtest_tmpdir.empty()); \
|
|
EXPECT_FALSE(wtest_tmpdir.empty()); \
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+namespace {
|
|
|
|
+void StripTrailingSlashes(string* str) {
|
|
|
|
+ int i = str->size() - 1;
|
|
|
|
+ for (; i >= 0 && ((*str)[i] == '/' || (*str)[i] == '\\'); --i) {}
|
|
|
|
+ str->resize(i+1);
|
|
|
|
+}
|
|
|
|
+} // namespace
|
|
|
|
+
|
|
void IoWin32Test::SetUp() {
|
|
void IoWin32Test::SetUp() {
|
|
test_tmpdir = string(TestTempDir());
|
|
test_tmpdir = string(TestTempDir());
|
|
wtest_tmpdir.clear();
|
|
wtest_tmpdir.clear();
|
|
if (test_tmpdir.empty()) {
|
|
if (test_tmpdir.empty()) {
|
|
const char* test_tmpdir_env = getenv("TEST_TMPDIR");
|
|
const char* test_tmpdir_env = getenv("TEST_TMPDIR");
|
|
- if (test_tmpdir_env != nullptr && *test_tmpdir_env) {
|
|
|
|
|
|
+ if (test_tmpdir_env != NULL && *test_tmpdir_env) {
|
|
test_tmpdir = string(test_tmpdir_env);
|
|
test_tmpdir = string(test_tmpdir_env);
|
|
}
|
|
}
|
|
|
|
|
|
// Only Bazel defines TEST_TMPDIR, CMake does not, so look for other
|
|
// Only Bazel defines TEST_TMPDIR, CMake does not, so look for other
|
|
// suitable environment variables.
|
|
// suitable environment variables.
|
|
if (test_tmpdir.empty()) {
|
|
if (test_tmpdir.empty()) {
|
|
- for (const char* name : {"TEMP", "TMP"}) {
|
|
|
|
|
|
+ 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);
|
|
test_tmpdir_env = getenv(name);
|
|
- if (test_tmpdir_env != nullptr && *test_tmpdir_env) {
|
|
|
|
|
|
+ if (test_tmpdir_env != NULL && *test_tmpdir_env) {
|
|
test_tmpdir = string(test_tmpdir_env);
|
|
test_tmpdir = string(test_tmpdir_env);
|
|
break;
|
|
break;
|
|
}
|
|
}
|
|
@@ -128,9 +138,7 @@ void IoWin32Test::SetUp() {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- while (test_tmpdir.back() == '/' || test_tmpdir.back() == '\\') {
|
|
|
|
- test_tmpdir.pop_back();
|
|
|
|
- }
|
|
|
|
|
|
+ StripTrailingSlashes(&test_tmpdir);
|
|
test_tmpdir += "\\io_win32_unittest.tmp";
|
|
test_tmpdir += "\\io_win32_unittest.tmp";
|
|
|
|
|
|
// CreateDirectoryA's limit is 248 chars, see MSDN.
|
|
// CreateDirectoryA's limit is 248 chars, see MSDN.
|
|
@@ -185,7 +193,7 @@ bool IoWin32Test::DeleteAllUnder(wstring path) {
|
|
path = wstring(L"\\\\?\\") + path;
|
|
path = wstring(L"\\\\?\\") + path;
|
|
}
|
|
}
|
|
// Append "\" if necessary.
|
|
// Append "\" if necessary.
|
|
- if (path.back() != '\\') {
|
|
|
|
|
|
+ if (path[path.size() - 1] != '\\') {
|
|
path.push_back('\\');
|
|
path.push_back('\\');
|
|
}
|
|
}
|
|
|
|
|
|
@@ -326,7 +334,7 @@ TEST_F(IoWin32Test, ChdirTest) {
|
|
|
|
|
|
TEST_F(IoWin32Test, AsWindowsPathTest) {
|
|
TEST_F(IoWin32Test, AsWindowsPathTest) {
|
|
DWORD size = GetCurrentDirectoryW(0, NULL);
|
|
DWORD size = GetCurrentDirectoryW(0, NULL);
|
|
- unique_ptr<wchar_t[]> cwd_str(new wchar_t[size]);
|
|
|
|
|
|
+ scoped_array<wchar_t> cwd_str(new wchar_t[size]);
|
|
EXPECT_GT(GetCurrentDirectoryW(size, cwd_str.get()), 0);
|
|
EXPECT_GT(GetCurrentDirectoryW(size, cwd_str.get()), 0);
|
|
wstring cwd = wstring(L"\\\\?\\") + cwd_str.get();
|
|
wstring cwd = wstring(L"\\\\?\\") + cwd_str.get();
|
|
|
|
|
|
@@ -363,5 +371,5 @@ TEST_F(IoWin32Test, AsWindowsPathTest) {
|
|
} // namespace protobuf
|
|
} // namespace protobuf
|
|
} // namespace google
|
|
} // namespace google
|
|
|
|
|
|
-#endif // defined(_WIN32)
|
|
|
|
|
|
+#endif // defined(_MSC_VER)
|
|
|
|
|