Skip to content

Commit 337a4b5

Browse files
committed
fileinfo: detect benchmark tests
modify our current parser to detect benchmark tests
1 parent 99af6b3 commit 337a4b5

File tree

2 files changed

+46
-8
lines changed

2 files changed

+46
-8
lines changed

language/go/fileinfo.go

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ type fileInfo struct {
6161
// name ends with "_test"
6262
isExternalTest bool
6363

64+
// hasBenchmarkTests is true if the file contains benchmark tests.
65+
hasBenchmarkTests bool
66+
6467
// imports is a list of packages imported by a file. It does not include
6568
// "C" or anything from the standard library.
6669
imports []string
@@ -214,6 +217,7 @@ func otherFileInfo(path string) fileInfo {
214217
func goFileInfo(path, srcdir string) fileInfo {
215218
info := fileNameInfo(path)
216219
fset := token.NewFileSet()
220+
// parse the file for imports and comments (for CGO)
217221
pf, err := parser.ParseFile(fset, info.path, nil, parser.ImportsOnly|parser.ParseComments)
218222
if err != nil {
219223
log.Printf("%s: error reading go file: %v", info.path, err)
@@ -274,8 +278,8 @@ func goFileInfo(path, srcdir string) fileInfo {
274278
}
275279
info.tags = tags
276280

277-
if importsEmbed || info.packageName == "main" {
278-
pf, err = parser.ParseFile(fset, info.path, nil, parser.ParseComments)
281+
if importsEmbed || info.packageName == "main" || info.isTest {
282+
pf, err = parser.ParseFile(fset, info.path, nil, parser.ParseComments|parser.SkipObjectResolution)
279283
if err != nil {
280284
log.Printf("%s: error reading go file: %v", info.path, err)
281285
return info
@@ -309,6 +313,27 @@ func goFileInfo(path, srcdir string) fileInfo {
309313
}
310314
}
311315
}
316+
for _, decl := range pf.Decls {
317+
if fdecl, ok := decl.(*ast.FuncDecl); ok {
318+
if info.isTest &&
319+
strings.HasPrefix(fdecl.Name.Name, "Benchmark") &&
320+
fdecl.Type.Params.NumFields() == 1 {
321+
starExpr, ok := fdecl.Type.Params.List[0].Type.(*ast.StarExpr)
322+
if !ok {
323+
continue
324+
}
325+
//validate that the only function param is of type *testing.B
326+
selector, ok := starExpr.X.(*ast.SelectorExpr)
327+
if !ok || selector.Sel.String() != "B" {
328+
continue
329+
}
330+
if pkgIdent, ok := selector.X.(*ast.Ident); ok && pkgIdent.Name == "testing" {
331+
info.hasBenchmarkTests = true
332+
break
333+
}
334+
}
335+
}
336+
}
312337
}
313338

314339
return info

language/go/fileinfo_go_test.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,18 @@ func TestGoFileInfo(t *testing.T) {
6565
isTest: false,
6666
},
6767
},
68+
{
69+
"hash bench test",
70+
"foo_test.go",
71+
`package foo_test
72+
73+
func BenchmarkFoo(b *testing.B) {}`,
74+
fileInfo{
75+
packageName: "foo",
76+
isTest: true,
77+
hasBenchmarkTests: true,
78+
},
79+
},
6880
{
6981
"single import",
7082
"foo.go",
@@ -183,12 +195,13 @@ var src string
183195
got := goFileInfo(path, "")
184196
// Clear fields we don't care about for testing.
185197
got = fileInfo{
186-
packageName: got.packageName,
187-
isTest: got.isTest,
188-
imports: got.imports,
189-
embeds: got.embeds,
190-
isCgo: got.isCgo,
191-
tags: got.tags,
198+
packageName: got.packageName,
199+
isTest: got.isTest,
200+
imports: got.imports,
201+
embeds: got.embeds,
202+
isCgo: got.isCgo,
203+
tags: got.tags,
204+
hasBenchmarkTests: got.hasBenchmarkTests,
192205
}
193206
for i := range got.embeds {
194207
got.embeds[i] = fileEmbed{path: got.embeds[i].path}

0 commit comments

Comments
 (0)