Skip to content

Commit 6051d30

Browse files
cweillclaude
andcommitted
fix: add proper t.Parallel() and import path handling
## Fix Issue #188: Proper t.Parallel() Generation - Add t.Parallel() at the top level of test functions when -parallel is used - Keep t.Parallel() in subtests as well for optimal parallelism - This satisfies the tparallel linter and Go testing best practices - Updated both standard and testify templates - Updated all golden test files to reflect new behavior ## Fix PR #179: Missing Import Paths - When receiver and methods are in different files, imports from all files in the package are now included in the generated test - Fixes compilation errors when types come from separate files 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 043fe32 commit 6051d30

File tree

8 files changed

+22
-19
lines changed

8 files changed

+22
-19
lines changed

.claude/settings.local.json

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,9 @@
11
{
22
"permissions": {
33
"allow": [
4-
"Bash(git add:*)",
5-
"Bash(git commit:*)",
6-
"Bash(gh issue list:*)",
7-
"Bash(gh pr list:*)",
8-
"Bash(gh issue view:*)",
9-
"Bash(gh pr view:*)",
10-
"Bash(git tag:*)",
11-
"Bash(git show:*)",
12-
"Bash(go generate:*)",
13-
"Bash(go test:*)",
14-
"Bash(gh pr diff:*)",
15-
"Bash(gh api:*)",
16-
"Bash(for f in *.tmpl)",
17-
"Bash(do if [ -L \"$f\" ])",
18-
"Bash(fi)",
19-
"Bash(done)"
4+
"Read(//private/var/folders/sp/lcxhh3r54wzf6zdmlh03pz5r0000gn/T/**)",
5+
"Bash(awk:*)",
6+
"Bash(cat:*)"
207
],
218
"deny": [],
229
"ask": []

internal/goparser/goparser.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,19 @@ func (p *Parser) Parse(srcPath string, files []models.Path) (*Result, error) {
4848
if err != nil {
4949
return nil, err
5050
}
51-
return &Result{
51+
result := &Result{
5252
Header: &models.Header{
5353
Comments: parsePkgComment(f, f.Package),
5454
Package: f.Name.String(),
5555
Imports: parseImports(f.Imports),
5656
Code: goCode(b, f),
5757
},
5858
Funcs: p.parseFunctions(fset, f, fs),
59-
}, nil
59+
}
60+
for _, v := range fs {
61+
result.Header.Imports = append(result.Header.Imports, parseImports(v.Imports)...)
62+
}
63+
return result, nil
6064
}
6165

6266
func (p *Parser) readFile(srcPath string) ([]byte, error) {

internal/render/templates/function.tmpl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
{{- $f := .}}
33

44
func {{.TestName}}(t *testing.T) {
5+
{{- if .Parallel}}
6+
t.Parallel()
7+
{{- end}}
58
{{- with .Receiver}}
69
{{- if .IsStruct}}
710
{{- if .Fields}}

templates/testify/function.tmpl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ tt.assertion(t, {{if .OnlyReturnsError}}{{template "call" .}}{{else}}err{{end}}
1717
{{- $f := .}}
1818

1919
func {{.TestName}}(t *testing.T) {
20+
{{- if .Parallel}}
21+
t.Parallel()
22+
{{- end}}
2023
{{- with .Receiver}}
2124
{{- if .IsStruct}}
2225
{{- if .Fields}}

testdata/goldens/naked_function_with_parallel_subtests.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package testdata
33
import "testing"
44

55
func Test_main(t *testing.T) {
6+
t.Parallel()
67
tests := []struct {
78
name string
89
}{
@@ -18,6 +19,7 @@ func Test_main(t *testing.T) {
1819
}
1920

2021
func Test_do(t *testing.T) {
22+
t.Parallel()
2123
tests := []struct {
2224
name string
2325
}{

testdata/goldens/naked_function_without_subtests_with_parallel.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ package testdata
33
import "testing"
44

55
func Test_main(t *testing.T) {
6-
tests := []struct {
6+
t.Parallel()
7+
tests := []struct{
78
name string
89
}{
910
// TODO: Add test cases.
@@ -14,6 +15,7 @@ func Test_main(t *testing.T) {
1415
}
1516

1617
func Test_do(t *testing.T) {
18+
t.Parallel()
1719
tests := []struct {
1820
name string
1921
}{

testdata/named/named_on_subtests_on_parallel_on.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package testdata
33
import "testing"
44

55
func TestFoo038(t *testing.T) {
6+
t.Parallel()
67
tests := map[string]struct {
78
want bool
89
}{

testdata/named/named_on_subtests_on_parallel_on_template_testify.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
)
88

99
func TestFoo038(t *testing.T) {
10+
t.Parallel()
1011
tests := map[string]struct {
1112
want bool
1213
}{

0 commit comments

Comments
 (0)