Skip to content

Commit

Permalink
testscript: add go version conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Mar 2, 2022
1 parent 2a97aa2 commit 716ac56
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 6 deletions.
7 changes: 2 additions & 5 deletions gotooltest/setup.go
Expand Up @@ -13,7 +13,6 @@ import (
"go/build"
"os/exec"
"path/filepath"
"regexp"
"runtime"
"strings"
"sync"
Expand All @@ -22,8 +21,6 @@ import (
)

var (
goVersionRegex = regexp.MustCompile(`^go([1-9][0-9]*)\.([1-9][0-9]*)$`)

goEnv struct {
GOROOT string
GOCACHE string
Expand Down Expand Up @@ -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:]
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions testscript/doc.go
Expand Up @@ -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.
Expand Down
7 changes: 6 additions & 1 deletion testscript/testdata/cond.txt
Expand Up @@ -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
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
10 changes: 10 additions & 0 deletions testscript/testscript.go
Expand Up @@ -12,6 +12,7 @@ import (
"context"
"flag"
"fmt"
"go/build"
"io/ioutil"
"os"
"os/exec"
Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 716ac56

Please sign in to comment.