Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions tsc/internal/testrunner/compiler_runner.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package testrunner

import (
"flag"
"fmt"
"math/rand/v2"
"os"
Expand Down Expand Up @@ -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)
}
}
Expand Down
92 changes: 92 additions & 0 deletions tsc/internal/testrunner/compiler_test_filter.go
Original file line number Diff line number Diff line change
@@ -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:])
}
52 changes: 52 additions & 0 deletions tsc/internal/testrunner/compiler_test_filter_test.go
Original file line number Diff line number Diff line change
@@ -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)
})
}
}
Loading