Skip to content
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

Add flag to ignore missing calls (off by default) #19

Merged
merged 5 commits into from Jun 7, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion main.go
Expand Up @@ -7,5 +7,5 @@ import (
)

func main() {
singlechecker.Main(paralleltest.NewAnalyzer())
singlechecker.Main(paralleltest.Analyzer)
}
39 changes: 24 additions & 15 deletions pkg/paralleltest/paralleltest.go
Expand Up @@ -15,16 +15,21 @@ It also checks that the t.Parallel is used if multiple tests cases are run as pa
As part of ensuring parallel tests works as expected it checks for reinitialising of the range value
over the test cases.(https://tinyurl.com/y6555cy6)`

func NewAnalyzer() *analysis.Analyzer {
return &analysis.Analyzer{
Name: "paralleltest",
Doc: Doc,
Run: run,
Requires: []*analysis.Analyzer{inspect.Analyzer},
}
var Analyzer = &analysis.Analyzer{
Name: "paralleltest",
Doc: Doc,
Run: run,
Requires: []*analysis.Analyzer{inspect.Analyzer},
}

var ignoreMissing bool

func init() {
Analyzer.Flags.BoolVar(&ignoreMissing, "i", false, "ignore missing calls to t.Parallel")
}

func run(pass *analysis.Pass) (interface{}, error) {

inspector := inspector.New(pass.Files)

nodeFilter := []ast.Node{
Expand Down Expand Up @@ -52,12 +57,12 @@ func run(pass *analysis.Pass) (interface{}, error) {

case *ast.ExprStmt:
ast.Inspect(v, func(n ast.Node) bool {
// Check if the test method is calling t.parallel
// Check if the test method is calling t.Parallel
if !funcHasParallelMethod {
funcHasParallelMethod = methodParallelIsCalledInTestFunction(n, testVar)
}

// Check if the t.Run within the test function is calling t.parallel
// Check if the t.Run within the test function is calling t.Parallel
if methodRunIsCalledInTestFunction(n, testVar) {
// n is a call to t.Run; find out the name of the subtest's *testing.T parameter.
innerTestVar := getRunCallbackParameterName(n)
Expand All @@ -77,7 +82,7 @@ func run(pass *analysis.Pass) (interface{}, error) {
return true
})

// Check if the range over testcases is calling t.parallel
// Check if the range over testcases is calling t.Parallel
case *ast.RangeStmt:
rangeNode = v

Expand Down Expand Up @@ -114,22 +119,26 @@ func run(pass *analysis.Pass) (interface{}, error) {
}
}

if !funcHasParallelMethod {
if !ignoreMissing && !funcHasParallelMethod {
pass.Reportf(node.Pos(), "Function %s missing the call to method parallel\n", funcDecl.Name.Name)
}

if rangeStatementOverTestCasesExists && rangeNode != nil {
if !rangeStatementHasParallelMethod {
pass.Reportf(rangeNode.Pos(), "Range statement for test %s missing the call to method parallel in test Run\n", funcDecl.Name.Name)
if !ignoreMissing {
pass.Reportf(rangeNode.Pos(), "Range statement for test %s missing the call to method parallel in test Run\n", funcDecl.Name.Name)
}
} else if loopVariableUsedInRun != nil {
pass.Reportf(rangeNode.Pos(), "Range statement for test %s does not reinitialise the variable %s\n", funcDecl.Name.Name, *loopVariableUsedInRun)
}
}

// Check if the t.Run is more than one as there is no point making one test parallel
if numberOfTestRun > 1 && len(positionOfTestRunNode) > 0 {
for _, n := range positionOfTestRunNode {
pass.Reportf(n.Pos(), "Function %s has missing the call to method parallel in the test run\n", funcDecl.Name.Name)
if !ignoreMissing {
if numberOfTestRun > 1 && len(positionOfTestRunNode) > 0 {
for _, n := range positionOfTestRunNode {
pass.Reportf(n.Pos(), "Function %s has missing the call to method parallel in the test run\n", funcDecl.Name.Name)
}
}
}
})
Expand Down
2 changes: 1 addition & 1 deletion pkg/paralleltest/paralleltest_test.go
Expand Up @@ -16,5 +16,5 @@ func TestAll(t *testing.T) {
}

testdata := filepath.Join(filepath.Dir(wd), "paralleltest", "testdata")
analysistest.Run(t, testdata, NewAnalyzer(), "t")
analysistest.Run(t, testdata, Analyzer, "t")
}