From 830d4649b42451161adbdf907d9935e648f17d8c Mon Sep 17 00:00:00 2001 From: Matt Clarkson Date: Tue, 10 May 2022 05:21:05 -0700 Subject: [PATCH] Export `BAZEL_TEST=1` for `bazel test` executables [Catch2][catch2] has [learnt][pr2399] how to output JUnit to the `XML_OUTPUT_FILE`. However, the upstream developers [felt][comment] that `XML_OUTPUT_FILE` is too generic to blanket enable JUnit output to the file as there are various XML reporters for Catch2. This patch enables exporting `BAZEL_TEST` environment variable so that Catch2 can enable JUnit output support unconditionally in their build when both `BAZEL_TEST` _and_ `XML_OUTPUT_FILE` are available. [catch2]: https://github.com/catchorg/Catch2 [pr2399]: https://github.com/catchorg/Catch2/pull/2399 [comment]: https://github.com/catchorg/Catch2/pull/2399#issuecomment-1092965791 Closes #15393. PiperOrigin-RevId: 447706388 --- site/en/reference/test-encyclopedia.md | 5 +++++ tools/test/test-setup.sh | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/site/en/reference/test-encyclopedia.md b/site/en/reference/test-encyclopedia.md index c6d4642e62a83e..5e94c0e21eb03e 100644 --- a/site/en/reference/test-encyclopedia.md +++ b/site/en/reference/test-encyclopedia.md @@ -452,6 +452,11 @@ The initial environment block shall be composed as follows: class="external">JUnit test result schema. optional + + BAZEL_TEST + Signifies test executable is being driven by bazel test + required + The environment may contain additional entries. Tests should not depend on the diff --git a/tools/test/test-setup.sh b/tools/test/test-setup.sh index 182507fcdb57e0..1cc73137d75653 100755 --- a/tools/test/test-setup.sh +++ b/tools/test/test-setup.sh @@ -37,6 +37,11 @@ function is_absolute { # root. EXEC_ROOT="$PWD" +# Declare that the executable is running in a `bazel test` environment +# This allows test frameworks to enable output to the unprefixed environment variable +# For example, if `BAZEL_TEST` and `XML_OUTPUT_FILE` are defined, write JUnit output +export BAZEL_TEST=1 + # Bazel sets some environment vars to relative paths to improve caching and # support remote execution, where the absolute path may not be known to Bazel. # Convert them to absolute paths here before running the actual test.