Skip to content

Commit bd0dd3f

Browse files
map bazel's --test_runner_fail_fast to go's -failfast (#3058)
The following are now equivalent ```sh > bazel test --test_runner_fail_fast //path/to/my/go/test > bazel test --test_arg=-failfast //path/to/my/go/test ```
1 parent a93cae3 commit bd0dd3f

File tree

8 files changed

+103
-2
lines changed

8 files changed

+103
-2
lines changed

docs/go/core/rules.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
[test_arg]: https://docs.bazel.build/versions/master/user-manual.html#flag--test_arg
2525
[test_filter]: https://docs.bazel.build/versions/master/user-manual.html#flag--test_filter
2626
[test_env]: https://docs.bazel.build/versions/master/user-manual.html#flag--test_env
27+
[test_runner_fail_fast]: https://docs.bazel.build/versions/master/command-line-reference.html#flag--test_runner_fail_fast
2728
[write a CROSSTOOL file]: https://github.com/bazelbuild/bazel/wiki/Yet-Another-CROSSTOOL-Writing-Tutorial
2829
[bazel]: https://pkg.go.dev/github.com/bazelbuild/rules_go/go/tools/bazel?tab=doc
2930
[go_library]: #go_library
@@ -70,6 +71,7 @@ sufficient to match the capabilities of the normal go tools.
7071
- [test_arg]
7172
- [test_filter]
7273
- [test_env]
74+
- [test_runner_fail_fast]
7375
- [write a CROSSTOOL file]
7476
- [bazel]
7577

docs/go/core/rules.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
[test_arg]: https://docs.bazel.build/versions/master/user-manual.html#flag--test_arg
2727
[test_filter]: https://docs.bazel.build/versions/master/user-manual.html#flag--test_filter
2828
[test_env]: https://docs.bazel.build/versions/master/user-manual.html#flag--test_env
29+
[test_runner_fail_fast]: https://docs.bazel.build/versions/master/command-line-reference.html#flag--test_runner_fail_fast
2930
[write a CROSSTOOL file]: https://github.com/bazelbuild/bazel/wiki/Yet-Another-CROSSTOOL-Writing-Tutorial
3031
[bazel]: https://pkg.go.dev/github.com/bazelbuild/rules_go/go/tools/bazel?tab=doc
3132
[go_library]: #go_library
@@ -72,6 +73,7 @@ sufficient to match the capabilities of the normal go tools.
7273
- [test_arg]
7374
- [test_filter]
7475
- [test_env]
76+
- [test_runner_fail_fast]
7577
- [write a CROSSTOOL file]
7678
- [bazel]
7779

@@ -306,7 +308,9 @@ This builds a set of tests that can be run with `bazel test`.<br><br>
306308
<test_filter_>` argument to Bazel. You can pass arguments to tests by passing
307309
`--test_arg=arg <test_arg_>` arguments to Bazel, and you can set environment
308310
variables in the test environment by passing
309-
`--test_env=VAR=value <test_env_>`.<br><br>
311+
`--test_env=VAR=value <test_env_>`. You can terminate test execution after the first
312+
failure by passing the `--test_runner_fast_fast <test_runner_fail_fast_>` argument
313+
to Bazel. This is equivalent to passing `--test_arg=-failfast <test_arg_>`.<br><br>
310314
To write structured testlog information to Bazel's `XML_OUTPUT_FILE`, tests
311315
ran with `bazel test` execute using a wrapper. This functionality can be
312316
disabled by setting `GO_TEST_WRAP=0` in the test environment. Additionally,

go/core.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Core Go rules
2626
.. _test_arg: https://docs.bazel.build/versions/master/user-manual.html#flag--test_arg
2727
.. _test_filter: https://docs.bazel.build/versions/master/user-manual.html#flag--test_filter
2828
.. _test_env: https://docs.bazel.build/versions/master/user-manual.html#flag--test_env
29+
.. _test_runner_fail_fast: https://docs.bazel.build/versions/master/command-line-reference.html#flag--test_runner_fail_fast
2930
.. _write a CROSSTOOL file: https://github.com/bazelbuild/bazel/wiki/Yet-Another-CROSSTOOL-Writing-Tutorial
3031
.. _bazel: https://pkg.go.dev/github.com/bazelbuild/rules_go/go/tools/bazel?tab=doc
3132
.. _introduction: /docs/go/core/rules.md#introduction

go/private/rules/test.bzl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,9 @@ _go_test_kwargs = {
421421
<test_filter_>` argument to Bazel. You can pass arguments to tests by passing
422422
`--test_arg=arg <test_arg_>` arguments to Bazel, and you can set environment
423423
variables in the test environment by passing
424-
`--test_env=VAR=value <test_env_>`.<br><br>
424+
`--test_env=VAR=value <test_env_>`. You can terminate test execution after the first
425+
failure by passing the `--test_runner_fast_fast <test_runner_fail_fast_>` argument
426+
to Bazel. This is equivalent to passing `--test_arg=-failfast <test_arg_>`.<br><br>
425427
To write structured testlog information to Bazel's `XML_OUTPUT_FILE`, tests
426428
ran with `bazel test` execute using a wrapper. This functionality can be
427429
disabled by setting `GO_TEST_WRAP=0` in the test environment. Additionally,

go/tools/builders/generate_test_main.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,10 @@ func main() {
181181
flag.Lookup("test.run").Value.Set(filter)
182182
}
183183
184+
if failfast := os.Getenv("TESTBRIDGE_TEST_RUNNER_FAIL_FAST"); failfast != "" {
185+
flag.Lookup("test.failfast").Value.Set("true")
186+
}
187+
184188
{{if ne .CoverMode ""}}
185189
if len(coverdata.Counters) > 0 {
186190
testing.RegisterCover(testing.Cover{

tests/core/go_test/BUILD.bazel

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,11 @@ go_library(
139139
importpath = "github.com/bazelbuild/rules_go/tests/core/go_test/data_test_dep",
140140
)
141141

142+
go_bazel_test(
143+
name = "test_fail_fast_test",
144+
srcs = ["test_fail_fast_test.go"],
145+
)
146+
142147
go_bazel_test(
143148
name = "test_filter_test",
144149
srcs = ["test_filter_test.go"],

tests/core/go_test/README.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,14 @@ Checks that data dependencies, including those inherited from ``deps`` and
7575
``embed``, are visible to tests at run-time. Source files should not be
7676
visible at run-time.
7777

78+
test_fail_fast_test
79+
----------------
80+
81+
Checks that ``--test_runner_fail_fast`` actually enables stopping test execution after
82+
the first failure.
83+
84+
Verifies #3055.
85+
7886
test_filter_test
7987
----------------
8088

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Copyright 2022 The Bazel Authors. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package test_fail_fast_test
16+
17+
import (
18+
"bytes"
19+
"io/ioutil"
20+
"path/filepath"
21+
"testing"
22+
23+
"github.com/bazelbuild/rules_go/go/tools/bazel_testing"
24+
)
25+
26+
func TestMain(m *testing.M) {
27+
bazel_testing.TestMain(m, bazel_testing.Args{
28+
Main: `
29+
-- BUILD.bazel --
30+
load("@io_bazel_rules_go//go:def.bzl", "go_test")
31+
32+
go_test(
33+
name = "fail_fast_test",
34+
srcs = ["fail_fast_test.go"],
35+
)
36+
37+
-- fail_fast_test.go --
38+
package test_fail_fast
39+
40+
import "testing"
41+
42+
func TestShouldFail(t *testing.T) {
43+
t.Fail()
44+
}
45+
46+
func TestShouldNotRun(t *testing.T) {
47+
t.Fail()
48+
}
49+
`,
50+
})
51+
}
52+
53+
func Test(t *testing.T) {
54+
if err := bazel_testing.RunBazel("test", "//:fail_fast_test", "--test_runner_fail_fast"); err == nil {
55+
t.Fatal("got success; want failure")
56+
} else if bErr, ok := err.(*bazel_testing.StderrExitError); !ok {
57+
t.Fatalf("got %v; want StderrExitError", err)
58+
} else if code := bErr.Err.ExitCode(); code != 3 {
59+
t.Fatalf("got code %d; want code 3", code)
60+
}
61+
62+
logPath := filepath.FromSlash("bazel-testlogs/fail_fast_test/test.log")
63+
logData, err := ioutil.ReadFile(logPath)
64+
if err != nil {
65+
t.Fatal(err)
66+
}
67+
68+
if !bytes.Contains(logData, []byte("TestShouldFail")) {
69+
t.Fatalf("test log does not contain 'TestShouldFail': %q", logData)
70+
}
71+
72+
if bytes.Contains(logData, []byte("TestShouldNotRun")) {
73+
t.Fatalf("test log contains 'TestShouldNotRun' but should not: %q", logData)
74+
}
75+
}

0 commit comments

Comments
 (0)