|
| 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