52
// Collect line numbers where we need to insert t.Parallel().
53
// Each entry is the line of the opening '{' of the test function body.
55
>
line int // line number of the '{' opening the function body
56
>
paramName string // name of the *testing.T parameter
57
>
}
58
>
var insertions []insertion
59
>
60
>
for _, decl := range f.Decls {
61
>
fn, ok := decl.(*ast.FuncDecl)
62
>
if !ok {
63
>
continue
64
}
66
continue
67
}
69
>
if paramName == "" {
70
continue
71
}
73
continue
74
}
76
continue
77
}
79
>
insertions = append(insertions, insertion{line: bodyLine, paramName: paramName})
80
}
81
83
return nil
84
}
85
86
// Sort by line descending so insertions don't shift line numbers of subsequent insertions.
88
return insertions[i].line > insertions[j].line
89
})
90
92
>
if err != nil {
93
return fmt.Errorf("stat %s: %w", path, err)
94
}
95
97
>
if err != nil {
98
return fmt.Errorf("read %s: %w", path, err)
99
}
100
102
>
for _, ins := range insertions {
103
>
// ins.line is 1-indexed, so it conveniently equals the 0-based index
104
>
// of the line right after '{', which is where we want to insert.
105
>
idx := ins.line
106
>
newLine := "\t" + ins.paramName + ".Parallel()"
107
>
lines = append(lines[:idx+1], lines[idx:]...)
108
>
lines[idx] = newLine
109
>
}
110
111
>
if err := os.WriteFile(path, []byte(strings.Join(lines, "\n")), fi.Mode()); err != nil {
parallelize.go
112
return fmt.Errorf("write %s: %w", path, err)
113
}
114
116
>
return nil
117
}
118
119
// isTestFunc returns true for func TestXxx(t *testing.T).
121
>
if fn.Recv != nil {
122
return false // method, not a function
123
}
125
return false
126
}
128
return false
129
}
131
}
132
133
// testingTParamName returns the name of the *testing.T parameter, or "" if not found.
134
>
func testingTParamName(fn *ast.FuncDecl) string {
parallelize.go
135
>
if fn.Type.Params == nil || len(fn.Type.Params.List) == 0 {
136
return ""
137
}
139
>
starExpr, ok := field.Type.(*ast.StarExpr)
140
>
if !ok {
141
continue
142
}
144
>
if !ok {
145
continue
146
}
148
>
if !ok {
149
continue
150
}
151
>
if pkg.Name == "testing" && selExpr.Sel.Name == "T" {
parallelize.go
152
>
if len(field.Names) > 0 {
153
>
return field.Names[0].Name
154
>
}
155
}
156
}