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 <austin@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
Reviewed-by: Keith Randall <khr@google.com>
This commit is contained in:
Austin Clements 2023-05-31 12:32:29 -04:00
parent 7dc8509c69
commit 563556ccc7
1 changed files with 19 additions and 11 deletions

View File

@ -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)