Merge "Move CapturedStderr to test_util library"
diff --git a/base/include/android-base/test_utils.h b/base/include/android-base/test_utils.h
index 4ea3c8e..c0bf0c1 100644
--- a/base/include/android-base/test_utils.h
+++ b/base/include/android-base/test_utils.h
@@ -48,4 +48,21 @@
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 1ee181a..2d9c2ba 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 635af6c..3b3d698 100644
--- a/base/test_utils.cpp
+++ b/base/test_utils.cpp
@@ -102,3 +102,32 @@
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.
+}