testFuncNames scans a Go source file and returns the names of all top-level test functions (Test*, Benchmark*, Example*, Fuzz*).
(path string)
| 232 | // testFuncNames scans a Go source file and returns the names of all top-level |
| 233 | // test functions (Test*, Benchmark*, Example*, Fuzz*). |
| 234 | func testFuncNames(path string) ([]string, error) { |
| 235 | f, err := os.Open(path) |
| 236 | if err != nil { |
| 237 | return nil, err |
| 238 | } |
| 239 | defer f.Close() |
| 240 | var names []string |
| 241 | sc := bufio.NewScanner(f) |
| 242 | for sc.Scan() { |
| 243 | rest, ok := strings.CutPrefix(sc.Text(), "func ") |
| 244 | if !ok { |
| 245 | continue |
| 246 | } |
| 247 | for _, prefix := range []string{"Test", "Benchmark", "Example", "Fuzz"} { |
| 248 | if strings.HasPrefix(rest, prefix) { |
| 249 | if i := strings.IndexByte(rest, '('); i > 0 { |
| 250 | names = append(names, rest[:i]) |
| 251 | } |
| 252 | break |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 | return names, sc.Err() |
| 257 | } |
| 258 | |
| 259 | // runTests runs the tests in pt and sends the results on ch. It sends a |
| 260 | // testAttempt for each test and a final testAttempt per pkg with pkgFinished |