-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnapshot.go
More file actions
151 lines (136 loc) · 3.47 KB
/
snapshot.go
File metadata and controls
151 lines (136 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package main
import (
"go/ast"
"go/parser"
"go/token"
"strings"
"unicode"
"unicode/utf8"
)
type fileSnapshot struct {
packageName string
tests map[string]lineRange
}
func parseFileSnapshot(data []byte) (fileSnapshot, error) {
fset := token.NewFileSet()
file, err := parser.ParseFile(fset, "", data, parser.SkipObjectResolution)
if err != nil {
return fileSnapshot{}, err
}
testingDotImport := hasTestingDotImport(file)
snapshot := fileSnapshot{
packageName: file.Name.Name,
tests: map[string]lineRange{},
}
for _, decl := range file.Decls {
funcDecl, ok := decl.(*ast.FuncDecl)
if !ok || funcDecl.Name == nil {
continue
}
if isTopLevelTestFunc(funcDecl, testingDotImport) || isTopLevelFuzzFunc(funcDecl, testingDotImport) || isTopLevelExampleFunc(funcDecl) {
snapshot.tests[funcDecl.Name.Name] = nodeRange(fset, decl)
}
}
return snapshot, nil
}
func hasTestingDotImport(file *ast.File) bool {
for _, importSpec := range file.Imports {
if importSpec == nil || importSpec.Path == nil {
continue
}
if importSpec.Name == nil || importSpec.Name.Name != "." {
continue
}
if strings.Trim(importSpec.Path.Value, `"`) == "testing" {
return true
}
}
return false
}
func nodeRange(fset *token.FileSet, node ast.Node) lineRange {
start := fset.Position(node.Pos()).Line
end := fset.Position(node.End()).Line
if end < start {
end = start
}
return lineRange{Start: start, End: end}
}
func isTopLevelTestFunc(fn *ast.FuncDecl, testingDotImport bool) bool {
if fn.Recv != nil || !hasRunnableName(fn.Name, "Test", false) {
return false
}
if hasParamSelectorName(fn, "T") {
return true
}
return testingDotImport && hasParamIdentName(fn, "T")
}
func isTopLevelFuzzFunc(fn *ast.FuncDecl, testingDotImport bool) bool {
if fn.Recv != nil || !hasRunnableName(fn.Name, "Fuzz", false) {
return false
}
if hasParamSelectorName(fn, "F") {
return true
}
return testingDotImport && hasParamIdentName(fn, "F")
}
func isTopLevelExampleFunc(fn *ast.FuncDecl) bool {
return fn.Recv == nil && hasRunnableName(fn.Name, "Example", true) && fn.Type != nil && fn.Type.Params != nil && len(fn.Type.Params.List) == 0
}
func hasRunnableName(name *ast.Ident, prefix string, allowBare bool) bool {
if name == nil {
return false
}
rest, ok := strings.CutPrefix(name.Name, prefix)
if !ok {
return false
}
if rest == "" {
return allowBare
}
r, _ := utf8.DecodeRuneInString(rest)
return r == '_' || !unicode.IsLower(r)
}
func hasParamSelectorName(fn *ast.FuncDecl, expectedName string) bool {
if fn.Type == nil || fn.Type.Params == nil {
return false
}
params := fn.Type.Params.List
if len(params) != 1 {
return false
}
name, ok := pointerSelectorName(params[0].Type)
return ok && name == expectedName
}
func hasParamIdentName(fn *ast.FuncDecl, expectedName string) bool {
if fn.Type == nil || fn.Type.Params == nil {
return false
}
params := fn.Type.Params.List
if len(params) != 1 {
return false
}
name, ok := pointerIdentName(params[0].Type)
return ok && name == expectedName
}
func pointerSelectorName(expr ast.Expr) (string, bool) {
star, ok := expr.(*ast.StarExpr)
if !ok {
return "", false
}
selector, ok := star.X.(*ast.SelectorExpr)
if !ok || selector.Sel == nil {
return "", false
}
return selector.Sel.Name, true
}
func pointerIdentName(expr ast.Expr) (string, bool) {
star, ok := expr.(*ast.StarExpr)
if !ok {
return "", false
}
ident, ok := star.X.(*ast.Ident)
if !ok {
return "", false
}
return ident.Name, true
}