Simplifies implementation by defining a POSIX portability layer; adds the death test style flag to --help.

This commit is contained in:
zhanyong.wan
2009-03-26 19:03:47 +00:00
parent f3c6efd8d7
commit 3c7bbf5b46
12 changed files with 184 additions and 268 deletions

View File

@@ -60,8 +60,9 @@
#include "src/gtest-internal-inl.h"
#undef GTEST_IMPLEMENTATION_
using testing::Message;
namespace posix = ::testing::internal::posix;
using testing::Message;
using testing::internal::DeathTest;
using testing::internal::DeathTestFactory;
using testing::internal::FilePath;
@@ -105,11 +106,7 @@ class TestForDeathTest : public testing::Test {
TestForDeathTest() : original_dir_(FilePath::GetCurrentDir()) {}
virtual ~TestForDeathTest() {
#if GTEST_OS_WINDOWS
_chdir(original_dir_.c_str());
#else
chdir(original_dir_.c_str());
#endif
posix::chdir(original_dir_.c_str());
}
// A static member function that's expected to die.
@@ -348,13 +345,7 @@ TEST_F(TestForDeathTest, MemberFunctionFastStyle) {
EXPECT_DEATH(MemberFunction(), "inside.*MemberFunction");
}
void ChangeToRootDir() {
#if GTEST_OS_WINDOWS
_chdir("\\");
#else
chdir("/");
#endif // GTEST_OS_WINDOWS
}
void ChangeToRootDir() { posix::chdir(GTEST_PATH_SEP_); }
// Tests that death tests work even if the current directory has been
// changed.

View File

@@ -86,18 +86,17 @@ TEST(GetCurrentDirTest, ReturnsCurrentDir) {
const FilePath original_dir = FilePath::GetCurrentDir();
EXPECT_FALSE(original_dir.IsEmpty());
#if GTEST_OS_WINDOWS
_chdir(GTEST_PATH_SEP_);
posix::chdir(GTEST_PATH_SEP_);
const FilePath cwd = FilePath::GetCurrentDir();
_chdir(original_dir.c_str());
posix::chdir(original_dir.c_str());
#if GTEST_OS_WINDOWS
// Skips the ":".
const char* const cwd_without_drive = strchr(cwd.c_str(), ':');
ASSERT_TRUE(cwd_without_drive != NULL);
EXPECT_STREQ(GTEST_PATH_SEP_, cwd_without_drive + 1);
#else
chdir(GTEST_PATH_SEP_);
EXPECT_STREQ(GTEST_PATH_SEP_, FilePath::GetCurrentDir().c_str());
chdir(original_dir.c_str());
EXPECT_STREQ(GTEST_PATH_SEP_, cwd.c_str());
#endif
}
@@ -436,22 +435,14 @@ class DirectoryCreationTest : public Test {
remove(testdata_file_.c_str());
remove(unique_file0_.c_str());
remove(unique_file1_.c_str());
#if GTEST_OS_WINDOWS
_rmdir(testdata_path_.c_str());
#else
rmdir(testdata_path_.c_str());
#endif // GTEST_OS_WINDOWS
posix::rmdir(testdata_path_.c_str());
}
virtual void TearDown() {
remove(testdata_file_.c_str());
remove(unique_file0_.c_str());
remove(unique_file1_.c_str());
#if GTEST_OS_WINDOWS
_rmdir(testdata_path_.c_str());
#else
rmdir(testdata_path_.c_str());
#endif // GTEST_OS_WINDOWS
posix::rmdir(testdata_path_.c_str());
}
String TempDir() const {
@@ -459,13 +450,7 @@ class DirectoryCreationTest : public Test {
return String("\\temp\\");
#elif GTEST_OS_WINDOWS
// MSVC 8 deprecates getenv(), so we want to suppress warning 4996
// (deprecated function) there.
#pragma warning(push) // Saves the current warning state.
#pragma warning(disable:4996) // Temporarily disables warning 4996.
const char* temp_dir = getenv("TEMP");
#pragma warning(pop) // Restores the warning state.
const char* temp_dir = posix::getenv("TEMP");
if (temp_dir == NULL || temp_dir[0] == '\0')
return String("\\temp\\");
else if (String(temp_dir).EndsWith("\\"))
@@ -478,16 +463,7 @@ class DirectoryCreationTest : public Test {
}
void CreateTextFile(const char* filename) {
#if GTEST_OS_WINDOWS
// MSVC 8 deprecates fopen(), so we want to suppress warning 4996
// (deprecated function) there.#pragma warning(push)
#pragma warning(push) // Saves the current warning state.
#pragma warning(disable:4996) // Temporarily disables warning 4996.
FILE* f = fopen(filename, "w");
#pragma warning(pop) // Restores the warning state.
#else // We are on Linux or Mac OS.
FILE* f = fopen(filename, "w");
#endif // GTEST_OS_WINDOWS
FILE* f = posix::fopen(filename, "w");
fprintf(f, "text\n");
fclose(f);
}

View File

@@ -150,22 +150,14 @@ class XmlOutputChangeDirTest : public Test {
protected:
virtual void SetUp() {
original_working_dir_ = FilePath::GetCurrentDir();
ChDir("..");
posix::chdir("..");
// This will make the test fail if run from the root directory.
EXPECT_STRNE(original_working_dir_.c_str(),
FilePath::GetCurrentDir().c_str());
}
virtual void TearDown() {
ChDir(original_working_dir_.c_str());
}
void ChDir(const char* dir) {
#if GTEST_OS_WINDOWS
_chdir(dir);
#else
chdir(dir);
#endif
posix::chdir(original_working_dir_.c_str());
}
FilePath original_working_dir_;

View File

@@ -55,6 +55,7 @@ else:
PROGRAM_PATH = os.path.join(gtest_test_utils.GetBuildDir(), PROGRAM)
FLAG_PREFIX = '--gtest_'
CATCH_EXCEPTIONS_FLAG = FLAG_PREFIX + 'catch_exceptions'
DEATH_TEST_STYLE_FLAG = FLAG_PREFIX + 'death_test_style'
# The help message must match this regex.
HELP_REGEX = re.compile(
@@ -99,8 +100,10 @@ class GTestHelpTest(unittest.TestCase):
self.assert_(HELP_REGEX.search(output), output)
if IS_WINDOWS:
self.assert_(CATCH_EXCEPTIONS_FLAG in output, output)
self.assert_(DEATH_TEST_STYLE_FLAG not in output, output)
else:
self.assert_(CATCH_EXCEPTIONS_FLAG not in output, output)
self.assert_(DEATH_TEST_STYLE_FLAG in output, output)
def testPrintsHelpWithFullFlag(self):
self.TestHelpFlag('--help')

View File

@@ -60,6 +60,7 @@
using testing::ScopedFakeTestPartResultReporter;
using testing::TestPartResultArray;
namespace posix = ::testing::internal::posix;
using testing::internal::String;
// Tests catching fatal failures.
@@ -989,16 +990,9 @@ int main(int argc, char **argv) {
// Skip the usual output capturing if we're running as the child
// process of an threadsafe-style death test.
#if GTEST_OS_WINDOWS
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable:4996)
#endif // _MSC_VER
freopen("nul:", "w", stdout);
#ifdef _MSC_VER
#pragma warning(pop)
#endif // _MSC_VER
posix::freopen("nul:", "w", stdout);
#else
freopen("/dev/null", "w", stdout);
posix::freopen("/dev/null", "w", stdout);
#endif // GTEST_OS_WINDOWS
return RUN_ALL_TESTS();
}