From: Austin Clements Date: Wed, 31 May 2023 16:32:29 +0000 (-0400) Subject: cmd/cgo/internal/testerrors: pass if GOEXPERIMENT=cgocheck2 is set X-Git-Tag: go1.21rc1~131 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=563556ccc7c4099ac896c87178043b60365d33b5;p=gostls13.git cmd/cgo/internal/testerrors: pass if GOEXPERIMENT=cgocheck2 is set The tests in this package are meant to check cgocheck and cgocheck2 mode, so they're of course sensitive to whether they're set. Currently, the test will set GOEXPERIMENT=cgocheck2 for tests of cgocheck2 mode, but won't *unset* cgocheck2 mode if it's already in the environment for tests that expect it to be off. This means GOEXPERIMENT=cgocheck2 go test cmd/cgo/internal/testerrors fails. Fix this by removing cgocheck2 from GOEXPERIMENT if it's set and the test case expects it to be unset. Change-Id: If663e41b791fb89df9940bc5356a566e2a54a77a Reviewed-on: https://go-review.googlesource.com/c/go/+/499557 Run-TryBot: Austin Clements TryBot-Result: Gopher Robot Reviewed-by: Keith Randall Reviewed-by: Keith Randall --- diff --git a/src/cmd/cgo/internal/testerrors/ptr_test.go b/src/cmd/cgo/internal/testerrors/ptr_test.go index 33126f40ae..7f56501c58 100644 --- a/src/cmd/cgo/internal/testerrors/ptr_test.go +++ b/src/cmd/cgo/internal/testerrors/ptr_test.go @@ -15,6 +15,7 @@ import ( "os/exec" "path/filepath" "runtime" + "slices" "strings" "sync/atomic" "testing" @@ -593,18 +594,25 @@ func buildPtrTests(t *testing.T, gopath string, cgocheck2 bool) (exe string) { cmd := exec.Command("go", "build", "-o", exeName) cmd.Dir = src cmd.Env = append(os.Environ(), "GOPATH="+gopath) - if cgocheck2 { - found := false - for i, e := range cmd.Env { - if strings.HasPrefix(e, "GOEXPERIMENT=") { - cmd.Env[i] = e + ",cgocheck2" - found = true - } - } - if !found { - cmd.Env = append(cmd.Env, "GOEXPERIMENT=cgocheck2") - } + + // Set or remove cgocheck2 from the environment. + goexperiment := strings.Split(os.Getenv("GOEXPERIMENT"), ",") + if len(goexperiment) == 1 && goexperiment[0] == "" { + goexperiment = nil + } + i := slices.Index(goexperiment, "cgocheck2") + changed := false + if cgocheck2 && i < 0 { + goexperiment = append(goexperiment, "cgocheck2") + changed = true + } else if !cgocheck2 && i >= 0 { + goexperiment = append(goexperiment[:i], goexperiment[i+1:]...) + changed = true } + if changed { + cmd.Env = append(cmd.Env, "GOEXPERIMENT="+strings.Join(goexperiment, ",")) + } + out, err := cmd.CombinedOutput() if err != nil { t.Fatalf("go build: %v\n%s", err, out)