From 8c176302e6a6b4de40079af42c30d8d00da89f30 Mon Sep 17 00:00:00 2001 From: Wei Wang Date: Fri, 21 Oct 2016 09:23:39 -0700 Subject: [PATCH] Move CapturedStderr to test_util library Bug: 32181382 Test: run /data/nativetest(64)/binderTextOutputTest Change-Id: Ifb2e1f6af2c3f57b5cbed7dde65efb31253c52a2 --- base/include/android-base/test_utils.h | 17 ++++++++++++ base/logging_test.cpp | 36 -------------------------- base/test_utils.cpp | 29 +++++++++++++++++++++ 3 files changed, 46 insertions(+), 36 deletions(-) diff --git a/base/include/android-base/test_utils.h b/base/include/android-base/test_utils.h index 4ea3c8e48..c0bf0c1e1 100644 --- a/base/include/android-base/test_utils.h +++ b/base/include/android-base/test_utils.h @@ -48,4 +48,21 @@ class TemporaryDir { DISALLOW_COPY_AND_ASSIGN(TemporaryDir); }; +class CapturedStderr { + public: + CapturedStderr(); + ~CapturedStderr(); + + int fd() const; + + private: + void init(); + void reset(); + + TemporaryFile temp_file_; + int old_stderr_; + + DISALLOW_COPY_AND_ASSIGN(CapturedStderr); +}; + #endif // ANDROID_BASE_TEST_UTILS_H diff --git a/base/logging_test.cpp b/base/logging_test.cpp index 1ee181a42..2d9c2baec 100644 --- a/base/logging_test.cpp +++ b/base/logging_test.cpp @@ -37,42 +37,6 @@ #define HOST_TEST(suite, name) TEST(suite, name) #endif -class CapturedStderr { - public: - CapturedStderr() : old_stderr_(-1) { - init(); - } - - ~CapturedStderr() { - reset(); - } - - int fd() const { - return temp_file_.fd; - } - - private: - void init() { -#if defined(_WIN32) - // On Windows, stderr is often buffered, so make sure it is unbuffered so - // that we can immediately read back what was written to stderr. - ASSERT_EQ(0, setvbuf(stderr, NULL, _IONBF, 0)); -#endif - old_stderr_ = dup(STDERR_FILENO); - ASSERT_NE(-1, old_stderr_); - ASSERT_NE(-1, dup2(fd(), STDERR_FILENO)); - } - - void reset() { - ASSERT_NE(-1, dup2(old_stderr_, STDERR_FILENO)); - ASSERT_EQ(0, close(old_stderr_)); - // Note: cannot restore prior setvbuf() setting. - } - - TemporaryFile temp_file_; - int old_stderr_; -}; - #if defined(_WIN32) static void ExitSignalAbortHandler(int) { _exit(3); diff --git a/base/test_utils.cpp b/base/test_utils.cpp index 635af6c6e..3b3d698cc 100644 --- a/base/test_utils.cpp +++ b/base/test_utils.cpp @@ -102,3 +102,32 @@ bool TemporaryDir::init(const std::string& tmp_dir) { OS_PATH_SEPARATOR); return (mkdtemp(path) != nullptr); } + +CapturedStderr::CapturedStderr() : old_stderr_(-1) { + init(); +} + +CapturedStderr::~CapturedStderr() { + reset(); +} + +int CapturedStderr::fd() const { + return temp_file_.fd; +} + +void CapturedStderr::init() { +#if defined(_WIN32) + // On Windows, stderr is often buffered, so make sure it is unbuffered so + // that we can immediately read back what was written to stderr. + CHECK_EQ(0, setvbuf(stderr, NULL, _IONBF, 0)); +#endif + old_stderr_ = dup(STDERR_FILENO); + CHECK_NE(-1, old_stderr_); + CHECK_NE(-1, dup2(fd(), STDERR_FILENO)); +} + +void CapturedStderr::reset() { + CHECK_NE(-1, dup2(old_stderr_, STDERR_FILENO)); + CHECK_EQ(0, close(old_stderr_)); + // Note: cannot restore prior setvbuf() setting. +}