Skip to content

Commit

Permalink
go_test: don't require TestMain to call os.Exit (#2716)
Browse files Browse the repository at this point in the history
  • Loading branch information
rabbbit authored and Jay Conrod committed Dec 2, 2020
1 parent 5e0b2dd commit a59f6a1
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 0 deletions.
5 changes: 5 additions & 0 deletions go/tools/builders/generate_test_main.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ import (
"os/exec"
"path/filepath"
"runtime"
{{if .TestMain}}
"reflect"
{{end}}
"strconv"
"testing"
"testing/internal/testdeps"
Expand Down Expand Up @@ -169,6 +172,8 @@ func main() {
os.Exit(m.Run())
{{else}}
{{.TestMain}}(m)
{{/* See golang.org/issue/34129 and golang.org/cl/219639 */}}
os.Exit(int(reflect.ValueOf(m).Elem().FieldByName("exitCode").Int()))
{{end}}
}
`
Expand Down
6 changes: 6 additions & 0 deletions tests/core/go_test/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,9 @@ go_library(
importpath = "github.com/bazelbuild/rules_go/tests/core/go_test/indirect_import_dep",
deps = [":indirect_import_lib"],
)

go_bazel_test(
name = "testmain_without_exit_test",
srcs = ["testmain_without_exit_test.go"],
)

7 changes: 7 additions & 0 deletions tests/core/go_test/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Basic go_test functionality

.. _go_test: /go/core.rst#_go_test
.. _#1877: https://github.com/bazelbuild/rules_go/issues/1877
.. _#34129: https:////github.com/golang/go/issues/34129

Tests to ensure that basic features of `go_test`_ are working as expected.

Expand Down Expand Up @@ -97,3 +98,9 @@ indirect_import_test
Checks that an external test can import another package that imports the library
under test. The other package should be compiled against the internal test
package, not the library under test. Verifies `#1877`_.

testmain_without_exit
---------------------

Checks that TestMain without calling os.Exit directly works.
Verifies `#34129`_ from Go 1.15.
59 changes: 59 additions & 0 deletions tests/core/go_test/testmain_without_exit_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright 2020 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package testmain_without_exit

import (
"testing"

"github.com/bazelbuild/rules_go/go/tools/bazel_testing"
)

func TestMain(m *testing.M) {
bazel_testing.TestMain(m, bazel_testing.Args{
Main: `
-- BUILD.bazel --
load("@io_bazel_rules_go//go:def.bzl", "go_test")
go_test(
name = "main_without_exit_test",
srcs = ["main_without_exit_test.go"],
)
-- main_without_exit_test.go --
package test_main_without_exit
import "testing"
func TestMain(m *testing.M) {
m.Run()
}
func TestShouldFail(t *testing.T) {
t.Fail()
}
`,
})
}

func Test(t *testing.T) {
err := bazel_testing.RunBazel("test", "//:main_without_exit_test")
if err == nil {
t.Fatal("expected bazel test to have failed")
}

if xerr, ok := err.(*bazel_testing.StderrExitError); !ok || xerr.Err.ExitCode() != 3 {
t.Fatalf("expected bazel tests to fail with exit code 3 (TESTS_FAILED), got: %s", err)
}
}

0 comments on commit a59f6a1

Please sign in to comment.