Skip to content

Commit 62ceed7

Browse files
myitcvrogpeppe
authored andcommitted
gotooltest: add support for version constraints (#40)
This effectively mirrors the go/build constraint supports such as go1.12 to check that the Go version is >= 1.12. Also remove some duplicated code in testscript that I had previously missed was already present in gotooltest.
1 parent e4e7fb0 commit 62ceed7

File tree

5 files changed

+40
-2
lines changed

5 files changed

+40
-2
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ os:
55
- osx
66
go:
77
- "1.11.x"
8+
- "1.12beta1"
89
env:
910
- GO111MODULE=on
1011
go_import_path: github.com/rogpeppe/go-internal

gotooltest/script_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2018 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package gotooltest_test
6+
7+
import (
8+
"testing"
9+
10+
"github.com/rogpeppe/go-internal/gotooltest"
11+
"github.com/rogpeppe/go-internal/testscript"
12+
)
13+
14+
func TestSimple(t *testing.T) {
15+
p := testscript.Params{
16+
Dir: "testdata",
17+
}
18+
if err := gotooltest.Setup(&p); err != nil {
19+
t.Fatal(err)
20+
}
21+
testscript.Run(t, p)
22+
}

gotooltest/setup.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,21 @@ package gotooltest
88

99
import (
1010
"fmt"
11+
"go/build"
1112
"os/exec"
1213
"path/filepath"
14+
"regexp"
1315
"runtime"
1416
"strings"
1517

1618
"github.com/rogpeppe/go-internal/imports"
1719
"github.com/rogpeppe/go-internal/testscript"
1820
)
1921

22+
var (
23+
goVersionRegex = regexp.MustCompile(`^go([1-9][0-9]*)\.(0|[1-9][0-9]*)$`)
24+
)
25+
2026
type testContext struct {
2127
goroot string
2228
gocache string
@@ -56,6 +62,14 @@ func Setup(p *testscript.Params) error {
5662
return false, nil
5763
}
5864
}
65+
if goVersionRegex.MatchString(cond) {
66+
for _, v := range build.Default.ReleaseTags {
67+
if cond == v {
68+
return true, nil
69+
}
70+
}
71+
return false, nil
72+
}
5973
if origCondition == nil {
6074
return false, fmt.Errorf("unknown condition %q", cond)
6175
}

gotooltest/testdata/version.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
go version
2+
[go1.11] [!go1.12] stdout go1\.11
3+
[go1.12] stdout go1\.12

testscript/testscript.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,8 +367,6 @@ Script:
367367
// condition reports whether the given condition is satisfied.
368368
func (ts *TestScript) condition(cond string) (bool, error) {
369369
switch cond {
370-
case runtime.GOOS, runtime.GOARCH, runtime.Compiler:
371-
return true, nil
372370
case "short":
373371
return testing.Short(), nil
374372
case "net":

0 commit comments

Comments
 (0)