Simplify handling of environment in Bazel's support

This commit is contained in:
Martin Hořeňovský 2022-10-22 13:10:26 +02:00
parent 3fed2307e7
commit d1e7544e9f
No known key found for this signature in database
GPG Key ID: DE48307B8B0D381A

View File

@ -9,12 +9,12 @@
#include <catch2/catch_user_config.hpp> #include <catch2/catch_user_config.hpp>
#include <catch2/internal/catch_enforce.hpp> #include <catch2/internal/catch_enforce.hpp>
#include <catch2/internal/catch_parse_numbers.hpp> #include <catch2/internal/catch_parse_numbers.hpp>
#include <catch2/internal/catch_platform.hpp>
#include <catch2/internal/catch_stdstreams.hpp> #include <catch2/internal/catch_stdstreams.hpp>
#include <catch2/internal/catch_stringref.hpp> #include <catch2/internal/catch_stringref.hpp>
#include <catch2/internal/catch_string_manip.hpp> #include <catch2/internal/catch_string_manip.hpp>
#include <catch2/internal/catch_test_spec_parser.hpp> #include <catch2/internal/catch_test_spec_parser.hpp>
#include <catch2/interfaces/catch_interfaces_tag_alias_registry.hpp> #include <catch2/interfaces/catch_interfaces_tag_alias_registry.hpp>
#include <catch2/internal/catch_getenv.hpp>
#include <fstream> #include <fstream>
@ -24,24 +24,8 @@ namespace Catch {
static bool enableBazelEnvSupport() { static bool enableBazelEnvSupport() {
#if defined( CATCH_CONFIG_BAZEL_SUPPORT ) #if defined( CATCH_CONFIG_BAZEL_SUPPORT )
return true; return true;
#elif defined( CATCH_PLATFORM_WINDOWS_UWP )
// UWP does not support environment variables
return false;
#else #else
return Detail::getEnv( "BAZEL_TEST" ) != nullptr;
# 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 #endif
} }
@ -51,23 +35,9 @@ namespace Catch {
}; };
static Optional<bazelShardingOptions> readBazelShardingOptions() { static Optional<bazelShardingOptions> readBazelShardingOptions() {
#if defined( CATCH_PLATFORM_WINDOWS_UWP ) const auto bazelShardIndex = Detail::getEnv( "TEST_SHARD_INDEX" );
// We cannot read environment variables on UWP platforms const auto bazelShardTotal = Detail::getEnv( "TEST_TOTAL_SHARDS" );
return {} const auto bazelShardInfoFile = Detail::getEnv( "TEST_SHARD_STATUS_FILE" );
#else
# if defined( _MSC_VER )
# pragma warning( push )
# pragma warning( disable : 4996 ) // use getenv_s instead of getenv
# endif
const auto bazelShardIndex = std::getenv( "TEST_SHARD_INDEX" );
const auto bazelShardTotal = std::getenv( "TEST_TOTAL_SHARDS" );
const auto bazelShardInfoFile = std::getenv( "TEST_SHARD_STATUS_FILE" );
# if defined( _MSC_VER )
# pragma warning( pop )
# endif
const bool has_all = const bool has_all =
@ -110,8 +80,6 @@ namespace Catch {
return bazelShardingOptions{ return bazelShardingOptions{
*shardIndex, *shardTotal, bazelShardInfoFile }; *shardIndex, *shardTotal, bazelShardInfoFile };
#endif
} }
} // end namespace } // end namespace
@ -244,37 +212,25 @@ namespace Catch {
std::chrono::milliseconds Config::benchmarkWarmupTime() const { return std::chrono::milliseconds(m_data.benchmarkWarmupTime); } std::chrono::milliseconds Config::benchmarkWarmupTime() const { return std::chrono::milliseconds(m_data.benchmarkWarmupTime); }
void Config::readBazelEnvVars() { void Config::readBazelEnvVars() {
#if defined( CATCH_PLATFORM_WINDOWS_UWP )
// We cannot read environment variables on UWP platforms
#else
# if defined( _MSC_VER )
# pragma warning( push )
# pragma warning( disable : 4996 ) // use getenv_s instead of getenv
# endif
// 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.
const auto bazelOutputFile = std::getenv( "XML_OUTPUT_FILE" ); const auto bazelOutputFile = Detail::getEnv( "XML_OUTPUT_FILE" );
if ( bazelOutputFile ) { if ( bazelOutputFile ) {
m_data.reporterSpecifications.push_back( m_data.reporterSpecifications.push_back(
{ "junit", std::string( bazelOutputFile ), {}, {} } ); { "junit", std::string( bazelOutputFile ), {}, {} } );
} }
const auto bazelTestSpec = std::getenv( "TESTBRIDGE_TEST_ONLY" ); const auto bazelTestSpec = Detail::getEnv( "TESTBRIDGE_TEST_ONLY" );
if ( bazelTestSpec ) { if ( bazelTestSpec ) {
// Presumably the test spec from environment should overwrite // Presumably the test spec from environment should overwrite
// the one we got from CLI (if we got any) // the one we got from CLI (if we got any)
m_data.testsOrTags.clear(); m_data.testsOrTags.clear();
m_data.testsOrTags.push_back( bazelTestSpec ); m_data.testsOrTags.push_back( bazelTestSpec );
} }
# if defined( _MSC_VER )
# pragma warning( pop )
# endif
const auto bazelShardOptions = readBazelShardingOptions(); const auto bazelShardOptions = readBazelShardingOptions();
if ( bazelShardOptions ) { if ( bazelShardOptions ) {
@ -286,8 +242,6 @@ namespace Catch {
m_data.shardCount = bazelShardOptions->shardCount; m_data.shardCount = bazelShardOptions->shardCount;
} }
} }
#endif
} }
} // end namespace Catch } // end namespace Catch