Skip to content

Commit

Permalink
feat: allow to specify Go version
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandear committed May 3, 2023
1 parent c014cf9 commit 992f532
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 2 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ go vet -vettool=(which tenv) ./...

### option

The option `all` will run against whole test files (`_test.go`) regardless of method/function signatures.
The option `all` will run against whole test files (`_test.go`) regardless of method/function signatures.

The option `go` allows to specify Go version. If the version is not empty and lower than Go 1.17 the analysis will be skipped.

By default, only methods that take `*testing.T`, `*testing.B`, and `testing.TB` as arguments are checked.

Expand Down
43 changes: 42 additions & 1 deletion tenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tenv

import (
"go/ast"
"strconv"
"strings"

"golang.org/x/tools/go/analysis"
Expand All @@ -24,13 +25,22 @@ var Analyzer = &analysis.Analyzer{
var (
A = "all"
aflag bool

Go = "go"
goflag string
)

func init() {
Analyzer.Flags.BoolVar(&aflag, A, false, "the all option will run against all method in test file")
Analyzer.Flags.StringVar(&goflag, Go, "1.17", "Go version")
}

func run(pass *analysis.Pass) (interface{}, error) {
if goVersionLower117(goflag) {
// Do nothing because T.Setenv added in go1.17
return nil, nil
}

inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)

nodeFilter := []ast.Node{
Expand All @@ -44,7 +54,6 @@ func run(pass *analysis.Pass) (interface{}, error) {
checkFuncDecl(pass, n, pass.Fset.File(n.Pos()).Name())
case *ast.FuncLit:
checkFuncLit(pass, n, pass.Fset.File(n.Pos()).Name())
}
})

return nil, nil
Expand Down Expand Up @@ -211,3 +220,35 @@ func checkSelectorExprTarget(typ *ast.SelectorExpr) bool {
targetName := x.Name + "." + typ.Sel.Name
return targetName == "testing.TB"
}

// goVersionLower117 returns true if version is lower than go1.17.
// In case of any parse errors it returns false.
func goVersionLower117(version string) bool {
version = strings.TrimPrefix(version, "go")
if version == "" {
return false
}

parts := strings.Split(version, ".")
if len(parts) != 2 {
return false
}

major, err := strconv.Atoi(parts[0])
if err != nil {
return false
}
if major < 1 {
return true
}

minor, err := strconv.Atoi(parts[1])
if err != nil {
return false
}
if minor < 17 {
return true
}

return false
}
7 changes: 7 additions & 0 deletions tenv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,10 @@ func TestAnalyzer(t *testing.T) {
testdata := testutil.WithModules(t, analysistest.TestData(), nil)
analysistest.Run(t, testdata, tenv.Analyzer, "a")
}

func TestAnalyzerGo116(t *testing.T) {
testdata := testutil.WithModules(t, analysistest.TestData(), nil)
a := tenv.Analyzer
a.Flags.Parse([]string{"-go", "1.16"})
analysistest.Run(t, testdata, a, "go116")
}
3 changes: 3 additions & 0 deletions testdata/src/go116/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module go116

go 1.16
12 changes: 12 additions & 0 deletions testdata/src/go116/go116_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package go116

import (
"os"
"testing"
)

func TestF(t *testing.T) {
os.Setenv("a", "b") // if -go = 1.16, ""
err := os.Setenv("a", "b") // if -go = 1.16, ""
_ = err
}

0 comments on commit 992f532

Please sign in to comment.