interp/build: improve Go version handling.

This commit is contained in:
Ludovic Fernandez
2019-07-30 23:34:04 +02:00
committed by Traefiker Bot
parent aaddc39981
commit ee81ee7fea
6 changed files with 97 additions and 11 deletions

View File

@@ -44,7 +44,6 @@
path = "cmd/goexports/goexports.go"
text = "SA1019: importer.For is deprecated: use ForCompiler, which populates a FileSet with the positions of objects created by the importer."
# structcheck false-positive
[[issues.exclude-rules]]
path = "interp/interp.go"
text = "`(astDot|cfgDot|noRun)` is unused"
path = "interp/.+_test\\.go"
linters = ["goconst"]

View File

@@ -1,6 +1,7 @@
package interp
import (
"go/build"
"go/parser"
"path"
"runtime"
@@ -57,7 +58,7 @@ func buildOptionOk(tag string) bool {
var (
goos = runtime.GOOS
goarch = runtime.GOARCH
goversion = goNumVersion()
goversion = goMinorVersion(build.Default)
)
// buildTagOk returns true if a build tag matches, false otherwise
@@ -85,11 +86,20 @@ func buildTagOk(s string) (r bool) {
return
}
// goNumVersion returns the go minor version number
func goNumVersion() int {
v := strings.Split(runtime.Version(), ".")
n, _ := strconv.Atoi(v[1])
return n
// goMinorVersion returns the go minor version number
func goMinorVersion(ctx build.Context) int {
current := ctx.ReleaseTags[len(ctx.ReleaseTags)-1]
v := strings.Split(current, ".")
if len(v) < 2 {
panic("unsupported Go version: " + current)
}
m, err := strconv.Atoi(v[1])
if err != nil {
panic("unsupported Go version: " + current)
}
return m
}
// skipFile returns true if file should be skipped

View File

@@ -1,6 +1,8 @@
package interp
import (
"go/build"
"math"
"testing"
)
@@ -18,10 +20,45 @@ func TestBuildTag(t *testing.T) {
tests := []testBuild{
{"// +build linux", true},
{"// +build windows", false},
{"// +build go1.9", true},
{"// +build go1.11", true},
{"// +build !go1.12", true},
{"// +build go1.12", false},
{"// +build !go1.10", false},
{"// +build !go1.12", true},
{"// +build ignore", false},
{"// +build linux,amd64", true},
{"// +build linux,i386", false},
{"// +build linux,i386 go1.11", true},
{"// +build linux\n// +build amd64", true},
{"// +build linux\n\n\n// +build amd64", true},
{"// +build linux\n// +build i386", false},
}
i := New(Options{})
for _, test := range tests {
test := test
src := test.src + "\npackage x"
t.Run(test.src, func(t *testing.T) {
if r := i.buildOk("", src); r != test.res {
t.Errorf("got %v, want %v", r, test.res)
}
})
}
}
func TestBuildTagDevel(t *testing.T) {
// Assume a specific OS, arch and go version no matter the real underlying system
oo, oa, ov := goos, goarch, goversion
goos, goarch, goversion = "linux", "amd64", math.MaxInt16
defer func() { goos, goarch, goversion = oo, oa, ov }()
tests := []testBuild{
{"// +build linux", true},
{"// +build windows", false},
{"// +build go1.11", true},
{"// +build !go1.12", false},
{"// +build go1.12", true},
{"// +build !go1.10", false},
{"// +build go1.9", true},
{"// +build ignore", false},
{"// +build linux,amd64", true},
@@ -36,7 +73,7 @@ func TestBuildTag(t *testing.T) {
for _, test := range tests {
test := test
src := test.src + "\npackage x"
t.Run("", func(t *testing.T) {
t.Run(test.src, func(t *testing.T) {
if r := i.buildOk("", src); r != test.res {
t.Errorf("got %v, want %v", r, test.res)
}
@@ -72,3 +109,37 @@ func TestBuildFile(t *testing.T) {
})
}
}
func Test_goMinorVersion(t *testing.T) {
tests := []struct {
desc string
context build.Context
expected int
}{
{
desc: "stable",
context: build.Context{ReleaseTags: []string{
"go1.1", "go1.2", "go1.3", "go1.4", "go1.5", "go1.6", "go1.7", "go1.8", "go1.9", "go1.10", "go1.11", "go1.12",
}},
expected: 12,
},
{
desc: "devel/beta/rc",
context: build.Context{ReleaseTags: []string{
"go1.1", "go1.2", "go1.3", "go1.4", "go1.5", "go1.6", "go1.7", "go1.8", "go1.9", "go1.10", "go1.11", "go1.12", "go1.13",
}},
expected: 13,
},
}
for _, test := range tests {
test := test
t.Run(test.desc, func(t *testing.T) {
minor := goMinorVersion(test.context)
if minor != test.expected {
t.Errorf("got %v, want %v", minor, test.expected)
}
})
}
}

View File

@@ -1,3 +1,5 @@
// +build go1.11,!go1.13
package stdlib
import "reflect"

View File

@@ -1,3 +1,5 @@
// +build go1.11,!go1.13
package syscall
import "reflect"

View File

@@ -1,3 +1,5 @@
// +build go1.11,!go1.13
package unsafe
import "reflect"