Skip to content

Commit 961ab06

Browse files
committed
Incremental correctness
1 parent f4d57f5 commit 961ab06

11 files changed

+365
-269
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
}
@@ -219,14 +227,17 @@ func (s *testSys) baselineOutput(baseline io.Writer) {
219227
}
220228
// todo screen clears
221229
s.printOutputs(baseline)
230+
}
231+
232+
func (s *testSys) clearOutput() {
222233
s.output = []string{}
223234
}
224235

225236
func (s *testSys) baselineFSwithDiff(baseline io.Writer) {
226237
// 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
227238
snap := map[string]*diffEntry{}
228239

229-
err := s.FS().WalkDir("/", func(path string, d vfs.DirEntry, e error) error {
240+
err := s.fsFromFileMap().WalkDir("/", func(path string, d vfs.DirEntry, e error) error {
230241
if e != nil {
231242
return e
232243
}
@@ -235,7 +246,7 @@ func (s *testSys) baselineFSwithDiff(baseline io.Writer) {
235246
return nil
236247
}
237248

238-
newContents, ok := s.TestFS().InnerReadFile(path)
249+
newContents, ok := s.fsFromFileMap().ReadFile(path)
239250
if !ok {
240251
return nil
241252
}
@@ -254,16 +265,16 @@ func (s *testSys) baselineFSwithDiff(baseline io.Writer) {
254265
}
255266
if s.serializedDiff != nil {
256267
for path := range s.serializedDiff.snap {
257-
_, ok := s.TestFS().InnerReadFile(path)
268+
_, ok := s.fsFromFileMap().ReadFile(path)
258269
if !ok {
259270
// report deleted
260271
s.reportFSEntryDiff(baseline, nil, path)
261272
}
262273
}
263274
}
264275
var defaultLibs collections.SyncSet[string]
265-
if s.fs.defaultLibs != nil {
266-
s.fs.defaultLibs.Range(func(libPath string) bool {
276+
if s.testFs().defaultLibs != nil {
277+
s.testFs().defaultLibs.Range(func(libPath string) bool {
267278
defaultLibs.Add(libPath)
268279
return true
269280
})
@@ -284,7 +295,7 @@ func (s *testSys) reportFSEntryDiff(baseline io.Writer, newDirContent *diffEntry
284295
}
285296
// todo handle more cases of fs changes
286297
if oldDirContent == nil {
287-
if s.fs.defaultLibs == nil || !s.fs.defaultLibs.Has(path) {
298+
if s.testFs().defaultLibs == nil || !s.testFs().defaultLibs.Has(path) {
288299
fmt.Fprint(baseline, "//// [", path, "] *new* \n", newDirContent.content, "\n")
289300
}
290301
} else if newDirContent == nil {
@@ -293,7 +304,7 @@ func (s *testSys) reportFSEntryDiff(baseline io.Writer, newDirContent *diffEntry
293304
fmt.Fprint(baseline, "//// [", path, "] *modified* \n", newDirContent.content, "\n")
294305
} else if newDirContent.fileInfo.ModTime() != oldDirContent.fileInfo.ModTime() {
295306
fmt.Fprint(baseline, "//// [", path, "] *modified time*\n")
296-
} else if defaultLibs != nil && defaultLibs.Has(path) && s.fs.defaultLibs != nil && !s.fs.defaultLibs.Has(path) {
307+
} else if defaultLibs != nil && defaultLibs.Has(path) && s.testFs().defaultLibs != nil && !s.testFs().defaultLibs.Has(path) {
297308
// Lib file that was read
298309
fmt.Fprint(baseline, "//// [", path, "] *Lib*\n", newDirContent.content, "\n")
299310
}
@@ -304,33 +315,33 @@ func (s *testSys) printOutputs(baseline io.Writer) {
304315
fmt.Fprint(baseline, strings.Join(s.output, "\n"))
305316
}
306317

307-
func (s *testSys) WriteFileNoError(path string, content string, writeByteOrderMark bool) {
308-
if err := s.FS().WriteFile(path, content, writeByteOrderMark); err != nil {
318+
func (s *testSys) writeFileNoError(path string, content string, writeByteOrderMark bool) {
319+
if err := s.fsFromFileMap().WriteFile(path, content, writeByteOrderMark); err != nil {
309320
panic(err)
310321
}
311322
}
312323

313-
func (s *testSys) ReplaceFileText(path string, oldText string, newText string) {
314-
content, ok := s.FS().ReadFile(path)
324+
func (s *testSys) replaceFileText(path string, oldText string, newText string) {
325+
content, ok := s.fsFromFileMap().ReadFile(path)
315326
if !ok {
316327
panic("File not found: " + path)
317328
}
318329
content = strings.Replace(content, oldText, newText, 1)
319-
s.WriteFileNoError(path, content, false)
330+
s.writeFileNoError(path, content, false)
320331
}
321332

322-
func (s *testSys) AppendFile(path string, text string) {
323-
content, ok := s.FS().ReadFile(path)
333+
func (s *testSys) appendFile(path string, text string) {
334+
content, ok := s.fsFromFileMap().ReadFile(path)
324335
if !ok {
325336
panic("File not found: " + path)
326337
}
327-
s.WriteFileNoError(path, content+text, false)
338+
s.writeFileNoError(path, content+text, false)
328339
}
329340

330-
func (s *testSys) PrependFile(path string, text string) {
331-
content, ok := s.FS().ReadFile(path)
341+
func (s *testSys) prependFile(path string, text string) {
342+
content, ok := s.fsFromFileMap().ReadFile(path)
332343
if !ok {
333344
panic("File not found: " + path)
334345
}
335-
s.WriteFileNoError(path, text+content, false)
346+
s.writeFileNoError(path, text+content, false)
336347
}

0 commit comments

Comments
 (0)