summaryrefslogtreecommitdiffstats
path: root/base/test_utils.cpp
diff options
context:
space:
mode:
authorWei Wang <wvw@google.com>2016-10-21 09:23:39 -0700
committerWei Wang <wvw@google.com>2016-10-22 10:35:21 -0700
commit8c176302e6a6b4de40079af42c30d8d00da89f30 (patch)
treec063a1d84d1fbb8a9c4b70d2339aeea0063aa08d /base/test_utils.cpp
parentc75a32aae30f8cadbb5608d2fb680ef326708fcd (diff)
downloadcore-8c176302e6a6b4de40079af42c30d8d00da89f30.tar.gz
core-8c176302e6a6b4de40079af42c30d8d00da89f30.tar.bz2
core-8c176302e6a6b4de40079af42c30d8d00da89f30.zip
Move CapturedStderr to test_util library
Bug: 32181382 Test: run /data/nativetest(64)/binderTextOutputTest Change-Id: Ifb2e1f6af2c3f57b5cbed7dde65efb31253c52a2
Diffstat (limited to 'base/test_utils.cpp')
-rw-r--r--base/test_utils.cpp29
1 files changed, 29 insertions, 0 deletions
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.
+}