Skip to content

Commit 04b68cb

Browse files
committed
Incremental correctness
1 parent ac20ac4 commit 04b68cb

File tree

9 files changed

+326
-268
lines changed

9 files changed

+326
-268
lines changed

internal/execute/testfs_test.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,33 @@ import (
55
"github.com/microsoft/typescript-go/internal/vfs"
66
)
77

8-
type testFsTrackingLibs struct {
8+
type testFs struct {
99
vfs.FS
10-
defaultLibs *collections.SyncSet[string]
10+
defaultLibs *collections.SyncSet[string]
11+
writtenFiles collections.SyncSet[string]
1112
}
1213

13-
func NewFSTrackingLibs(fs vfs.FS) *testFsTrackingLibs {
14-
return &testFsTrackingLibs{FS: fs}
15-
}
16-
17-
func (f *testFsTrackingLibs) removeIgnoreLibPath(path string) {
14+
func (f *testFs) removeIgnoreLibPath(path string) {
1815
if f.defaultLibs != nil && f.defaultLibs.Has(path) {
1916
f.defaultLibs.Delete(path)
2017
}
2118
}
2219

2320
// ReadFile reads the file specified by path and returns the content.
2421
// If the file fails to be read, ok will be false.
25-
func (f *testFsTrackingLibs) ReadFile(path string) (contents string, ok bool) {
22+
func (f *testFs) ReadFile(path string) (contents string, ok bool) {
2623
f.removeIgnoreLibPath(path)
2724
return f.FS.ReadFile(path)
2825
}
2926

30-
func (f *testFsTrackingLibs) WriteFile(path string, data string, writeByteOrderMark bool) error {
27+
func (f *testFs) WriteFile(path string, data string, writeByteOrderMark bool) error {
3128
f.removeIgnoreLibPath(path)
29+
f.writtenFiles.Add(path)
3230
return f.FS.WriteFile(path, data, writeByteOrderMark)
3331
}
3432

3533
// Removes `path` and all its contents. Will return the first error it encounters.
36-
func (f *testFsTrackingLibs) Remove(path string) error {
34+
func (f *testFs) Remove(path string) error {
3735
f.removeIgnoreLibPath(path)
3836
return f.FS.Remove(path)
3937
}

internal/execute/testsys_test.go

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,11 @@ func newTestSys(fileOrFolderList FileMap, cwd string) *testSys {
5555
cwd = "/home/src/workspaces/project"
5656
}
5757
sys := &testSys{
58-
fs: NewFSTrackingLibs(incrementaltestutil.NewFsHandlingBuildInfo(vfstest.FromMap(fileOrFolderList, true /*useCaseSensitiveFileNames*/))),
58+
fs: &incrementaltestutil.FsHandlingBuildInfo{
59+
FS: &testFs{
60+
FS: vfstest.FromMap(fileOrFolderList, true /*useCaseSensitiveFileNames*/),
61+
},
62+
},
5963
defaultLibraryPath: tscLibPath,
6064
cwd: cwd,
6165
files: slices.Collect(maps.Keys(fileOrFolderList)),
@@ -91,7 +95,7 @@ type testSys struct {
9195
currentWrite *strings.Builder
9296
serializedDiff *snapshot
9397

94-
fs *testFsTrackingLibs
98+
fs *incrementaltestutil.FsHandlingBuildInfo
9599
defaultLibraryPath string
96100
cwd string
97101
files []string
@@ -114,18 +118,22 @@ func (s *testSys) FS() vfs.FS {
114118
return s.fs
115119
}
116120

117-
func (s *testSys) TestFS() *incrementaltestutil.FsHandlingBuildInfo {
118-
return s.fs.FS.(*incrementaltestutil.FsHandlingBuildInfo)
121+
func (s *testSys) testFs() *testFs {
122+
return s.fs.FS.(*testFs)
123+
}
124+
125+
func (s *testSys) fsFromFileMap() vfs.FS {
126+
return s.testFs().FS
119127
}
120128

121129
func (s *testSys) ensureLibPathExists(path string) {
122130
path = tscLibPath + "/" + path
123-
if _, ok := s.TestFS().ReadFile(path); !ok {
124-
if s.fs.defaultLibs == nil {
125-
s.fs.defaultLibs = &collections.SyncSet[string]{}
131+
if _, ok := s.fsFromFileMap().ReadFile(path); !ok {
132+
if s.testFs().defaultLibs == nil {
133+
s.testFs().defaultLibs = &collections.SyncSet[string]{}
126134
}
127-
s.fs.defaultLibs.Add(path)
128-
err := s.TestFS().WriteFile(path, tscDefaultLibContent, false)
135+
s.testFs().defaultLibs.Add(path)
136+
err := s.fsFromFileMap().WriteFile(path, tscDefaultLibContent, false)
129137
if err != nil {
130138
panic("Failed to write default library file: " + err.Error())
131139
}
@@ -223,14 +231,17 @@ func (s *testSys) baselineOutput(baseline io.Writer) {
223231
}
224232
// todo screen clears
225233
s.printOutputs(baseline)
234+
}
235+
236+
func (s *testSys) clearOutput() {
226237
s.output = []string{}
227238
}
228239

229240
func (s *testSys) baselineFSwithDiff(baseline io.Writer) {
230241
// todo: baselines the entire fs, possibly doesn't correctly diff all cases of emitted files, since emit isn't fully implemented and doesn't always emit the same way as strada
231242
snap := map[string]*diffEntry{}
232243

233-
err := s.FS().WalkDir("/", func(path string, d vfs.DirEntry, e error) error {
244+
err := s.fsFromFileMap().WalkDir("/", func(path string, d vfs.DirEntry, e error) error {
234245
if e != nil {
235246
return e
236247
}
@@ -239,7 +250,7 @@ func (s *testSys) baselineFSwithDiff(baseline io.Writer) {
239250
return nil
240251
}
241252

242-
newContents, ok := s.TestFS().InnerReadFile(path)
253+
newContents, ok := s.fsFromFileMap().ReadFile(path)
243254
if !ok {
244255
return nil
245256
}
@@ -258,16 +269,16 @@ func (s *testSys) baselineFSwithDiff(baseline io.Writer) {
258269
}
259270
if s.serializedDiff != nil {
260271
for path := range s.serializedDiff.snap {
261-
_, ok := s.TestFS().InnerReadFile(path)
272+
_, ok := s.fsFromFileMap().ReadFile(path)
262273
if !ok {
263274
// report deleted
264275
s.reportFSEntryDiff(baseline, nil, path)
265276
}
266277
}
267278
}
268279
var defaultLibs collections.SyncSet[string]
269-
if s.fs.defaultLibs != nil {
270-
s.fs.defaultLibs.Range(func(libPath string) bool {
280+
if s.testFs().defaultLibs != nil {
281+
s.testFs().defaultLibs.Range(func(libPath string) bool {
271282
defaultLibs.Add(libPath)
272283
return true
273284
})
@@ -288,7 +299,7 @@ func (s *testSys) reportFSEntryDiff(baseline io.Writer, newDirContent *diffEntry
288299
}
289300
// todo handle more cases of fs changes
290301
if oldDirContent == nil {
291-
if s.fs.defaultLibs == nil || !s.fs.defaultLibs.Has(path) {
302+
if s.testFs().defaultLibs == nil || !s.testFs().defaultLibs.Has(path) {
292303
fmt.Fprint(baseline, "//// [", path, "] *new* \n", newDirContent.content, "\n")
293304
}
294305
} else if newDirContent == nil {
@@ -297,7 +308,7 @@ func (s *testSys) reportFSEntryDiff(baseline io.Writer, newDirContent *diffEntry
297308
fmt.Fprint(baseline, "//// [", path, "] *modified* \n", newDirContent.content, "\n")
298309
} else if newDirContent.fileInfo.ModTime() != oldDirContent.fileInfo.ModTime() {
299310
fmt.Fprint(baseline, "//// [", path, "] *modified time*\n")
300-
} else if defaultLibs != nil && defaultLibs.Has(path) && s.fs.defaultLibs != nil && !s.fs.defaultLibs.Has(path) {
311+
} else if defaultLibs != nil && defaultLibs.Has(path) && s.testFs().defaultLibs != nil && !s.testFs().defaultLibs.Has(path) {
301312
// Lib file that was read
302313
fmt.Fprint(baseline, "//// [", path, "] *Lib*\n", newDirContent.content, "\n")
303314
}
@@ -308,33 +319,33 @@ func (s *testSys) printOutputs(baseline io.Writer) {
308319
fmt.Fprint(baseline, strings.Join(s.output, "\n"))
309320
}
310321

311-
func (s *testSys) WriteFileNoError(path string, content string, writeByteOrderMark bool) {
312-
if err := s.FS().WriteFile(path, content, writeByteOrderMark); err != nil {
322+
func (s *testSys) writeFileNoError(path string, content string, writeByteOrderMark bool) {
323+
if err := s.fsFromFileMap().WriteFile(path, content, writeByteOrderMark); err != nil {
313324
panic(err)
314325
}
315326
}
316327

317-
func (s *testSys) ReplaceFileText(path string, oldText string, newText string) {
318-
content, ok := s.FS().ReadFile(path)
328+
func (s *testSys) replaceFileText(path string, oldText string, newText string) {
329+
content, ok := s.fsFromFileMap().ReadFile(path)
319330
if !ok {
320331
panic("File not found: " + path)
321332
}
322333
content = strings.Replace(content, oldText, newText, 1)
323-
s.WriteFileNoError(path, content, false)
334+
s.writeFileNoError(path, content, false)
324335
}
325336

326-
func (s *testSys) AppendFile(path string, text string) {
327-
content, ok := s.FS().ReadFile(path)
337+
func (s *testSys) appendFile(path string, text string) {
338+
content, ok := s.fsFromFileMap().ReadFile(path)
328339
if !ok {
329340
panic("File not found: " + path)
330341
}
331-
s.WriteFileNoError(path, content+text, false)
342+
s.writeFileNoError(path, content+text, false)
332343
}
333344

334-
func (s *testSys) PrependFile(path string, text string) {
335-
content, ok := s.FS().ReadFile(path)
345+
func (s *testSys) prependFile(path string, text string) {
346+
content, ok := s.fsFromFileMap().ReadFile(path)
336347
if !ok {
337348
panic("File not found: " + path)
338349
}
339-
s.WriteFileNoError(path, text+content, false)
350+
s.writeFileNoError(path, text+content, false)
340351
}

0 commit comments

Comments
 (0)