-
-
Notifications
You must be signed in to change notification settings - Fork 17
fixes False positive with test builder pattern #54
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
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 | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -89,6 +89,7 @@ func (a *parallelAnalyzer) analyzeTestRun(pass *analysis.Pass, n ast.Node, testV | |||||||
| return true | ||||||||
| }) | ||||||||
| } else if ident, ok := callExpr.Args[1].(*ast.Ident); ok { | ||||||||
| // Case 2: Direct function identifier: t.Run("name", myFunc) | ||||||||
| foundFunc := false | ||||||||
| for _, file := range pass.Files { | ||||||||
| for _, decl := range file.Decls { | ||||||||
|
|
@@ -109,6 +110,9 @@ func (a *parallelAnalyzer) analyzeTestRun(pass *analysis.Pass, n ast.Node, testV | |||||||
| if !foundFunc { | ||||||||
| analysis.hasParallel = false | ||||||||
| } | ||||||||
| } else if builderCall, ok := callExpr.Args[1].(*ast.CallExpr); ok { | ||||||||
| // Case 3: Function call that returns a function: t.Run("name", builder()) | ||||||||
| analysis.hasParallel = a.checkBuilderFunctionForParallel(pass, builderCall) | ||||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
|
|
@@ -230,6 +234,86 @@ func (a *parallelAnalyzer) analyzeTestFunction(pass *analysis.Pass, funcDecl *as | |||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| // checkBuilderFunctionForParallel analyzes a function call that returns a test function | ||||||||
| // to see if the returned function contains t.Parallel() | ||||||||
| func (a *parallelAnalyzer) checkBuilderFunctionForParallel(pass *analysis.Pass, builderCall *ast.CallExpr) bool { | ||||||||
| // Get the name of the builder function being called | ||||||||
| var builderFuncName string | ||||||||
| switch fun := builderCall.Fun.(type) { | ||||||||
| case *ast.Ident: | ||||||||
| builderFuncName = fun.Name | ||||||||
| case *ast.SelectorExpr: | ||||||||
| // Handle method calls like obj.Builder() | ||||||||
| builderFuncName = fun.Sel.Name | ||||||||
| default: | ||||||||
| return false | ||||||||
| } | ||||||||
|
|
||||||||
| if builderFuncName == "" { | ||||||||
| return false | ||||||||
| } | ||||||||
|
|
||||||||
| // Find the builder function declaration | ||||||||
| for _, file := range pass.Files { | ||||||||
| for _, decl := range file.Decls { | ||||||||
| funcDecl, ok := decl.(*ast.FuncDecl) | ||||||||
| if !ok || funcDecl.Name.Name != builderFuncName { | ||||||||
| continue | ||||||||
| } | ||||||||
|
|
||||||||
| // Found the builder function, analyze it and return immediately | ||||||||
| hasParallel := false | ||||||||
| ast.Inspect(funcDecl, func(n ast.Node) bool { | ||||||||
| if hasParallel { | ||||||||
| return false | ||||||||
| } | ||||||||
|
|
||||||||
|
Comment on lines
+267
to
+270
|
||||||||
| if hasParallel { | |
| return false | |
| } |
Uh oh!
There was an error while loading. Please reload this page.