Skip to content

Commit

Permalink
bug: fix check of subtest builder from another file
Browse files Browse the repository at this point in the history
  • Loading branch information
kulti committed Jan 28, 2022
1 parent f64c636 commit 54eb306
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
33 changes: 31 additions & 2 deletions pkg/analyzer/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,8 +412,8 @@ func unwrapTestingFunctionBuilding(pass *analysis.Pass, expr ast.Expr, testFuncT
funcDecl.Body = f.Body
funcDecl.Type = f.Type
case *ast.Ident:
funObjDecl, ok := f.Obj.Decl.(*ast.FuncDecl)
if !ok {
funObjDecl := findFunctionDeclaration(pass, f)
if funObjDecl == nil {
return nil
}

Expand Down Expand Up @@ -533,3 +533,32 @@ func findSelectroDeclaration(pass *analysis.Pass, expr *ast.SelectorExpr) *ast.F

return nil
}

// findFunctionDeclaration returns function declaration called by identity.
func findFunctionDeclaration(pass *analysis.Pass, ident *ast.Ident) *ast.FuncDecl {
if ident.Obj != nil {
if funObjDecl, ok := ident.Obj.Decl.(*ast.FuncDecl); ok {
return funObjDecl
}
}

obj := pass.TypesInfo.ObjectOf(ident)
if obj == nil {
return nil
}

for _, file := range pass.Files {
for _, decl := range file.Decls {
funcDecl, ok := decl.(*ast.FuncDecl)
if !ok {
continue
}

if funcDecl.Name.Pos() == obj.Pos() {
return funcDecl
}
}
}

return nil
}
1 change: 1 addition & 0 deletions pkg/analyzer/testdata/src/t/s.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func (sh subhelper) anotherCheck(t *testing.T) {} // want "test helper function
func TestSubtestWithBuilder(t *testing.T) {
t.Run("sub1", subtestBuilder("first"))
t.Run("sub2", subtestBuilder("second"))
t.Run("sub_from_other_file", subtestBuilderAnotherFile())
t.Run("anon", func() func(t *testing.T) { return func(t *testing.T) {} }())

fixture := subtestFixture{}
Expand Down
7 changes: 7 additions & 0 deletions pkg/analyzer/testdata/src/t/ss.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package t

import "testing"

func subtestBuilderAnotherFile() func(*testing.T) {
return func(t *testing.T) {}
}

0 comments on commit 54eb306

Please sign in to comment.