]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/fix: move cgo and 'go build' support checks into TestRewrite
authorBryan C. Mills <bcmills@google.com>
Tue, 2 May 2023 15:59:19 +0000 (11:59 -0400)
committerGopher Robot <gobot@golang.org>
Thu, 4 May 2023 18:02:12 +0000 (18:02 +0000)
This avoids unnecessary work to determine 'go build' and cgo support
if we're not actually running the test (as in 'go test -list').

Change-Id: Id175a759605b2130d4de8bff8eba4c23fe65ccba
Reviewed-on: https://go-review.googlesource.com/c/go/+/491657
Run-TryBot: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Auto-Submit: Bryan Mills <bcmills@google.com>
Reviewed-by: Austin Clements <austin@google.com>
src/cmd/fix/main_test.go

index 2b293077d13f6df09d8b5373cd57bc7ae1c0f408..cafd116cfd6b357f5737d2a77db7bb4bd6a0cb93 100644 (file)
@@ -14,20 +14,6 @@ import (
        "testing"
 )
 
-func init() {
-       // If cgo is enabled, enforce that cgo commands invoked by cmd/fix
-       // do not fail during testing.
-       if testenv.HasCGO() && testenv.HasGoBuild() {
-               // The reportCgoError hook is global, so we can't set it per-test
-               // if we want to be able to run those tests in parallel.
-               // Instead, simply set it to panic on error: the goroutine dump
-               // from the panic should help us determine which test failed.
-               reportCgoError = func(err error) {
-                       panic(fmt.Sprintf("unexpected cgo error: %v", err))
-               }
-       }
-}
-
 type testCase struct {
        Name    string
        Fn      func(*ast.File) bool
@@ -91,6 +77,22 @@ func parseFixPrint(t *testing.T, fn func(*ast.File) bool, desc, in string, mustB
 }
 
 func TestRewrite(t *testing.T) {
+       // If cgo is enabled, enforce that cgo commands invoked by cmd/fix
+       // do not fail during testing.
+       if testenv.HasCGO() {
+               testenv.MustHaveGoBuild(t) // Really just 'go tool cgo', but close enough.
+
+               // The reportCgoError hook is global, so we can't set it per-test
+               // if we want to be able to run those tests in parallel.
+               // Instead, simply set it to panic on error: the goroutine dump
+               // from the panic should help us determine which test failed.
+               prevReportCgoError := reportCgoError
+               reportCgoError = func(err error) {
+                       panic(fmt.Sprintf("unexpected cgo error: %v", err))
+               }
+               t.Cleanup(func() { reportCgoError = prevReportCgoError })
+       }
+
        for _, tt := range testCases {
                tt := tt
                t.Run(tt.Name, func(t *testing.T) {