Change Bazel XML support to depend upon BAZEL_TEST

This commit is contained in:
Brandon Jones 2022-06-14 10:56:19 +01:00 committed by Martin Hořeňovský
parent 078201fcf4
commit 7e4ec432d0
4 changed files with 64 additions and 18 deletions

View File

@ -98,11 +98,13 @@ is equivalent with the out-of-the-box experience.
## Bazel support ## Bazel support
When `CATCH_CONFIG_BAZEL_SUPPORT` is defined, Catch2 will register a `JUnit` When `CATCH_CONFIG_BAZEL_SUPPORT` is defined or when `BAZEL_TEST=1` (which is set by the Bazel inside of a test environment),
reporter writing to a path pointed by `XML_OUTPUT_FILE` provided by Bazel. Catch2 will register a `JUnit` reporter writing to a path pointed by `XML_OUTPUT_FILE` provided by Bazel.
> `CATCH_CONFIG_BAZEL_SUPPORT` was [introduced](https://github.com/catchorg/Catch2/pull/2399) in Catch2 3.0.1. > `CATCH_CONFIG_BAZEL_SUPPORT` was [introduced](https://github.com/catchorg/Catch2/pull/2399) in Catch2 3.0.1.
> `CATCH_CONFIG_BAZEL_SUPPORT` was [deprecated](https://github.com/catchorg/Catch2/pull/2459) in Catch2 X.Y.Z.
## C++11 toggles ## C++11 toggles
CATCH_CONFIG_CPP11_TO_STRING // Use `std::to_string` CATCH_CONFIG_CPP11_TO_STRING // Use `std::to_string`

View File

@ -17,6 +17,15 @@ as it can be replaced by `Catch.cmake` that provides the function
command line interface instead of parsing C++ code with regular expressions. command line interface instead of parsing C++ code with regular expressions.
### `CATCH_CONFIG_BAZEL_SUPPORT`
Catch2 supports writing the Bazel JUnit XML output file when it is aware
that is within a bazel testing environment. Originally there was no way
to accurately probe the environment for this information so the flag
`CATCH_CONFIG_BAZEL_SUPPORT` was added. This now deprecated. Bazel has now had a change
where it will export `BAZEL_TEST=1` for purposes like the above. Catch2
will now instead inspect the environment instead of relying on build configuration.
--- ---
[Home](Readme.md#top) [Home](Readme.md#top)

View File

@ -15,6 +15,9 @@
namespace Catch { namespace Catch {
// Requires declaration for -Wmissing-declarations.
bool provideBazelReporterOutput();
bool operator==( ProcessedReporterSpec const& lhs, bool operator==( ProcessedReporterSpec const& lhs,
ProcessedReporterSpec const& rhs ) { ProcessedReporterSpec const& rhs ) {
return lhs.name == rhs.name && return lhs.name == rhs.name &&
@ -59,27 +62,27 @@ namespace Catch {
} ); } );
} }
#if defined( CATCH_CONFIG_BAZEL_SUPPORT ) if(provideBazelReporterOutput()){
// Register a JUnit reporter for Bazel. Bazel sets an environment // Register a JUnit reporter for Bazel. Bazel sets an environment
// variable with the path to XML output. If this file is written to // variable with the path to XML output. If this file is written to
// during test, Bazel will not generate a default XML output. // during test, Bazel will not generate a default XML output.
// This allows the XML output file to contain higher level of detail // This allows the XML output file to contain higher level of detail
// than what is possible otherwise. // than what is possible otherwise.
# if defined( _MSC_VER ) # if defined( _MSC_VER )
// On Windows getenv throws a warning as there is no input validation, // On Windows getenv throws a warning as there is no input validation,
// since the key is hardcoded, this should not be an issue. // since the key is hardcoded, this should not be an issue.
# pragma warning( push ) # pragma warning( push )
# pragma warning( disable : 4996 ) # pragma warning( disable : 4996 )
# endif # endif
const auto bazelOutputFilePtr = std::getenv( "XML_OUTPUT_FILE" ); const auto bazelOutputFilePtr = std::getenv( "XML_OUTPUT_FILE" );
# if defined( _MSC_VER ) # if defined( _MSC_VER )
# pragma warning( pop ) # pragma warning( pop )
# endif # endif
if ( bazelOutputFilePtr != nullptr ) { if ( bazelOutputFilePtr != nullptr ) {
m_data.reporterSpecifications.push_back( m_data.reporterSpecifications.push_back(
{ "junit", std::string( bazelOutputFilePtr ), {}, {} } ); { "junit", std::string( bazelOutputFilePtr ), {}, {} } );
} }
#endif }
// We now fixup the reporter specs to handle default output spec, // We now fixup the reporter specs to handle default output spec,
@ -113,6 +116,26 @@ namespace Catch {
bool Config::listReporters() const { return m_data.listReporters; } bool Config::listReporters() const { return m_data.listReporters; }
bool Config::listListeners() const { return m_data.listListeners; } bool Config::listListeners() const { return m_data.listListeners; }
bool provideBazelReporterOutput() {
#ifdef CATCH_CONFIG_BAZEL_SUPPORT
return true;
#else
# if defined( _MSC_VER )
// On Windows getenv throws a warning as there is no input validation,
// since the switch is hardcoded, this should not be an issue.
# pragma warning( push )
# pragma warning( disable : 4996 )
# endif
return std::getenv("BAZEL_TEST") != nullptr;
# if defined( _MSC_VER )
# pragma warning( pop )
# endif
#endif
}
std::vector<std::string> const& Config::getTestsOrTags() const { return m_data.testsOrTags; } std::vector<std::string> const& Config::getTestsOrTags() const { return m_data.testsOrTags; }
std::vector<std::string> const& Config::getSectionsToRun() const { return m_data.sectionsToRun; } std::vector<std::string> const& Config::getSectionsToRun() const { return m_data.sectionsToRun; }

View File

@ -137,6 +137,18 @@ set_tests_properties(CATCH_CONFIG_BAZEL_REPORTER-1
LABELS "uses-python" LABELS "uses-python"
) )
# We must now test this works without the build flag.
add_executable( BazelReporterNoCatchConfig ${TESTS_DIR}/X30-BazelReporter.cpp )
target_link_libraries(BazelReporterNoCatchConfig Catch2WithMain)
add_test(NAME NO_CATCH_CONFIG_BAZEL_REPORTER-1
COMMAND
"${PYTHON_EXECUTABLE}" "${CATCH_DIR}/tests/TestScripts/testBazelReporter.py" $<TARGET_FILE:BazelReporterNoCatchConfig> "${CMAKE_CURRENT_BINARY_DIR}"
)
set_tests_properties(NO_CATCH_CONFIG_BAZEL_REPORTER-1
PROPERTIES
LABELS "uses-python"
ENVIRONMENT "BAZEL_TEST=1"
)
# The default handler on Windows leads to the just-in-time debugger firing, # The default handler on Windows leads to the just-in-time debugger firing,
# which makes this test unsuitable for CI and headless runs, as it opens # which makes this test unsuitable for CI and headless runs, as it opens