cmd/go: add hook to check for GOEXPERIMENT in script tests

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>
This commit is contained in:
Than McIntosh 2022-04-25 10:09:47 -04:00
parent 5f18e46328
commit 06e8022ed4
3 changed files with 39 additions and 0 deletions

View 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)
}

View 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

View File

@ -0,0 +1,10 @@
# Test that [GOEXPERIMENT:x] is accepted.
# Here fieldtrack is picked arbitrarily.
[GOEXPERIMENT:nofieldtrack] env
[GOEXPERIMENT:fieldtrack] env
#[GOEXPERIMENT:crashme] env