From 716ac56b1326e034a78989bafbc7ec565cbc584d Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Wed, 2 Mar 2022 01:44:12 +0100 Subject: [PATCH] testscript: add go version conditions --- gotooltest/setup.go | 7 ++----- testscript/doc.go | 1 + testscript/testdata/cond.txt | 7 ++++++- testscript/testscript.go | 10 ++++++++++ 4 files changed, 19 insertions(+), 6 deletions(-) diff --git a/gotooltest/setup.go b/gotooltest/setup.go index df587b61..68008e33 100644 --- a/gotooltest/setup.go +++ b/gotooltest/setup.go @@ -13,7 +13,6 @@ import ( "go/build" "os/exec" "path/filepath" - "regexp" "runtime" "strings" "sync" @@ -22,8 +21,6 @@ import ( ) var ( - goVersionRegex = regexp.MustCompile(`^go([1-9][0-9]*)\.([1-9][0-9]*)$`) - goEnv struct { GOROOT string GOCACHE string @@ -69,7 +66,7 @@ func initGoEnv() error { } version := goEnv.releaseTags[len(goEnv.releaseTags)-1] - if !goVersionRegex.MatchString(version) { + if !testscript.GoVersionRegex.MatchString(version) { return fmt.Errorf("invalid go version %q", version) } goEnv.goversion = version[2:] @@ -112,7 +109,7 @@ func Setup(p *testscript.Params) error { // that will be used. return cond == runtime.Compiler, nil } - if goVersionRegex.MatchString(cond) { + if testscript.GoVersionRegex.MatchString(cond) { for _, v := range build.Default.ReleaseTags { if cond == v { return true, nil diff --git a/testscript/doc.go b/testscript/doc.go index 24dc703a..ba91db36 100644 --- a/testscript/doc.go +++ b/testscript/doc.go @@ -107,6 +107,7 @@ should only run when the condition is satisfied. The predefined conditions are: - [exec:prog] for whether prog is available for execution (found by exec.LookPath) - [gc] for whether Go was built with gc - [gccgo] for whether Go was built with gccgo + - [go1.x] for whether the Go version is 1.x or later A condition can be negated: [!short] means to run the rest of the line when testing.Short() is false. diff --git a/testscript/testdata/cond.txt b/testscript/testdata/cond.txt index 4c2ca21c..60b62c2a 100644 --- a/testscript/testdata/cond.txt +++ b/testscript/testdata/cond.txt @@ -3,4 +3,9 @@ # test that exactly one of gc and gccgo is set [gc] exec sh -c 'echo gc >> go-compiler' [gccgo] exec sh -c 'echo gccgo >> go-compiler' -grep '\A(gc|gccgo)\n\z' go-compiler \ No newline at end of file +grep '\A(gc|gccgo)\n\z' go-compiler + +# test that go version build tags are set +[go1.1] exec sh -c 'echo go1.1 >> go-version' +[go2.1] exec sh -c 'echo go2.1 >> go-version' +grep '\Ago1\.1\n\z' go-version \ No newline at end of file diff --git a/testscript/testscript.go b/testscript/testscript.go index 586c5a60..951ce5db 100644 --- a/testscript/testscript.go +++ b/testscript/testscript.go @@ -12,6 +12,7 @@ import ( "context" "flag" "fmt" + "go/build" "io/ioutil" "os" "os/exec" @@ -30,6 +31,8 @@ import ( "github.com/rogpeppe/go-internal/txtar" ) +var GoVersionRegex = regexp.MustCompile(`^go([1-9][0-9]*)\.([1-9][0-9]*)$`) + var execCache par.Cache // If -testwork is specified, the test prints the name of the temp directory @@ -597,6 +600,13 @@ func (ts *TestScript) condition(cond string) (bool, error) { // binary was built with but not necessarily the compiler // that will be used. return cond == runtime.Compiler, nil + case GoVersionRegex.MatchString(cond): + for _, v := range build.Default.ReleaseTags { + if cond == v { + return true, nil + } + } + return false, nil case ts.params.Condition != nil: return ts.params.Condition(cond) default: