remove unused packages

This commit is contained in:
Egor Aristov 2025-10-15 19:23:59 +03:00
parent eab0468d4d
commit 4ad04fae5b
Signed by: egor3f
GPG Key ID: 40482A264AAEC85F
5 changed files with 0 additions and 938 deletions

View File

@ -1,606 +0,0 @@
package testrunner
import (
"fmt"
"math/rand/v2"
"os"
"path/filepath"
"regexp"
"slices"
"strings"
"testing"
"efprojects.com/kitten-ipc/kitcom/internal/tsgo/ast"
"efprojects.com/kitten-ipc/kitcom/internal/tsgo/checker"
"efprojects.com/kitten-ipc/kitcom/internal/tsgo/core"
"efprojects.com/kitten-ipc/kitcom/internal/tsgo/repo"
"efprojects.com/kitten-ipc/kitcom/internal/tsgo/testutil"
"efprojects.com/kitten-ipc/kitcom/internal/tsgo/testutil/baseline"
"efprojects.com/kitten-ipc/kitcom/internal/tsgo/testutil/harnessutil"
"efprojects.com/kitten-ipc/kitcom/internal/tsgo/testutil/tsbaseline"
"efprojects.com/kitten-ipc/kitcom/internal/tsgo/tsoptions"
"efprojects.com/kitten-ipc/kitcom/internal/tsgo/tspath"
"efprojects.com/kitten-ipc/kitcom/internal/tsgo/vfs/osvfs"
"gotest.tools/v3/assert"
)
var (
compilerBaselineRegex = regexp.MustCompile(`\.tsx?$`)
requireStr = "require("
referencesRegex = regexp.MustCompile(`reference\spath`)
)
// Posix-style path to sources under test
var srcFolder = "/.src"
type CompilerTestType int
const (
TestTypeConformance CompilerTestType = iota
TestTypeRegression
)
func (t *CompilerTestType) String() string {
if *t == TestTypeRegression {
return "compiler"
}
return "conformance"
}
type CompilerBaselineRunner struct {
isSubmodule bool
testFiles []string
basePath string
testSuitName string
}
var _ Runner = (*CompilerBaselineRunner)(nil)
func NewCompilerBaselineRunner(testType CompilerTestType, isSubmodule bool) *CompilerBaselineRunner {
testSuitName := testType.String()
var basePath string
if isSubmodule {
basePath = "../_submodules/TypeScript/tests/cases/" + testSuitName
} else {
basePath = "tests/cases/" + testSuitName
}
return &CompilerBaselineRunner{
basePath: basePath,
testSuitName: testSuitName,
isSubmodule: isSubmodule,
}
}
func (r *CompilerBaselineRunner) EnumerateTestFiles() []string {
if len(r.testFiles) > 0 {
return r.testFiles
}
files, err := harnessutil.EnumerateFiles(r.basePath, compilerBaselineRegex, true /*recursive*/)
if err != nil {
panic("Could not read compiler test files: " + err.Error())
}
r.testFiles = files
return files
}
var skippedTests = []string{
// These tests contain options that have been completely removed, so fail to parse.
"preserveUnusedImports.ts",
"noCrashWithVerbatimModuleSyntaxAndImportsNotUsedAsValues.ts",
"verbatimModuleSyntaxCompat.ts",
"preserveValueImports_importsNotUsedAsValues.ts",
"importsNotUsedAsValues_error.ts",
"alwaysStrictNoImplicitUseStrict.ts",
"nonPrimitiveIndexingWithForInSupressError.ts",
"parameterInitializerBeforeDestructuringEmit.ts",
"mappedTypeUnionConstraintInferences.ts",
"lateBoundConstraintTypeChecksCorrectly.ts",
"keyofDoesntContainSymbols.ts",
"isolatedModulesOut.ts",
"noStrictGenericChecks.ts",
"noImplicitUseStrict_umd.ts",
"noImplicitUseStrict_system.ts",
"noImplicitUseStrict_es6.ts",
"noImplicitUseStrict_commonjs.ts",
"noImplicitUseStrict_amd.ts",
"noImplicitAnyIndexingSuppressed.ts",
"excessPropertyErrorsSuppressed.ts",
}
func (r *CompilerBaselineRunner) RunTests(t *testing.T) {
r.cleanUpLocal(t)
files := r.EnumerateTestFiles()
for _, filename := range files {
if slices.Contains(skippedTests, tspath.GetBaseFileName(filename)) {
continue
}
r.runTest(t, filename)
}
}
var localBasePath = filepath.Join(repo.TestDataPath, "baselines", "local")
func (r *CompilerBaselineRunner) cleanUpLocal(t *testing.T) {
localPath := filepath.Join(localBasePath, core.IfElse(r.isSubmodule, "diff", ""), r.testSuitName)
err := os.RemoveAll(localPath)
if err != nil {
panic("Could not clean up local compiler tests: " + err.Error())
}
}
// Set of compiler options for which we allow variations to be specified in the test file,
// for instance `// @strict: true, false`.
var compilerVaryBy map[string]struct{} = getCompilerVaryByMap()
func getCompilerVaryByMap() map[string]struct{} {
varyByOptions := append(
core.Map(core.Filter(tsoptions.OptionsDeclarations, func(option *tsoptions.CommandLineOption) bool {
return !option.IsCommandLineOnly &&
(option.Kind == tsoptions.CommandLineOptionTypeBoolean || option.Kind == tsoptions.CommandLineOptionTypeEnum) &&
(option.AffectsProgramStructure ||
option.AffectsEmit ||
option.AffectsModuleResolution ||
option.AffectsBindDiagnostics ||
option.AffectsSemanticDiagnostics ||
option.AffectsSourceFile ||
option.AffectsDeclarationPath ||
option.AffectsBuildInfo)
}), func(option *tsoptions.CommandLineOption) string {
return option.Name
}),
// explicit variations that do not match above conditions
"noEmit",
"isolatedModules")
varyByMap := make(map[string]struct{})
for _, option := range varyByOptions {
varyByMap[strings.ToLower(option)] = struct{}{}
}
return varyByMap
}
func (r *CompilerBaselineRunner) runTest(t *testing.T, filename string) {
test := getCompilerFileBasedTest(t, filename)
basename := tspath.GetBaseFileName(filename)
if len(test.configurations) > 0 {
for _, config := range test.configurations {
testName := basename
if config.Name != "" {
testName += " " + config.Name
}
t.Run(testName, func(t *testing.T) { r.runSingleConfigTest(t, testName, test, config) })
}
} else {
t.Run(basename, func(t *testing.T) { r.runSingleConfigTest(t, basename, test, nil) })
}
}
func (r *CompilerBaselineRunner) runSingleConfigTest(t *testing.T, testName string, test *compilerFileBasedTest, config *harnessutil.NamedTestConfiguration) {
t.Parallel()
defer testutil.RecoverAndFail(t, "Panic on compiling test "+test.filename)
payload := makeUnitsFromTest(test.content, test.filename)
compilerTest := newCompilerTest(t, testName, test.filename, &payload, config)
switch compilerTest.options.GetEmitModuleKind() {
case core.ModuleKindAMD, core.ModuleKindUMD, core.ModuleKindSystem:
t.Skipf("Skipping test %s with unsupported module kind %s", testName, compilerTest.options.GetEmitModuleKind())
}
compilerTest.verifyDiagnostics(t, r.testSuitName, r.isSubmodule)
compilerTest.verifyJavaScriptOutput(t, r.testSuitName, r.isSubmodule)
compilerTest.verifySourceMapOutput(t, r.testSuitName, r.isSubmodule)
compilerTest.verifySourceMapRecord(t, r.testSuitName, r.isSubmodule)
compilerTest.verifyTypesAndSymbols(t, r.testSuitName, r.isSubmodule)
compilerTest.verifyModuleResolution(t, r.testSuitName, r.isSubmodule)
// !!! Verify all baselines
compilerTest.verifyUnionOrdering(t)
compilerTest.verifyParentPointers(t)
}
type compilerFileBasedTest struct {
filename string
content string
configurations []*harnessutil.NamedTestConfiguration
}
func getCompilerFileBasedTest(t *testing.T, filename string) *compilerFileBasedTest {
content, ok := osvfs.FS().ReadFile(filename)
if !ok {
panic("Could not read test file: " + filename)
}
settings := extractCompilerSettings(content)
configurations := harnessutil.GetFileBasedTestConfigurations(t, settings, compilerVaryBy)
return &compilerFileBasedTest{
filename: filename,
content: content,
configurations: configurations,
}
}
type compilerTest struct {
testName string
filename string
basename string
configuredName string // name with configuration description, e.g. `file`
options *core.CompilerOptions
harnessOptions *harnessutil.HarnessOptions
result *harnessutil.CompilationResult
tsConfigFiles []*harnessutil.TestFile
toBeCompiled []*harnessutil.TestFile // equivalent to the files that will be passed on the command line
otherFiles []*harnessutil.TestFile // equivalent to other files on the file system not directly passed to the compiler (ie things that are referenced by other files)
hasNonDtsFiles bool
}
type testCaseContentWithConfig struct {
testCaseContent
configuration harnessutil.TestConfiguration
}
func newCompilerTest(
t *testing.T,
testName string,
filename string,
testContent *testCaseContent,
namedConfiguration *harnessutil.NamedTestConfiguration,
) *compilerTest {
basename := tspath.GetBaseFileName(filename)
configuredName := basename
if namedConfiguration != nil && namedConfiguration.Name != "" {
extname := tspath.GetAnyExtensionFromPath(basename, nil, false)
extensionlessBasename := basename[:len(basename)-len(extname)]
configuredName = fmt.Sprintf("%s(%s)%s", extensionlessBasename, namedConfiguration.Name, extname)
}
var configuration harnessutil.TestConfiguration
if namedConfiguration != nil {
configuration = namedConfiguration.Config
}
testCaseContentWithConfig := testCaseContentWithConfig{
testCaseContent: *testContent,
configuration: configuration,
}
harnessConfig := testCaseContentWithConfig.configuration
currentDirectory := tspath.GetNormalizedAbsolutePath(harnessConfig["currentdirectory"], srcFolder)
units := testCaseContentWithConfig.testUnitData
var toBeCompiled []*harnessutil.TestFile
var otherFiles []*harnessutil.TestFile
var tsConfig *tsoptions.ParsedCommandLine
hasNonDtsFiles := core.Some(
units,
func(unit *testUnit) bool { return !tspath.FileExtensionIs(unit.name, tspath.ExtensionDts) })
var tsConfigFiles []*harnessutil.TestFile
if testCaseContentWithConfig.tsConfig != nil {
tsConfig = testCaseContentWithConfig.tsConfig
tsConfigFiles = []*harnessutil.TestFile{
createHarnessTestFile(testCaseContentWithConfig.tsConfigFileUnitData, currentDirectory),
}
for _, unit := range units {
if slices.Contains(
tsConfig.ParsedConfig.FileNames,
tspath.GetNormalizedAbsolutePath(unit.name, currentDirectory),
) {
toBeCompiled = append(toBeCompiled, createHarnessTestFile(unit, currentDirectory))
} else {
otherFiles = append(otherFiles, createHarnessTestFile(unit, currentDirectory))
}
}
} else {
baseUrl, ok := harnessConfig["baseurl"]
if ok && !tspath.IsRootedDiskPath(baseUrl) {
harnessConfig["baseurl"] = tspath.GetNormalizedAbsolutePath(baseUrl, currentDirectory)
}
lastUnit := units[len(units)-1]
// We need to assemble the list of input files for the compiler and other related files on the 'filesystem' (ie in a multi-file test)
// If the last file in a test uses require or a triple slash reference we'll assume all other files will be brought in via references,
// otherwise, assume all files are just meant to be in the same compilation session without explicit references to one another.
if testCaseContentWithConfig.configuration["noimplicitreferences"] != "" ||
strings.Contains(lastUnit.content, requireStr) ||
referencesRegex.MatchString(lastUnit.content) {
toBeCompiled = append(toBeCompiled, createHarnessTestFile(lastUnit, currentDirectory))
for _, unit := range units[:len(units)-1] {
otherFiles = append(otherFiles, createHarnessTestFile(unit, currentDirectory))
}
} else {
toBeCompiled = core.Map(units, func(unit *testUnit) *harnessutil.TestFile { return createHarnessTestFile(unit, currentDirectory) })
}
}
result := harnessutil.CompileFiles(
t,
toBeCompiled,
otherFiles,
harnessConfig,
tsConfig,
currentDirectory,
testCaseContentWithConfig.symlinks,
)
return &compilerTest{
testName: testName,
filename: filename,
basename: basename,
configuredName: configuredName,
options: result.Options,
harnessOptions: result.HarnessOptions,
result: result,
tsConfigFiles: tsConfigFiles,
toBeCompiled: toBeCompiled,
otherFiles: otherFiles,
hasNonDtsFiles: hasNonDtsFiles,
}
}
var concurrentSkippedErrorBaselines = map[string]string{
"circular1.ts": "Circular error reported in an extra position.",
"circular3.ts": "Circular error reported in an extra position.",
"recursiveExportAssignmentAndFindAliasedType1.ts": "Circular error reported in an extra position.",
"recursiveExportAssignmentAndFindAliasedType2.ts": "Circular error reported in an extra position.",
"recursiveExportAssignmentAndFindAliasedType3.ts": "Circular error reported in an extra position.",
"typeOnlyMerge2.ts": "Type-only merging is not detected when files are checked on different checkers.",
"typeOnlyMerge3.ts": "Type-only merging is not detected when files are checked on different checkers.",
}
func (c *compilerTest) verifyDiagnostics(t *testing.T, suiteName string, isSubmodule bool) {
t.Run("error", func(t *testing.T) {
if !testutil.TestProgramIsSingleThreaded() {
if msg, ok := concurrentSkippedErrorBaselines[c.basename]; ok {
t.Skipf("Skipping in concurrent mode: %s", msg)
}
}
defer testutil.RecoverAndFail(t, "Panic on creating error baseline for test "+c.filename)
files := core.Concatenate(c.tsConfigFiles, core.Concatenate(c.toBeCompiled, c.otherFiles))
tsbaseline.DoErrorBaseline(t, c.configuredName, files, c.result.Diagnostics, c.result.Options.Pretty.IsTrue(), baseline.Options{
Subfolder: suiteName,
IsSubmodule: isSubmodule,
IsSubmoduleAccepted: c.containsUnsupportedOptionsForDiagnostics(),
DiffFixupOld: func(old string) string {
var sb strings.Builder
sb.Grow(len(old))
for line := range strings.SplitSeq(old, "\n") {
const (
relativePrefixNew = "==== "
relativePrefixOld = relativePrefixNew + "./"
)
if rest, ok := strings.CutPrefix(line, relativePrefixOld); ok {
line = relativePrefixNew + rest
}
sb.WriteString(line)
sb.WriteString("\n")
}
return sb.String()[:sb.Len()-1]
},
})
})
}
var skippedEmitTests = map[string]string{
"filesEmittingIntoSameOutput.ts": "Output order nondeterministic due to collision on filename during parallel emit.",
"jsFileCompilationWithJsEmitPathSameAsInput.ts": "Output order nondeterministic due to collision on filename during parallel emit.",
"grammarErrors.ts": "Output order nondeterministic due to collision on filename during parallel emit.",
"jsFileCompilationEmitBlockedCorrectly.ts": "Output order nondeterministic due to collision on filename during parallel emit.",
"jsDeclarationsReexportAliasesEsModuleInterop.ts": "cls.d.ts is missing statements when run concurrently.",
"jsFileCompilationWithoutJsExtensions.ts": "No files are emitted.",
"typeOnlyMerge2.ts": "Nondeterministic contents when run concurrently.",
"typeOnlyMerge3.ts": "Nondeterministic contents when run concurrently.",
}
func (c *compilerTest) verifyJavaScriptOutput(t *testing.T, suiteName string, isSubmodule bool) {
if !c.hasNonDtsFiles {
return
}
if c.options.OutFile != "" {
// Just return, no t.Skip; this is unsupported so testing them is not helpful.
return
}
t.Run("output", func(t *testing.T) {
if msg, ok := skippedEmitTests[c.basename]; ok {
t.Skip(msg)
}
defer testutil.RecoverAndFail(t, "Panic on creating js output for test "+c.filename)
headerComponents := tspath.GetPathComponentsRelativeTo(repo.TestDataPath, c.filename, tspath.ComparePathsOptions{})
if isSubmodule {
headerComponents = headerComponents[4:] // Strip "./../_submodules/TypeScript" prefix
}
header := tspath.GetPathFromPathComponents(headerComponents)
tsbaseline.DoJSEmitBaseline(
t,
c.configuredName,
header,
c.options,
c.result,
c.tsConfigFiles,
c.toBeCompiled,
c.otherFiles,
c.harnessOptions,
baseline.Options{Subfolder: suiteName, IsSubmodule: isSubmodule},
)
})
}
func (c *compilerTest) verifySourceMapOutput(t *testing.T, suiteName string, isSubmodule bool) {
if c.options.OutFile != "" {
// Just return, no t.Skip; this is unsupported so testing them is not helpful.
return
}
t.Run("sourcemap", func(t *testing.T) {
defer testutil.RecoverAndFail(t, "Panic on creating source map output for test "+c.filename)
headerComponents := tspath.GetPathComponentsRelativeTo(repo.TestDataPath, c.filename, tspath.ComparePathsOptions{})
if isSubmodule {
headerComponents = headerComponents[4:] // Strip "./../_submodules/TypeScript" prefix
}
header := tspath.GetPathFromPathComponents(headerComponents)
tsbaseline.DoSourcemapBaseline(
t,
c.configuredName,
header,
c.options,
c.result,
c.harnessOptions,
baseline.Options{Subfolder: suiteName, IsSubmodule: isSubmodule},
)
})
}
func (c *compilerTest) verifySourceMapRecord(t *testing.T, suiteName string, isSubmodule bool) {
if c.options.OutFile != "" {
// Just return, no t.Skip; this is unsupported so testing them is not helpful.
return
}
t.Run("sourcemap record", func(t *testing.T) {
defer testutil.RecoverAndFail(t, "Panic on creating source map record for test "+c.filename)
headerComponents := tspath.GetPathComponentsRelativeTo(repo.TestDataPath, c.filename, tspath.ComparePathsOptions{})
if isSubmodule {
headerComponents = headerComponents[4:] // Strip "./../_submodules/TypeScript" prefix
}
header := tspath.GetPathFromPathComponents(headerComponents)
tsbaseline.DoSourcemapRecordBaseline(
t,
c.configuredName,
header,
c.options,
c.result,
c.harnessOptions,
baseline.Options{Subfolder: suiteName, IsSubmodule: isSubmodule},
)
})
}
func (c *compilerTest) verifyTypesAndSymbols(t *testing.T, suiteName string, isSubmodule bool) {
noTypesAndSymbols := c.harnessOptions.NoTypesAndSymbols
if noTypesAndSymbols {
return
}
program := c.result.Program
allFiles := core.Filter(
core.Concatenate(c.toBeCompiled, c.otherFiles),
func(f *harnessutil.TestFile) bool {
return program.GetSourceFile(f.UnitName) != nil
},
)
headerComponents := tspath.GetPathComponentsRelativeTo(repo.TestDataPath, c.filename, tspath.ComparePathsOptions{})
if isSubmodule {
headerComponents = headerComponents[4:] // Strip "./../_submodules/TypeScript" prefix
}
header := tspath.GetPathFromPathComponents(headerComponents)
tsbaseline.DoTypeAndSymbolBaseline(
t,
c.configuredName,
header,
program,
allFiles,
baseline.Options{Subfolder: suiteName, IsSubmodule: isSubmodule},
false,
false,
len(c.result.Diagnostics) > 0,
)
}
func (c *compilerTest) verifyModuleResolution(t *testing.T, suiteName string, isSubmodule bool) {
if !c.options.TraceResolution.IsTrue() {
return
}
t.Run("module resolution", func(t *testing.T) {
defer testutil.RecoverAndFail(t, "Panic on creating module resolution baseline for test "+c.filename)
tsbaseline.DoModuleResolutionBaseline(t, c.configuredName, c.result.Trace, baseline.Options{
Subfolder: suiteName,
IsSubmodule: isSubmodule,
SkipDiffWithOld: true,
})
})
}
func createHarnessTestFile(unit *testUnit, currentDirectory string) *harnessutil.TestFile {
return &harnessutil.TestFile{
UnitName: tspath.GetNormalizedAbsolutePath(unit.name, currentDirectory),
Content: unit.content,
}
}
func (c *compilerTest) verifyUnionOrdering(t *testing.T) {
t.Run("union ordering", func(t *testing.T) {
checkers, done := c.result.Program.GetTypeCheckers(t.Context())
defer done()
for _, c := range checkers {
for union := range c.UnionTypes() {
types := union.Types()
reversed := slices.Clone(types)
slices.Reverse(reversed)
slices.SortFunc(reversed, checker.CompareTypes)
assert.Assert(t, slices.Equal(reversed, types), "compareTypes does not sort union types consistently")
shuffled := slices.Clone(types)
rng := rand.New(rand.NewPCG(1234, 5678))
for range 10 {
rng.Shuffle(len(shuffled), func(i, j int) { shuffled[i], shuffled[j] = shuffled[j], shuffled[i] })
slices.SortFunc(shuffled, checker.CompareTypes)
assert.Assert(t, slices.Equal(shuffled, types), "compareTypes does not sort union types consistently")
}
}
}
})
}
func (c *compilerTest) verifyParentPointers(t *testing.T) {
t.Run("source file parent pointers", func(t *testing.T) {
var parent *ast.Node
var verifier func(n *ast.Node) bool
verifier = func(n *ast.Node) bool {
if n == nil {
return false
}
assert.Assert(t, n.Parent != nil, "parent node does not exist")
elab := ""
if !ast.NodeIsSynthesized(n) {
elab += ast.GetSourceFileOfNode(n).Text()[n.Loc.Pos():n.Loc.End()]
} else {
elab += "!synthetic! no text available"
}
assert.Assert(t, n.Parent == parent, "parent node does not match traversed parent: "+n.Kind.String()+": "+elab)
oldParent := parent
parent = n
n.ForEachChild(verifier)
parent = oldParent
return false
}
for _, f := range c.result.Program.GetSourceFiles() {
if c.result.Program.IsSourceFileDefaultLibrary(f.Path()) {
continue
}
parent = f.AsNode()
f.AsNode().ForEachChild(verifier)
}
})
}
func (c *compilerTest) containsUnsupportedOptionsForDiagnostics() bool {
if len(c.result.Program.UnsupportedExtensions()) != 0 {
return true
}
if c.options.BaseUrl != "" {
return true
}
if c.options.OutFile != "" {
return true
}
return false
}

View File

@ -1,50 +0,0 @@
package testrunner
import (
"testing"
"efprojects.com/kitten-ipc/kitcom/internal/tsgo/bundled"
"efprojects.com/kitten-ipc/kitcom/internal/tsgo/collections"
"efprojects.com/kitten-ipc/kitcom/internal/tsgo/repo"
"efprojects.com/kitten-ipc/kitcom/internal/tsgo/tspath"
"gotest.tools/v3/assert"
)
// Runs the new compiler tests and produces baselines (e.g. `test1.symbols`).
func TestLocal(t *testing.T) { runCompilerTests(t, false) } //nolint:paralleltest
// Runs the old compiler tests, and produces new baselines (e.g. `test1.symbols`)
// and a diff between the new and old baselines (e.g. `test1.symbols.diff`).
func TestSubmodule(t *testing.T) { runCompilerTests(t, true) } //nolint:paralleltest
func runCompilerTests(t *testing.T, isSubmodule bool) {
t.Parallel()
if isSubmodule {
repo.SkipIfNoTypeScriptSubmodule(t)
}
if !bundled.Embedded {
// Without embedding, we'd need to read all of the lib files out from disk into the MapFS.
// Just skip this for now.
t.Skip("bundled files are not embedded")
}
runners := []*CompilerBaselineRunner{
NewCompilerBaselineRunner(TestTypeRegression, isSubmodule),
NewCompilerBaselineRunner(TestTypeConformance, isSubmodule),
}
var seenTests collections.Set[string]
for _, runner := range runners {
for _, test := range runner.EnumerateTestFiles() {
test = tspath.GetBaseFileName(test)
assert.Assert(t, !seenTests.Has(test), "Duplicate test file: %s", test)
seenTests.Add(test)
}
}
for _, runner := range runners {
runner.RunTests(t)
}
}

View File

@ -1,14 +0,0 @@
package testrunner
import "testing"
type Runner interface {
EnumerateTestFiles() []string
RunTests(t *testing.T)
}
func runTests(t *testing.T, runners []Runner) {
for _, runner := range runners {
runner.RunTests(t)
}
}

View File

@ -1,227 +0,0 @@
package testrunner
import (
"regexp"
"slices"
"strings"
"efprojects.com/kitten-ipc/kitcom/internal/tsgo/ast"
"efprojects.com/kitten-ipc/kitcom/internal/tsgo/core"
"efprojects.com/kitten-ipc/kitcom/internal/tsgo/parser"
"efprojects.com/kitten-ipc/kitcom/internal/tsgo/scanner"
"efprojects.com/kitten-ipc/kitcom/internal/tsgo/testutil/harnessutil"
"efprojects.com/kitten-ipc/kitcom/internal/tsgo/tsoptions"
"efprojects.com/kitten-ipc/kitcom/internal/tsgo/tsoptions/tsoptionstest"
"efprojects.com/kitten-ipc/kitcom/internal/tsgo/tspath"
)
var lineDelimiter = regexp.MustCompile("\r?\n")
// This maps a compiler setting to its value as written in the test file. For example, if a test file contains:
//
// // @target: esnext, es2015
//
// Then the map will map "target" to "esnext, es2015"
type rawCompilerSettings map[string]string
// All the necessary information to turn a multi file test into useful units for later compilation
type testUnit struct {
content string
name string
}
type testCaseContent struct {
testUnitData []*testUnit
tsConfig *tsoptions.ParsedCommandLine
tsConfigFileUnitData *testUnit
symlinks map[string]string
}
// Regex for parsing options in the format "@Alpha: Value of any sort"
var optionRegex = regexp.MustCompile(`(?m)^\/{2}\s*@(\w+)\s*:\s*([^\r\n]*)`)
// Regex for parsing @link option
var linkRegex = regexp.MustCompile(`(?m)^\/{2}\s*@link\s*:\s*([^\r\n]*)\s*->\s*([^\r\n]*)`)
// File-specific directives used by fourslash tests
var fourslashDirectives = []string{"emitthisfile"}
// Given a test file containing // @FileName directives,
// return an array of named units of code to be added to an existing compiler instance.
func makeUnitsFromTest(code string, fileName string) testCaseContent {
testUnits, symlinks, currentDirectory, _, _ := ParseTestFilesAndSymlinks(
code,
fileName,
func(filename string, content string, fileOptions map[string]string) (*testUnit, error) {
return &testUnit{content: content, name: filename}, nil
},
)
if currentDirectory == "" {
currentDirectory = srcFolder
}
// unit tests always list files explicitly
allFiles := make(map[string]string)
for _, data := range testUnits {
allFiles[tspath.GetNormalizedAbsolutePath(data.name, currentDirectory)] = data.content
}
parseConfigHost := tsoptionstest.NewVFSParseConfigHost(allFiles, currentDirectory, true /*useCaseSensitiveFileNames*/)
// check if project has tsconfig.json in the list of files
var tsConfig *tsoptions.ParsedCommandLine
var tsConfigFileUnitData *testUnit
for i, data := range testUnits {
if harnessutil.GetConfigNameFromFileName(data.name) != "" {
configFileName := tspath.GetNormalizedAbsolutePath(data.name, currentDirectory)
path := tspath.ToPath(data.name, parseConfigHost.GetCurrentDirectory(), parseConfigHost.Vfs.UseCaseSensitiveFileNames())
configJson := parser.ParseSourceFile(ast.SourceFileParseOptions{
FileName: configFileName,
Path: path,
}, data.content, core.ScriptKindJSON)
tsConfigSourceFile := &tsoptions.TsConfigSourceFile{
SourceFile: configJson,
}
configDir := tspath.GetDirectoryPath(configFileName)
tsConfig = tsoptions.ParseJsonSourceFileConfigFileContent(
tsConfigSourceFile,
parseConfigHost,
configDir,
nil, /*existingOptions*/
configFileName,
nil, /*resolutionStack*/
nil, /*extraFileExtensions*/
nil /*extendedConfigCache*/)
tsConfigFileUnitData = data
// delete tsconfig file entry from the list
testUnits = slices.Delete(testUnits, i, i+1)
break
}
}
return testCaseContent{
testUnitData: testUnits,
tsConfig: tsConfig,
tsConfigFileUnitData: tsConfigFileUnitData,
symlinks: symlinks,
}
}
// Given a test file containing // @FileName and // @symlink directives,
// return an array of named units of code to be added to an existing compiler instance,
// along with a map of symlinks and the current directory.
func ParseTestFilesAndSymlinks[T any](
code string,
fileName string,
parseFile func(filename string, content string, fileOptions map[string]string) (T, error),
) (units []T, symlinks map[string]string, currentDir string, globalOptions map[string]string, e error) {
// List of all the subfiles we've parsed out
var testUnits []T
lines := lineDelimiter.Split(code, -1)
// Stuff related to the subfile we're parsing
var currentFileContent strings.Builder
var currentFileName string
var currentDirectory string
var parseError error
currentFileOptions := make(map[string]string)
symlinks = make(map[string]string)
globalOptions = make(map[string]string)
for _, line := range lines {
ok := parseSymlinkFromTest(line, symlinks)
if ok {
continue
}
if testMetaData := optionRegex.FindStringSubmatch(line); testMetaData != nil {
// Comment line, check for global/file @options and record them
metaDataName := strings.ToLower(testMetaData[1])
metaDataValue := strings.TrimSpace(testMetaData[2])
if metaDataName == "currentdirectory" {
currentDirectory = metaDataValue
}
if metaDataName != "filename" {
if slices.Contains(fourslashDirectives, metaDataName) {
// File-specific option
currentFileOptions[metaDataName] = metaDataValue
} else {
// Global option
if existingValue, ok := globalOptions[metaDataName]; ok && existingValue != metaDataValue {
// !!! This would break existing submodule tests
// panic("Duplicate global option: " + metaDataName)
}
globalOptions[metaDataName] = metaDataValue
}
continue
}
// New metadata statement after having collected some code to go with the previous metadata
if currentFileName != "" {
// Store result file
newTestFile, e := parseFile(currentFileName, currentFileContent.String(), currentFileOptions)
if e != nil {
parseError = e
break
}
testUnits = append(testUnits, newTestFile)
// Reset local data
currentFileContent.Reset()
currentFileName = metaDataValue
currentFileOptions = make(map[string]string)
} else {
// First metadata marker in the file
currentFileName = strings.TrimSpace(testMetaData[2])
if currentFileContent.Len() != 0 && scanner.SkipTrivia(currentFileContent.String(), 0) != currentFileContent.Len() {
panic("Non-comment test content appears before the first '// @Filename' directive")
}
currentFileContent.Reset()
}
} else {
// Subfile content line
// Append to the current subfile content, inserting a newline if needed
if currentFileContent.Len() != 0 {
// End-of-line
currentFileContent.WriteRune('\n')
}
currentFileContent.WriteString(line)
}
}
// normalize the fileName for the single file case
if len(testUnits) == 0 && len(currentFileName) == 0 {
currentFileName = tspath.GetBaseFileName(fileName)
}
// if there are no parse errors so far, parse the rest of the file
if parseError == nil {
// EOF, push whatever remains
newTestFile2, e := parseFile(currentFileName, currentFileContent.String(), currentFileOptions)
parseError = e
testUnits = append(testUnits, newTestFile2)
}
return testUnits, symlinks, currentDirectory, globalOptions, parseError
}
func extractCompilerSettings(content string) rawCompilerSettings {
opts := make(map[string]string)
for _, match := range optionRegex.FindAllStringSubmatch(content, -1) {
opts[strings.ToLower(match[1])] = strings.TrimSuffix(strings.TrimSpace(match[2]), ";")
}
return opts
}
func parseSymlinkFromTest(line string, symlinks map[string]string) bool {
linkMetaData := linkRegex.FindStringSubmatch(line)
if len(linkMetaData) == 0 {
return false
}
symlinks[strings.TrimSpace(linkMetaData[2])] = strings.TrimSpace(linkMetaData[1])
return true
}

View File

@ -1,41 +0,0 @@
package testrunner
import (
"testing"
"github.com/google/go-cmp/cmp"
"gotest.tools/v3/assert"
)
func TestMakeUnitsFromTest(t *testing.T) {
t.Parallel()
code := `// @strict: true
// @noEmit: true
// @filename: firstFile.ts
function foo() { return "a"; }
// normal comment
// @filename: secondFile.ts
// some other comment
function bar() { return "b"; }`
testUnit1 := &testUnit{
content: `function foo() { return "a"; }
// normal comment`,
name: "firstFile.ts",
}
testUnit2 := &testUnit{
content: `// some other comment
function bar() { return "b"; }`,
name: "secondFile.ts",
}
testContent := testCaseContent{
testUnitData: []*testUnit{testUnit1, testUnit2},
tsConfig: nil,
tsConfigFileUnitData: nil,
symlinks: make(map[string]string),
}
assert.DeepEqual(
t,
makeUnitsFromTest(code, "simpleTest.ts"),
testContent,
cmp.AllowUnexported(testCaseContent{}, testUnit{}))
}