diff --git a/tests/ExtraTests/CMakeLists.txt b/tests/ExtraTests/CMakeLists.txt index 3244e10f..b503ca44 100644 --- a/tests/ExtraTests/CMakeLists.txt +++ b/tests/ExtraTests/CMakeLists.txt @@ -256,6 +256,51 @@ set_tests_properties( ) +if (MSVC) + set(_NullFile "NUL") +else() + set(_NullFile "/dev/null") +endif() + +add_executable(ListenerStdoutCaptureInMultireporter ${TESTS_DIR}/X24-ListenerStdoutCaptureInMultireporter.cpp) +target_link_libraries(ListenerStdoutCaptureInMultireporter PRIVATE Catch2::Catch2WithMain) + +# This test checks that there is nothing written out from the process, +# but if CMake is running the tests under Valgrind or similar tool, then +# that will write its own output to stdout and the test would fail. +if (NOT MEMORYCHECK_COMMAND) + add_test( + NAME MultiReporter::NoncapturingListenerDoesntCauseStdoutPassThrough + COMMAND ListenerStdoutCaptureInMultireporter + --reporter xml::${_NullFile} + --reporter junit::${_NullFile} + ) + + set_tests_properties( + MultiReporter::NoncapturingListenerDoesntCauseStdoutPassThrough + PROPERTIES + PASS_REGULAR_EXPRESSION "X24 - NonCapturingListener initialized" + FAIL_REGULAR_EXPRESSION "X24 - FooBarBaz" + ) +endif() + + +add_executable(ListenerCanAskForCapturedStdout ${TESTS_DIR}/X25-ListenerCanAskForCapturedStdout.cpp) +target_link_libraries(ListenerCanAskForCapturedStdout PRIVATE Catch2::Catch2WithMain) +add_test( + NAME MultiReporter::CapturingListenerCausesStdoutCapture + COMMAND ListenerCanAskForCapturedStdout + --reporter compact::${_NullFile} + --reporter console::${_NullFile} +) +set_tests_properties( + MultiReporter::CapturingListenerCausesStdoutCapture + PROPERTIES + PASS_REGULAR_EXPRESSION "CapturingListener initialized" + FAIL_REGULAR_EXPRESSION "X25 - ERROR" +) + + add_executable(DuplicatedTestCases-SameNameAndTags ${TESTS_DIR}/X31-DuplicatedTestCases.cpp) target_link_libraries(DuplicatedTestCases-SameNameAndTags PRIVATE Catch2::Catch2WithMain) add_test( diff --git a/tests/ExtraTests/X24-ListenerStdoutCaptureInMultireporter.cpp b/tests/ExtraTests/X24-ListenerStdoutCaptureInMultireporter.cpp new file mode 100644 index 00000000..4c6148fd --- /dev/null +++ b/tests/ExtraTests/X24-ListenerStdoutCaptureInMultireporter.cpp @@ -0,0 +1,40 @@ + +// Copyright Catch2 Authors +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// https://www.boost.org/LICENSE_1_0.txt) + +// SPDX-License-Identifier: BSL-1.0 + +/**\file + * Registers custom listener that does not ask for captured stdout/err + * + * Running the binary with this listener, and asking for multiple _capturing_ + * reporters (one would be sufficient, but that would also mean depending on + * implementation details inside Catch2's handling of listeners), we check that + * nothing is written to stdout, because listeners should not be considered in + * whether the stdout should be passed-through or not. + */ + +#include +#include +#include + +#include + +namespace { + class NonCapturingListener : public Catch::EventListenerBase { + public: + NonCapturingListener( Catch::ReporterConfig const& config ): + EventListenerBase( config ) { + m_preferences.shouldRedirectStdOut = false; + std::cerr << "X24 - NonCapturingListener initialized.\n"; + } + }; +} + +CATCH_REGISTER_LISTENER( NonCapturingListener ) + +TEST_CASE( "Writes to stdout" ) { + std::cout << "X24 - FooBarBaz\n"; +} diff --git a/tests/ExtraTests/X25-ListenerCanAskForCapturedStdout.cpp b/tests/ExtraTests/X25-ListenerCanAskForCapturedStdout.cpp new file mode 100644 index 00000000..e831abca --- /dev/null +++ b/tests/ExtraTests/X25-ListenerCanAskForCapturedStdout.cpp @@ -0,0 +1,47 @@ + +// Copyright Catch2 Authors +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// https://www.boost.org/LICENSE_1_0.txt) + +// SPDX-License-Identifier: BSL-1.0 + +/**\file + * Registers custom listener that asks for captured stdout/err + * + * Running the binary with this listener, and asking for multiple _noncapturing_ + * reporters (one would be sufficient, but that would also mean depending on + * implementation details inside Catch2's handling of listeners), we check that + * the listener gets redirected stdout, even though the reporters didn't ask for + * it. + */ + +#include +#include +#include + +#include + +namespace { + class CapturingListener : public Catch::EventListenerBase { + public: + CapturingListener( Catch::ReporterConfig const& config ): + EventListenerBase( config ) { + m_preferences.shouldRedirectStdOut = true; + std::cerr << "CapturingListener initialized\n"; + } + + void + testCaseEnded(Catch::TestCaseStats const& testCaseStats) override { + if ( testCaseStats.stdOut.empty() ) { + std::cerr << "X25 - ERROR: empty stdout\n"; + } + } + }; +} + +CATCH_REGISTER_LISTENER( CapturingListener ) + +TEST_CASE( "Writes to stdout" ) { + std::cout << "X25 - FooBarBaz\n"; +}