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

@@ -48,7 +48,6 @@
#if GTEST_OS_WINDOWS
#include <windows.h>
#else
#include <unistd.h>
#include <sys/mman.h>
#include <sys/wait.h>
#endif // GTEST_OS_WINDOWS
@@ -205,15 +204,7 @@ void DeathTestAbort(const String& message) {
const InternalRunDeathTestFlag* const flag =
GetUnitTestImpl()->internal_run_death_test_flag();
if (flag != NULL) {
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable: 4996) // Suppresses deprecation warning
// about POSIX functions in MSVC.
#endif // _MSC_VER
FILE* parent = fdopen(flag->write_fd(), "w");
#ifdef _MSC_VER
#pragma warning(pop)
#endif // _MSC_VER
FILE* parent = posix::fdopen(flag->write_fd(), "w");
fputc(kDeathTestInternalError, parent);
fprintf(parent, "%s", message.c_str());
fflush(parent);
@@ -258,15 +249,7 @@ void DeathTestAbort(const String& message) {
// Returns the message describing the last system error in errno.
String GetLastErrnoDescription() {
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable: 4996) // Suppresses deprecation warning
// about POSIX functions in MSVC.
#endif // _MSC_VER
return String(errno == 0 ? "" : strerror(errno));
#ifdef _MSC_VER
#pragma warning(pop)
#endif // _MSC_VER
return String(errno == 0 ? "" : posix::strerror(errno));
}
// This is called from a death test parent process to read a failure
@@ -279,15 +262,7 @@ static void FailFromInternalError(int fd) {
int num_read;
do {
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable: 4996) // Suppresses deprecation warning
// about POSIX functions in MSVC.
#endif // _MSC_VER
while ((num_read = static_cast<int>(read(fd, buffer, 255))) > 0) {
#ifdef _MSC_VER
#pragma warning(pop)
#endif // _MSC_VER
while ((num_read = posix::read(fd, buffer, 255)) > 0) {
buffer[num_read] = '\0';
error << buffer;
}
@@ -397,11 +372,6 @@ class DeathTestImpl : public DeathTest {
// member, and closes read_fd_. Outputs diagnostics and terminates in
// case of unexpected codes.
void DeathTestImpl::ReadAndInterpretStatusByte() {
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable: 4996) // Suppresses deprecation warning
// about POSIX functions in MSVC.
#endif // _MSC_VER
char flag;
int bytes_read;
@@ -410,7 +380,7 @@ void DeathTestImpl::ReadAndInterpretStatusByte() {
// its success), so it's okay to call this in the parent before
// the child process has exited.
do {
bytes_read = static_cast<int>(read(read_fd(), &flag, 1));
bytes_read = posix::read(read_fd(), &flag, 1);
} while (bytes_read == -1 && errno == EINTR);
if (bytes_read == 0) {
@@ -437,11 +407,8 @@ void DeathTestImpl::ReadAndInterpretStatusByte() {
Message() << "Read from death test child process failed: "
<< GetLastErrnoDescription());
}
GTEST_DEATH_TEST_CHECK_SYSCALL_(close(read_fd()));
GTEST_DEATH_TEST_CHECK_SYSCALL_(posix::close(read_fd()));
set_read_fd(-1);
#ifdef _MSC_VER
#pragma warning(pop)
#endif // _MSC_VER
}
// Signals that the death test code which should have exited, didn't.
@@ -454,18 +421,8 @@ void DeathTestImpl::Abort(AbortReason reason) {
// to the pipe, then exit.
const char status_ch =
reason == TEST_DID_NOT_DIE ? kDeathTestLived : kDeathTestReturned;
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable: 4996) // Suppresses deprecation warning
// about POSIX functions.
#endif // _MSC_VER
GTEST_DEATH_TEST_CHECK_SYSCALL_(write(write_fd(), &status_ch, 1));
GTEST_DEATH_TEST_CHECK_SYSCALL_(close(write_fd()));
#ifdef _MSC_VER
#pragma warning(pop)
#endif // _MSC_VER
GTEST_DEATH_TEST_CHECK_SYSCALL_(posix::write(write_fd(), &status_ch, 1));
GTEST_DEATH_TEST_CHECK_SYSCALL_(posix::close(write_fd()));
_exit(1); // Exits w/o any normal exit hooks (we were supposed to crash)
}

View File

@@ -33,22 +33,17 @@
#include <gtest/internal/gtest-port.h>
#include <stdlib.h>
#include <string.h>
#ifdef _WIN32_WCE
#include <windows.h>
#elif GTEST_OS_WINDOWS
#include <direct.h>
#include <io.h>
#include <sys/stat.h>
#elif GTEST_OS_SYMBIAN
// Symbian OpenC has PATH_MAX in sys/syslimits.h
#include <sys/syslimits.h>
#include <unistd.h>
#else
#include <limits.h>
#include <sys/stat.h> // NOLINT
#include <unistd.h> // NOLINT
#include <climits> // Some Linux distributions define PATH_MAX here.
#endif // _WIN32_WCE or _WIN32
@@ -172,13 +167,9 @@ bool FilePath::FileOrDirectoryExists() const {
const DWORD attributes = GetFileAttributes(unicode);
delete [] unicode;
return attributes != kInvalidFileAttributes;
#elif GTEST_OS_WINDOWS
struct _stat file_stat = {};
return _stat(pathname_.c_str(), &file_stat) == 0;
#else
struct stat file_stat;
memset(&file_stat, 0, sizeof(file_stat));
return stat(pathname_.c_str(), &file_stat) == 0;
posix::stat_struct file_stat;
return posix::stat(pathname_.c_str(), &file_stat) == 0;
#endif // _WIN32_WCE
}
@@ -191,6 +182,10 @@ bool FilePath::DirectoryExists() const {
// Windows (like "C:\\").
const FilePath& path(IsRootDirectory() ? *this :
RemoveTrailingPathSeparator());
#else
const FilePath& path(*this);
#endif
#ifdef _WIN32_WCE
LPCWSTR unicode = String::AnsiToUtf16(path.c_str());
const DWORD attributes = GetFileAttributes(unicode);
@@ -200,16 +195,11 @@ bool FilePath::DirectoryExists() const {
result = true;
}
#else
struct _stat file_stat = {};
result = _stat(path.c_str(), &file_stat) == 0 &&
(_S_IFDIR & file_stat.st_mode) != 0;
posix::stat_struct file_stat;
result = posix::stat(path.c_str(), &file_stat) == 0 &&
posix::IsDir(file_stat);
#endif // _WIN32_WCE
#else
struct stat file_stat;
memset(&file_stat, 0, sizeof(file_stat));
result = stat(pathname_.c_str(), &file_stat) == 0 &&
S_ISDIR(file_stat.st_mode);
#endif // GTEST_OS_WINDOWS
return result;
}

View File

@@ -42,10 +42,6 @@
#include <unistd.h>
#endif // GTEST_OS_WINDOWS
#if GTEST_USES_SIMPLE_RE
#include <string.h>
#endif
#ifdef _WIN32_WCE
#include <windows.h> // For TerminateProcess()
#endif // _WIN32_WCE
@@ -350,11 +346,7 @@ bool RE::PartialMatch(const char* str, const RE& re) {
void RE::Init(const char* regex) {
pattern_ = full_pattern_ = NULL;
if (regex != NULL) {
#if GTEST_OS_WINDOWS
pattern_ = _strdup(regex);
#else
pattern_ = strdup(regex);
#endif
pattern_ = posix::strdup(regex);
}
is_valid_ = ValidateRegex(regex);
@@ -510,17 +502,9 @@ void CaptureStderr() {
::std::string GetCapturedStderr() {
g_captured_stderr->StopCapture();
// Disables Microsoft deprecation warning for fopen and fclose.
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable: 4996)
#endif // _MSC_VER
FILE* const file = fopen(g_captured_stderr->filename().c_str(), "r");
FILE* const file = posix::fopen(g_captured_stderr->filename().c_str(), "r");
const ::std::string content = ReadEntireFile(file);
fclose(file);
#ifdef _MSC_VER
#pragma warning(pop)
#endif // _MSC_VER
posix::fclose(file);
delete g_captured_stderr;
g_captured_stderr = NULL;
@@ -541,10 +525,12 @@ const ::std::vector<String>& GetArgvs() { return g_argvs; }
#endif // GTEST_HAS_DEATH_TEST
#ifdef _WIN32_WCE
namespace posix {
void abort() {
DebugBreak();
TerminateProcess(GetCurrentProcess(), 1);
}
} // namespace posix
#endif // _WIN32_WCE
// Returns the name of the environment variable corresponding to the
@@ -609,7 +595,7 @@ bool ParseInt32(const Message& src_text, const char* str, Int32* value) {
// The value is considered true iff it's not "0".
bool BoolFromGTestEnv(const char* flag, bool default_value) {
const String env_var = FlagToEnvVar(flag);
const char* const string_value = GetEnv(env_var.c_str());
const char* const string_value = posix::getenv(env_var.c_str());
return string_value == NULL ?
default_value : strcmp(string_value, "0") != 0;
}
@@ -619,7 +605,7 @@ bool BoolFromGTestEnv(const char* flag, bool default_value) {
// doesn't represent a valid 32-bit integer, returns default_value.
Int32 Int32FromGTestEnv(const char* flag, Int32 default_value) {
const String env_var = FlagToEnvVar(flag);
const char* const string_value = GetEnv(env_var.c_str());
const char* const string_value = posix::getenv(env_var.c_str());
if (string_value == NULL) {
// The environment variable is not set.
return default_value;
@@ -641,7 +627,7 @@ Int32 Int32FromGTestEnv(const char* flag, Int32 default_value) {
// the given flag; if it's not set, returns default_value.
const char* StringFromGTestEnv(const char* flag, const char* default_value) {
const String env_var = FlagToEnvVar(flag);
const char* const value = GetEnv(env_var.c_str());
const char* const value = posix::getenv(env_var.c_str());
return value == NULL ? default_value : value;
}

View File

@@ -81,7 +81,7 @@ void TestPartResultArray::Append(const TestPartResult& result) {
const TestPartResult& TestPartResultArray::GetTestPartResult(int index) const {
if (index < 0 || index >= size()) {
printf("\nInvalid index (%d) into TestPartResultArray.\n", index);
internal::abort();
internal::posix::abort();
}
const internal::ListNode<TestPartResult>* p = list_->Head();

View File

@@ -39,7 +39,6 @@
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wchar.h>
#include <wctype.h>
@@ -125,8 +124,6 @@
#undef GTEST_IMPLEMENTATION_
#if GTEST_OS_WINDOWS
#define fileno _fileno
#define isatty _isatty
#define vsnprintf _vsnprintf
#endif // GTEST_OS_WINDOWS
@@ -806,16 +803,7 @@ static char* CloneString(const char* str, size_t length) {
return NULL;
} else {
char* const clone = new char[length + 1];
// MSVC 8 deprecates strncpy(), so we want to suppress warning
// 4996 (deprecated function) there.
#if GTEST_OS_WINDOWS // We are on Windows.
#pragma warning(push) // Saves the current warning state.
#pragma warning(disable:4996) // Temporarily disables warning 4996.
strncpy(clone, str, length);
#pragma warning(pop) // Restores the warning state.
#else // We are on Linux or Mac OS.
strncpy(clone, str, length);
#endif // GTEST_OS_WINDOWS
posix::strncpy(clone, str, length);
clone[length] = '\0';
return clone;
}
@@ -1455,17 +1443,8 @@ char* CodePointToUtf8(UInt32 code_point, char* str) {
// the terminating nul character). We are asking for 32 character
// buffer just in case. This is also enough for strncpy to
// null-terminate the destination string.
// MSVC 8 deprecates strncpy(), so we want to suppress warning
// 4996 (deprecated function) there.
#if GTEST_OS_WINDOWS // We are on Windows.
#pragma warning(push) // Saves the current warning state.
#pragma warning(disable:4996) // Temporarily disables warning 4996.
#endif
strncpy(str, String::Format("(Invalid Unicode 0x%X)", code_point).c_str(),
32);
#if GTEST_OS_WINDOWS // We are on Windows.
#pragma warning(pop) // Restores the warning state.
#endif
posix::strncpy(
str, String::Format("(Invalid Unicode 0x%X)", code_point).c_str(), 32);
str[31] = '\0'; // Makes sure no change in the format to strncpy leaves
// the result unterminated.
}
@@ -1603,15 +1582,11 @@ AssertionResult CmpHelperSTRNE(const char* s1_expression,
// NULL C string is considered different to any non-NULL C string,
// including the empty string.
bool String::CaseInsensitiveCStringEquals(const char * lhs, const char * rhs) {
if ( lhs == NULL ) return rhs == NULL;
if ( rhs == NULL ) return false;
#if GTEST_OS_WINDOWS
return _stricmp(lhs, rhs) == 0;
#else // GTEST_OS_WINDOWS
return strcasecmp(lhs, rhs) == 0;
#endif // GTEST_OS_WINDOWS
if (lhs == NULL)
return rhs == NULL;
if (rhs == NULL)
return false;
return posix::strcasecmp(lhs, rhs) == 0;
}
// Compares two wide C strings, ignoring case. Returns true iff they
@@ -2519,7 +2494,7 @@ bool ShouldUseColor(bool stdout_is_tty) {
return stdout_is_tty;
#else
// On non-Windows platforms, we rely on the TERM variable.
const char* const term = GetEnv("TERM");
const char* const term = posix::getenv("TERM");
const bool term_supports_color =
String::CStringEquals(term, "xterm") ||
String::CStringEquals(term, "xterm-color") ||
@@ -2549,7 +2524,7 @@ void ColoredPrintf(GTestColor color, const char* fmt, ...) {
const bool use_color = false;
#else
static const bool in_color_mode =
ShouldUseColor(isatty(fileno(stdout)) != 0);
ShouldUseColor(posix::isatty(posix::fileno(stdout)) != 0);
const bool use_color = in_color_mode && (color != COLOR_DEFAULT);
#endif // defined(_WIN32_WCE) || GTEST_OS_SYMBIAN || GTEST_OS_ZOS
// The '!= 0' comparison is necessary to satisfy MSVC 7.1.
@@ -2631,8 +2606,8 @@ void PrettyUnitTestResultPrinter::OnUnitTestStart(
if (internal::ShouldShard(kTestTotalShards, kTestShardIndex, false)) {
ColoredPrintf(COLOR_YELLOW,
"Note: This is test shard %s of %s.\n",
internal::GetEnv(kTestShardIndex),
internal::GetEnv(kTestTotalShards));
internal::posix::getenv(kTestShardIndex),
internal::posix::getenv(kTestTotalShards));
}
const internal::UnitTestImpl* const impl = unit_test->impl();
@@ -2950,17 +2925,7 @@ void XmlUnitTestResultPrinter::OnUnitTestEnd(const UnitTest* unit_test) {
internal::FilePath output_dir(output_file.RemoveFileName());
if (output_dir.CreateDirectoriesRecursively()) {
// MSVC 8 deprecates fopen(), so we want to suppress warning 4996
// (deprecated function) there.
#if GTEST_OS_WINDOWS
// We are on Windows.
#pragma warning(push) // Saves the current warning state.
#pragma warning(disable:4996) // Temporarily disables warning 4996.
xmlout = fopen(output_file_.c_str(), "w");
#pragma warning(pop) // Restores the warning state.
#else // We are on Linux or Mac OS.
xmlout = fopen(output_file_.c_str(), "w");
#endif // GTEST_OS_WINDOWS
xmlout = internal::posix::fopen(output_file_.c_str(), "w");
}
if (xmlout == NULL) {
// TODO(wan): report the reason of the failure.
@@ -3697,17 +3662,9 @@ int UnitTestImpl::RunAllTests() {
// function will write over it. If the variable is present, but the file cannot
// be created, prints an error and exits.
void WriteToShardStatusFileIfNeeded() {
const char* const test_shard_file = GetEnv(kTestShardStatusFile);
const char* const test_shard_file = posix::getenv(kTestShardStatusFile);
if (test_shard_file != NULL) {
#ifdef _MSC_VER // MSVC 8 deprecates fopen().
#pragma warning(push) // Saves the current warning state.
#pragma warning(disable:4996) // Temporarily disables warning on
// deprecated functions.
#endif
FILE* const file = fopen(test_shard_file, "w");
#ifdef _MSC_VER
#pragma warning(pop) // Restores the warning state.
#endif
FILE* const file = posix::fopen(test_shard_file, "w");
if (file == NULL) {
ColoredPrintf(COLOR_RED,
"Could not write to the test shard status file \"%s\" "
@@ -3772,7 +3729,7 @@ bool ShouldShard(const char* total_shards_env,
// returns default_val. If it is not an Int32, prints an error
// and aborts.
Int32 Int32FromEnvOrDie(const char* const var, Int32 default_val) {
const char* str_val = GetEnv(var);
const char* str_val = posix::getenv(var);
if (str_val == NULL) {
return default_val;
}
@@ -4175,7 +4132,11 @@ static const char kColorEncodedHelpMessage[] =
" Generate an XML report in the given directory or with the given file\n"
" name. @YFILE_PATH@D defaults to @Gtest_details.xml@D.\n"
"\n"
"Failure Behavior:\n"
"Assertion Behavior:\n"
#if GTEST_HAS_DEATH_TEST && !GTEST_OS_WINDOWS
" @G--" GTEST_FLAG_PREFIX_ "death_test_style=@Y(@Gfast@Y|@Gthreadsafe@Y)@D\n"
" Set the default death test style.\n"
#endif // GTEST_HAS_DEATH_TEST && !GTEST_OS_WINDOWS
" @G--" GTEST_FLAG_PREFIX_ "break_on_failure@D\n"
" Turn assertion failures into debugger break-points.\n"
" @G--" GTEST_FLAG_PREFIX_ "throw_on_failure@D\n"