From d1e7544e9f8b233af8ffd37df9bee12492215aff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Ho=C5=99e=C5=88ovsk=C3=BD?= Date: Sat, 22 Oct 2022 13:10:26 +0200 Subject: [PATCH] Simplify handling of environment in Bazel's support --- src/catch2/catch_config.cpp | 60 +++++-------------------------------- 1 file changed, 7 insertions(+), 53 deletions(-) diff --git a/src/catch2/catch_config.cpp b/src/catch2/catch_config.cpp index 19a7551a..c1682fac 100644 --- a/src/catch2/catch_config.cpp +++ b/src/catch2/catch_config.cpp @@ -9,12 +9,12 @@ #include #include #include -#include #include #include #include #include #include +#include #include @@ -24,24 +24,8 @@ namespace Catch { static bool enableBazelEnvSupport() { #if defined( CATCH_CONFIG_BAZEL_SUPPORT ) return true; -#elif defined( CATCH_PLATFORM_WINDOWS_UWP ) - // UWP does not support environment variables - return false; #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 + return Detail::getEnv( "BAZEL_TEST" ) != nullptr; #endif } @@ -51,23 +35,9 @@ namespace Catch { }; static Optional readBazelShardingOptions() { -#if defined( CATCH_PLATFORM_WINDOWS_UWP ) - // We cannot read environment variables on UWP platforms - return {} -#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 auto bazelShardIndex = Detail::getEnv( "TEST_SHARD_INDEX" ); + const auto bazelShardTotal = Detail::getEnv( "TEST_TOTAL_SHARDS" ); + const auto bazelShardInfoFile = Detail::getEnv( "TEST_SHARD_STATUS_FILE" ); const bool has_all = @@ -110,8 +80,6 @@ namespace Catch { return bazelShardingOptions{ *shardIndex, *shardTotal, bazelShardInfoFile }; -#endif - } } // end namespace @@ -244,37 +212,25 @@ namespace Catch { std::chrono::milliseconds Config::benchmarkWarmupTime() const { return std::chrono::milliseconds(m_data.benchmarkWarmupTime); } 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 // variable with the path to XML output. If this file is written to // during test, Bazel will not generate a default XML output. // This allows the XML output file to contain higher level of detail // than what is possible otherwise. - const auto bazelOutputFile = std::getenv( "XML_OUTPUT_FILE" ); + const auto bazelOutputFile = Detail::getEnv( "XML_OUTPUT_FILE" ); if ( bazelOutputFile ) { m_data.reporterSpecifications.push_back( { "junit", std::string( bazelOutputFile ), {}, {} } ); } - const auto bazelTestSpec = std::getenv( "TESTBRIDGE_TEST_ONLY" ); + const auto bazelTestSpec = Detail::getEnv( "TESTBRIDGE_TEST_ONLY" ); if ( bazelTestSpec ) { // Presumably the test spec from environment should overwrite // the one we got from CLI (if we got any) m_data.testsOrTags.clear(); m_data.testsOrTags.push_back( bazelTestSpec ); } -# if defined( _MSC_VER ) -# pragma warning( pop ) -# endif const auto bazelShardOptions = readBazelShardingOptions(); if ( bazelShardOptions ) { @@ -286,8 +242,6 @@ namespace Catch { m_data.shardCount = bazelShardOptions->shardCount; } } - -#endif } } // end namespace Catch