summaryrefslogtreecommitdiff
path: root/base/test_utils.cpp
diff options
context:
space:
mode:
authorChristopher Ferris <cferris@google.com>2018-08-30 13:31:45 -0700
committerChristopher Ferris <cferris@google.com>2018-09-04 14:29:23 -0700
commiteea85c9aabf38d39e0474716df3b10502ced9586 (patch)
treef8982eaa50950ff776df46f8b6cce211cdaf02bd /base/test_utils.cpp
parent640ceee567c7f7e3832f9de189580fd3e138e951 (diff)
Add Start/Stop/Reset to CapturedStdFd.
Move the fd() function to be private since it should not have been exposed in the first place. Fix the way logging_test uses CapturedXXX. Adding this because the new isolated testing doesn't print errors to stderr so the ASSERT_ EXPECT_ messages can get swallowed. Also, it's easier to reuse a CapturedXXX object in a test with these functions. Test: New unit tests pass. Change-Id: I38b113fc184146ce434802f80a9b7997fa83e78a
Diffstat (limited to 'base/test_utils.cpp')
-rw-r--r--base/test_utils.cpp23
1 files changed, 17 insertions, 6 deletions
diff --git a/base/test_utils.cpp b/base/test_utils.cpp
index 5096369e7d..4d9466b30c 100644
--- a/base/test_utils.cpp
+++ b/base/test_utils.cpp
@@ -126,11 +126,13 @@ bool TemporaryDir::init(const std::string& tmp_dir) {
}
CapturedStdFd::CapturedStdFd(int std_fd) : std_fd_(std_fd), old_fd_(-1) {
- Init();
+ Start();
}
CapturedStdFd::~CapturedStdFd() {
- Reset();
+ if (old_fd_ != -1) {
+ Stop();
+ }
}
int CapturedStdFd::fd() const {
@@ -144,19 +146,28 @@ std::string CapturedStdFd::str() {
return result;
}
-void CapturedStdFd::Init() {
+void CapturedStdFd::Reset() {
+ // Do not reset while capturing.
+ CHECK_EQ(-1, old_fd_);
+ CHECK_EQ(0, TEMP_FAILURE_RETRY(lseek(fd(), 0, SEEK_SET)));
+ CHECK_EQ(0, ftruncate(fd(), 0));
+}
+
+void CapturedStdFd::Start() {
#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.
- if (std_fd_ == STDERR_FILENO) CHECK_EQ(0, setvbuf(stderr, NULL, _IONBF, 0));
+ if (std_fd_ == STDERR_FILENO) CHECK_EQ(0, setvbuf(stderr, nullptr, _IONBF, 0));
#endif
old_fd_ = dup(std_fd_);
CHECK_NE(-1, old_fd_);
CHECK_NE(-1, dup2(fd(), std_fd_));
}
-void CapturedStdFd::Reset() {
+void CapturedStdFd::Stop() {
+ CHECK_NE(-1, old_fd_);
CHECK_NE(-1, dup2(old_fd_, std_fd_));
- CHECK_EQ(0, close(old_fd_));
+ close(old_fd_);
+ old_fd_ = -1;
// Note: cannot restore prior setvbuf() setting.
}