Skip to content

Add "accepted diffs" to test runner, accept diffs for Programs with unsupported extensions #655

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 18, 2025
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,5 @@ custom-gcl.hash
.DS_Store

.idea

!testdata/submoduleAccepted.txt
11 changes: 2 additions & 9 deletions cmd/tsgo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,15 +237,8 @@ func main() {
printDiagnostics(ts.SortAndDeduplicateDiagnostics(diagnostics), host, compilerOptions)
}

var unsupportedExtensions []string
for _, file := range program.SourceFiles() {
extension := tspath.TryGetExtensionFromPath(file.FileName())
if extension == tspath.ExtensionTsx || slices.Contains(tspath.SupportedJSExtensionsFlat, extension) {
unsupportedExtensions = core.AppendIfUnique(unsupportedExtensions, extension)
}
}
if len(unsupportedExtensions) != 0 {
fmt.Fprintf(os.Stderr, "Warning: The project contains unsupported file types (%s), which are currently not fully type-checked.\n", strings.Join(unsupportedExtensions, ", "))
if exts := program.UnsupportedExtensions(); len(exts) != 0 {
fmt.Fprintf(os.Stderr, "Warning: The project contains unsupported file types (%s), which are currently not fully type-checked.\n", strings.Join(exts, ", "))
}

if compilerOptions.ListFiles.IsTrue() {
Expand Down
16 changes: 16 additions & 0 deletions internal/compiler/program.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ type Program struct {

commonSourceDirectory string
commonSourceDirectoryOnce sync.Once

// List of present unsupported extensions
unsupportedExtensions []string
}

var extensions = []string{".ts", ".tsx"}
Expand Down Expand Up @@ -155,6 +158,13 @@ func NewProgram(options ProgramOptions) *Program {
p.filesByPath[file.Path()] = file
}

for _, file := range p.files {
extension := tspath.TryGetExtensionFromPath(file.FileName())
if extension == tspath.ExtensionTsx || slices.Contains(tspath.SupportedJSExtensionsFlat, extension) {
p.unsupportedExtensions = append(p.unsupportedExtensions, extension)
}
}

return p
}

Expand Down Expand Up @@ -609,3 +619,9 @@ type FileIncludeReason struct {
Kind FileIncludeKind
Index int
}

// UnsupportedExtensions returns a list of all present "unsupported" extensions,
// e.g. extensions that are not yet supported by the port.
func (p *Program) UnsupportedExtensions() []string {
return p.unsupportedExtensions
}
6 changes: 5 additions & 1 deletion internal/testrunner/compiler_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,11 @@ func (c *compilerTest) verifyDiagnostics(t *testing.T, suiteName string, isSubmo

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})
tsbaseline.DoErrorBaseline(t, c.configuredName, files, c.result.Diagnostics, c.result.Options.Pretty.IsTrue(), baseline.Options{
Subfolder: suiteName,
IsSubmodule: isSubmodule,
IsSubmoduleAccepted: len(c.result.Program.UnsupportedExtensions()) != 0, // TODO(jakebailey): read submoduleAccepted.txt
})
})
}

Expand Down
186 changes: 119 additions & 67 deletions internal/testutil/baseline/baseline.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,103 @@ import (
"path/filepath"
"regexp"
"strings"
"sync"
"testing"

"github.com/microsoft/typescript-go/internal/core"
"github.com/microsoft/typescript-go/internal/repo"
"github.com/microsoft/typescript-go/internal/tspath"
"github.com/pkg/diff"
"gotest.tools/v3/assert"
)

type Options struct {
Subfolder string
IsSubmodule bool
DiffFixupOld func(string) string
Subfolder string
IsSubmodule bool
IsSubmoduleAccepted bool
DiffFixupOld func(string) string
}

const (
NoContent = "<no content>"
submoduleFolder = "submodule"
)
const NoContent = "<no content>"

func Run(t *testing.T, fileName string, actual string, opts Options) {
if opts.IsSubmodule {
opts.Subfolder = filepath.Join(submoduleFolder, opts.Subfolder)
diff := getBaselineDiff(t, actual, fileName, opts.DiffFixupOld)
diffFileName := fileName + ".diff"
writeComparison(t, diff, diffFileName, false, opts)
origSubfolder := opts.Subfolder

{
subfolder := opts.Subfolder
if opts.IsSubmodule {
subfolder = filepath.Join("submodule", subfolder)
}

localPath := filepath.Join(localRoot, subfolder, fileName)
referencePath := filepath.Join(referenceRoot, subfolder, fileName)

writeComparison(t, actual, localPath, referencePath, false)
}

if !opts.IsSubmodule {
// Not a submodule, no diffs.
return
}

submoduleReference := filepath.Join(submoduleReferenceRoot, fileName)
submoduleExpected := readFileOrNoContent(submoduleReference)

const (
submoduleFolder = "submodule"
submoduleAcceptedFolder = "submoduleAccepted"
)

diffFileName := fileName + ".diff"
isSubmoduleAccepted := opts.IsSubmoduleAccepted || submoduleAcceptedFileNames().Has(origSubfolder+"/"+diffFileName)

outRoot := core.IfElse(isSubmoduleAccepted, submoduleAcceptedFolder, submoduleFolder)
unusedOutRoot := core.IfElse(isSubmoduleAccepted, submoduleFolder, submoduleAcceptedFolder)

{
localPath := filepath.Join(localRoot, outRoot, origSubfolder, diffFileName)
referencePath := filepath.Join(referenceRoot, outRoot, origSubfolder, diffFileName)

diff := getBaselineDiff(t, actual, submoduleExpected, fileName, opts.DiffFixupOld)
writeComparison(t, diff, localPath, referencePath, false)
}

// Delete the other diff file if it exists
{
localPath := filepath.Join(localRoot, unusedOutRoot, origSubfolder, diffFileName)
referencePath := filepath.Join(referenceRoot, unusedOutRoot, origSubfolder, diffFileName)
writeComparison(t, NoContent, localPath, referencePath, false)
}
writeComparison(t, actual, fileName, false, opts)
}

func getBaselineDiff(t *testing.T, actual string, fileName string, fixupOld func(string) string) string {
expected := NoContent
refFileName := submoduleReferencePath(fileName, "" /*subfolder*/)
if content, err := os.ReadFile(refFileName); err == nil {
expected = string(content)
var submoduleAcceptedFileNames = sync.OnceValue(func() *core.Set[string] {
var set core.Set[string]

submoduleAccepted := filepath.Join(repo.TestDataPath, "submoduleAccepted.txt")
if content, err := os.ReadFile(submoduleAccepted); err == nil {
for line := range strings.SplitSeq(string(content), "\n") {
line = strings.TrimSpace(line)
if line == "" || line[0] == '#' {
continue
}
set.Add(line)
}
} else {
panic(fmt.Sprintf("failed to read submodule accepted file: %v", err))
}

return &set
})

func readFileOrNoContent(fileName string) string {
content, err := os.ReadFile(fileName)
if err != nil {
return NoContent
}
return string(content)
}

func getBaselineDiff(t *testing.T, actual string, expected string, fileName string, fixupOld func(string) string) string {
if fixupOld != nil {
expected = fixupOld(expected)
}
Expand Down Expand Up @@ -77,84 +141,72 @@ func getBaselineDiff(t *testing.T, actual string, fileName string, fixupOld func
var fixUnifiedDiff = regexp.MustCompile(`@@ -\d+,\d+ \+\d+,\d+ @@`)

func RunAgainstSubmodule(t *testing.T, fileName string, actual string, opts Options) {
writeComparison(t, actual, fileName, true /*useSubmodule*/, opts)
local := filepath.Join(localRoot, opts.Subfolder, fileName)
reference := filepath.Join(submoduleReferenceRoot, opts.Subfolder, fileName)
writeComparison(t, actual, local, reference, true)
}

func writeComparison(t *testing.T, actual string, relativeFileName string, useSubmodule bool, opts Options) {
if actual == "" {
func writeComparison(t *testing.T, actualContent string, local, reference string, comparingAgainstSubmodule bool) {
if actualContent == "" {
panic("the generated content was \"\". Return 'baseline.NoContent' if no baselining is required.")
}
var (
localFileName string
referenceFileName string
)

if useSubmodule {
localFileName = submoduleLocalPath(relativeFileName, opts.Subfolder)
referenceFileName = submoduleReferencePath(relativeFileName, opts.Subfolder)
} else {
localFileName = localPath(relativeFileName, opts.Subfolder)
referenceFileName = referencePath(relativeFileName, opts.Subfolder)
}

if err := os.MkdirAll(filepath.Dir(localFileName), 0o755); err != nil {
t.Error(fmt.Errorf("failed to create directories for the local baseline file %s: %w", localFileName, err))
if err := os.MkdirAll(filepath.Dir(local), 0o755); err != nil {
t.Error(fmt.Errorf("failed to create directories for the local baseline file %s: %w", local, err))
return
}

if _, err := os.Stat(localFileName); err == nil {
if err := os.Remove(localFileName); err != nil {
t.Error(fmt.Errorf("failed to remove the local baseline file %s: %w", localFileName, err))
if _, err := os.Stat(local); err == nil {
if err := os.Remove(local); err != nil {
t.Error(fmt.Errorf("failed to remove the local baseline file %s: %w", local, err))
return
}
}

expected := NoContent
foundExpected := false
if content, err := os.ReadFile(referenceFileName); err == nil {
if content, err := os.ReadFile(reference); err == nil {
expected = string(content)
foundExpected = true
}

if expected != actual || actual == NoContent && foundExpected {
if actual == NoContent {
if err := os.WriteFile(localFileName+".delete", []byte{}, 0o644); err != nil {
t.Error(fmt.Errorf("failed to write the local baseline file %s: %w", localFileName+".delete", err))
if expected != actualContent || actualContent == NoContent && foundExpected {
if actualContent == NoContent {
if err := os.WriteFile(local+".delete", []byte{}, 0o644); err != nil {
t.Error(fmt.Errorf("failed to write the local baseline file %s: %w", local+".delete", err))
return
}
} else {
if err := os.WriteFile(localFileName, []byte(actual), 0o644); err != nil {
t.Error(fmt.Errorf("failed to write the local baseline file %s: %w", localFileName, err))
if err := os.WriteFile(local, []byte(actualContent), 0o644); err != nil {
t.Error(fmt.Errorf("failed to write the local baseline file %s: %w", local, err))
return
}
}

if _, err := os.Stat(referenceFileName); err != nil {
if useSubmodule {
t.Errorf("the baseline file %s does not exist in the TypeScript submodule", referenceFileName)
relReference, err := filepath.Rel(repo.RootPath, reference)
assert.NilError(t, err)
relReference = tspath.NormalizeSlashes(relReference)

relLocal, err := filepath.Rel(repo.RootPath, local)
assert.NilError(t, err)
relLocal = tspath.NormalizeSlashes(relLocal)

if _, err := os.Stat(reference); err != nil {
if comparingAgainstSubmodule {
t.Errorf("the baseline file %s does not exist in the TypeScript submodule", relReference)
} else {
t.Errorf("new baseline created at %s.", localFileName)
t.Errorf("new baseline created at %s.", relLocal)
}
} else if useSubmodule {
t.Errorf("the baseline file %s does not match the reference in the TypeScript submodule", relativeFileName)
} else if comparingAgainstSubmodule {
t.Errorf("the baseline file %s does not match the reference in the TypeScript submodule", relReference)
} else {
t.Errorf("the baseline file %s has changed. (Run `hereby baseline-accept` if the new baseline is correct.)", relativeFileName)
t.Errorf("the baseline file %s has changed. (Run `hereby baseline-accept` if the new baseline is correct.)", relReference)
}
}
}

func localPath(fileName string, subfolder string) string {
return filepath.Join(repo.TestDataPath, "baselines", "local", subfolder, fileName)
}

func submoduleLocalPath(fileName string, subfolder string) string {
return filepath.Join(repo.TestDataPath, "baselines", "tmp", subfolder, fileName)
}

func referencePath(fileName string, subfolder string) string {
return filepath.Join(repo.TestDataPath, "baselines", "reference", subfolder, fileName)
}

func submoduleReferencePath(fileName string, subfolder string) string {
return filepath.Join(repo.TypeScriptSubmodulePath, "tests", "baselines", "reference", subfolder, fileName)
}
var (
localRoot = filepath.Join(repo.TestDataPath, "baselines", "local")
referenceRoot = filepath.Join(repo.TestDataPath, "baselines", "reference")
submoduleReferenceRoot = filepath.Join(repo.TypeScriptSubmodulePath, "tests", "baselines", "reference")
)
1 change: 1 addition & 0 deletions internal/testutil/tsbaseline/type_symbol_baseline.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func DoTypeAndSymbolBaseline(

return sb.String()[:sb.Len()-1]
}
typesOpts.IsSubmoduleAccepted = len(program.UnsupportedExtensions()) != 0 // TODO(jakebailey): read submoduleAccepted.txt

checkBaselines(t, baselinePath, allFiles, fullWalker, header, typesOpts, false /*isSymbolBaseline*/)
})
Expand Down
Loading