Atlas › Test

skips_test_with_parallelize:ignore

Exact test identity: go.temporal.io/server/tools/parallelize/TestProcessFile/skips_test_with_parallelize:ignore

Package
go.temporal.io/server/tools/parallelize
Suite / test hierarchy
TestProcessFile/skips_test_with_parallelize:ignore
Test
skips_test_with_parallelize:ignore
Introduced at
parallelize.go ×2 Frontier kind: Joint frontier
Covered ranges
26
Covered lines
62
Covered files
1

Covered source

Expand a file to inspect source; the > gutter marks covered lines.

go.temporal.io/server/tools/parallelize/parallelize.go 62 covered LOC · 26 ranges

Open complete file

43 }
44
45 > func processFile(path string) error { parallelize.go
46 > fset := token.NewFileSet()
47 > f, err := parser.ParseFile(fset, path, nil, parser.ParseComments)
48 > if err != nil {
49 return fmt.Errorf("parse %s: %w", path, err)
50 }
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.
54 > type insertion struct { parallelize.go
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 }
65 > if !isTestFunc(fn) { parallelize.go
66 continue
67 }
68 > paramName := testingTParamName(fn) parallelize.go
69 > if paramName == "" {
70 continue
71 }
72 > if hasParallelCall(fn.Body, paramName) { parallelize.go
73 continue
74 }
75 > if hasNoLintComment(fn) { parallelize.go
76 > continue parallelize.go
77 }
78 bodyLine := fset.Position(fn.Body.Lbrace).Line
80 }
81
82 > if len(insertions) == 0 { parallelize.go
83 > return nil parallelize.go
84 > }
85
86 // Sort by line descending so insertions don't shift line numbers of subsequent insertions.
118
119 // isTestFunc returns true for func TestXxx(t *testing.T).
120 > func isTestFunc(fn *ast.FuncDecl) bool { parallelize.go
121 > if fn.Recv != nil {
122 return false // method, not a function
123 }
124 > if !strings.HasPrefix(fn.Name.Name, "Test") { parallelize.go
125 return false
126 }
127 > if fn.Body == nil { parallelize.go
128 return false
129 }
130 > return testingTParamName(fn) != "" parallelize.go
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 }
138 > for _, field := range fn.Type.Params.List { parallelize.go
139 > starExpr, ok := field.Type.(*ast.StarExpr)
140 > if !ok {
141 continue
142 }
143 > selExpr, ok := starExpr.X.(*ast.SelectorExpr) parallelize.go
144 > if !ok {
145 continue
146 }
147 > pkg, ok := selExpr.X.(*ast.Ident) parallelize.go
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 }
159
160 // hasNoLintComment checks for //parallelize:ignore in the function's doc comment.
161 > func hasNoLintComment(fn *ast.FuncDecl) bool { parallelize.go
162 > if fn.Doc == nil {
163 return false
164 }
165 > for _, c := range fn.Doc.List { parallelize.go
166 > if strings.Contains(c.Text, "parallelize:ignore") {
167 > return true
168 > }
169 }
170 return false
172
173 // hasParallelCall checks if the function body already contains <param>.Parallel().
174 > func hasParallelCall(body *ast.BlockStmt, paramName string) bool { parallelize.go
175 > found := false
176 > ast.Inspect(body, func(n ast.Node) bool {
177 > if found {
178 return false
179 }
180 > call, ok := n.(*ast.CallExpr) parallelize.go
181 > if !ok {
182 > return true
183 > }
184 > sel, ok := call.Fun.(*ast.SelectorExpr)
185 > if !ok {
186 return true
187 }
188 > ident, ok := sel.X.(*ast.Ident) parallelize.go
189 > if !ok {
190 return true
191 }
192 > if ident.Name == paramName && sel.Sel.Name == "Parallel" { parallelize.go
193 found = true
194 }
195 > return true parallelize.go
196 })
197 > return found parallelize.go
198 }