From: Bryan C. Mills Date: Tue, 2 May 2023 15:59:19 +0000 (-0400) Subject: cmd/fix: move cgo and 'go build' support checks into TestRewrite X-Git-Tag: go1.21rc1~681 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=c6116bea0309c28500ba0a233e5b0ab01a491a1b;p=gostls13.git cmd/fix: move cgo and 'go build' support checks into TestRewrite 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 TryBot-Result: Gopher Robot Auto-Submit: Bryan Mills Reviewed-by: Austin Clements --- diff --git a/src/cmd/fix/main_test.go b/src/cmd/fix/main_test.go index 2b293077d1..cafd116cfd 100644 --- a/src/cmd/fix/main_test.go +++ b/src/cmd/fix/main_test.go @@ -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) {