From 7d2451f119457db33eb777f97b7b88849743352e Mon Sep 17 00:00:00 2001 From: SimonChh Date: Sat, 2 Mar 2019 15:18:48 -0500 Subject: [PATCH] Fix output redirection in failing tests (#1525) Fixes #1514 --- include/internal/catch_output_redirect.cpp | 9 +++++++++ include/internal/catch_output_redirect.h | 15 +++++++++++++++ include/internal/catch_run_context.cpp | 6 +----- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/include/internal/catch_output_redirect.cpp b/include/internal/catch_output_redirect.cpp index 86b0f6a0..a0a9aff9 100644 --- a/include/internal/catch_output_redirect.cpp +++ b/include/internal/catch_output_redirect.cpp @@ -49,6 +49,15 @@ namespace Catch { {} auto RedirectedStdErr::str() const -> std::string { return m_rss.str(); } + RedirectedStreams::RedirectedStreams(std::string& redirectedCout, std::string& redirectedCerr) + : m_redirectedCout(redirectedCout), + m_redirectedCerr(redirectedCerr) + {} + + RedirectedStreams::~RedirectedStreams() { + m_redirectedCout += m_redirectedStdOut.str(); + m_redirectedCerr += m_redirectedStdErr.str(); + } #if defined(CATCH_CONFIG_NEW_CAPTURE) diff --git a/include/internal/catch_output_redirect.h b/include/internal/catch_output_redirect.h index ac040405..3d1c2d45 100644 --- a/include/internal/catch_output_redirect.h +++ b/include/internal/catch_output_redirect.h @@ -46,6 +46,21 @@ namespace Catch { auto str() const -> std::string; }; + class RedirectedStreams { + public: + RedirectedStreams(RedirectedStreams const&) = delete; + RedirectedStreams& operator=(RedirectedStreams const&) = delete; + RedirectedStreams(RedirectedStreams&&) = delete; + RedirectedStreams& operator=(RedirectedStreams&&) = delete; + + RedirectedStreams(std::string& redirectedCout, std::string& redirectedCerr); + ~RedirectedStreams(); + private: + std::string& m_redirectedCout; + std::string& m_redirectedCerr; + RedirectedStdOut m_redirectedStdOut; + RedirectedStdErr m_redirectedStdErr; + }; #if defined(CATCH_CONFIG_NEW_CAPTURE) diff --git a/include/internal/catch_run_context.cpp b/include/internal/catch_run_context.cpp index 77dcaae0..c4defc9d 100644 --- a/include/internal/catch_run_context.cpp +++ b/include/internal/catch_run_context.cpp @@ -67,7 +67,6 @@ namespace Catch { GeneratorTracker::~GeneratorTracker() {} } - RunContext::RunContext(IConfigPtr const& _config, IStreamingReporterPtr&& reporter) : m_runInfo(_config->name()), m_context(getCurrentMutableContext()), @@ -323,13 +322,10 @@ namespace Catch { CATCH_TRY { if (m_reporter->getPreferences().shouldRedirectStdOut) { #if !defined(CATCH_CONFIG_EXPERIMENTAL_REDIRECT) - RedirectedStdOut redirectedStdOut; - RedirectedStdErr redirectedStdErr; + RedirectedStreams redirectedStreams(redirectedCout, redirectedCerr); timer.start(); invokeActiveTestCase(); - redirectedCout += redirectedStdOut.str(); - redirectedCerr += redirectedStdErr.str(); #else OutputRedirect r(redirectedCout, redirectedCerr); timer.start();