Skip to content

Commit

Permalink
Merge pull request #1 from kunwardeep/run-only-test-files
Browse files Browse the repository at this point in the history
Run only test files
  • Loading branch information
kunwardeep authored Sep 25, 2020
2 parents 4b3e527 + 6a287ef commit c3dfdc6
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 100 deletions.
15 changes: 0 additions & 15 deletions cmd/go-printf-func-name/main.go

This file was deleted.

File renamed without changes.
10 changes: 10 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package main

import (
"github.com/jirfag/go-printf-func-name/pkg/paralleltest"
"golang.org/x/tools/go/analysis/singlechecker"
)

func main() {
singlechecker.Main(paralleltest.NewAnalyzer())
}
82 changes: 0 additions & 82 deletions pkg/analyzer/analyzer.go

This file was deleted.

128 changes: 128 additions & 0 deletions pkg/paralleltest/paralleltest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package paralleltest

import (
"fmt"
"go/ast"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
"golang.org/x/tools/go/ast/inspector"
"strings"
)

// TODO add ignoring ability flag
func NewAnalyzer() *analysis.Analyzer {
return &analysis.Analyzer{
Name: "paralleltest",
Doc: "Checks that tests have t.Parallel enabled",
Run: run,
Requires: []*analysis.Analyzer{inspect.Analyzer},
}
}

func run(pass *analysis.Pass) (interface{}, error) {
// Run only for test files
inspector := inspector.New(getTestFiles(pass))

nodeFilter := []ast.Node{
(*ast.FuncDecl)(nil),
}

inspector.Preorder(nodeFilter, func(node ast.Node) {
funcDecl := node.(*ast.FuncDecl)
funcHasParallelMethod := false
rangeStatementExists := false
rangeHasParallelMethod := false

// Check runs for test functions only
if !isItATestFunction(funcDecl) {
return
}

for _, l := range funcDecl.Body.List {
switch v := l.(type) {

// Check if the test method is calling t.parallel
case *ast.ExprStmt:
ast.Inspect(v, func(n ast.Node) bool {
if funcHasParallelMethod == false {
funcHasParallelMethod = callExprCallsMethodParallel(n)
}
return true
})

case *ast.RangeStmt:
// Check if the range over testcases is calling t.parallel
// TODO: Check range statements is over testcases and not any other ranges

rangeStatementExists = true
// TODO: Also check for the assignment tc:tc
ast.Inspect(v, func(n ast.Node) bool {
if rangeHasParallelMethod == false {
rangeHasParallelMethod = callExprCallsMethodParallel(n)
}
return true
})
}
}

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

})

return nil, nil
}

func getTestFiles(pass *analysis.Pass) []*ast.File {
testFileSuffix := "_test.go"

var testFiles []*ast.File
for _, f := range pass.Files {
fileName := pass.Fset.Position(f.Pos()).Filename
if strings.HasSuffix(fileName,testFileSuffix ) {
testFiles = append(testFiles, f)
}
}
return testFiles
}

func callExprCallsMethodParallel(node ast.Node) bool {
methodName := "Parallel"

switch n := node.(type) {
default:
case *ast.CallExpr:
if fun, ok := n.Fun.(*ast.SelectorExpr); ok {
return fun.Sel.Name == methodName
}
}
return false
}

func isItATestFunction(funcDecl *ast.FuncDecl) bool {
testMethodParamName := "t"
testMethodPackageType := "testing"
testMethodStruct := "T"

if funcDecl.Type.Params != nil {
if len(funcDecl.Type.Params.List) > 1 {
return false
}

for _, param := range funcDecl.Type.Params.List {
if param.Names[0].String() == testMethodParamName {
if starExp, ok := param.Type.(*ast.StarExpr); ok {
if selectExpr, ok := starExp.X.(*ast.SelectorExpr); ok {
return fmt.Sprint(selectExpr.X) == testMethodPackageType &&
fmt.Sprint(selectExpr.Sel) == testMethodStruct
}
}
}
}
}
return false
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package analyzer_test
package paralleltest

import (
"os"
"path/filepath"
"testing"

"github.com/jirfag/go-printf-func-name/pkg/analyzer"
"golang.org/x/tools/go/analysis/analysistest"
)

Expand All @@ -16,5 +15,5 @@ func TestAll(t *testing.T) {
}

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

0 comments on commit c3dfdc6

Please sign in to comment.