mirror of
https://github.com/microsoft/TypeScript.git
synced 2025-11-18 17:21:48 +00:00
Add wrapSystem function
This commit is contained in:
+87
-85
@@ -95,6 +95,8 @@ module Playback {
|
||||
|
||||
export interface PlaybackIO extends Harness.IO, PlaybackControl { }
|
||||
|
||||
export interface PlaybackSystem extends ts.System, PlaybackControl { }
|
||||
|
||||
function createEmptyLog(): IOLog {
|
||||
return {
|
||||
timestamp: (new Date()).toString(),
|
||||
@@ -114,8 +116,10 @@ module Playback {
|
||||
};
|
||||
}
|
||||
|
||||
function initWrapper<T>(wrapper: PlaybackControl, underlying: T) {
|
||||
Object.keys(underlying).forEach(prop => {
|
||||
function initWrapper(wrapper: PlaybackSystem, underlying: ts.System): void;
|
||||
function initWrapper(wrapper: PlaybackIO, underlying: Harness.IO): void;
|
||||
function initWrapper(wrapper: PlaybackSystem | PlaybackIO, underlying: ts.System | Harness.IO): void {
|
||||
ts.forEach(Object.keys(underlying), prop => {
|
||||
(<any>wrapper)[prop] = (<any>underlying)[prop];
|
||||
});
|
||||
|
||||
@@ -135,6 +139,79 @@ module Playback {
|
||||
wrapper.startRecord = (fileNameBase) => {
|
||||
recordLogFileNameBase = fileNameBase;
|
||||
recordLog = createEmptyLog();
|
||||
|
||||
if (underlying.args !== undefined && typeof underlying.args !== "function") {
|
||||
recordLog.arguments = <string[]>underlying.args;
|
||||
}
|
||||
};
|
||||
|
||||
wrapper.startReplayFromFile = logFn => {
|
||||
wrapper.startReplayFromString(underlying.readFile(logFn));
|
||||
};
|
||||
wrapper.endRecord = () => {
|
||||
if (recordLog !== undefined) {
|
||||
let i = 0;
|
||||
let fn = () => recordLogFileNameBase + i + ".json";
|
||||
while (underlying.fileExists(fn())) i++;
|
||||
underlying.writeFile(fn(), JSON.stringify(recordLog));
|
||||
recordLog = undefined;
|
||||
}
|
||||
};
|
||||
|
||||
wrapper.fileExists = recordReplay(wrapper.fileExists, underlying)(
|
||||
(path) => callAndRecord(underlying.fileExists(path), recordLog.fileExists, { path: path }),
|
||||
memoize((path) => {
|
||||
// If we read from the file, it must exist
|
||||
if (findResultByPath(wrapper, replayLog.filesRead, path, null) !== null) {
|
||||
return true;
|
||||
} else {
|
||||
return findResultByFields(replayLog.fileExists, { path: path }, false);
|
||||
}
|
||||
})
|
||||
);
|
||||
|
||||
wrapper.getExecutingFilePath = () => {
|
||||
if (replayLog !== undefined) {
|
||||
return replayLog.executingPath;
|
||||
} else if (recordLog !== undefined) {
|
||||
return recordLog.executingPath = underlying.getExecutingFilePath();
|
||||
} else {
|
||||
return underlying.getExecutingFilePath();
|
||||
}
|
||||
};
|
||||
|
||||
wrapper.getCurrentDirectory = () => {
|
||||
if (replayLog !== undefined) {
|
||||
return replayLog.currentDirectory || "";
|
||||
} else if (recordLog !== undefined) {
|
||||
return recordLog.currentDirectory = underlying.getCurrentDirectory();
|
||||
} else {
|
||||
return underlying.getCurrentDirectory();
|
||||
}
|
||||
};
|
||||
|
||||
wrapper.resolvePath = recordReplay(wrapper.resolvePath, underlying)(
|
||||
(path) => callAndRecord(underlying.resolvePath(path), recordLog.pathsResolved, { path: path }),
|
||||
memoize((path) => findResultByFields(replayLog.pathsResolved, { path: path }, !ts.isRootedDiskPath(ts.normalizeSlashes(path)) && replayLog.currentDirectory ? replayLog.currentDirectory + "/" + path : ts.normalizeSlashes(path))));
|
||||
|
||||
wrapper.readFile = recordReplay(wrapper.readFile, underlying)(
|
||||
(path) => {
|
||||
let result = underlying.readFile(path);
|
||||
let logEntry = { path: path, codepage: 0, result: { contents: result, codepage: 0 } };
|
||||
recordLog.filesRead.push(logEntry);
|
||||
return result;
|
||||
},
|
||||
memoize((path) => findResultByPath(wrapper, replayLog.filesRead, path).contents));
|
||||
|
||||
wrapper.writeFile = recordReplay(wrapper.writeFile, underlying)(
|
||||
(path, contents) => callAndRecord(underlying.writeFile(path, contents), recordLog.filesWritten, { path: path, contents: contents, bom: false }),
|
||||
(path, contents) => noOpReplay("writeFile"));
|
||||
|
||||
wrapper.exit = (exitCode) => {
|
||||
if (recordLog !== undefined) {
|
||||
wrapper.endRecord();
|
||||
}
|
||||
underlying.exit(exitCode);
|
||||
};
|
||||
}
|
||||
|
||||
@@ -227,93 +304,18 @@ module Playback {
|
||||
let wrapper: PlaybackIO = <any>{};
|
||||
initWrapper(wrapper, underlying);
|
||||
|
||||
wrapper.startReplayFromFile = logFn => {
|
||||
wrapper.startReplayFromString(underlying.readFile(logFn));
|
||||
};
|
||||
wrapper.endRecord = () => {
|
||||
if (recordLog !== undefined) {
|
||||
let i = 0;
|
||||
let fn = () => recordLogFileNameBase + i + ".json";
|
||||
while (underlying.fileExists(fn())) i++;
|
||||
underlying.writeFile(fn(), JSON.stringify(recordLog));
|
||||
recordLog = undefined;
|
||||
}
|
||||
};
|
||||
|
||||
wrapper.args = () => {
|
||||
if (replayLog !== undefined) {
|
||||
return replayLog.arguments;
|
||||
} else if (recordLog !== undefined) {
|
||||
recordLog.arguments = underlying.args();
|
||||
}
|
||||
return underlying.args();
|
||||
}
|
||||
|
||||
wrapper.newLine = () => underlying.newLine();
|
||||
wrapper.useCaseSensitiveFileNames = () => underlying.useCaseSensitiveFileNames();
|
||||
wrapper.directoryName = (path): string => { throw new Error("NotSupported"); };
|
||||
wrapper.createDirectory = path => { throw new Error("NotSupported"); };
|
||||
wrapper.createDirectory = (path): void => { throw new Error("NotSupported"); };
|
||||
wrapper.directoryExists = (path): boolean => { throw new Error("NotSupported"); };
|
||||
wrapper.deleteFile = path => { throw new Error("NotSupported"); };
|
||||
wrapper.deleteFile = (path): void => { throw new Error("NotSupported"); };
|
||||
wrapper.listFiles = (path, filter, options): string[] => { throw new Error("NotSupported"); };
|
||||
wrapper.log = text => underlying.log(text);
|
||||
|
||||
wrapper.fileExists = recordReplay(wrapper.fileExists, underlying)(
|
||||
(path) => callAndRecord(underlying.fileExists(path), recordLog.fileExists, { path: path }),
|
||||
memoize((path) => {
|
||||
// If we read from the file, it must exist
|
||||
if (findResultByPath(wrapper, replayLog.filesRead, path, null) !== null) {
|
||||
return true;
|
||||
} else {
|
||||
return findResultByFields(replayLog.fileExists, { path: path }, false);
|
||||
}
|
||||
})
|
||||
);
|
||||
|
||||
wrapper.getExecutingFilePath = () => {
|
||||
if (replayLog !== undefined) {
|
||||
return replayLog.executingPath;
|
||||
} else if (recordLog !== undefined) {
|
||||
return recordLog.executingPath = underlying.getExecutingFilePath();
|
||||
} else {
|
||||
return underlying.getExecutingFilePath();
|
||||
}
|
||||
};
|
||||
|
||||
wrapper.getCurrentDirectory = () => {
|
||||
if (replayLog !== undefined) {
|
||||
return replayLog.currentDirectory || "";
|
||||
} else if (recordLog !== undefined) {
|
||||
return recordLog.currentDirectory = underlying.getCurrentDirectory();
|
||||
} else {
|
||||
return underlying.getCurrentDirectory();
|
||||
}
|
||||
};
|
||||
|
||||
wrapper.resolvePath = recordReplay(wrapper.resolvePath, underlying)(
|
||||
(path) => callAndRecord(underlying.resolvePath(path), recordLog.pathsResolved, { path: path }),
|
||||
memoize((path) => findResultByFields(replayLog.pathsResolved, { path: path }, !ts.isRootedDiskPath(ts.normalizeSlashes(path)) && replayLog.currentDirectory ? replayLog.currentDirectory + "/" + path : ts.normalizeSlashes(path))));
|
||||
|
||||
wrapper.readFile = recordReplay(wrapper.readFile, underlying)(
|
||||
(path) => {
|
||||
let result = underlying.readFile(path);
|
||||
let logEntry = { path: path, codepage: 0, result: { contents: result, codepage: 0 } };
|
||||
recordLog.filesRead.push(logEntry);
|
||||
return result;
|
||||
},
|
||||
memoize((path) => findResultByPath(wrapper, replayLog.filesRead, path).contents));
|
||||
|
||||
wrapper.writeFile = recordReplay(wrapper.writeFile, underlying)(
|
||||
(path, contents) => callAndRecord(underlying.writeFile(path, contents), recordLog.filesWritten, { path: path, contents: contents, bom: false }),
|
||||
(path, contents) => noOpReplay("writeFile"));
|
||||
|
||||
wrapper.exit = (exitCode) => {
|
||||
if (recordLog !== undefined) {
|
||||
wrapper.endRecord();
|
||||
}
|
||||
underlying.exit(exitCode);
|
||||
};
|
||||
|
||||
return wrapper;
|
||||
}
|
||||
|
||||
export function wrapSystem(underlying: ts.System): PlaybackSystem {
|
||||
let wrapper: PlaybackSystem = <any>{};
|
||||
initWrapper(wrapper, underlying);
|
||||
return wrapper;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user