mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 22:02:23 +01:00
Refactor stdout and stderr capturing to a library (#4970)
This should make it easy to add more tests which need the specific behavior here. It also isolates where we reach into GoogleTest's internals to a single common place.
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
|
||||
// Exceptions. See /LICENSE for license information.
|
||||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
|
||||
#include "testing/base/capture_std_streams.h"
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
|
||||
#include "common/ostream.h"
|
||||
|
||||
namespace Carbon::Testing::Internal {
|
||||
|
||||
// While these are marked as "internal" APIs, they seem to work and be pretty
|
||||
// widely used for their exact documented behavior.
|
||||
using ::testing::internal::CaptureStderr;
|
||||
using ::testing::internal::CaptureStdout;
|
||||
using ::testing::internal::GetCapturedStderr;
|
||||
using ::testing::internal::GetCapturedStdout;
|
||||
|
||||
auto BeginStdStreamCapture() -> void {
|
||||
CaptureStderr();
|
||||
CaptureStdout();
|
||||
}
|
||||
auto EndStdStreamCapture(std::string& out, std::string& err) -> void {
|
||||
// No need to flush stderr.
|
||||
err = GetCapturedStderr();
|
||||
llvm::outs().flush();
|
||||
out = GetCapturedStdout();
|
||||
}
|
||||
|
||||
} // namespace Carbon::Testing::Internal
|
||||
Reference in New Issue
Block a user