-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: allow to specify Go version #31
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
package tenv | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"go/ast" | ||
"strconv" | ||
"strings" | ||
|
||
"golang.org/x/tools/go/analysis" | ||
|
@@ -24,13 +27,27 @@ 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) { | ||
lower, err := goVersionLower117(goflag) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if lower { | ||
// Do nothing because T.Setenv added in go1.17 | ||
return nil, nil | ||
} | ||
|
||
inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) | ||
|
||
nodeFilter := []ast.Node{ | ||
|
@@ -211,3 +228,33 @@ func checkSelectorExprTarget(typ *ast.SelectorExpr) bool { | |
targetName := x.Name + "." + typ.Sel.Name | ||
return targetName == "testing.TB" | ||
} | ||
|
||
// goVersionLower117 returns true if version is lower than Go 1.17 or empty. | ||
// version must be in the format 'go1.17', '1.17'. | ||
// In case of an invalid input returns not-nil error. | ||
func goVersionLower117(version string) (bool, error) { | ||
version = strings.TrimPrefix(version, "go") | ||
if version == "" { | ||
return false, nil | ||
} | ||
|
||
parts := strings.Split(version, ".") | ||
if len(parts) != 2 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If 1.17.N is passed, this linter occurs an error at this line. Is this intent ? |
||
return false, errors.New(`go version must has format "go<MAJOR>.<MINOR>" or "<MAJOR>.<MINOR>"`) | ||
} | ||
|
||
major, err := strconv.Atoi(parts[0]) | ||
if err != nil { | ||
alexandear marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return false, fmt.Errorf("go version major part must be a number: %w", err) | ||
} | ||
if major < 1 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If Go become Go2 in the future (probably not), this linter must doesn't work. So I think this doesn't need. What do you think ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The linter might be actual even on Go2. But I can remove this check, no problem. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's remove this check, thanks. |
||
return true, nil | ||
} | ||
|
||
minor, err := strconv.Atoi(parts[1]) | ||
if err != nil { | ||
return false, fmt.Errorf("go version minor part must be a number: %w", err) | ||
} | ||
|
||
return minor < 17, nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module go116 | ||
|
||
go 1.16 |
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 | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's better to print the warning about specifying lower or empty version. What do you think ?