aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/google/protobuf/testing
diff options
context:
space:
mode:
Diffstat (limited to 'src/google/protobuf/testing')
-rw-r--r--src/google/protobuf/testing/googletest.cc34
-rw-r--r--src/google/protobuf/testing/googletest.h6
2 files changed, 38 insertions, 2 deletions
diff --git a/src/google/protobuf/testing/googletest.cc b/src/google/protobuf/testing/googletest.cc
index aabc657f..65c1154f 100644
--- a/src/google/protobuf/testing/googletest.cc
+++ b/src/google/protobuf/testing/googletest.cc
@@ -126,9 +126,28 @@ string TestTempDir() {
return temp_dir_deleter_.GetTempDir();
}
+// TODO(kenton): Share duplicated code below. Too busy/lazy for now.
+
+static string stdout_capture_filename_;
static string stderr_capture_filename_;
+static int original_stdout_ = -1;
static int original_stderr_ = -1;
+void CaptureTestStdout() {
+ GOOGLE_CHECK_EQ(original_stdout_, -1) << "Already capturing.";
+
+ stdout_capture_filename_ = TestTempDir() + "/captured_stdout";
+
+ int fd = open(stdout_capture_filename_.c_str(),
+ O_WRONLY | O_CREAT | O_EXCL | O_BINARY, 0777);
+ GOOGLE_CHECK(fd >= 0) << "open: " << strerror(errno);
+
+ original_stdout_ = dup(1);
+ close(1);
+ dup2(fd, 1);
+ close(fd);
+}
+
void CaptureTestStderr() {
GOOGLE_CHECK_EQ(original_stderr_, -1) << "Already capturing.";
@@ -144,6 +163,21 @@ void CaptureTestStderr() {
close(fd);
}
+string GetCapturedTestStdout() {
+ GOOGLE_CHECK_NE(original_stdout_, -1) << "Not capturing.";
+
+ close(1);
+ dup2(original_stdout_, 1);
+ original_stdout_ = -1;
+
+ string result;
+ File::ReadFileToStringOrDie(stdout_capture_filename_, &result);
+
+ remove(stdout_capture_filename_.c_str());
+
+ return result;
+}
+
string GetCapturedTestStderr() {
GOOGLE_CHECK_NE(original_stderr_, -1) << "Not capturing.";
diff --git a/src/google/protobuf/testing/googletest.h b/src/google/protobuf/testing/googletest.h
index 9420641a..bb4aafab 100644
--- a/src/google/protobuf/testing/googletest.h
+++ b/src/google/protobuf/testing/googletest.h
@@ -33,10 +33,12 @@ string TestSourceDir();
// placed.
string TestTempDir();
-// Capture all text written to stderr.
+// Capture all text written to stdout or stderr.
+void CaptureTestStdout();
void CaptureTestStderr();
-// Stop capturing stderr and return the text captured.
+// Stop capturing stdout or stderr and return the text captured.
+string GetCapturedTestStdout();
string GetCapturedTestStderr();
// For use with ScopedMemoryLog::GetMessages(). Inside Google the LogLevel