@@ -8,7 +8,12 @@ interface FileInformation {
8
8
}
9
9
10
10
interface FindFileResult {
11
+ }
11
12
13
+ interface IOLogFile {
14
+ path : string ;
15
+ codepage : number ;
16
+ result ?: FileInformation ;
12
17
}
13
18
14
19
interface IOLog {
@@ -17,11 +22,7 @@ interface IOLog {
17
22
executingPath : string ;
18
23
currentDirectory : string ;
19
24
useCustomLibraryFile ?: boolean ;
20
- filesRead : {
21
- path : string ;
22
- codepage : number ;
23
- result ?: FileInformation ;
24
- } [ ] ;
25
+ filesRead : IOLogFile [ ] ;
25
26
filesWritten : {
26
27
path : string ;
27
28
contents : string ;
@@ -61,7 +62,7 @@ interface IOLog {
61
62
} [ ] ;
62
63
directoriesRead : {
63
64
path : string ,
64
- extension : string [ ] ,
65
+ extensions : string [ ] ,
65
66
exclude : string [ ] ,
66
67
include : string [ ] ,
67
68
result : string [ ]
@@ -170,8 +171,7 @@ namespace Playback {
170
171
path => callAndRecord ( underlying . fileExists ( path ) , recordLog . fileExists , { path } ) ,
171
172
memoize ( path => {
172
173
// If we read from the file, it must exist
173
- const noResult = { } ;
174
- if ( findResultByPath ( wrapper , replayLog . filesRead , path , noResult ) !== noResult ) {
174
+ if ( findFileByPath ( wrapper , replayLog . filesRead , path , /*throwFileNotFoundError*/ false ) ) {
175
175
return true ;
176
176
}
177
177
else {
@@ -215,16 +215,30 @@ namespace Playback {
215
215
recordLog . filesRead . push ( logEntry ) ;
216
216
return result ;
217
217
} ,
218
- memoize ( path => findResultByPath ( wrapper , replayLog . filesRead , path ) . contents ) ) ;
218
+ memoize ( path => findFileByPath ( wrapper , replayLog . filesRead , path , /*throwFileNotFoundError*/ true ) . contents ) ) ;
219
219
220
220
wrapper . readDirectory = recordReplay ( wrapper . readDirectory , underlying ) (
221
- ( path , extension , exclude , include ) => {
222
- const result = ( < ts . System > underlying ) . readDirectory ( path , extension , exclude , include ) ;
223
- const logEntry = { path, extension , exclude, include, result } ;
221
+ ( path , extensions , exclude , include ) => {
222
+ const result = ( < ts . System > underlying ) . readDirectory ( path , extensions , exclude , include ) ;
223
+ const logEntry = { path, extensions , exclude, include, result } ;
224
224
recordLog . directoriesRead . push ( logEntry ) ;
225
225
return result ;
226
226
} ,
227
- ( path , extension , exclude ) => findResultByPath ( wrapper , replayLog . directoriesRead , path ) ) ;
227
+ ( path , extensions , exclude ) => {
228
+ // Because extensions is an array of all allowed extension, we will want to merge each of the replayLog.directoriesRead into one
229
+ // if each of the directoriesRead has matched path with the given path (directory with same path but different extension will considered
230
+ // different entry).
231
+ // TODO (yuisu): We can certainly remove these once we recapture the RWC using new API
232
+ const normalizedPath = ts . normalizePath ( path ) . toLowerCase ( ) ;
233
+ const result : string [ ] = [ ] ;
234
+ for ( const directory of replayLog . directoriesRead ) {
235
+ if ( ts . normalizeSlashes ( directory . path ) . toLowerCase ( ) === normalizedPath ) {
236
+ result . push ( ...directory . result ) ;
237
+ }
238
+ }
239
+
240
+ return result ;
241
+ } ) ;
228
242
229
243
wrapper . writeFile = recordReplay ( wrapper . writeFile , underlying ) (
230
244
( path : string , contents : string ) => callAndRecord ( underlying . writeFile ( path , contents ) , recordLog . filesWritten , { path, contents, bom : false } ) ,
@@ -279,30 +293,22 @@ namespace Playback {
279
293
return results [ 0 ] . result ;
280
294
}
281
295
282
- function findResultByPath < T > ( wrapper : { resolvePath ( s : string ) : string } , logArray : { path : string ; result ?: T } [ ] , expectedPath : string , defaultValue ?: T ) : T {
296
+ function findFileByPath ( wrapper : { resolvePath ( s : string ) : string } , logArray : IOLogFile [ ] ,
297
+ expectedPath : string , throwFileNotFoundError : boolean ) : FileInformation {
283
298
const normalizedName = ts . normalizePath ( expectedPath ) . toLowerCase ( ) ;
284
299
// Try to find the result through normal fileName
285
- for ( let i = 0 ; i < logArray . length ; i ++ ) {
286
- if ( ts . normalizeSlashes ( logArray [ i ] . path ) . toLowerCase ( ) === normalizedName ) {
287
- return logArray [ i ] . result ;
288
- }
289
- }
290
- // Fallback, try to resolve the target paths as well
291
- if ( replayLog . pathsResolved . length > 0 ) {
292
- const normalizedResolvedName = wrapper . resolvePath ( expectedPath ) . toLowerCase ( ) ;
293
- for ( let i = 0 ; i < logArray . length ; i ++ ) {
294
- if ( wrapper . resolvePath ( logArray [ i ] . path ) . toLowerCase ( ) === normalizedResolvedName ) {
295
- return logArray [ i ] . result ;
296
- }
300
+ for ( const log of logArray ) {
301
+ if ( ts . normalizeSlashes ( log . path ) . toLowerCase ( ) === normalizedName ) {
302
+ return log . result ;
297
303
}
298
304
}
299
305
300
306
// If we got here, we didn't find a match
301
- if ( defaultValue === undefined ) {
307
+ if ( throwFileNotFoundError ) {
302
308
throw new Error ( "No matching result in log array for path: " + expectedPath ) ;
303
309
}
304
310
else {
305
- return defaultValue ;
311
+ return undefined ;
306
312
}
307
313
}
308
314
0 commit comments