Skip to content

Commit

Permalink
fix: filter sub-test functions from thelper checks
Browse files Browse the repository at this point in the history
  • Loading branch information
kulti committed Jan 8, 2021
1 parent f3e8da7 commit 1c4fa19
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 2 deletions.
58 changes: 56 additions & 2 deletions pkg/analyzer/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ func (t thelper) run(pass *analysis.Pass) (interface{}, error) {
nodeFilter := []ast.Node{
(*ast.FuncDecl)(nil),
(*ast.FuncLit)(nil),
(*ast.CallExpr)(nil),
}
inspect.Preorder(nodeFilter, func(node ast.Node) {
var fd funcDecl
Expand All @@ -138,10 +139,14 @@ func (t thelper) run(pass *analysis.Pass) (interface{}, error) {
fd.Body = n.Body
fd.Name = ast.NewIdent("")
case *ast.FuncDecl:
fd.Pos = n.Pos()
fd.Pos = n.Name.NamePos
fd.Type = n.Type
fd.Body = n.Body
fd.Name = n.Name
case *ast.CallExpr:
reports.Filter(subtestPos(pass, n, tCheckOpts.tbRun))
reports.Filter(subtestPos(pass, n, bCheckOpts.tbRun))
return
default:
return
}
Expand All @@ -159,6 +164,7 @@ type checkFuncOpts struct {
skipPrefix string
varName string
tbHelper types.Object
tbRun types.Object
tbType types.Type
ctxType types.Type
checkBegin bool
Expand All @@ -177,10 +183,16 @@ func (t thelper) buildTestCheckFuncOpts(pass *analysis.Pass, ctxType types.Type)
return checkFuncOpts{}, false
}

tRun, _, _ := types.LookupFieldOrMethod(tObj.Type(), true, tObj.Pkg(), "Run")
if tRun == nil {
return checkFuncOpts{}, false
}

return checkFuncOpts{
skipPrefix: "Test",
varName: "t",
tbHelper: tHelper,
tbRun: tRun,
tbType: types.NewPointer(tObj.Type()),
ctxType: ctxType,
checkBegin: t.enabledChecks.Enabled(checkTBegin),
Expand All @@ -200,10 +212,16 @@ func (t thelper) buildBenchmarkCheckFuncOpts(pass *analysis.Pass, ctxType types.
return checkFuncOpts{}, false
}

bRun, _, _ := types.LookupFieldOrMethod(bObj.Type(), true, bObj.Pkg(), "Run")
if bRun == nil {
return checkFuncOpts{}, false
}

return checkFuncOpts{
skipPrefix: "Benchmark",
varName: "b",
tbHelper: bHelper,
tbRun: bRun,
tbType: types.NewPointer(bObj.Type()),
ctxType: ctxType,
checkBegin: t.enabledChecks.Enabled(checkBBegin),
Expand Down Expand Up @@ -286,10 +304,46 @@ func isTHelperCall(pass *analysis.Pass, s ast.Stmt, tHelper types.Object) bool {
return false
}

return isSelectorCall(pass, selExpr, tHelper)
}

func subtestPos(pass *analysis.Pass, e *ast.CallExpr, tbRun types.Object) token.Pos {
selExpr, ok := e.Fun.(*ast.SelectorExpr)
if !ok {
return token.NoPos
}

if !isSelectorCall(pass, selExpr, tbRun) {
return token.NoPos
}

if len(e.Args) != 2 {
return token.NoPos
}

anonFunLit, ok := e.Args[1].(*ast.FuncLit)
if ok {
return anonFunLit.Pos()
}

funIdent, ok := e.Args[1].(*ast.Ident)
if !ok {
return token.NoPos
}

funDef, ok := pass.TypesInfo.Uses[funIdent]
if !ok {
return token.NoPos
}

return funDef.Pos()
}

func isSelectorCall(pass *analysis.Pass, selExpr *ast.SelectorExpr, callObj types.Object) bool {
sel, ok := pass.TypesInfo.Selections[selExpr]
if !ok {
return false
}

return sel.Obj() == tHelper
return sel.Obj() == callObj
}
13 changes: 13 additions & 0 deletions pkg/analyzer/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

type reports struct {
reports []report
filter map[token.Pos]struct{}
}

type report struct {
Expand All @@ -24,8 +25,20 @@ func (rr *reports) Reportf(pos token.Pos, format string, args ...interface{}) {
})
}

func (rr *reports) Filter(pos token.Pos) {
if pos.IsValid() {
if rr.filter == nil {
rr.filter = make(map[token.Pos]struct{})
}
rr.filter[pos] = struct{}{}
}
}

func (rr reports) Flush(pass *analysis.Pass) {
for _, r := range rr.reports {
if _, ok := rr.filter[r.pos]; ok {
continue
}
pass.Reportf(r.pos, r.format, r.args...)
}
}
14 changes: 14 additions & 0 deletions pkg/analyzer/testdata/src/b/s.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package b

import "testing"

func TestSubtestWithAnonymous(t *testing.T) {
t.Run("sub", func(t *testing.T) {})
}

func TestSubtest(t *testing.T) {
t.Run("sub", check)
}

func check(t *testing.T) {
}
14 changes: 14 additions & 0 deletions pkg/analyzer/testdata/src/t/s.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package t

import "testing"

func BenchmarkSubtestWithAnonymous(b *testing.B) {
b.Run("sub", func(b *testing.B) {})
}

func BenchmarkSubtest(b *testing.B) {
b.Run("sub", check)
}

func check(b *testing.B) {
}

0 comments on commit 1c4fa19

Please sign in to comment.