Skip to content

Commit 3461ca1

Browse files
twpaynemvdan
authored andcommitted
testscript: add go version conditions
1 parent 635e0f0 commit 3461ca1

File tree

4 files changed

+17
-17
lines changed

4 files changed

+17
-17
lines changed

gotooltest/setup.go

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"bytes"
1111
"encoding/json"
1212
"fmt"
13-
"go/build"
1413
"os/exec"
1514
"path/filepath"
1615
"regexp"
@@ -104,21 +103,6 @@ func Setup(p *testscript.Params) error {
104103
p.Cmds = make(map[string]func(ts *testscript.TestScript, neg bool, args []string))
105104
}
106105
p.Cmds["go"] = cmdGo
107-
origCondition := p.Condition
108-
p.Condition = func(cond string) (bool, error) {
109-
if goVersionRegex.MatchString(cond) {
110-
for _, v := range build.Default.ReleaseTags {
111-
if cond == v {
112-
return true, nil
113-
}
114-
}
115-
return false, nil
116-
}
117-
if origCondition == nil {
118-
return false, fmt.Errorf("unknown condition %q", cond)
119-
}
120-
return origCondition(cond)
121-
}
122106
return nil
123107
}
124108

testscript/doc.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ should only run when the condition is satisfied. The predefined conditions are:
107107
- [exec:prog] for whether prog is available for execution (found by exec.LookPath)
108108
- [gc] for whether Go was built with gc
109109
- [gccgo] for whether Go was built with gccgo
110+
- [go1.x] for whether the Go version is 1.x or later
110111
111112
A condition can be negated: [!short] means to run the rest of the line
112113
when testing.Short() is false.

testscript/testdata/cond.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,9 @@
33
# test that exactly one of gc and gccgo is set
44
[gc] exec sh -c 'echo gc >> go-compiler'
55
[gccgo] exec sh -c 'echo gccgo >> go-compiler'
6-
grep '\A(gc|gccgo)\n\z' go-compiler
6+
grep '\A(gc|gccgo)\n\z' go-compiler
7+
8+
# test that go version build tags are set
9+
[go1.1] exec sh -c 'echo go1.1 >> go-version'
10+
[go2.1] exec sh -c 'echo go2.1 >> go-version'
11+
grep '\Ago1\.1\n\z' go-version

testscript/testscript.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"context"
1313
"flag"
1414
"fmt"
15+
"go/build"
1516
"io/ioutil"
1617
"os"
1718
"os/exec"
@@ -30,6 +31,8 @@ import (
3031
"github.com/rogpeppe/go-internal/txtar"
3132
)
3233

34+
var goVersionRegex = regexp.MustCompile(`^go([1-9][0-9]*)\.([1-9][0-9]*)$`)
35+
3336
var execCache par.Cache
3437

3538
// 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) {
597600
// binary was built with but not necessarily the compiler
598601
// that will be used.
599602
return cond == runtime.Compiler, nil
603+
case goVersionRegex.MatchString(cond):
604+
for _, v := range build.Default.ReleaseTags {
605+
if cond == v {
606+
return true, nil
607+
}
608+
}
609+
return false, nil
600610
case ts.params.Condition != nil:
601611
return ts.params.Condition(cond)
602612
default:

0 commit comments

Comments
 (0)