]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: add hook to check for GOEXPERIMENT in script tests
authorThan McIntosh <thanm@google.com>
Mon, 25 Apr 2022 14:09:47 +0000 (10:09 -0400)
committerThan McIntosh <thanm@google.com>
Wed, 28 Sep 2022 11:49:00 +0000 (11:49 +0000)
Add a new hook to allow script tests to check whether a specific
GOEXPERIMENT is enabled.

Updates #51430.

Change-Id: Icdf39f845ff2c8b10c634d49e9c27bc90e7984f8
Reviewed-on: https://go-review.googlesource.com/c/go/+/402174
Reviewed-by: Bryan Mills <bcmills@google.com>
Run-TryBot: Than McIntosh <thanm@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>

src/cmd/go/script_test.go
src/cmd/go/testdata/script/README
src/cmd/go/testdata/script/check_goexperiment.txt [new file with mode: 0644]

index 006c4346c7e9f96addcae7649730b41972880489..4f519aa0ee6dc3a053b914b894548946071852f4 100644 (file)
@@ -14,6 +14,7 @@ import (
        "flag"
        "fmt"
        "go/build"
+       "internal/buildcfg"
        "internal/testenv"
        "internal/txtar"
        "io/fs"
@@ -246,6 +247,24 @@ func goVersion() (string, error) {
 
 var execCache par.Cache
 
+func goExperimentIsValid(expname string) bool {
+       for _, exp := range buildcfg.Experiment.All() {
+               if expname == exp || expname == "no"+exp || "no"+expname == exp {
+                       return true
+               }
+       }
+       return false
+}
+
+func goExperimentIsEnabled(expname string) bool {
+       for _, exp := range buildcfg.Experiment.Enabled() {
+               if exp == expname {
+                       return true
+               }
+       }
+       return false
+}
+
 // run runs the test script.
 func (ts *testScript) run() {
        // Truncate log at end of last phase marker,
@@ -444,6 +463,15 @@ Script:
                                        ok = sys.BuildModeSupported(runtime.Compiler, value, runtime.GOOS, runtime.GOARCH)
                                        break
                                }
+                               if strings.HasPrefix(cond.tag, "GOEXPERIMENT:") {
+                                       rawval := strings.TrimPrefix(cond.tag, "GOEXPERIMENT:")
+                                       value := strings.TrimSpace(rawval)
+                                       if !goExperimentIsValid(value) {
+                                               ts.fatalf("unknown/unrecognized GOEXPERIMENT %q", value)
+                                       }
+                                       ok = goExperimentIsEnabled(value)
+                                       break
+                               }
                                if !imports.KnownArch[cond.tag] && !imports.KnownOS[cond.tag] && cond.tag != "gc" && cond.tag != "gccgo" {
                                        ts.fatalf("unknown condition %q", cond.tag)
                                }
index e52917684f8c86c4fd9b8192de6e1f9ea2a082a9..6acef31018767be4af71cebce8a7d09791fd8eca 100644 (file)
@@ -99,6 +99,7 @@ should only run when the condition is satisfied. The available conditions are:
    - [buildmode:value] for whether -buildmode=value is supported
    - [trimpath] for whether the 'go' binary was built with -trimpath
    - [mismatched-goroot] for whether the test's GOROOT_FINAL does not match the real GOROOT
+   - [GOEXPERIMENT:expname] for whether the GOEXPERIMENT 'expname' is enabled
 
 A condition can be negated: [!short] means to run the rest of the line
 when testing.Short() is false. Multiple conditions may be given for a single
diff --git a/src/cmd/go/testdata/script/check_goexperiment.txt b/src/cmd/go/testdata/script/check_goexperiment.txt
new file mode 100644 (file)
index 0000000..3434cb9
--- /dev/null
@@ -0,0 +1,10 @@
+# Test that [GOEXPERIMENT:x] is accepted.
+# Here fieldtrack is picked arbitrarily.
+
+[GOEXPERIMENT:nofieldtrack] env
+
+[GOEXPERIMENT:fieldtrack] env
+
+#[GOEXPERIMENT:crashme] env
+
+