@@ -10,48 +10,90 @@ import (
10
10
"strings"
11
11
"time"
12
12
13
- "github.com/microsoft/typescript-go/internal/bundled "
13
+ "github.com/microsoft/typescript-go/internal/collections "
14
14
"github.com/microsoft/typescript-go/internal/testutil/incrementaltestutil"
15
+ "github.com/microsoft/typescript-go/internal/tsoptions"
15
16
"github.com/microsoft/typescript-go/internal/vfs"
16
17
"github.com/microsoft/typescript-go/internal/vfs/vfstest"
17
18
)
18
19
19
- type FileMap map [string ]string
20
+ type FileMap map [string ]any
21
+
22
+ var (
23
+ tscLibPath = "/home/src/tslibs/TS/Lib"
24
+ tscDefaultLibContent = `/// <reference no-default-lib="true"/>
25
+ interface Boolean {}
26
+ interface Function {}
27
+ interface CallableFunction {}
28
+ interface NewableFunction {}
29
+ interface IArguments {}
30
+ interface Number { toExponential: any; }
31
+ interface Object {}
32
+ interface RegExp {}
33
+ interface String { charAt: any; }
34
+ interface Array<T> { length: number; [n: number]: T; }
35
+ interface ReadonlyArray<T> {}
36
+ interface SymbolConstructor {
37
+ (desc?: string | number): symbol;
38
+ for(name: string): symbol;
39
+ readonly toStringTag: symbol;
40
+ }
41
+ declare var Symbol: SymbolConstructor;
42
+ interface Symbol {
43
+ readonly [Symbol.toStringTag]: string;
44
+ }
45
+ declare const console: { log(msg: any): void; };`
46
+ )
20
47
21
48
func newTestSys (fileOrFolderList FileMap , cwd string ) * testSys {
22
49
if cwd == "" {
23
50
cwd = "/home/src/workspaces/project"
24
51
}
25
- return & testSys {
26
- fs : incrementaltestutil .NewFsHandlingBuildInfo ( bundled . WrapFS (vfstest .FromMap (fileOrFolderList , true /*useCaseSensitiveFileNames*/ ))),
27
- defaultLibraryPath : bundled . LibPath () ,
52
+ sys := & testSys {
53
+ fs : NewFSTrackingLibs ( incrementaltestutil .NewFsHandlingBuildInfo (vfstest .FromMap (fileOrFolderList , true /*useCaseSensitiveFileNames*/ ))),
54
+ defaultLibraryPath : tscLibPath ,
28
55
cwd : cwd ,
29
56
files : slices .Collect (maps .Keys (fileOrFolderList )),
30
57
output : []string {},
31
58
currentWrite : & strings.Builder {},
32
59
start : time .Now (),
33
60
}
61
+
62
+ // Ensure the default library file is present
63
+ sys .ensureLibPathExists ("lib.d.ts" )
64
+ for _ , libFile := range tsoptions .TargetToLibMap () {
65
+ sys .ensureLibPathExists (libFile )
66
+ }
67
+ for libFile := range tsoptions .LibFilesSet .Keys () {
68
+ sys .ensureLibPathExists (libFile )
69
+ }
70
+ return sys
71
+ }
72
+
73
+ type diffEntry struct {
74
+ content string
75
+ fileInfo fs.FileInfo
76
+ }
77
+
78
+ type snapshot struct {
79
+ snap map [string ]* diffEntry
80
+ defaultLibs * collections.Set [string ]
34
81
}
35
82
36
83
type testSys struct {
37
84
// todo: original has write to output as a string[] because the separations are needed for baselining
38
85
output []string
39
86
currentWrite * strings.Builder
40
- serializedDiff map [ string ] string
87
+ serializedDiff * snapshot
41
88
42
- fs * incrementaltestutil. FsHandlingBuildInfo
89
+ fs * testFsTrackingLibs
43
90
defaultLibraryPath string
44
91
cwd string
45
92
files []string
46
93
47
94
start time.Time
48
95
}
49
96
50
- func (s * testSys ) IsTestDone () bool {
51
- // todo: test is done if there are no edits left. Edits are not yet implemented
52
- return true
53
- }
54
-
55
97
func (s * testSys ) Now () time.Time {
56
98
// todo: make a "test time" structure
57
99
return time .Now ()
@@ -66,7 +108,18 @@ func (s *testSys) FS() vfs.FS {
66
108
}
67
109
68
110
func (s * testSys ) TestFS () * incrementaltestutil.FsHandlingBuildInfo {
69
- return s .fs
111
+ return s .fs .fs .(* incrementaltestutil.FsHandlingBuildInfo )
112
+ }
113
+
114
+ func (s * testSys ) ensureLibPathExists (path string ) {
115
+ path = tscLibPath + "/" + path
116
+ if _ , ok := s .TestFS ().ReadFile (path ); ! ok {
117
+ if s .fs .defaultLibs == nil {
118
+ s .fs .defaultLibs = collections.NewSetWithSizeHint [string ](tsoptions .LibFilesSet .Len () + len (tsoptions .TargetToLibMap ()) + 1 )
119
+ }
120
+ s .fs .defaultLibs .Add (path )
121
+ s .TestFS ().WriteFile (path , tscDefaultLibContent , false )
122
+ }
70
123
}
71
124
72
125
func (s * testSys ) DefaultLibraryPath () string {
@@ -115,7 +168,7 @@ func (s *testSys) baselineOutput(baseline io.Writer) {
115
168
116
169
func (s * testSys ) baselineFSwithDiff (baseline io.Writer ) {
117
170
// 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
118
- snap := map [string ]string {}
171
+ snap := map [string ]* diffEntry {}
119
172
120
173
err := s .FS ().WalkDir ("/" , func (path string , d vfs.DirEntry , e error ) error {
121
174
if e != nil {
@@ -130,37 +183,62 @@ func (s *testSys) baselineFSwithDiff(baseline io.Writer) {
130
183
if ! ok {
131
184
return nil
132
185
}
133
- snap [path ] = newContents
134
- reportFSEntryDiff (baseline , s .serializedDiff [path ], newContents , path )
186
+ fileInfo , err := d .Info ()
187
+ if err != nil {
188
+ return nil
189
+ }
190
+ newEntry := & diffEntry {content : newContents , fileInfo : fileInfo }
191
+ snap [path ] = newEntry
192
+ s .reportFSEntryDiff (baseline , newEntry , path )
135
193
136
194
return nil
137
195
})
138
196
if err != nil && ! errors .Is (err , fs .ErrNotExist ) {
139
197
panic ("walkdir error during diff: " + err .Error ())
140
198
}
141
- for path , oldDirContents := range s .serializedDiff {
142
- if s .FS ().FileExists (path ) {
143
- _ , ok := s .FS ().ReadFile (path )
144
- if ! ok {
145
- // report deleted
146
- reportFSEntryDiff (baseline , oldDirContents , "" , path )
199
+ if s .serializedDiff != nil {
200
+ for path := range s .serializedDiff .snap {
201
+ if s .FS ().FileExists (path ) {
202
+ _ , ok := s .TestFS ().FS ().ReadFile (path )
203
+ if ! ok {
204
+ // report deleted
205
+ s .reportFSEntryDiff (baseline , nil , path )
206
+ }
147
207
}
148
208
}
149
209
}
150
- s .serializedDiff = snap
210
+ var defaultLibs * collections.Set [string ]
211
+ if s .fs .defaultLibs != nil {
212
+ defaultLibs = s .fs .defaultLibs .Clone ()
213
+ }
214
+ s .serializedDiff = & snapshot {
215
+ snap : snap ,
216
+ defaultLibs : defaultLibs ,
217
+ }
151
218
fmt .Fprintln (baseline )
152
219
}
153
220
154
- func reportFSEntryDiff (baseline io.Writer , oldDirContent string , newDirContent string , path string ) {
221
+ func (s * testSys ) reportFSEntryDiff (baseline io.Writer , newDirContent * diffEntry , path string ) {
222
+ var oldDirContent * diffEntry
223
+ var defaultLibs * collections.Set [string ]
224
+ if s .serializedDiff != nil {
225
+ oldDirContent = s .serializedDiff .snap [path ]
226
+ defaultLibs = s .serializedDiff .defaultLibs
227
+ }
155
228
// todo handle more cases of fs changes
156
- if oldDirContent == "" {
157
- fmt .Fprint (baseline , "//// [" , path , "] new file\n " , newDirContent , "\n " )
158
- } else if newDirContent == "" {
159
- fmt .Fprint (baseline , "//// [" , path , "] deleted\n " )
160
- } else if newDirContent == oldDirContent {
161
- fmt .Fprint (baseline , "//// [" , path , "] no change\n " )
162
- } else {
163
- fmt .Fprint (baseline , "//// [" , path , "] modified. new content:\n " , newDirContent , "\n " )
229
+ if oldDirContent == nil {
230
+ if s .fs .defaultLibs == nil || ! s .fs .defaultLibs .Has (path ) {
231
+ fmt .Fprint (baseline , "//// [" , path , "] *new* \n " , newDirContent .content , "\n " )
232
+ }
233
+ } else if newDirContent == nil {
234
+ fmt .Fprint (baseline , "//// [" , path , "] *deleted*\n " )
235
+ } else if newDirContent .content != oldDirContent .content {
236
+ fmt .Fprint (baseline , "//// [" , path , "] *modified* \n " , newDirContent , "\n " )
237
+ } else if newDirContent .fileInfo .ModTime () != oldDirContent .fileInfo .ModTime () {
238
+ fmt .Fprint (baseline , "//// [" , path , "] *modified time*\n " )
239
+ } else if defaultLibs != nil && defaultLibs .Has (path ) && s .fs .defaultLibs != nil && ! s .fs .defaultLibs .Has (path ) {
240
+ // Lib file that was read
241
+ fmt .Fprint (baseline , "//// [" , path , "] *Lib*\n " , newDirContent .content , "\n " )
164
242
}
165
243
}
166
244
0 commit comments