Skip to content

Commit 6e4fdcf

Browse files
Support the go1.21 testing.Testing() function (bazel-contrib#4190)
**What type of PR is this?** Feature **What does this PR do? Why is it needed?** This change adds in the necessary flags in `go test` to allow [`testing.Testing`] to work as expected. According to the [upstream source], it is set with a `-X` option in the `go` tooling, so this change makes `rules_go` operate accordingly. This should be safe for older Go versions too, given that the link command [docs](https://pkg.go.dev/cmd/link) say that the `-X` arg is not "effective" when the target variable is not defined in the code. Tests are also provided, and `buildifier` moved around some unrelated lines. I'd be happy to undo the unrelated changes. [`testing.Testing`]: https://pkg.go.dev/testing#Testing [upstream source]: https://cs.opensource.google/go/go/+/refs/tags/go1.21.0:src/testing/testing.go;l=647-661 **Which issues(s) does this PR fix?** Fixes bazel-contrib#3686. Closes bazel-contrib#4096 **Other notes for review** This is intended to replace bazel-contrib#4096, which seems to be inactive. The functional code change in this PR was done at the same location.
1 parent 4874f9a commit 6e4fdcf

File tree

5 files changed

+66
-2
lines changed

5 files changed

+66
-2
lines changed

go/private/rules/test.bzl

+6
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,12 @@ def _go_test_impl(ctx):
155155
# in bzltestutil/init.go.
156156
test_gc_linkopts.extend(["-X", "+initfirst/github.com/bazelbuild/rules_go/go/tools/bzltestutil/chdir.RunDir=" + run_dir])
157157

158+
# This is needed for the testing.Testing() function to work in go
159+
# 1.21+. See
160+
# https://cs.opensource.google/go/go/+/refs/tags/go1.21.0:src/testing/testing.go;l=647-661
161+
# for more details.
162+
test_gc_linkopts.extend(["-X", "testing.testBinary=1"])
163+
158164
# Now compile the test binary itself
159165
test_deps = external_archive.direct + [external_archive] + ctx.attr._testmain_additional_deps
160166
if go.coverage_enabled:

tests/core/go_binary/BUILD.bazel

+23-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
load("@bazel_skylib//rules:build_test.bzl", "build_test")
2+
load("@bazel_skylib//rules:run_binary.bzl", "run_binary")
13
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library", "go_test")
24
load("@io_bazel_rules_go//go/tools/bazel_testing:def.bzl", "go_bazel_test")
35
load(":linkmode.bzl", "linkmode_pie_wrapper")
@@ -241,7 +243,27 @@ go_binary(
241243
go_binary(
242244
name = "meaning2",
243245
srcs = [
244-
"//tests/core/go_library:use_syso_srcs",
245246
"meaning2.go",
247+
"//tests/core/go_library:use_syso_srcs",
246248
],
247249
)
250+
251+
# These three verify that testing.Testing() returns `false` in a
252+
# `go_binary`.
253+
go_binary(
254+
name = "testing_testing_bin",
255+
srcs = ["testing_testing.go"],
256+
)
257+
258+
run_binary(
259+
name = "testing_testing_bin_run",
260+
outs = ["testing_testing_bin_run.out"],
261+
args = ["$(location :testing_testing_bin_run.out)"],
262+
tags = ["manual"],
263+
tool = ":testing_testing_bin",
264+
)
265+
266+
build_test(
267+
name = "testing_testing_test",
268+
targets = [":testing_testing_bin_run"],
269+
)
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
"os"
7+
)
8+
9+
func main() {
10+
if testing.Testing() {
11+
panic("testing.Testing() returned 'true' in a binary")
12+
}
13+
14+
file, err := os.Create(os.Args[1])
15+
if err != nil {
16+
panic(fmt.Sprintf("Failed to open output file %s", err))
17+
}
18+
file.Close()
19+
}

tests/core/go_test/BUILD.bazel

+7-1
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,12 @@ test_suite(
208208
tests = ["same_package_{}_test".format(i) for i in range(1, 80)],
209209
)
210210

211+
# Verifies that testing.Testing() is true in a `go_test`.
212+
go_test(
213+
name = "testing_testing_test",
214+
srcs = ["testing_testing_test.go"],
215+
)
216+
211217
go_bazel_test(
212218
name = "testmain_without_exit_test",
213219
srcs = ["testmain_without_exit_test.go"],
@@ -289,7 +295,7 @@ go_test(
289295
go_test(
290296
name = "syso_direct_test",
291297
srcs = [
292-
"//tests/core/go_library:use_syso_srcs",
293298
"syso_direct_test.go",
299+
"//tests/core/go_library:use_syso_srcs",
294300
],
295301
)
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package testing_testing
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestMain(m *testing.M) {
8+
if ! testing.Testing() {
9+
panic("testing.Testing() returned 'false' in a test")
10+
}
11+
}

0 commit comments

Comments
 (0)