Skip to content

Commit eeaaeb6

Browse files
authored
Add "accepted diffs" to test runner, accept diffs for Programs with unsupported extensions (#655)
1 parent 026e5f9 commit eeaaeb6

File tree

1,992 files changed

+146
-77
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,992 files changed

+146
-77
lines changed

Diff for: .gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -192,3 +192,5 @@ custom-gcl.hash
192192
.DS_Store
193193

194194
.idea
195+
196+
!testdata/submoduleAccepted.txt

Diff for: cmd/tsgo/main.go

+2-9
Original file line numberDiff line numberDiff line change
@@ -237,15 +237,8 @@ func main() {
237237
printDiagnostics(ts.SortAndDeduplicateDiagnostics(diagnostics), host, compilerOptions)
238238
}
239239

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

251244
if compilerOptions.ListFiles.IsTrue() {

Diff for: internal/compiler/program.go

+16
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ type Program struct {
6363

6464
commonSourceDirectory string
6565
commonSourceDirectoryOnce sync.Once
66+
67+
// List of present unsupported extensions
68+
unsupportedExtensions []string
6669
}
6770

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

161+
for _, file := range p.files {
162+
extension := tspath.TryGetExtensionFromPath(file.FileName())
163+
if extension == tspath.ExtensionTsx || slices.Contains(tspath.SupportedJSExtensionsFlat, extension) {
164+
p.unsupportedExtensions = append(p.unsupportedExtensions, extension)
165+
}
166+
}
167+
158168
return p
159169
}
160170

@@ -609,3 +619,9 @@ type FileIncludeReason struct {
609619
Kind FileIncludeKind
610620
Index int
611621
}
622+
623+
// UnsupportedExtensions returns a list of all present "unsupported" extensions,
624+
// e.g. extensions that are not yet supported by the port.
625+
func (p *Program) UnsupportedExtensions() []string {
626+
return p.unsupportedExtensions
627+
}

Diff for: internal/testrunner/compiler_runner.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,11 @@ func (c *compilerTest) verifyDiagnostics(t *testing.T, suiteName string, isSubmo
337337

338338
defer testutil.RecoverAndFail(t, "Panic on creating error baseline for test "+c.filename)
339339
files := core.Concatenate(c.tsConfigFiles, core.Concatenate(c.toBeCompiled, c.otherFiles))
340-
tsbaseline.DoErrorBaseline(t, c.configuredName, files, c.result.Diagnostics, c.result.Options.Pretty.IsTrue(), baseline.Options{Subfolder: suiteName, IsSubmodule: isSubmodule})
340+
tsbaseline.DoErrorBaseline(t, c.configuredName, files, c.result.Diagnostics, c.result.Options.Pretty.IsTrue(), baseline.Options{
341+
Subfolder: suiteName,
342+
IsSubmodule: isSubmodule,
343+
IsSubmoduleAccepted: len(c.result.Program.UnsupportedExtensions()) != 0, // TODO(jakebailey): read submoduleAccepted.txt
344+
})
341345
})
342346
}
343347

Diff for: internal/testutil/baseline/baseline.go

+119-67
Original file line numberDiff line numberDiff line change
@@ -6,39 +6,103 @@ import (
66
"path/filepath"
77
"regexp"
88
"strings"
9+
"sync"
910
"testing"
1011

12+
"github.com/microsoft/typescript-go/internal/core"
1113
"github.com/microsoft/typescript-go/internal/repo"
14+
"github.com/microsoft/typescript-go/internal/tspath"
1215
"github.com/pkg/diff"
16+
"gotest.tools/v3/assert"
1317
)
1418

1519
type Options struct {
16-
Subfolder string
17-
IsSubmodule bool
18-
DiffFixupOld func(string) string
20+
Subfolder string
21+
IsSubmodule bool
22+
IsSubmoduleAccepted bool
23+
DiffFixupOld func(string) string
1924
}
2025

21-
const (
22-
NoContent = "<no content>"
23-
submoduleFolder = "submodule"
24-
)
26+
const NoContent = "<no content>"
2527

2628
func Run(t *testing.T, fileName string, actual string, opts Options) {
27-
if opts.IsSubmodule {
28-
opts.Subfolder = filepath.Join(submoduleFolder, opts.Subfolder)
29-
diff := getBaselineDiff(t, actual, fileName, opts.DiffFixupOld)
30-
diffFileName := fileName + ".diff"
31-
writeComparison(t, diff, diffFileName, false, opts)
29+
origSubfolder := opts.Subfolder
30+
31+
{
32+
subfolder := opts.Subfolder
33+
if opts.IsSubmodule {
34+
subfolder = filepath.Join("submodule", subfolder)
35+
}
36+
37+
localPath := filepath.Join(localRoot, subfolder, fileName)
38+
referencePath := filepath.Join(referenceRoot, subfolder, fileName)
39+
40+
writeComparison(t, actual, localPath, referencePath, false)
41+
}
42+
43+
if !opts.IsSubmodule {
44+
// Not a submodule, no diffs.
45+
return
46+
}
47+
48+
submoduleReference := filepath.Join(submoduleReferenceRoot, fileName)
49+
submoduleExpected := readFileOrNoContent(submoduleReference)
50+
51+
const (
52+
submoduleFolder = "submodule"
53+
submoduleAcceptedFolder = "submoduleAccepted"
54+
)
55+
56+
diffFileName := fileName + ".diff"
57+
isSubmoduleAccepted := opts.IsSubmoduleAccepted || submoduleAcceptedFileNames().Has(origSubfolder+"/"+diffFileName)
58+
59+
outRoot := core.IfElse(isSubmoduleAccepted, submoduleAcceptedFolder, submoduleFolder)
60+
unusedOutRoot := core.IfElse(isSubmoduleAccepted, submoduleFolder, submoduleAcceptedFolder)
61+
62+
{
63+
localPath := filepath.Join(localRoot, outRoot, origSubfolder, diffFileName)
64+
referencePath := filepath.Join(referenceRoot, outRoot, origSubfolder, diffFileName)
65+
66+
diff := getBaselineDiff(t, actual, submoduleExpected, fileName, opts.DiffFixupOld)
67+
writeComparison(t, diff, localPath, referencePath, false)
68+
}
69+
70+
// Delete the other diff file if it exists
71+
{
72+
localPath := filepath.Join(localRoot, unusedOutRoot, origSubfolder, diffFileName)
73+
referencePath := filepath.Join(referenceRoot, unusedOutRoot, origSubfolder, diffFileName)
74+
writeComparison(t, NoContent, localPath, referencePath, false)
3275
}
33-
writeComparison(t, actual, fileName, false, opts)
3476
}
3577

36-
func getBaselineDiff(t *testing.T, actual string, fileName string, fixupOld func(string) string) string {
37-
expected := NoContent
38-
refFileName := submoduleReferencePath(fileName, "" /*subfolder*/)
39-
if content, err := os.ReadFile(refFileName); err == nil {
40-
expected = string(content)
78+
var submoduleAcceptedFileNames = sync.OnceValue(func() *core.Set[string] {
79+
var set core.Set[string]
80+
81+
submoduleAccepted := filepath.Join(repo.TestDataPath, "submoduleAccepted.txt")
82+
if content, err := os.ReadFile(submoduleAccepted); err == nil {
83+
for line := range strings.SplitSeq(string(content), "\n") {
84+
line = strings.TrimSpace(line)
85+
if line == "" || line[0] == '#' {
86+
continue
87+
}
88+
set.Add(line)
89+
}
90+
} else {
91+
panic(fmt.Sprintf("failed to read submodule accepted file: %v", err))
92+
}
93+
94+
return &set
95+
})
96+
97+
func readFileOrNoContent(fileName string) string {
98+
content, err := os.ReadFile(fileName)
99+
if err != nil {
100+
return NoContent
41101
}
102+
return string(content)
103+
}
104+
105+
func getBaselineDiff(t *testing.T, actual string, expected string, fileName string, fixupOld func(string) string) string {
42106
if fixupOld != nil {
43107
expected = fixupOld(expected)
44108
}
@@ -77,84 +141,72 @@ func getBaselineDiff(t *testing.T, actual string, fileName string, fixupOld func
77141
var fixUnifiedDiff = regexp.MustCompile(`@@ -\d+,\d+ \+\d+,\d+ @@`)
78142

79143
func RunAgainstSubmodule(t *testing.T, fileName string, actual string, opts Options) {
80-
writeComparison(t, actual, fileName, true /*useSubmodule*/, opts)
144+
local := filepath.Join(localRoot, opts.Subfolder, fileName)
145+
reference := filepath.Join(submoduleReferenceRoot, opts.Subfolder, fileName)
146+
writeComparison(t, actual, local, reference, true)
81147
}
82148

83-
func writeComparison(t *testing.T, actual string, relativeFileName string, useSubmodule bool, opts Options) {
84-
if actual == "" {
149+
func writeComparison(t *testing.T, actualContent string, local, reference string, comparingAgainstSubmodule bool) {
150+
if actualContent == "" {
85151
panic("the generated content was \"\". Return 'baseline.NoContent' if no baselining is required.")
86152
}
87-
var (
88-
localFileName string
89-
referenceFileName string
90-
)
91-
92-
if useSubmodule {
93-
localFileName = submoduleLocalPath(relativeFileName, opts.Subfolder)
94-
referenceFileName = submoduleReferencePath(relativeFileName, opts.Subfolder)
95-
} else {
96-
localFileName = localPath(relativeFileName, opts.Subfolder)
97-
referenceFileName = referencePath(relativeFileName, opts.Subfolder)
98-
}
99153

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

105-
if _, err := os.Stat(localFileName); err == nil {
106-
if err := os.Remove(localFileName); err != nil {
107-
t.Error(fmt.Errorf("failed to remove the local baseline file %s: %w", localFileName, err))
159+
if _, err := os.Stat(local); err == nil {
160+
if err := os.Remove(local); err != nil {
161+
t.Error(fmt.Errorf("failed to remove the local baseline file %s: %w", local, err))
108162
return
109163
}
110164
}
111165

112166
expected := NoContent
113167
foundExpected := false
114-
if content, err := os.ReadFile(referenceFileName); err == nil {
168+
if content, err := os.ReadFile(reference); err == nil {
115169
expected = string(content)
116170
foundExpected = true
117171
}
118172

119-
if expected != actual || actual == NoContent && foundExpected {
120-
if actual == NoContent {
121-
if err := os.WriteFile(localFileName+".delete", []byte{}, 0o644); err != nil {
122-
t.Error(fmt.Errorf("failed to write the local baseline file %s: %w", localFileName+".delete", err))
173+
if expected != actualContent || actualContent == NoContent && foundExpected {
174+
if actualContent == NoContent {
175+
if err := os.WriteFile(local+".delete", []byte{}, 0o644); err != nil {
176+
t.Error(fmt.Errorf("failed to write the local baseline file %s: %w", local+".delete", err))
123177
return
124178
}
125179
} else {
126-
if err := os.WriteFile(localFileName, []byte(actual), 0o644); err != nil {
127-
t.Error(fmt.Errorf("failed to write the local baseline file %s: %w", localFileName, err))
180+
if err := os.WriteFile(local, []byte(actualContent), 0o644); err != nil {
181+
t.Error(fmt.Errorf("failed to write the local baseline file %s: %w", local, err))
128182
return
129183
}
130184
}
131185

132-
if _, err := os.Stat(referenceFileName); err != nil {
133-
if useSubmodule {
134-
t.Errorf("the baseline file %s does not exist in the TypeScript submodule", referenceFileName)
186+
relReference, err := filepath.Rel(repo.RootPath, reference)
187+
assert.NilError(t, err)
188+
relReference = tspath.NormalizeSlashes(relReference)
189+
190+
relLocal, err := filepath.Rel(repo.RootPath, local)
191+
assert.NilError(t, err)
192+
relLocal = tspath.NormalizeSlashes(relLocal)
193+
194+
if _, err := os.Stat(reference); err != nil {
195+
if comparingAgainstSubmodule {
196+
t.Errorf("the baseline file %s does not exist in the TypeScript submodule", relReference)
135197
} else {
136-
t.Errorf("new baseline created at %s.", localFileName)
198+
t.Errorf("new baseline created at %s.", relLocal)
137199
}
138-
} else if useSubmodule {
139-
t.Errorf("the baseline file %s does not match the reference in the TypeScript submodule", relativeFileName)
200+
} else if comparingAgainstSubmodule {
201+
t.Errorf("the baseline file %s does not match the reference in the TypeScript submodule", relReference)
140202
} else {
141-
t.Errorf("the baseline file %s has changed. (Run `hereby baseline-accept` if the new baseline is correct.)", relativeFileName)
203+
t.Errorf("the baseline file %s has changed. (Run `hereby baseline-accept` if the new baseline is correct.)", relReference)
142204
}
143205
}
144206
}
145207

146-
func localPath(fileName string, subfolder string) string {
147-
return filepath.Join(repo.TestDataPath, "baselines", "local", subfolder, fileName)
148-
}
149-
150-
func submoduleLocalPath(fileName string, subfolder string) string {
151-
return filepath.Join(repo.TestDataPath, "baselines", "tmp", subfolder, fileName)
152-
}
153-
154-
func referencePath(fileName string, subfolder string) string {
155-
return filepath.Join(repo.TestDataPath, "baselines", "reference", subfolder, fileName)
156-
}
157-
158-
func submoduleReferencePath(fileName string, subfolder string) string {
159-
return filepath.Join(repo.TypeScriptSubmodulePath, "tests", "baselines", "reference", subfolder, fileName)
160-
}
208+
var (
209+
localRoot = filepath.Join(repo.TestDataPath, "baselines", "local")
210+
referenceRoot = filepath.Join(repo.TestDataPath, "baselines", "reference")
211+
submoduleReferenceRoot = filepath.Join(repo.TypeScriptSubmodulePath, "tests", "baselines", "reference")
212+
)

Diff for: internal/testutil/tsbaseline/type_symbol_baseline.go

+1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ func DoTypeAndSymbolBaseline(
7171

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

7576
checkBaselines(t, baselinePath, allFiles, fullWalker, header, typesOpts, false /*isSymbolBaseline*/)
7677
})

0 commit comments

Comments
 (0)