Progmasoft Git

catch3

Progmasoft Catch3: an independently maintained C++17 test framework with a parallel runner, property checks, snapshots and structured results.

Clonegit clone https://git.progmasoft.com/catch3

Support Bazel's TEST_RANDOM_SEED

As with other Bazel env vars, it overrides the corresponding CLI
parameter if both are set.

Closes #3021
Closes #3024
Commit
91b3b3bf4057e2a7e41bf1edd3e2c68b31bb3747
Author
Martin Hořeňovský ·
Parents
a00d6544375e

Files · Download .tar.gz

Changes

 docs/ci-and-misc.md         |  6 +++++-
 src/catch2/catch_config.cpp | 15 +++++++++++++++
 tests/CMakeLists.txt        | 33 +++++++++++++++++++++++++++++++++
 3 files changed, 53 insertions(+), 1 deletion(-)

diff --git a/docs/ci-and-misc.md b/docs/ci-and-misc.md
index d2ef3fd7..1b57a912 100644
--- a/docs/ci-and-misc.md
+++ b/docs/ci-and-misc.md
@@ -67,12 +67,13 @@ test execution. Specifically it understands
  * Test filtering via `TESTBRIDGE_TEST_ONLY`
  * Test sharding via `TEST_SHARD_INDEX`, `TEST_TOTAL_SHARDS`, and `TEST_SHARD_STATUS_FILE`
  * Creating a file to signal premature test exit via `TEST_PREMATURE_EXIT_FILE`
+ * Setting the RNG seed via `TEST_RANDOM_SEED`
 
 > Support for `XML_OUTPUT_FILE` was [introduced](https://github.com/catchorg/Catch2/pull/2399) in Catch2 3.0.1
 
 > Support for `TESTBRIDGE_TEST_ONLY` and sharding was introduced in Catch2 3.2.0
 
-> Support for `TEST_PREMATURE_EXIT_FILE` was introduced in Catch2 X.Y.Z
+> Support for `TEST_PREMATURE_EXIT_FILE` and `TEST_RANDOM_SEED` was introduced in Catch2 X.Y.Z
 
 This integration is enabled via either a [compile time configuration
 option](configuration.md#bazel-support), or via `BAZEL_TEST` environment
@@ -80,6 +81,9 @@ variable set to "1".
 
 > Support for `BAZEL_TEST` was [introduced](https://github.com/catchorg/Catch2/pull/2459) in Catch2 3.1.0
 
+Note that if both the Bazel environment var and command line option for
+something are used, the environment variable wins.
+
 
 ## Low-level tools
 
diff --git a/src/catch2/catch_config.cpp b/src/catch2/catch_config.cpp
index 978bcfd7..fef03d7d 100644
--- a/src/catch2/catch_config.cpp
+++ b/src/catch2/catch_config.cpp
@@ -253,6 +253,21 @@ namespace Catch {
         if (bazelExitGuardFile) {
             m_data.prematureExitGuardFilePath = bazelExitGuardFile;
         }
+
+        const auto bazelRandomSeed = Detail::getEnv( "TEST_RANDOM_SEED" );
+        if ( bazelRandomSeed ) {
+            auto parsedSeed = parseUInt( bazelRandomSeed, 0 );
+            if ( !parsedSeed ) {
+                // Currently we handle issues with parsing other Bazel Env
+                // options by warning and ignoring the issue. So we do the
+                // same for random seed option.
+                Catch::cerr()
+                    << "Warning: could not parse 'TEST_RANDOM_SEED' ('"
+                    << bazelRandomSeed << "') as proper seed.\n";
+            } else {
+                m_data.rngSeed = *parsedSeed;
+            }
+        }
     }
 
 } // end namespace Catch
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index 8f8a3755..651ac835 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -662,5 +662,38 @@ foreach(reporterName # "Automake" - the simple .trs format does not support any
   )
 endforeach()
 
+add_test(NAME "Bazel::RngSeedEnvVar::JustEnv"
+  COMMAND
+    $<TARGET_FILE:SelfTest> "Factorials are computed"
+)
+set_tests_properties("Bazel::RngSeedEnvVar::JustEnv"
+  PROPERTIES
+    ENVIRONMENT "BAZEL_TEST=1;TEST_RANDOM_SEED=18181818"
+    PASS_REGULAR_EXPRESSION "Randomness seeded to: 18181818"
+)
+
+add_test(NAME "Bazel::RngSeedEnvVar::EnvHasPriorityOverCLI"
+  COMMAND
+    $<TARGET_FILE:SelfTest> "Factorials are computed"
+    --rng-seed 17171717
+)
+set_tests_properties("Bazel::RngSeedEnvVar::EnvHasPriorityOverCLI"
+  PROPERTIES
+    ENVIRONMENT "BAZEL_TEST=1;TEST_RANDOM_SEED=18181818"
+    PASS_REGULAR_EXPRESSION "Randomness seeded to: 18181818"
+)
+
+add_test(NAME "Bazel::RngSeedEnvVar::MalformedValueIsIgnored"
+  COMMAND
+    $<TARGET_FILE:SelfTest> "Factorials are computed"
+    --rng-seed 17171717
+)
+set_tests_properties("Bazel::RngSeedEnvVar::MalformedValueIsIgnored"
+  PROPERTIES
+    ENVIRONMENT "BAZEL_TEST=1;TEST_RANDOM_SEED=XOXOXOXO"
+    PASS_REGULAR_EXPRESSION "Randomness seeded to: 17171717"
+)
+
+
 list(APPEND CATCH_TEST_TARGETS SelfTest)
 set(CATCH_TEST_TARGETS ${CATCH_TEST_TARGETS} PARENT_SCOPE)