]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/cgo/internal/testerrors: pass if GOEXPERIMENT=cgocheck2 is set
authorAustin Clements <austin@google.com>
Wed, 31 May 2023 16:32:29 +0000 (12:32 -0400)
committerAustin Clements <austin@google.com>
Thu, 1 Jun 2023 18:30:29 +0000 (18:30 +0000)
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>
src/cmd/cgo/internal/testerrors/ptr_test.go

index 33126f40ae382dae7c4b07920714ba1a8044eb16..7f56501c589ca471774be264b55d48d41dbe02ca 100644 (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)