]> Cypherpunks repositories - gostls13.git/commitdiff
[dev.typeparams] test: cleanup 'go env' and -goexperiment
authorMatthew Dempsky <mdempsky@google.com>
Fri, 2 Jul 2021 22:42:20 +0000 (15:42 -0700)
committerMatthew Dempsky <mdempsky@google.com>
Thu, 22 Jul 2021 17:01:49 +0000 (17:01 +0000)
This CL makes two related changes:

1. It uses 'go env -json' to query the environment configuration,
rather than attempting to manually reconstruct the values that cmd/go
is going to use.

2. It changes the -goexperiment flag to *extend* any ambient
GOEXPERIMENT configuration. Notably, this means that '-goexperiment
fieldtrack' now tests fieldtracking in conjunction with any other
experiments (e.g., unified IR). Tests that want to test an exact
GOEXPERIMENT config should use '-goexperiment none,foo' instead.

Change-Id: I96a97198209e540e934fe7035110c3ae3a8f0e6a
Reviewed-on: https://go-review.googlesource.com/c/go/+/332610
Trust: Matthew Dempsky <mdempsky@google.com>
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
test/run.go

index 1e01d160674912d4893678cf43c55d4dacdcd784..23eebcee2e83f9004f28b7810514930a828e7384 100644 (file)
@@ -9,6 +9,7 @@ package main
 
 import (
        "bytes"
+       "encoding/json"
        "errors"
        "flag"
        "fmt"
@@ -49,17 +50,36 @@ var (
        shards = flag.Int("shards", 0, "number of shards. If 0, all tests are run. This is used by the continuous build.")
 )
 
-var unifiedEnabled, defaultGLevels = func() (bool, string) {
-       // TODO(mdempsky): Change this to just "go env GOEXPERIMENT" after
-       // CL 328751 is merged back to dev.typeparams. In the mean time, we
-       // infer whether the "unified" experiment is default enabled by
-       // inspecting the output from `go tool compile -V`.
-       output := runOutput(goTool(), "tool", "compile", "-V")
+type envVars struct {
+       GOOS         string
+       GOARCH       string
+       GOEXPERIMENT string
+       CGO_ENABLED  string
+}
+
+var env = func() (res envVars) {
+       cmd := exec.Command("go", "env", "-json")
+       stdout, err := cmd.StdoutPipe()
+       if err != nil {
+               log.Fatal("StdoutPipe:", err)
+       }
+       if err := cmd.Start(); err != nil {
+               log.Fatal("Start:", err)
+       }
+       if err := json.NewDecoder(stdout).Decode(&res); err != nil {
+               log.Fatal("Decode:", err)
+       }
+       if err := cmd.Wait(); err != nil {
+               log.Fatal("Wait:", err)
+       }
+       return
+}()
 
+var unifiedEnabled, defaultGLevels = func() (bool, string) {
        // TODO(mdempsky): This will give false negatives if the unified
        // experiment is enabled by default, but presumably at that point we
        // won't need to disable tests for it anymore anyway.
-       enabled := strings.Contains(output, "unified")
+       enabled := strings.Contains(","+env.GOEXPERIMENT+",", ",unified,")
 
        // Normal test runs should test with both -G=0 and -G=3 for types2
        // coverage. But the unified experiment always uses types2, so
@@ -81,8 +101,9 @@ func defaultAllCodeGen() bool {
 }
 
 var (
-       goos, goarch string
-       cgoEnabled   bool
+       goos          = env.GOOS
+       goarch        = env.GOARCH
+       cgoEnabled, _ = strconv.ParseBool(env.CGO_ENABLED)
 
        // dirs are the directories to look for *.go files in.
        // TODO(bradfitz): just use all directories?
@@ -116,12 +137,6 @@ func main() {
                glevels = append(glevels, glevel)
        }
 
-       goos = getenv("GOOS", runtime.GOOS)
-       goarch = getenv("GOARCH", runtime.GOARCH)
-
-       cgoEnv := runOutput(goTool(), "env", "CGO_ENABLED")
-       cgoEnabled, _ = strconv.ParseBool(strings.TrimSpace(cgoEnv))
-
        findExecCmd()
 
        // Disable parallelism if printing or if using a simulator.
@@ -214,17 +229,6 @@ func main() {
        }
 }
 
-// runOutput runs the specified command and returns its output as a
-// string. If the command fails, runOutput logs the error and exits.
-func runOutput(name string, args ...string) string {
-       cmd := exec.Command(name, args...)
-       output, err := cmd.Output()
-       if err != nil {
-               log.Fatalf("running %v: %v", cmd, err)
-       }
-       return string(output)
-}
-
 // goTool reports the path of the go tool to use to run the tests.
 // If possible, use the same Go used to run run.go, otherwise
 // fallback to the go version found in the PATH.
@@ -672,6 +676,8 @@ func (t *test) run() {
                return
        }
 
+       goexp := env.GOEXPERIMENT
+
        // collect flags
        for len(args) > 0 && strings.HasPrefix(args[0], "-") {
                switch args[0] {
@@ -698,7 +704,11 @@ func (t *test) run() {
                        }
                case "-goexperiment": // set GOEXPERIMENT environment
                        args = args[1:]
-                       runenv = append(runenv, "GOEXPERIMENT="+args[0])
+                       if goexp != "" {
+                               goexp += ","
+                       }
+                       goexp += args[0]
+                       runenv = append(runenv, "GOEXPERIMENT="+goexp)
 
                default:
                        flags = append(flags, args[0])
@@ -1258,7 +1268,7 @@ func (t *test) run() {
                runInDir = ""
                var out []byte
                var err error
-               if len(flags)+len(args) == 0 && t.goGcflagsIsEmpty() && !*linkshared && goarch == runtime.GOARCH && goos == runtime.GOOS {
+               if len(flags)+len(args) == 0 && t.goGcflagsIsEmpty() && !*linkshared && goarch == runtime.GOARCH && goos == runtime.GOOS && goexp == env.GOEXPERIMENT {
                        // If we're not using special go command flags,
                        // skip all the go command machinery.
                        // This avoids any time the go command would