Skip to content

Commit c2730ce

Browse files
authored
Fix reading files from IOLog because previous our API captures (#9483)
* Fix reading files from IOLog because previous our API captures * Refactoring the ioLog
1 parent 19c141a commit c2730ce

File tree

1 file changed

+34
-28
lines changed

1 file changed

+34
-28
lines changed

src/harness/loggedIO.ts

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@ interface FileInformation {
88
}
99

1010
interface FindFileResult {
11+
}
1112

13+
interface IOLogFile {
14+
path: string;
15+
codepage: number;
16+
result?: FileInformation;
1217
}
1318

1419
interface IOLog {
@@ -17,11 +22,7 @@ interface IOLog {
1722
executingPath: string;
1823
currentDirectory: string;
1924
useCustomLibraryFile?: boolean;
20-
filesRead: {
21-
path: string;
22-
codepage: number;
23-
result?: FileInformation;
24-
}[];
25+
filesRead: IOLogFile[];
2526
filesWritten: {
2627
path: string;
2728
contents: string;
@@ -61,7 +62,7 @@ interface IOLog {
6162
}[];
6263
directoriesRead: {
6364
path: string,
64-
extension: string[],
65+
extensions: string[],
6566
exclude: string[],
6667
include: string[],
6768
result: string[]
@@ -170,8 +171,7 @@ namespace Playback {
170171
path => callAndRecord(underlying.fileExists(path), recordLog.fileExists, { path }),
171172
memoize(path => {
172173
// 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)) {
175175
return true;
176176
}
177177
else {
@@ -215,16 +215,30 @@ namespace Playback {
215215
recordLog.filesRead.push(logEntry);
216216
return result;
217217
},
218-
memoize(path => findResultByPath(wrapper, replayLog.filesRead, path).contents));
218+
memoize(path => findFileByPath(wrapper, replayLog.filesRead, path, /*throwFileNotFoundError*/ true).contents));
219219

220220
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 };
224224
recordLog.directoriesRead.push(logEntry);
225225
return result;
226226
},
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+
});
228242

229243
wrapper.writeFile = recordReplay(wrapper.writeFile, underlying)(
230244
(path: string, contents: string) => callAndRecord(underlying.writeFile(path, contents), recordLog.filesWritten, { path, contents, bom: false }),
@@ -279,30 +293,22 @@ namespace Playback {
279293
return results[0].result;
280294
}
281295

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 {
283298
const normalizedName = ts.normalizePath(expectedPath).toLowerCase();
284299
// 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;
297303
}
298304
}
299305

300306
// If we got here, we didn't find a match
301-
if (defaultValue === undefined) {
307+
if (throwFileNotFoundError) {
302308
throw new Error("No matching result in log array for path: " + expectedPath);
303309
}
304310
else {
305-
return defaultValue;
311+
return undefined;
306312
}
307313
}
308314

0 commit comments

Comments
 (0)