Skip to content

Commit dbdf908

Browse files
Marcin RadomskiGoogle-ML-Automation
authored andcommitted
[XLA] Add googletest wrapper that adds status asserts to gmock.h
And replace @com_google_googletest for internal uses. The only exceptions that still need to rely on upstream googletest (renamed to @com_google_googletest_upstream) are the wrapper and its direct dependencies (right now just gutil, and through it absl). This makes it possible to use the same assert macros internally and in OSS. Implement TF_{ASSERT,EXPECT}_OK in terms of unprefixed ones for verification. PiperOrigin-RevId: 837550145
1 parent 0f18fdb commit dbdf908

File tree

13 files changed

+121
-9
lines changed

13 files changed

+121
-9
lines changed

MODULE.bazel

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ bazel_dep(name = "bazel_skylib", version = "1.8.1")
1010
bazel_dep(name = "boringssl", version = "0.20250818.0")
1111
bazel_dep(name = "curl", version = "8.11.0")
1212
bazel_dep(name = "google_benchmark", version = "1.8.5", repo_name = "com_google_benchmark")
13-
bazel_dep(name = "googletest", version = "1.17.0", repo_name = "com_google_googletest")
13+
bazel_dep(name = "googletest", version = "1.17.0", repo_name = "com_google_googletest_upstream")
14+
bazel_dep(name = "xla_googletest_wrapper", version = "1.17.0", repo_name = "com_google_googletest")
1415
bazel_dep(name = "grpc", version = "1.74.1", repo_name = "com_github_grpc_grpc")
1516
bazel_dep(name = "gutil", version = "20250502.0", repo_name = "com_google_gutil");
1617
bazel_dep(name = "jsoncpp", version = "1.9.6", repo_name = "jsoncpp_git")

third_party/absl/workspace.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@ def repo():
2626
],
2727
repo_mapping = {
2828
"@google_benchmark": "@com_google_benchmark",
29-
"@googletest": "@com_google_googletest",
29+
"@googletest": "@com_google_googletest_upstream",
3030
},
3131
)

third_party/gutil/workspace.bzl

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ def repo():
1515
strip_prefix = "gutil-{commit}".format(commit = GUTIL_COMMIT),
1616
urls = tf_mirror_urls("https://github.com/google/gutil/archive/{commit}.tar.gz".format(commit = GUTIL_COMMIT)),
1717
repo_mapping = {
18-
"@absl-cpp": "@com_google_absl",
19-
"@google_benchmark": "@com_google_benchmark",
20-
"@googletest": "@com_google_googletest",
18+
"@com_google_googletest": "@com_google_googletest_upstream",
2119
},
2220
)

workspace2.bzl

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")
77
load("@io_bazel_rules_closure//closure:defs.bzl", "filegroup_external")
88
load("@rules_ml_toolchain//gpu/sycl:sycl_configure.bzl", "sycl_configure")
99
load("@rules_ml_toolchain//gpu/sycl:sycl_init_repository.bzl", "sycl_init_repository")
10-
load("//third_party:repo.bzl", "tf_http_archive", "tf_mirror_urls")
10+
load("//third_party:repo.bzl", "tf_http_archive", "tf_mirror_urls", "tf_vendored")
1111
load("//third_party/absl:workspace.bzl", absl = "repo")
1212
load("//third_party/benchmark:workspace.bzl", benchmark = "repo")
1313
load("//third_party/clang_toolchain:cc_configure_clang.bzl", "cc_download_clang_toolchain")
@@ -305,8 +305,13 @@ def _tf_repositories():
305305
},
306306
)
307307

308-
tf_http_archive(
308+
tf_vendored(
309309
name = "com_google_googletest",
310+
path = "xla_googletest_wrapper",
311+
)
312+
313+
tf_http_archive(
314+
name = "com_google_googletest_upstream",
310315
# Use the commit on 2025/6/09:
311316
# https://github.com/google/googletest/commit/28e9d1f26771c6517c3b4be10254887673c94018
312317
sha256 = "f253ca1a07262f8efde8328e4b2c68979e40ddfcfc001f70d1d5f612c7de2974",

xla/tsl/lib/core/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ cc_library(
4343
"//xla/tsl/platform:status_matchers",
4444
"//xla/tsl/platform:test",
4545
"@com_google_absl//absl/status:status_matchers",
46+
"@com_google_googletest//:gtest_for_library",
4647
],
4748
)
4849

xla/tsl/lib/core/status_test_util.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@ limitations under the License.
1616
#ifndef XLA_TSL_LIB_CORE_STATUS_TEST_UTIL_H_
1717
#define XLA_TSL_LIB_CORE_STATUS_TEST_UTIL_H_
1818

19+
#include <gmock/gmock.h> // IWYU pragma: keep
1920
#include "absl/status/status_matchers.h" // IWYU pragma: keep
2021
#include "xla/tsl/platform/status_matchers.h"
2122
#include "xla/tsl/platform/test.h"
2223

2324
// Macros for testing the results of functions that return tensorflow::Status.
24-
#define TF_EXPECT_OK(statement) EXPECT_THAT((statement), ::absl_testing::IsOk())
25-
#define TF_ASSERT_OK(statement) ASSERT_THAT((statement), ::absl_testing::IsOk())
25+
#define TF_EXPECT_OK(statement) EXPECT_OK((statement))
26+
#define TF_ASSERT_OK(statement) ASSERT_OK((statement))
2627

2728
// There are no EXPECT_NOT_OK/ASSERT_NOT_OK macros since they would not
2829
// provide much value (when they fail, they would just print the OK status

xla_googletest_wrapper/BUILD.bazel

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package(default_visibility = ["//visibility:public"])
2+
3+
cc_library(
4+
name = "gtest",
5+
# Upstream gtest is *not* marked testonly for some reason, but
6+
# gutil:status_matchers is, and non-testonly targets can't depend on
7+
# testonly ones.
8+
#
9+
# XLA doesn't use gtest in non-testonly targets though so making this
10+
# testonly should (TM) be fine.
11+
testonly = True,
12+
hdrs = [
13+
"include/gmock/gmock.h",
14+
"include/gtest/gtest.h",
15+
],
16+
includes = [
17+
"include",
18+
],
19+
deps = [
20+
"@com_google_googletest_upstream//:gtest",
21+
"@com_google_gutil//gutil:status_matchers",
22+
],
23+
)
24+
25+
cc_library(
26+
name = "gtest_main",
27+
deps = [
28+
"@com_google_googletest_upstream//:gtest_main",
29+
],
30+
)
31+
32+
alias(
33+
name = "gtest_for_library",
34+
actual = ":gtest",
35+
)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Version should match the wrapped googletest.
2+
module(name = "xla_googletest_wrapper", version = "1.17.0")

xla_googletest_wrapper/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
A gtest wrapper that adds ASSERT_OK, EXPECT_OK, ASSERT_OK_AND_ASSIGN to gmock.h
2+
so that the header's provided functionality matches internal gmock.
3+
4+
The repo contains a minimal set of reexports necessary to build XLA with this as
5+
a drop-in replacement for googletest.

xla_googletest_wrapper/REPO.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Mark the repo as a bazel repo.

0 commit comments

Comments
 (0)