mirror of
https://github.com/catchorg/Catch2.git
synced 2024-11-16 18:52:25 +01:00
Large output redirect refactor
This rework changes two important things 1) the output redirect is deactivated while control is given to the reporters. This means that combining reporters that write to stdout with capturing reporters, e.g. `./tests -s -r console -r junit::out=junit.xml`, no longer leads to the capturing reporter seeing all the output from the other reporter captured. Trying this with the `SelfTest` binary would previously lead to JUnit spending **hours** trying to escape all of ConsoleReporter's output and write it to the output file. I actually ended up killing the process after 3 hours, during which the JUnit reporter wrote something like 50 MBs of output to a file. 2) The redirect object's lifetime is tied to the `RunContext`, instead of being constructed for every partial test case run separately. This has no effect on the basic StreamRedirect, but improves the FileRedirect significantly. Previously, running many tests in single process with this redirect (e.g. running `SelfTest -r junit`) would cause later tests to always fail before starting, due to exceeding the limit of temporary files. For the current `SelfTest` binary, the old implementation would lead to **295** test failures from not being able to initiate the redirect. The new implementation completely eliminates them. ---- There is one downside to the new implementation of FileRedirect, specific to Linux. Running the `SelfTest` binary on Linux causes 3-4 tests to have no captured stdout/stderr, even though the tests were supposed to be writing there (there was no output to the actual stdout/stderr either, the output was just completely lost). Since this never happen for smaller test case sets, nor does it reproduce on other platforms, this implementation is still strictly better than the old one, and thus it can get reasonably merged.
This commit is contained in:
parent
a579b5f640
commit
f24569a1b4
@ -5,142 +5,335 @@
|
|||||||
// https://www.boost.org/LICENSE_1_0.txt)
|
// https://www.boost.org/LICENSE_1_0.txt)
|
||||||
|
|
||||||
// SPDX-License-Identifier: BSL-1.0
|
// SPDX-License-Identifier: BSL-1.0
|
||||||
#include <catch2/internal/catch_output_redirect.hpp>
|
#include <catch2/internal/catch_compiler_capabilities.hpp>
|
||||||
#include <catch2/internal/catch_enforce.hpp>
|
#include <catch2/internal/catch_enforce.hpp>
|
||||||
|
#include <catch2/internal/catch_output_redirect.hpp>
|
||||||
|
#include <catch2/internal/catch_platform.hpp>
|
||||||
|
#include <catch2/internal/catch_reusable_string_stream.hpp>
|
||||||
#include <catch2/internal/catch_stdstreams.hpp>
|
#include <catch2/internal/catch_stdstreams.hpp>
|
||||||
|
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
#include <iosfwd>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
|
||||||
#if defined(CATCH_CONFIG_NEW_CAPTURE)
|
#if defined( CATCH_CONFIG_NEW_CAPTURE )
|
||||||
#if defined(_MSC_VER)
|
# if defined( _MSC_VER )
|
||||||
#include <io.h> //_dup and _dup2
|
# include <io.h> //_dup and _dup2
|
||||||
#define dup _dup
|
# define dup _dup
|
||||||
#define dup2 _dup2
|
# define dup2 _dup2
|
||||||
#define fileno _fileno
|
# define fileno _fileno
|
||||||
#else
|
# else
|
||||||
#include <unistd.h> // dup and dup2
|
# include <unistd.h> // dup and dup2
|
||||||
#endif
|
# endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
namespace Catch {
|
namespace Catch {
|
||||||
|
|
||||||
RedirectedStream::RedirectedStream( std::ostream& originalStream, std::ostream& redirectionStream )
|
namespace {
|
||||||
: m_originalStream( originalStream ),
|
//! A no-op implementation, used if no reporter wants output
|
||||||
|
//! redirection.
|
||||||
|
class NoopRedirect : public OutputRedirect {
|
||||||
|
void activateImpl() override {}
|
||||||
|
void deactivateImpl() override {}
|
||||||
|
std::string getStdout() override { return {}; }
|
||||||
|
std::string getStderr() override { return {}; }
|
||||||
|
void clearBuffers() override {}
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Redirects specific stream's rdbuf with another's.
|
||||||
|
*
|
||||||
|
* Redirection can be stopped and started on-demand, assumes
|
||||||
|
* that the underlying stream's rdbuf aren't changed by other
|
||||||
|
* users.
|
||||||
|
*/
|
||||||
|
class RedirectedStreamNew {
|
||||||
|
std::ostream& m_originalStream;
|
||||||
|
std::ostream& m_redirectionStream;
|
||||||
|
std::streambuf* m_prevBuf;
|
||||||
|
|
||||||
|
public:
|
||||||
|
RedirectedStreamNew( std::ostream& originalStream,
|
||||||
|
std::ostream& redirectionStream ):
|
||||||
|
m_originalStream( originalStream ),
|
||||||
m_redirectionStream( redirectionStream ),
|
m_redirectionStream( redirectionStream ),
|
||||||
m_prevBuf( m_originalStream.rdbuf() )
|
m_prevBuf( m_originalStream.rdbuf() ) {}
|
||||||
{
|
|
||||||
|
void startRedirect() {
|
||||||
m_originalStream.rdbuf( m_redirectionStream.rdbuf() );
|
m_originalStream.rdbuf( m_redirectionStream.rdbuf() );
|
||||||
}
|
}
|
||||||
|
void stopRedirect() { m_originalStream.rdbuf( m_prevBuf ); }
|
||||||
|
};
|
||||||
|
|
||||||
RedirectedStream::~RedirectedStream() {
|
/**
|
||||||
m_originalStream.rdbuf( m_prevBuf );
|
* Redirects the `std::cout`, `std::cerr`, `std::clog` streams,
|
||||||
|
* but does not touch the actual `stdout`/`stderr` file descriptors.
|
||||||
|
*/
|
||||||
|
class StreamRedirect : public OutputRedirect {
|
||||||
|
ReusableStringStream m_redirectedOut, m_redirectedErr;
|
||||||
|
RedirectedStreamNew m_cout, m_cerr, m_clog;
|
||||||
|
|
||||||
|
public:
|
||||||
|
StreamRedirect():
|
||||||
|
m_cout( Catch::cout(), m_redirectedOut.get() ),
|
||||||
|
m_cerr( Catch::cerr(), m_redirectedErr.get() ),
|
||||||
|
m_clog( Catch::clog(), m_redirectedErr.get() ) {}
|
||||||
|
|
||||||
|
void activateImpl() override {
|
||||||
|
m_cout.startRedirect();
|
||||||
|
m_cerr.startRedirect();
|
||||||
|
m_clog.startRedirect();
|
||||||
}
|
}
|
||||||
|
void deactivateImpl() override {
|
||||||
RedirectedStdOut::RedirectedStdOut() : m_cout( Catch::cout(), m_rss.get() ) {}
|
m_cout.stopRedirect();
|
||||||
auto RedirectedStdOut::str() const -> std::string { return m_rss.str(); }
|
m_cerr.stopRedirect();
|
||||||
|
m_clog.stopRedirect();
|
||||||
RedirectedStdErr::RedirectedStdErr()
|
|
||||||
: m_cerr( Catch::cerr(), m_rss.get() ),
|
|
||||||
m_clog( Catch::clog(), m_rss.get() )
|
|
||||||
{}
|
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
|
std::string getStdout() override { return m_redirectedOut.str(); }
|
||||||
#if defined(CATCH_CONFIG_NEW_CAPTURE)
|
std::string getStderr() override { return m_redirectedErr.str(); }
|
||||||
|
void clearBuffers() override {
|
||||||
#if defined(_MSC_VER)
|
m_redirectedOut.str( "" );
|
||||||
TempFile::TempFile() {
|
m_redirectedErr.str( "" );
|
||||||
if (tmpnam_s(m_buffer)) {
|
|
||||||
CATCH_RUNTIME_ERROR("Could not get a temp filename");
|
|
||||||
}
|
}
|
||||||
if (fopen_s(&m_file, m_buffer, "w+")) {
|
};
|
||||||
|
|
||||||
|
#if defined( CATCH_CONFIG_NEW_CAPTURE )
|
||||||
|
|
||||||
|
// Windows's implementation of std::tmpfile is terrible (it tries
|
||||||
|
// to create a file inside system folder, thus requiring elevated
|
||||||
|
// privileges for the binary), so we have to use tmpnam(_s) and
|
||||||
|
// create the file ourselves there.
|
||||||
|
class TempFile {
|
||||||
|
public:
|
||||||
|
TempFile( TempFile const& ) = delete;
|
||||||
|
TempFile& operator=( TempFile const& ) = delete;
|
||||||
|
TempFile( TempFile&& ) = delete;
|
||||||
|
TempFile& operator=( TempFile&& ) = delete;
|
||||||
|
|
||||||
|
# if defined( _MSC_VER )
|
||||||
|
TempFile() {
|
||||||
|
if ( tmpnam_s( m_buffer ) ) {
|
||||||
|
CATCH_RUNTIME_ERROR( "Could not get a temp filename" );
|
||||||
|
}
|
||||||
|
if ( fopen_s( &m_file, m_buffer, "wb+" ) ) {
|
||||||
char buffer[100];
|
char buffer[100];
|
||||||
if (strerror_s(buffer, errno)) {
|
if ( strerror_s( buffer, errno ) ) {
|
||||||
CATCH_RUNTIME_ERROR("Could not translate errno to a string");
|
CATCH_RUNTIME_ERROR(
|
||||||
|
"Could not translate errno to a string" );
|
||||||
}
|
}
|
||||||
CATCH_RUNTIME_ERROR("Could not open the temp file: '" << m_buffer << "' because: " << buffer);
|
CATCH_RUNTIME_ERROR( "Could not open the temp file: '"
|
||||||
|
<< m_buffer
|
||||||
|
<< "' because: " << buffer );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#else
|
# else
|
||||||
TempFile::TempFile() {
|
TempFile() {
|
||||||
m_file = std::tmpfile();
|
m_file = std::tmpfile();
|
||||||
if (!m_file) {
|
if ( !m_file ) {
|
||||||
CATCH_RUNTIME_ERROR("Could not create a temp file.");
|
CATCH_RUNTIME_ERROR( "Could not create a temp file." );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
# endif
|
||||||
|
|
||||||
#endif
|
~TempFile() {
|
||||||
|
|
||||||
TempFile::~TempFile() {
|
|
||||||
// TBD: What to do about errors here?
|
// TBD: What to do about errors here?
|
||||||
std::fclose(m_file);
|
std::fclose( m_file );
|
||||||
// We manually create the file on Windows only, on Linux
|
// We manually create the file on Windows only, on Linux
|
||||||
// it will be autodeleted
|
// it will be autodeleted
|
||||||
#if defined(_MSC_VER)
|
# if defined( _MSC_VER )
|
||||||
std::remove(m_buffer);
|
std::remove( m_buffer );
|
||||||
#endif
|
# endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::FILE* getFile() { return m_file; }
|
||||||
FILE* TempFile::getFile() {
|
std::string getContents() {
|
||||||
return m_file;
|
ReusableStringStream sstr;
|
||||||
}
|
constexpr long buffer_size = 100;
|
||||||
|
char buffer[buffer_size + 1] = {};
|
||||||
std::string TempFile::getContents() {
|
long current_pos = ftell( m_file );
|
||||||
std::stringstream sstr;
|
CATCH_ENFORCE( current_pos >= 0,
|
||||||
char buffer[100] = {};
|
"ftell failed, errno: " << errno );
|
||||||
std::rewind(m_file);
|
std::rewind( m_file );
|
||||||
while (std::fgets(buffer, sizeof(buffer), m_file)) {
|
while ( current_pos > 0 ) {
|
||||||
|
auto read_characters =
|
||||||
|
std::fread( buffer,
|
||||||
|
1,
|
||||||
|
std::min( buffer_size, current_pos ),
|
||||||
|
m_file );
|
||||||
|
buffer[read_characters] = '\0';
|
||||||
sstr << buffer;
|
sstr << buffer;
|
||||||
|
current_pos -= static_cast<long>( read_characters );
|
||||||
}
|
}
|
||||||
return sstr.str();
|
return sstr.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
OutputRedirect::OutputRedirect(std::string& stdout_dest, std::string& stderr_dest) :
|
void clear() { std::rewind( m_file ); }
|
||||||
m_originalStdout(dup(1)),
|
|
||||||
m_originalStderr(dup(2)),
|
|
||||||
m_stdoutDest(stdout_dest),
|
|
||||||
m_stderrDest(stderr_dest) {
|
|
||||||
dup2(fileno(m_stdoutFile.getFile()), 1);
|
|
||||||
dup2(fileno(m_stderrFile.getFile()), 2);
|
|
||||||
}
|
|
||||||
|
|
||||||
OutputRedirect::~OutputRedirect() {
|
private:
|
||||||
|
std::FILE* m_file = nullptr;
|
||||||
|
char m_buffer[L_tmpnam] = { 0 };
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Redirects the actual `stdout`/`stderr` file descriptors.
|
||||||
|
*
|
||||||
|
* Works by replacing the file descriptors numbered 1 and 2
|
||||||
|
* with an open temporary file.
|
||||||
|
*/
|
||||||
|
class FileRedirect : public OutputRedirect {
|
||||||
|
TempFile m_outFile, m_errFile;
|
||||||
|
int m_originalOut = -1;
|
||||||
|
int m_originalErr = -1;
|
||||||
|
|
||||||
|
// Flushes cout/cerr/clog streams and stdout/stderr FDs
|
||||||
|
void flushEverything() {
|
||||||
Catch::cout() << std::flush;
|
Catch::cout() << std::flush;
|
||||||
fflush(stdout);
|
fflush( stdout );
|
||||||
// Since we support overriding these streams, we flush cerr
|
// Since we support overriding these streams, we flush cerr
|
||||||
// even though std::cerr is unbuffered
|
// even though std::cerr is unbuffered
|
||||||
Catch::cerr() << std::flush;
|
Catch::cerr() << std::flush;
|
||||||
Catch::clog() << std::flush;
|
Catch::clog() << std::flush;
|
||||||
fflush(stderr);
|
fflush( stderr );
|
||||||
|
|
||||||
dup2(m_originalStdout, 1);
|
|
||||||
dup2(m_originalStderr, 2);
|
|
||||||
|
|
||||||
m_stdoutDest += m_stdoutFile.getContents();
|
|
||||||
m_stderrDest += m_stderrFile.getContents();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public:
|
||||||
|
FileRedirect():
|
||||||
|
m_originalOut( dup( fileno( stdout ) ) ),
|
||||||
|
m_originalErr( dup( fileno( stderr ) ) ) {
|
||||||
|
CATCH_ENFORCE( m_originalOut >= 0, "Could not dup stdout" );
|
||||||
|
CATCH_ENFORCE( m_originalErr >= 0, "Could not dup stderr" );
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string getStdout() override { return m_outFile.getContents(); }
|
||||||
|
std::string getStderr() override { return m_errFile.getContents(); }
|
||||||
|
void clearBuffers() override {
|
||||||
|
m_outFile.clear();
|
||||||
|
m_errFile.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
void activateImpl() override {
|
||||||
|
// We flush before starting redirect, to ensure that we do
|
||||||
|
// not capture the end of message sent before activation.
|
||||||
|
flushEverything();
|
||||||
|
|
||||||
|
int ret;
|
||||||
|
ret = dup2( fileno( m_outFile.getFile() ), fileno( stdout ) );
|
||||||
|
CATCH_ENFORCE( ret >= 0,
|
||||||
|
"dup2 to stdout has failed, errno: " << errno );
|
||||||
|
ret = dup2( fileno( m_errFile.getFile() ), fileno( stderr ) );
|
||||||
|
CATCH_ENFORCE( ret >= 0,
|
||||||
|
"dup2 to stderr has failed, errno: " << errno );
|
||||||
|
}
|
||||||
|
void deactivateImpl() override {
|
||||||
|
// We flush before ending redirect, to ensure that we
|
||||||
|
// capture all messages sent while the redirect was active.
|
||||||
|
flushEverything();
|
||||||
|
|
||||||
|
int ret;
|
||||||
|
ret = dup2( m_originalOut, fileno( stdout ) );
|
||||||
|
CATCH_ENFORCE(
|
||||||
|
ret >= 0,
|
||||||
|
"dup2 of original stdout has failed, errno: " << errno );
|
||||||
|
ret = dup2( m_originalErr, fileno( stderr ) );
|
||||||
|
CATCH_ENFORCE(
|
||||||
|
ret >= 0,
|
||||||
|
"dup2 of original stderr has failed, errno: " << errno );
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
#endif // CATCH_CONFIG_NEW_CAPTURE
|
#endif // CATCH_CONFIG_NEW_CAPTURE
|
||||||
|
|
||||||
|
} // end namespace
|
||||||
|
|
||||||
|
bool isRedirectAvailable( OutputRedirect::Kind kind ) {
|
||||||
|
switch ( kind ) {
|
||||||
|
// These two are always available
|
||||||
|
case OutputRedirect::None:
|
||||||
|
case OutputRedirect::Streams:
|
||||||
|
return true;
|
||||||
|
#if defined( CATCH_CONFIG_NEW_CAPTURE )
|
||||||
|
case OutputRedirect::FileDescriptors:
|
||||||
|
return true;
|
||||||
|
#endif
|
||||||
|
default:
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Detail::unique_ptr<OutputRedirect> makeOutputRedirect( bool actual ) {
|
||||||
|
if ( actual ) {
|
||||||
|
// TODO: Clean this up later
|
||||||
|
#if defined( CATCH_CONFIG_NEW_CAPTURE )
|
||||||
|
return Detail::make_unique<FileRedirect>();
|
||||||
|
#else
|
||||||
|
return Detail::make_unique<StreamRedirect>();
|
||||||
|
#endif
|
||||||
|
} else {
|
||||||
|
return Detail::make_unique<NoopRedirect>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
RedirectGuard scopedActivate( OutputRedirect& redirectImpl ) {
|
||||||
|
return RedirectGuard( true, redirectImpl );
|
||||||
|
}
|
||||||
|
|
||||||
|
RedirectGuard scopedDeactivate( OutputRedirect& redirectImpl ) {
|
||||||
|
return RedirectGuard( false, redirectImpl );
|
||||||
|
}
|
||||||
|
|
||||||
|
OutputRedirect::~OutputRedirect() = default;
|
||||||
|
|
||||||
|
RedirectGuard::RedirectGuard( bool activate, OutputRedirect& redirectImpl ):
|
||||||
|
m_redirect( &redirectImpl ),
|
||||||
|
m_activate( activate ),
|
||||||
|
m_previouslyActive( redirectImpl.isActive() ) {
|
||||||
|
|
||||||
|
// Skip cases where there is no actual state change.
|
||||||
|
if ( m_activate == m_previouslyActive ) { return; }
|
||||||
|
|
||||||
|
if ( m_activate ) {
|
||||||
|
m_redirect->activate();
|
||||||
|
} else {
|
||||||
|
m_redirect->deactivate();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
RedirectGuard::~RedirectGuard() noexcept( false ) {
|
||||||
|
if ( m_moved ) { return; }
|
||||||
|
// Skip cases where there is no actual state change.
|
||||||
|
if ( m_activate == m_previouslyActive ) { return; }
|
||||||
|
|
||||||
|
if ( m_activate ) {
|
||||||
|
m_redirect->deactivate();
|
||||||
|
} else {
|
||||||
|
m_redirect->activate();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
RedirectGuard::RedirectGuard( RedirectGuard&& rhs ) noexcept:
|
||||||
|
m_redirect( rhs.m_redirect ),
|
||||||
|
m_activate( rhs.m_activate ),
|
||||||
|
m_previouslyActive( rhs.m_previouslyActive ),
|
||||||
|
m_moved( false ) {
|
||||||
|
rhs.m_moved = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
RedirectGuard& RedirectGuard::operator=( RedirectGuard&& rhs ) noexcept {
|
||||||
|
m_redirect = rhs.m_redirect;
|
||||||
|
m_activate = rhs.m_activate;
|
||||||
|
m_previouslyActive = rhs.m_previouslyActive;
|
||||||
|
m_moved = false;
|
||||||
|
rhs.m_moved = true;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace Catch
|
} // namespace Catch
|
||||||
|
|
||||||
#if defined(CATCH_CONFIG_NEW_CAPTURE)
|
#if defined( CATCH_CONFIG_NEW_CAPTURE )
|
||||||
#if defined(_MSC_VER)
|
# if defined( _MSC_VER )
|
||||||
#undef dup
|
# undef dup
|
||||||
#undef dup2
|
# undef dup2
|
||||||
#undef fileno
|
# undef fileno
|
||||||
#endif
|
# endif
|
||||||
#endif
|
#endif
|
||||||
|
@ -8,110 +8,69 @@
|
|||||||
#ifndef CATCH_OUTPUT_REDIRECT_HPP_INCLUDED
|
#ifndef CATCH_OUTPUT_REDIRECT_HPP_INCLUDED
|
||||||
#define CATCH_OUTPUT_REDIRECT_HPP_INCLUDED
|
#define CATCH_OUTPUT_REDIRECT_HPP_INCLUDED
|
||||||
|
|
||||||
#include <catch2/internal/catch_platform.hpp>
|
#include <catch2/internal/catch_unique_ptr.hpp>
|
||||||
#include <catch2/internal/catch_reusable_string_stream.hpp>
|
|
||||||
#include <catch2/internal/catch_compiler_capabilities.hpp>
|
|
||||||
|
|
||||||
#include <cstdio>
|
#include <cassert>
|
||||||
#include <iosfwd>
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
namespace Catch {
|
namespace Catch {
|
||||||
|
|
||||||
class RedirectedStream {
|
|
||||||
std::ostream& m_originalStream;
|
|
||||||
std::ostream& m_redirectionStream;
|
|
||||||
std::streambuf* m_prevBuf;
|
|
||||||
|
|
||||||
public:
|
|
||||||
RedirectedStream( std::ostream& originalStream, std::ostream& redirectionStream );
|
|
||||||
~RedirectedStream();
|
|
||||||
};
|
|
||||||
|
|
||||||
class RedirectedStdOut {
|
|
||||||
ReusableStringStream m_rss;
|
|
||||||
RedirectedStream m_cout;
|
|
||||||
public:
|
|
||||||
RedirectedStdOut();
|
|
||||||
auto str() const -> std::string;
|
|
||||||
};
|
|
||||||
|
|
||||||
// StdErr has two constituent streams in C++, std::cerr and std::clog
|
|
||||||
// This means that we need to redirect 2 streams into 1 to keep proper
|
|
||||||
// order of writes
|
|
||||||
class RedirectedStdErr {
|
|
||||||
ReusableStringStream m_rss;
|
|
||||||
RedirectedStream m_cerr;
|
|
||||||
RedirectedStream m_clog;
|
|
||||||
public:
|
|
||||||
RedirectedStdErr();
|
|
||||||
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)
|
|
||||||
|
|
||||||
// Windows's implementation of std::tmpfile is terrible (it tries
|
|
||||||
// to create a file inside system folder, thus requiring elevated
|
|
||||||
// privileges for the binary), so we have to use tmpnam(_s) and
|
|
||||||
// create the file ourselves there.
|
|
||||||
class TempFile {
|
|
||||||
public:
|
|
||||||
TempFile(TempFile const&) = delete;
|
|
||||||
TempFile& operator=(TempFile const&) = delete;
|
|
||||||
TempFile(TempFile&&) = delete;
|
|
||||||
TempFile& operator=(TempFile&&) = delete;
|
|
||||||
|
|
||||||
TempFile();
|
|
||||||
~TempFile();
|
|
||||||
|
|
||||||
std::FILE* getFile();
|
|
||||||
std::string getContents();
|
|
||||||
|
|
||||||
private:
|
|
||||||
std::FILE* m_file = nullptr;
|
|
||||||
#if defined(_MSC_VER)
|
|
||||||
char m_buffer[L_tmpnam] = { 0 };
|
|
||||||
#endif
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
class OutputRedirect {
|
class OutputRedirect {
|
||||||
|
bool m_redirectActive = false;
|
||||||
|
virtual void activateImpl() = 0;
|
||||||
|
virtual void deactivateImpl() = 0;
|
||||||
public:
|
public:
|
||||||
OutputRedirect(OutputRedirect const&) = delete;
|
enum Kind {
|
||||||
OutputRedirect& operator=(OutputRedirect const&) = delete;
|
//! No redirect (noop implementation)
|
||||||
OutputRedirect(OutputRedirect&&) = delete;
|
None,
|
||||||
OutputRedirect& operator=(OutputRedirect&&) = delete;
|
//! Redirect std::cout/std::cerr/std::clog streams internally
|
||||||
|
Streams,
|
||||||
|
//! Redirect the stdout/stderr file descriptors into files
|
||||||
OutputRedirect(std::string& stdout_dest, std::string& stderr_dest);
|
FileDescriptors,
|
||||||
~OutputRedirect();
|
|
||||||
|
|
||||||
private:
|
|
||||||
int m_originalStdout = -1;
|
|
||||||
int m_originalStderr = -1;
|
|
||||||
TempFile m_stdoutFile;
|
|
||||||
TempFile m_stderrFile;
|
|
||||||
std::string& m_stdoutDest;
|
|
||||||
std::string& m_stderrDest;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
virtual ~OutputRedirect(); // = default;
|
||||||
|
|
||||||
|
// TODO: Do we want to check that redirect is not active before retrieving the output?
|
||||||
|
virtual std::string getStdout() = 0;
|
||||||
|
virtual std::string getStderr() = 0;
|
||||||
|
virtual void clearBuffers() = 0;
|
||||||
|
bool isActive() const { return m_redirectActive; }
|
||||||
|
void activate() {
|
||||||
|
assert( !m_redirectActive && "redirect is already active" );
|
||||||
|
activateImpl();
|
||||||
|
m_redirectActive = true;
|
||||||
|
}
|
||||||
|
void deactivate() {
|
||||||
|
assert( m_redirectActive && "redirect is not active" );
|
||||||
|
deactivateImpl();
|
||||||
|
m_redirectActive = false;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
bool isRedirectAvailable( OutputRedirect::Kind kind);
|
||||||
|
Detail::unique_ptr<OutputRedirect> makeOutputRedirect( bool actual );
|
||||||
|
|
||||||
|
class RedirectGuard {
|
||||||
|
OutputRedirect* m_redirect;
|
||||||
|
bool m_activate;
|
||||||
|
bool m_previouslyActive;
|
||||||
|
bool m_moved = false;
|
||||||
|
|
||||||
|
public:
|
||||||
|
RedirectGuard( bool activate, OutputRedirect& redirectImpl );
|
||||||
|
~RedirectGuard() noexcept( false );
|
||||||
|
|
||||||
|
RedirectGuard( RedirectGuard const& ) = delete;
|
||||||
|
RedirectGuard& operator=( RedirectGuard const& ) = delete;
|
||||||
|
|
||||||
|
// C++14 needs move-able guards to return them from functions
|
||||||
|
RedirectGuard( RedirectGuard&& rhs ) noexcept;
|
||||||
|
RedirectGuard& operator=( RedirectGuard&& rhs ) noexcept;
|
||||||
|
};
|
||||||
|
|
||||||
|
RedirectGuard scopedActivate( OutputRedirect& redirectImpl );
|
||||||
|
RedirectGuard scopedDeactivate( OutputRedirect& redirectImpl );
|
||||||
|
|
||||||
} // end namespace Catch
|
} // end namespace Catch
|
||||||
|
|
||||||
|
@ -170,6 +170,7 @@ namespace Catch {
|
|||||||
m_config(_config),
|
m_config(_config),
|
||||||
m_reporter(CATCH_MOVE(reporter)),
|
m_reporter(CATCH_MOVE(reporter)),
|
||||||
m_lastAssertionInfo{ StringRef(), SourceLineInfo("",0), StringRef(), ResultDisposition::Normal },
|
m_lastAssertionInfo{ StringRef(), SourceLineInfo("",0), StringRef(), ResultDisposition::Normal },
|
||||||
|
m_outputRedirect( makeOutputRedirect( m_reporter->getPreferences().shouldRedirectStdOut ) ),
|
||||||
m_includeSuccessfulResults( m_config->includeSuccessfulResults() || m_reporter->getPreferences().shouldReportAllAssertions )
|
m_includeSuccessfulResults( m_config->includeSuccessfulResults() || m_reporter->getPreferences().shouldReportAllAssertions )
|
||||||
{
|
{
|
||||||
getCurrentMutableContext().setResultCapture( this );
|
getCurrentMutableContext().setResultCapture( this );
|
||||||
@ -236,15 +237,17 @@ namespace Catch {
|
|||||||
m_reporter->testCasePartialStarting(testInfo, testRuns);
|
m_reporter->testCasePartialStarting(testInfo, testRuns);
|
||||||
|
|
||||||
const auto beforeRunTotals = m_totals;
|
const auto beforeRunTotals = m_totals;
|
||||||
std::string oneRunCout, oneRunCerr;
|
runCurrentTest();
|
||||||
runCurrentTest(oneRunCout, oneRunCerr);
|
std::string oneRunCout = m_outputRedirect->getStdout();
|
||||||
|
std::string oneRunCerr = m_outputRedirect->getStderr();
|
||||||
|
m_outputRedirect->clearBuffers();
|
||||||
redirectedCout += oneRunCout;
|
redirectedCout += oneRunCout;
|
||||||
redirectedCerr += oneRunCerr;
|
redirectedCerr += oneRunCerr;
|
||||||
|
|
||||||
const auto singleRunTotals = m_totals.delta(beforeRunTotals);
|
const auto singleRunTotals = m_totals.delta(beforeRunTotals);
|
||||||
auto statsForOneRun = TestCaseStats(testInfo, singleRunTotals, CATCH_MOVE(oneRunCout), CATCH_MOVE(oneRunCerr), aborting());
|
auto statsForOneRun = TestCaseStats(testInfo, singleRunTotals, CATCH_MOVE(oneRunCout), CATCH_MOVE(oneRunCerr), aborting());
|
||||||
|
|
||||||
m_reporter->testCasePartialEnded(statsForOneRun, testRuns);
|
m_reporter->testCasePartialEnded(statsForOneRun, testRuns);
|
||||||
|
|
||||||
++testRuns;
|
++testRuns;
|
||||||
} while (!m_testCaseTracker->isSuccessfullyCompleted() && !aborting());
|
} while (!m_testCaseTracker->isSuccessfullyCompleted() && !aborting());
|
||||||
|
|
||||||
@ -289,7 +292,10 @@ namespace Catch {
|
|||||||
m_lastAssertionPassed = true;
|
m_lastAssertionPassed = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
m_reporter->assertionEnded(AssertionStats(result, m_messages, m_totals));
|
{
|
||||||
|
auto _ = scopedDeactivate( *m_outputRedirect );
|
||||||
|
m_reporter->assertionEnded( AssertionStats( result, m_messages, m_totals ) );
|
||||||
|
}
|
||||||
|
|
||||||
if ( result.getResultType() != ResultWas::Warning ) {
|
if ( result.getResultType() != ResultWas::Warning ) {
|
||||||
m_messageScopes.clear();
|
m_messageScopes.clear();
|
||||||
@ -306,6 +312,7 @@ namespace Catch {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void RunContext::notifyAssertionStarted( AssertionInfo const& info ) {
|
void RunContext::notifyAssertionStarted( AssertionInfo const& info ) {
|
||||||
|
auto _ = scopedDeactivate( *m_outputRedirect );
|
||||||
m_reporter->assertionStarting( info );
|
m_reporter->assertionStarting( info );
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -324,7 +331,10 @@ namespace Catch {
|
|||||||
SectionInfo sectionInfo( sectionLineInfo, static_cast<std::string>(sectionName) );
|
SectionInfo sectionInfo( sectionLineInfo, static_cast<std::string>(sectionName) );
|
||||||
m_lastAssertionInfo.lineInfo = sectionInfo.lineInfo;
|
m_lastAssertionInfo.lineInfo = sectionInfo.lineInfo;
|
||||||
|
|
||||||
m_reporter->sectionStarting(sectionInfo);
|
{
|
||||||
|
auto _ = scopedDeactivate( *m_outputRedirect );
|
||||||
|
m_reporter->sectionStarting( sectionInfo );
|
||||||
|
}
|
||||||
|
|
||||||
assertions = m_totals.assertions;
|
assertions = m_totals.assertions;
|
||||||
|
|
||||||
@ -384,7 +394,15 @@ namespace Catch {
|
|||||||
m_activeSections.pop_back();
|
m_activeSections.pop_back();
|
||||||
}
|
}
|
||||||
|
|
||||||
m_reporter->sectionEnded(SectionStats(CATCH_MOVE(endInfo.sectionInfo), assertions, endInfo.durationInSeconds, missingAssertions));
|
{
|
||||||
|
auto _ = scopedDeactivate( *m_outputRedirect );
|
||||||
|
m_reporter->sectionEnded(
|
||||||
|
SectionStats( CATCH_MOVE( endInfo.sectionInfo ),
|
||||||
|
assertions,
|
||||||
|
endInfo.durationInSeconds,
|
||||||
|
missingAssertions ) );
|
||||||
|
}
|
||||||
|
|
||||||
m_messages.clear();
|
m_messages.clear();
|
||||||
m_messageScopes.clear();
|
m_messageScopes.clear();
|
||||||
}
|
}
|
||||||
@ -401,15 +419,19 @@ namespace Catch {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void RunContext::benchmarkPreparing( StringRef name ) {
|
void RunContext::benchmarkPreparing( StringRef name ) {
|
||||||
m_reporter->benchmarkPreparing(name);
|
auto _ = scopedDeactivate( *m_outputRedirect );
|
||||||
|
m_reporter->benchmarkPreparing( name );
|
||||||
}
|
}
|
||||||
void RunContext::benchmarkStarting( BenchmarkInfo const& info ) {
|
void RunContext::benchmarkStarting( BenchmarkInfo const& info ) {
|
||||||
|
auto _ = scopedDeactivate( *m_outputRedirect );
|
||||||
m_reporter->benchmarkStarting( info );
|
m_reporter->benchmarkStarting( info );
|
||||||
}
|
}
|
||||||
void RunContext::benchmarkEnded( BenchmarkStats<> const& stats ) {
|
void RunContext::benchmarkEnded( BenchmarkStats<> const& stats ) {
|
||||||
|
auto _ = scopedDeactivate( *m_outputRedirect );
|
||||||
m_reporter->benchmarkEnded( stats );
|
m_reporter->benchmarkEnded( stats );
|
||||||
}
|
}
|
||||||
void RunContext::benchmarkFailed( StringRef error ) {
|
void RunContext::benchmarkFailed( StringRef error ) {
|
||||||
|
auto _ = scopedDeactivate( *m_outputRedirect );
|
||||||
m_reporter->benchmarkFailed( error );
|
m_reporter->benchmarkFailed( error );
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -440,8 +462,13 @@ namespace Catch {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void RunContext::handleFatalErrorCondition( StringRef message ) {
|
void RunContext::handleFatalErrorCondition( StringRef message ) {
|
||||||
|
// TODO: scoped deactivate here? Just give up and do best effort?
|
||||||
|
// the deactivation can break things further, OTOH so can the
|
||||||
|
// capture
|
||||||
|
auto _ = scopedDeactivate( *m_outputRedirect );
|
||||||
|
|
||||||
// First notify reporter that bad things happened
|
// First notify reporter that bad things happened
|
||||||
m_reporter->fatalErrorEncountered(message);
|
m_reporter->fatalErrorEncountered( message );
|
||||||
|
|
||||||
// Don't rebuild the result -- the stringification itself can cause more fatal errors
|
// Don't rebuild the result -- the stringification itself can cause more fatal errors
|
||||||
// Instead, fake a result data.
|
// Instead, fake a result data.
|
||||||
@ -468,7 +495,7 @@ namespace Catch {
|
|||||||
Counts assertions;
|
Counts assertions;
|
||||||
assertions.failed = 1;
|
assertions.failed = 1;
|
||||||
SectionStats testCaseSectionStats(CATCH_MOVE(testCaseSection), assertions, 0, false);
|
SectionStats testCaseSectionStats(CATCH_MOVE(testCaseSection), assertions, 0, false);
|
||||||
m_reporter->sectionEnded(testCaseSectionStats);
|
m_reporter->sectionEnded( testCaseSectionStats );
|
||||||
|
|
||||||
auto const& testInfo = m_activeTestCase->getTestCaseInfo();
|
auto const& testInfo = m_activeTestCase->getTestCaseInfo();
|
||||||
|
|
||||||
@ -499,7 +526,7 @@ namespace Catch {
|
|||||||
return m_totals.assertions.failed >= static_cast<std::size_t>(m_config->abortAfter());
|
return m_totals.assertions.failed >= static_cast<std::size_t>(m_config->abortAfter());
|
||||||
}
|
}
|
||||||
|
|
||||||
void RunContext::runCurrentTest(std::string & redirectedCout, std::string & redirectedCerr) {
|
void RunContext::runCurrentTest() {
|
||||||
auto const& testCaseInfo = m_activeTestCase->getTestCaseInfo();
|
auto const& testCaseInfo = m_activeTestCase->getTestCaseInfo();
|
||||||
SectionInfo testCaseSection(testCaseInfo.lineInfo, testCaseInfo.name);
|
SectionInfo testCaseSection(testCaseInfo.lineInfo, testCaseInfo.name);
|
||||||
m_reporter->sectionStarting(testCaseSection);
|
m_reporter->sectionStarting(testCaseSection);
|
||||||
@ -510,18 +537,8 @@ namespace Catch {
|
|||||||
|
|
||||||
Timer timer;
|
Timer timer;
|
||||||
CATCH_TRY {
|
CATCH_TRY {
|
||||||
if (m_reporter->getPreferences().shouldRedirectStdOut) {
|
{
|
||||||
#if !defined(CATCH_CONFIG_EXPERIMENTAL_REDIRECT)
|
auto _ = scopedActivate( *m_outputRedirect );
|
||||||
RedirectedStreams redirectedStreams(redirectedCout, redirectedCerr);
|
|
||||||
|
|
||||||
timer.start();
|
|
||||||
invokeActiveTestCase();
|
|
||||||
#else
|
|
||||||
OutputRedirect r(redirectedCout, redirectedCerr);
|
|
||||||
timer.start();
|
|
||||||
invokeActiveTestCase();
|
|
||||||
#endif
|
|
||||||
} else {
|
|
||||||
timer.start();
|
timer.start();
|
||||||
invokeActiveTestCase();
|
invokeActiveTestCase();
|
||||||
}
|
}
|
||||||
@ -566,11 +583,12 @@ namespace Catch {
|
|||||||
void RunContext::handleUnfinishedSections() {
|
void RunContext::handleUnfinishedSections() {
|
||||||
// If sections ended prematurely due to an exception we stored their
|
// If sections ended prematurely due to an exception we stored their
|
||||||
// infos here so we can tear them down outside the unwind process.
|
// infos here so we can tear them down outside the unwind process.
|
||||||
for (auto it = m_unfinishedSections.rbegin(),
|
for ( auto it = m_unfinishedSections.rbegin(),
|
||||||
itEnd = m_unfinishedSections.rend();
|
itEnd = m_unfinishedSections.rend();
|
||||||
it != itEnd;
|
it != itEnd;
|
||||||
++it)
|
++it ) {
|
||||||
sectionEnded(CATCH_MOVE(*it));
|
sectionEnded( CATCH_MOVE( *it ) );
|
||||||
|
}
|
||||||
m_unfinishedSections.clear();
|
m_unfinishedSections.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -29,6 +29,7 @@ namespace Catch {
|
|||||||
class IConfig;
|
class IConfig;
|
||||||
class IEventListener;
|
class IEventListener;
|
||||||
using IEventListenerPtr = Detail::unique_ptr<IEventListener>;
|
using IEventListenerPtr = Detail::unique_ptr<IEventListener>;
|
||||||
|
class OutputRedirect;
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
@ -115,7 +116,7 @@ namespace Catch {
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
void runCurrentTest( std::string& redirectedCout, std::string& redirectedCerr );
|
void runCurrentTest();
|
||||||
void invokeActiveTestCase();
|
void invokeActiveTestCase();
|
||||||
|
|
||||||
void resetAssertionInfo();
|
void resetAssertionInfo();
|
||||||
@ -148,6 +149,7 @@ namespace Catch {
|
|||||||
std::vector<SectionEndInfo> m_unfinishedSections;
|
std::vector<SectionEndInfo> m_unfinishedSections;
|
||||||
std::vector<ITracker*> m_activeSections;
|
std::vector<ITracker*> m_activeSections;
|
||||||
TrackerContext m_trackerContext;
|
TrackerContext m_trackerContext;
|
||||||
|
Detail::unique_ptr<OutputRedirect> m_outputRedirect;
|
||||||
FatalConditionHandler m_fatalConditionhandler;
|
FatalConditionHandler m_fatalConditionhandler;
|
||||||
bool m_lastAssertionPassed = false;
|
bool m_lastAssertionPassed = false;
|
||||||
bool m_shouldReportUnexpected = true;
|
bool m_shouldReportUnexpected = true;
|
||||||
|
Loading…
Reference in New Issue
Block a user