From c6116bea0309c28500ba0a233e5b0ab01a491a1b Mon Sep 17 00:00:00 2001 From: "Bryan C. Mills" Date: Tue, 2 May 2023 11:59:19 -0400 Subject: [PATCH] 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 --- src/cmd/fix/main_test.go | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) 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) { -- 2.48.1