this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

listests: clean a little

+29 -27
+29 -27
listests/main.go
··· 256 256 257 257 inspect := inspector.New(testFiles) 258 258 259 - tests := func() []*TestInfo { 260 - finder := newTestFinder(pkg.Fset, packageName, directory, logger) 261 - return finder.find(inspect) 262 - }() 263 - allTests = append(allTests, tests...) 259 + finder := newTestFinder(pkg.Fset, packageName, directory, logger) 260 + allTests = append(allTests, finder.find(inspect)...) 264 261 } 265 262 266 263 return allTests, nil ··· 278 275 directory string 279 276 logger func(string, ...any) 280 277 281 - allTests []*TestInfo 282 278 testMap map[ast.Node]*TestInfo 283 279 assignments map[string]ast.Node 284 280 } ··· 289 285 pkgName: pkgName, 290 286 directory: dir, 291 287 logger: logger, 292 - allTests: []*TestInfo{}, 293 288 testMap: make(map[ast.Node]*TestInfo), 294 289 assignments: make(map[string]ast.Node), 295 290 } 296 291 } 297 292 298 293 func (tf *testFinder) find(inspect *inspector.Inspector) []*TestInfo { 294 + var allTests []*TestInfo 295 + 299 296 nodeFilter := []ast.Node{ 300 297 (*ast.FuncDecl)(nil), 301 298 (*ast.CallExpr)(nil), ··· 310 307 311 308 switch n := node.(type) { 312 309 case *ast.FuncDecl: 313 - tf.handleFuncDecl(n) 310 + if test := tf.handleFuncDecl(n); test != nil { 311 + allTests = append(allTests, test) 312 + } 314 313 case *ast.CallExpr: 315 314 tf.handleCallExpr(n, stack) 316 315 case *ast.AssignStmt: ··· 322 321 return true 323 322 }) 324 323 325 - return tf.allTests 324 + return allTests 326 325 } 327 326 328 327 func (tf *testFinder) handleAssignStmt(n *ast.AssignStmt) { ··· 340 339 tf.assignments[ident.Name] = n.X 341 340 } 342 341 } 342 + 343 343 if n.Value != nil { 344 344 if ident, ok := n.Value.(*ast.Ident); ok { 345 345 tf.assignments[ident.Name] = n.X ··· 347 347 } 348 348 } 349 349 350 - func (tf *testFinder) handleFuncDecl(n *ast.FuncDecl) { 350 + func (tf *testFinder) handleFuncDecl(n *ast.FuncDecl) *TestInfo { 351 351 // New function, clear assignments. 352 352 // TODO: If subtest is using a variable from an outer scope, information is 353 353 // lost. 354 354 tf.assignments = make(map[string]ast.Node) 355 355 356 356 if n.Name == nil || !strings.HasPrefix(n.Name.Name, "Test") || !isTestFunction(n) { 357 - return 357 + return nil 358 358 } 359 359 360 360 filename := tf.fset.Position(n.Pos()).Filename ··· 385 385 } 386 386 387 387 tf.testMap[n] = test 388 - tf.allTests = append(tf.allTests, test) 388 + return test 389 389 } 390 390 391 391 func (tf *testFinder) handleCallExpr(n *ast.CallExpr, stack []ast.Node) { ··· 398 398 return 399 399 } 400 400 401 - subTest := tf.createSubTest(n, parentTest) 402 - if subTest == nil { 401 + subTests := tf.createSubTests(n, parentTest) 402 + if len(subTests) == 0 { 403 403 return 404 404 } 405 405 406 - parentTest.SubTests = append(parentTest.SubTests, subTest) 406 + parentTest.SubTests = append(parentTest.SubTests, subTests...) 407 407 408 - // Map the function literal body to this subtest so nested t.Run calls can 409 - // find it. 408 + // Map the function literal body to the last subtest so nested t.Run calls can 409 + // find it. For table tests, all subtests share the same t.Run call. 410 410 if funcLit, ok := n.Args[1].(*ast.FuncLit); ok && funcLit.Body != nil { 411 - tf.testMap[funcLit.Body] = subTest 411 + tf.testMap[funcLit.Body] = subTests[len(subTests)-1] 412 412 } 413 413 } 414 414 ··· 426 426 return nil 427 427 } 428 428 429 - func (tf *testFinder) createSubTest(n *ast.CallExpr, parentTest *TestInfo) *TestInfo { 429 + func (tf *testFinder) createSubTests(n *ast.CallExpr, parentTest *TestInfo) []*TestInfo { 430 430 filename := tf.fset.Position(n.Pos()).Filename 431 431 start := tf.fset.Position(n.Pos()) 432 432 end := tf.fset.Position(n.End()) ··· 435 435 case *ast.BasicLit: 436 436 if arg.Kind == token.STRING { 437 437 subtestName := strings.Trim(arg.Value, "\"'`") 438 - return tf.createNamedSubTest(subtestName, parentTest, filename, start, end) 438 + subTest := tf.createNamedSubTest(subtestName, parentTest, filename, start, end) 439 + return []*TestInfo{subTest} 439 440 } 440 441 case *ast.SelectorExpr: 441 442 if tableTests := tf.extractTableTestNames(arg); len(tableTests) > 0 { 443 + var subTests []*TestInfo 442 444 for _, tt := range tableTests { 443 445 subTest := tf.createNamedSubTest(tt.name, parentTest, filename, tt.start, tt.end) 444 - parentTest.SubTests = append(parentTest.SubTests, subTest) 446 + subTests = append(subTests, subTest) 445 447 } 446 - return nil 448 + return subTests 447 449 } 448 - return tf.createGeneratedSubTest(arg, parentTest, filename, start, end) 450 + subTest := tf.createGeneratedSubTest(arg, parentTest, filename, start, end) 451 + return []*TestInfo{subTest} 449 452 default: 450 - return tf.createGeneratedSubTest(arg, parentTest, filename, start, end) 453 + subTest := tf.createGeneratedSubTest(arg, parentTest, filename, start, end) 454 + return []*TestInfo{subTest} 451 455 } 452 456 return nil 453 457 } ··· 466 470 return nil 467 471 } 468 472 469 - var sliceExpr ast.Node 473 + sliceExpr := rangeExpr 470 474 if ident, ok := rangeExpr.(*ast.Ident); ok { 471 475 // Ranging over a variable, e.g. `for _, c := range cases`. 472 476 sliceExpr, ok = tf.assignments[ident.Name] 473 477 if !ok { 474 478 return nil 475 479 } 476 - } else { 477 - sliceExpr = rangeExpr 478 480 } 479 481 480 482 compLit, ok := sliceExpr.(*ast.CompositeLit)