| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package testfuncs |
| 6 |
|
| 7 |
import ( |
| 8 |
"go/ast" |
| 9 |
"go/constant" |
| 10 |
"go/types" |
| 11 |
"strings" |
| 12 |
"unicode" |
| 13 |
"unicode/utf8" |
| 14 |
|
| 15 |
"golang.org/x/tools/gopls/internal/cache/parsego" |
| 16 |
"golang.org/x/tools/gopls/internal/protocol" |
| 17 |
"golang.org/x/tools/gopls/internal/util/frob" |
| 18 |
"golang.org/x/tools/gopls/internal/util/safetoken" |
| 19 |
) |
| 20 |
|
| 21 |
|
| 22 |
type Index struct { |
| 23 |
pkg gobPackage |
| 24 |
} |
| 25 |
|
| 26 |
|
| 27 |
func Decode(data []byte) *Index { |
| 28 |
return &Index{packageCodec.Decode(data)} |
| 29 |
} |
| 30 |
|
| 31 |
|
| 32 |
func (index *Index) Encode() []byte { |
| 33 |
return packageCodec.Encode(index.pkg) |
| 34 |
} |
| 35 |
|
| 36 |
func (index *Index) All() []Result { |
| 37 |
var results []Result |
| 38 |
for _, file := range index.pkg.Files { |
| 39 |
for _, test := range file.Tests { |
| 40 |
results = append(results, test.result()) |
| 41 |
} |
| 42 |
} |
| 43 |
return results |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
type Result struct { |
| 48 |
Location protocol.Location |
| 49 |
Name string |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
func NewIndex(files []*parsego.File, info *types.Info) *Index { |
| 55 |
b := &indexBuilder{ |
| 56 |
fileIndex: make(map[protocol.DocumentURI]int), |
| 57 |
subNames: make(map[string]int), |
| 58 |
visited: make(map[*types.Func]bool), |
| 59 |
} |
| 60 |
return b.build(files, info) |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
func (b *indexBuilder) build(files []*parsego.File, info *types.Info) *Index { |
| 65 |
for _, file := range files { |
| 66 |
if !strings.HasSuffix(file.Tok.Name(), "_test.go") { |
| 67 |
continue |
| 68 |
} |
| 69 |
|
| 70 |
for _, decl := range file.File.Decls { |
| 71 |
decl, ok := decl.(*ast.FuncDecl) |
| 72 |
if !ok { |
| 73 |
continue |
| 74 |
} |
| 75 |
obj, ok := info.ObjectOf(decl.Name).(*types.Func) |
| 76 |
if !ok || !obj.Exported() { |
| 77 |
continue |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
if obj.Pkg() == nil { |
| 82 |
continue |
| 83 |
} |
| 84 |
|
| 85 |
isTest, isExample := isTestOrExample(obj) |
| 86 |
if !isTest && !isExample { |
| 87 |
continue |
| 88 |
} |
| 89 |
|
| 90 |
var t gobTest |
| 91 |
t.Name = decl.Name.Name |
| 92 |
t.Location.URI = file.URI |
| 93 |
t.Location.Range, _ = file.NodeRange(decl) |
| 94 |
|
| 95 |
i, ok := b.fileIndex[t.Location.URI] |
| 96 |
if !ok { |
| 97 |
i = len(b.Files) |
| 98 |
b.Files = append(b.Files, gobFile{}) |
| 99 |
b.fileIndex[t.Location.URI] = i |
| 100 |
} |
| 101 |
|
| 102 |
b.Files[i].Tests = append(b.Files[i].Tests, t) |
| 103 |
b.visited[obj] = true |
| 104 |
|
| 105 |
|
| 106 |
if isTest { |
| 107 |
b.Files[i].Tests = append(b.Files[i].Tests, b.findSubtests(t, decl.Type, decl.Body, file, files, info)...) |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
return &Index{pkg: b.gobPackage} |
| 113 |
} |
| 114 |
|
| 115 |
func (b *indexBuilder) findSubtests(parent gobTest, typ *ast.FuncType, body *ast.BlockStmt, file *parsego.File, files []*parsego.File, info *types.Info) []gobTest { |
| 116 |
if body == nil { |
| 117 |
return nil |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
if len(typ.Params.List[0].Names) == 0 { |
| 123 |
return nil |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
param := info.ObjectOf(typ.Params.List[0].Names[0]) |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
var tests []gobTest |
| 133 |
for _, stmt := range body.List { |
| 134 |
expr, ok := stmt.(*ast.ExprStmt) |
| 135 |
if !ok { |
| 136 |
continue |
| 137 |
} |
| 138 |
|
| 139 |
call, ok := expr.X.(*ast.CallExpr) |
| 140 |
if !ok || len(call.Args) != 2 { |
| 141 |
continue |
| 142 |
} |
| 143 |
fun, ok := call.Fun.(*ast.SelectorExpr) |
| 144 |
if !ok || fun.Sel.Name != "Run" { |
| 145 |
continue |
| 146 |
} |
| 147 |
recv, ok := fun.X.(*ast.Ident) |
| 148 |
if !ok || info.ObjectOf(recv) != param { |
| 149 |
continue |
| 150 |
} |
| 151 |
|
| 152 |
sig, ok := info.TypeOf(call.Args[1]).(*types.Signature) |
| 153 |
if !ok { |
| 154 |
continue |
| 155 |
} |
| 156 |
if _, ok := testKind(sig); !ok { |
| 157 |
continue |
| 158 |
} |
| 159 |
|
| 160 |
val := info.Types[call.Args[0]].Value |
| 161 |
if val == nil || val.Kind() != constant.String { |
| 162 |
continue |
| 163 |
} |
| 164 |
|
| 165 |
var t gobTest |
| 166 |
t.Name = b.uniqueName(parent.Name, rewrite(constant.StringVal(val))) |
| 167 |
t.Location.URI = file.URI |
| 168 |
t.Location.Range, _ = file.NodeRange(call) |
| 169 |
tests = append(tests, t) |
| 170 |
|
| 171 |
fn, typ, body := findFunc(files, info, body, call.Args[1]) |
| 172 |
if typ == nil { |
| 173 |
continue |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
if fn == nil { |
| 178 |
tests = append(tests, b.findSubtests(t, typ, body, file, files, info)...) |
| 179 |
continue |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
if isTest, _ := isTestOrExample(fn); isTest { |
| 184 |
continue |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
if b.visited[fn] { |
| 189 |
continue |
| 190 |
} |
| 191 |
|
| 192 |
b.visited[fn] = true |
| 193 |
tests = append(tests, b.findSubtests(t, typ, body, file, files, info)...) |
| 194 |
} |
| 195 |
return tests |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
func findFunc(files []*parsego.File, info *types.Info, body *ast.BlockStmt, expr ast.Expr) (*types.Func, *ast.FuncType, *ast.BlockStmt) { |
| 204 |
var obj types.Object |
| 205 |
switch arg := expr.(type) { |
| 206 |
case *ast.FuncLit: |
| 207 |
return nil, arg.Type, arg.Body |
| 208 |
|
| 209 |
case *ast.Ident: |
| 210 |
obj = info.ObjectOf(arg) |
| 211 |
if obj == nil { |
| 212 |
return nil, nil, nil |
| 213 |
} |
| 214 |
|
| 215 |
case *ast.SelectorExpr: |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
sel, ok := info.Selections[arg] |
| 221 |
if !ok { |
| 222 |
return nil, nil, nil |
| 223 |
} |
| 224 |
obj = sel.Obj() |
| 225 |
|
| 226 |
default: |
| 227 |
return nil, nil, nil |
| 228 |
} |
| 229 |
|
| 230 |
if v, ok := obj.(*types.Var); ok { |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
_, _ = v, body |
| 235 |
return nil, nil, nil |
| 236 |
} |
| 237 |
|
| 238 |
for _, file := range files { |
| 239 |
|
| 240 |
|
| 241 |
if _, err := safetoken.Offset(file.Tok, obj.Pos()); err != nil { |
| 242 |
continue |
| 243 |
} |
| 244 |
|
| 245 |
for _, decl := range file.File.Decls { |
| 246 |
decl, ok := decl.(*ast.FuncDecl) |
| 247 |
if !ok { |
| 248 |
continue |
| 249 |
} |
| 250 |
|
| 251 |
if info.ObjectOf(decl.Name) == obj { |
| 252 |
return obj.(*types.Func), decl.Type, decl.Body |
| 253 |
} |
| 254 |
} |
| 255 |
} |
| 256 |
return nil, nil, nil |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
func isTestOrExample(fn *types.Func) (isTest, isExample bool) { |
| 263 |
sig := fn.Type().(*types.Signature) |
| 264 |
if sig.Params().Len() == 0 && |
| 265 |
sig.Results().Len() == 0 { |
| 266 |
return false, isTestName(fn.Name(), "Example") |
| 267 |
} |
| 268 |
|
| 269 |
kind, ok := testKind(sig) |
| 270 |
if !ok { |
| 271 |
return false, false |
| 272 |
} |
| 273 |
switch kind.Name() { |
| 274 |
case "T": |
| 275 |
return isTestName(fn.Name(), "Test"), false |
| 276 |
case "B": |
| 277 |
return isTestName(fn.Name(), "Benchmark"), false |
| 278 |
case "F": |
| 279 |
return isTestName(fn.Name(), "Fuzz"), false |
| 280 |
default: |
| 281 |
return false, false |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
func isTestName(name, prefix string) bool { |
| 290 |
suffix, ok := strings.CutPrefix(name, prefix) |
| 291 |
if !ok { |
| 292 |
return false |
| 293 |
} |
| 294 |
if len(suffix) == 0 { |
| 295 |
|
| 296 |
return true |
| 297 |
} |
| 298 |
r, _ := utf8.DecodeRuneInString(suffix) |
| 299 |
return !unicode.IsLower(r) |
| 300 |
} |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
func testKind(sig *types.Signature) (*types.TypeName, bool) { |
| 305 |
if sig.Params().Len() != 1 || |
| 306 |
sig.Results().Len() != 0 { |
| 307 |
return nil, false |
| 308 |
} |
| 309 |
|
| 310 |
ptr, ok := sig.Params().At(0).Type().(*types.Pointer) |
| 311 |
if !ok { |
| 312 |
return nil, false |
| 313 |
} |
| 314 |
|
| 315 |
named, ok := ptr.Elem().(*types.Named) |
| 316 |
if !ok || named.Obj().Pkg() == nil || named.Obj().Pkg().Path() != "testing" { |
| 317 |
return nil, false |
| 318 |
} |
| 319 |
|
| 320 |
switch named.Obj().Name() { |
| 321 |
case "T", "B", "F": |
| 322 |
return named.Obj(), true |
| 323 |
} |
| 324 |
return nil, false |
| 325 |
} |
| 326 |
|
| 327 |
|
| 328 |
type indexBuilder struct { |
| 329 |
gobPackage |
| 330 |
fileIndex map[protocol.DocumentURI]int |
| 331 |
subNames map[string]int |
| 332 |
visited map[*types.Func]bool |
| 333 |
} |
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
var packageCodec = frob.CodecFor[gobPackage]() |
| 339 |
|
| 340 |
|
| 341 |
type gobPackage struct { |
| 342 |
Files []gobFile |
| 343 |
} |
| 344 |
|
| 345 |
type gobFile struct { |
| 346 |
Tests []gobTest |
| 347 |
} |
| 348 |
|
| 349 |
|
| 350 |
type gobTest struct { |
| 351 |
Location protocol.Location |
| 352 |
Name string |
| 353 |
} |
| 354 |
|
| 355 |
func (t *gobTest) result() Result { |
| 356 |
return Result(*t) |
| 357 |
} |