Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f1c5e9f

Browse files
committedDec 15, 2021
Add Bazel XML_OUTPUT_FILE processing to main
`bazel test` sets the `XML_OUTPUT_FILE` environment variable which specifies where the JUnit output of the test executable should be written. Picking up this variable allows catch2 to naturally integrate into the Bazel testing ecosystem out-of-the-box. The environment variable processing is hidden behind the `CATCH_CONFIG_MAIN_BAZEL_XML_OUTPUT_FILE` define and it _only_ enabled in the `BUILD.bazel`. This allows the Bazel ecosystem to create a `cc_test` target as follows: ``` cc_test( name = "some_test", deps = ["@catch2//:catch2_main"], srcs = globs("**/*_test.cpp"), ) ``` The output will be written to `bazel-testlogs/some_test/test.xml`
1 parent dcf9479 commit f1c5e9f

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed
 

‎BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ cc_library(
1616
# Static library, with main.
1717
cc_library(
1818
name = "catch2_main",
19+
local_defines = ["CATCH_CONFIG_MAIN_BAZEL_JUNIT_XML_OUTPUT_FILE"],
1920
srcs = ["src/catch2/internal/catch_main.cpp"],
2021
deps = [":catch2"],
2122
visibility = ["//visibility:public"],

‎src/catch2/internal/catch_main.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,18 @@ int main (int argc, char * argv[]) {
3333
// and its constructor, as it (optionally) registers leak detector
3434
(void)&Catch::leakDetector;
3535

36-
return Catch::Session().run( argc, argv );
36+
Catch::Session session;
37+
38+
#if defined(CATCH_CONFIG_MAIN_BAZEL_JUNIT_XML_OUTPUT_FILE)
39+
// Bazel expects test output to be written to this file in JUnit format.
40+
auto bazelOutputFile = std::getenv("XML_OUTPUT_FILE");
41+
if (bazelOutputFile != nullptr) {
42+
session.configData().reporterName = "junit";
43+
session.configData().outputFilename = bazelOutputFile;
44+
}
45+
#endif
46+
47+
return session.run( argc, argv );
3748
}
3849

3950
#endif // !defined(CATCH_AMALGAMATED_CUSTOM_MAIN

0 commit comments

Comments
 (0)
Please sign in to comment.