Skip to content

Add Bazel XML_OUTPUT_FILE processing to main #2333

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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`
  • Loading branch information
mattyclarkson committed Dec 15, 2021
commit f1c5e9f8d24d5d405b0263873aa96f883709b0e4
1 change: 1 addition & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
@@ -16,6 +16,7 @@ cc_library(
# Static library, with main.
cc_library(
name = "catch2_main",
local_defines = ["CATCH_CONFIG_MAIN_BAZEL_JUNIT_XML_OUTPUT_FILE"],
srcs = ["src/catch2/internal/catch_main.cpp"],
deps = [":catch2"],
visibility = ["//visibility:public"],
13 changes: 12 additions & 1 deletion src/catch2/internal/catch_main.cpp
Original file line number Diff line number Diff line change
@@ -33,7 +33,18 @@ int main (int argc, char * argv[]) {
// and its constructor, as it (optionally) registers leak detector
(void)&Catch::leakDetector;

return Catch::Session().run( argc, argv );
Catch::Session session;

#if defined(CATCH_CONFIG_MAIN_BAZEL_JUNIT_XML_OUTPUT_FILE)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a terrible define name. How about CATCH_CONFIG_BAZEL_SUPPORT? (Does Bazel have more of these customization env option?)

Also it needs to be documented in docs/configuration.md

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. I just sorta went for something. Happy to spin to CATCH_CONFIG_BAZEL_SUPPORT if we don't spin out another Bazel specific main.

// Bazel expects test output to be written to this file in JUnit format.
auto bazelOutputFile = std::getenv("XML_OUTPUT_FILE");
if (bazelOutputFile != nullptr) {
session.configData().reporterName = "junit";
session.configData().outputFilename = bazelOutputFile;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be redone with the multireporter support.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh, cool. Will look into that.

}
#endif

return session.run( argc, argv );
}

#endif // !defined(CATCH_AMALGAMATED_CUSTOM_MAIN