From 7e966bdd9dae1709c2381d547c06aa53a97fa3ad Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Wed, 7 Oct 2026 11:13:47 -0700 Subject: [PATCH] Try to filter go compiler test files before reading them and building compiler tests --- tsc/internal/testrunner/compiler_runner.go | 5 + .../testrunner/compiler_test_filter.go | 92 +++++++++++++++++++ .../testrunner/compiler_test_filter_test.go | 52 +++++++++++ 3 files changed, 149 insertions(+) create mode 100644 tsc/internal/testrunner/compiler_test_filter.go create mode 100644 tsc/internal/testrunner/compiler_test_filter_test.go diff --git a/tsc/internal/testrunner/compiler_runner.go b/tsc/internal/testrunner/compiler_runner.go index 2b8b4b28e6b66..6f2712632b5b8 100644 --- a/tsc/internal/testrunner/compiler_runner.go +++ b/tsc/internal/testrunner/compiler_runner.go @@ -1,6 +1,7 @@ package testrunner import ( + "flag" "fmt" "math/rand/v2" "os" @@ -126,11 +127,15 @@ var skippedTests = []string{ func (r *CompilerBaselineRunner) RunTests(t *testing.T) { r.cleanUpLocal(t) files := r.EnumerateTestFiles() + filter := compilerTestFileFilter(flag.Lookup("test.run").Value.String()) for _, filename := range files { if slices.Contains(skippedTests, tspath.GetBaseFileName(filename)) { continue } + if !filter(tspath.GetBaseFileName(filename)) { + continue + } r.runTest(t, filename) } } diff --git a/tsc/internal/testrunner/compiler_test_filter.go b/tsc/internal/testrunner/compiler_test_filter.go new file mode 100644 index 0000000000000..6a8b21a931f30 --- /dev/null +++ b/tsc/internal/testrunner/compiler_test_filter.go @@ -0,0 +1,92 @@ +package testrunner + +import ( + "regexp" + "strconv" + "strings" + "unicode" +) + +func compilerTestFileFilter(pattern string) func(string) bool { + acceptAll := func(string) bool { return true } + if compilerTestNameNeedsRewrite(pattern) { + return acceptAll + } + parts := splitCompilerTestFilter(pattern) + if len(parts) < 2 { + return acceptAll + } + component := parts[1] + anchored := strings.HasPrefix(component, "^") + expression, err := regexp.Compile(strings.TrimPrefix(component, "^")) + if err != nil { + return acceptAll + } + prefix, _ := expression.LiteralPrefix() + if prefix == "" || compilerTestNameNeedsRewrite(prefix) || (!anchored && !strings.ContainsFunc(prefix, unicode.IsUpper)) { + return acceptAll + } + return func(filename string) bool { + if compilerTestNameNeedsRewrite(filename) { + return true + } + if anchored { + return strings.HasPrefix(filename, prefix) || strings.HasPrefix(prefix, filename+"_") + } + if strings.Contains(filename, prefix) { + return true + } + for index := range filename { + if strings.HasPrefix(prefix, filename[index:]+"_") { + return true + } + } + return false + } +} + +func compilerTestNameNeedsRewrite(name string) bool { + return strings.ContainsFunc(name, func(character rune) bool { + return unicode.IsSpace(character) || !strconv.IsPrint(character) + }) +} + +func splitCompilerTestFilter(pattern string) []string { + var parts []string + start, depth := 0, 0 + inClass, escaped := false, false + for index, character := range pattern { + if escaped { + escaped = false + continue + } + if character == '\\' { + escaped = true + continue + } + if inClass { + if character == ']' { + inClass = false + } + continue + } + switch character { + case '[': + inClass = true + case '(': + depth++ + case ')': + depth-- + case '|': + if depth == 0 { + return nil + } + case '/': + if depth == 0 { + parts = append(parts, pattern[start:index]) + start = index + 1 + } + } + } + return append(parts, pattern[start:]) +} diff --git a/tsc/internal/testrunner/compiler_test_filter_test.go b/tsc/internal/testrunner/compiler_test_filter_test.go new file mode 100644 index 0000000000000..8b8588c8c5367 --- /dev/null +++ b/tsc/internal/testrunner/compiler_test_filter_test.go @@ -0,0 +1,52 @@ +package testrunner + +import ( + "testing" + + "gotest.tools/v3/assert" +) + +func TestCompilerTestFileFilter(t *testing.T) { + t.Parallel() + tests := []struct { + name string + pattern string + filename string + included bool + }{ + {"unfiltered", "", "other.ts", true}, + {"parentOnly", "TestLocal", "other.ts", true}, + {"filenamePrefix", "TestLocal/flakyDiagnostic1038", "flakyDiagnostic1038.ts", true}, + {"differentFilename", "TestLocal/flakyDiagnostic1038", "other.ts", false}, + {"substring", "TestLocal/Diagnostic1038", "flakyDiagnostic1038.ts", true}, + {"anchoredPrefix", "TestLocal/^lowercase", "lowercase.ts", true}, + {"anchoredMismatch", "TestLocal/^lowercase", "other.ts", false}, + {"unanchoredLowercaseFallback", "TestLocal/target", "other.ts", true}, + {"configuration", `TestLocal/^Example\.ts_target=es2020$`, "Example.ts", true}, + {"configurationSubstring", `TestLocal/Example\.ts_target=es2020$`, "someExample.ts", true}, + {"differentConfigurationFile", `TestLocal/^Example\.ts_target=es2020$`, "other.ts", false}, + {"spaceConfigurationFallback", `TestLocal/Example\.ts target=es2020/error`, "Example.ts", true}, + {"anchoredSpaceConfigurationFallback", `TestLocal/^Example\.ts target=es2020$`, "Example.ts", true}, + {"tabConfigurationFallback", "TestLocal/Example\\.ts\ttarget=es2020/error", "Example.ts", true}, + {"unicodeSpaceFallback", "TestLocal/^Example\u00a0Name", "Example_Name.ts", true}, + {"nonPrintablePatternFallback", "TestLocal/^Example\x00Name", "other.ts", true}, + {"escapedWhitespacePrefixFallback", `TestLocal/^Example\x20Name`, "other.ts", true}, + {"whitespaceFilenameFallback", "TestLocal/^Example_Name", "Example Name.ts", true}, + {"nonPrintableFilenameFallback", `TestLocal/^Example\\x00Name`, "Example\x00Name.ts", true}, + {"nestedSubtest", "TestLocal/flakyDiagnostic1038/error", "flakyDiagnostic1038.ts", true}, + {"caseInsensitiveFallback", "TestLocal/(?i)Example", "other.ts", true}, + {"alternationFallback", "TestLocal/Example|TestLocal/Other", "Other.ts", true}, + {"groupFallback", "TestLocal/(Example|Other)", "Other.ts", true}, + {"groupedSlash", "TestLocal/(Example/Other)", "other.ts", false}, + {"classSlashFallback", "TestLocal/[A/B]", "other.ts", true}, + {"escapedSlash", `TestLocal/Example\/Other`, "other.ts", false}, + {"invalidExpressionFallback", "TestLocal/(", "other.ts", true}, + {"anchoredParent", "^TestLocal$/Example", "Example.ts", true}, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + t.Parallel() + assert.Equal(t, compilerTestFileFilter(test.pattern)(test.filename), test.included) + }) + } +}