| 466 | const TEST_PATHS_KEEP = 100_000; |
| 467 | |
| 468 | function readsAsTestPath(filePath: string): boolean { |
| 469 | const lower = filePath.toLowerCase(); |
| 470 | const fileName = path.basename(filePath); // original case — needed for camelCase boundaries |
| 471 | const lowerName = fileName.toLowerCase(); |
| 472 | |
| 473 | // --- Filename patterns --- |
| 474 | if ( |
| 475 | lowerName.startsWith('test_') || // python: test_foo.py |
| 476 | lowerName.startsWith('test.') || |
| 477 | // separator-delimited: foo_test.go, foo.test.ts, foo-spec.rb, bar_spec.py, |
| 478 | // and Google's C++ foo_unittest.cc (protobuf, Chromium). A bare |
| 479 | // `unittest.go` is no test: promtool's is the code that runs rule tests. |
| 480 | /[._-](test|tests|unittest|unittests|spec|specs)\.[a-z0-9]+$/.test(lowerName) || |
| 481 | // CamelCase suffix (Java/Kotlin/Swift/C#/Scala): FooTest.kt, BarTests.swift, |
| 482 | // BazSpec.scala, QuxTestCase.java. Capital-led so "latest.kt"/"manifest.kt" |
| 483 | // (lowercase "test") are NOT matched. |
| 484 | /(?:Test|Tests|TestCase|Tester|Spec|Specs)\.[A-Za-z0-9]+$/.test(fileName) |
| 485 | ) { |
| 486 | return true; |
| 487 | } |
| 488 | |
| 489 | // --- Directory patterns --- |
| 490 | if ( |
| 491 | lower.includes('/tests/') || lower.includes('/test/') || |
| 492 | lower.includes('/__tests__/') || lower.includes('/spec/') || |
| 493 | lower.includes('/specs/') || lower.includes('/testlib/') || |
| 494 | lower.includes('/testing/') || lower.includes('/e2e/') || |
| 495 | lower.startsWith('test/') || lower.startsWith('tests/') || |
| 496 | lower.startsWith('spec/') || lower.startsWith('specs/') || |
| 497 | lower.startsWith('e2e/') || |
| 498 | // CamelCase test source-set dirs (Kotlin Multiplatform / Gradle / Xcode): |
| 499 | // jvmTest/, commonTest/, androidTest/, iosTest/, integrationTest/. Capital-led |
| 500 | // so "latest/" / "manifest/" are not matched. |
| 501 | /(?:^|\/)[A-Za-z0-9]*(?:Test|Tests|Spec)\//.test(filePath) || |
| 502 | // Test-support modules and doubles by directory name: Gradle's |
| 503 | // `core/data-test/`, `core/datastore-test/`, `:testing`; Go's `testdata/`; |
| 504 | // `testutil(s)/`, `test-utils/`, `fakes/`, `mocks/`, `__mocks__/`. |
| 505 | /(?:^|\/)(?:[\w.]+[-_]test(?:s|ing)?|testdata|testutils?|test[-_]utils?|fakes?|mocks?|__mocks__|stubs)\//.test(lower) || |
| 506 | // Unit-test trees: LLVM's and Breakpad's `unittests/`, glog's |
| 507 | // `dcheck_unittest/`. Not a bare `unittest/`: CPython's `Lib/unittest/` is |
| 508 | // the framework itself. |
| 509 | /(?:^|\/)(?:unittests|[\w.]+[-_]unittests?)\//.test(lower) |
| 510 | ) { |
| 511 | return true; |
| 512 | } |
| 513 | |
| 514 | return false; |
| 515 | } |
| 516 | |
| 517 | /** |
| 518 | * Check if a path is in a non-production directory (integration, sample, example, etc.) |