Move RWC runner to use Harness.IO

This commit is contained in:
Vladimir Matveev 2015-08-26 18:54:25 -07:00
parent 1d94653798
commit af2a49992f
4 changed files with 60 additions and 33 deletions

View File

@ -237,13 +237,13 @@ namespace ts {
}
];
export function parseCommandLine(commandLine: string[]): ParsedCommandLine {
export function parseCommandLine(commandLine: string[], readFile?: (fileName: string) => string): ParsedCommandLine {
let options: CompilerOptions = {};
let fileNames: string[] = [];
let errors: Diagnostic[] = [];
let shortOptionNames: Map<string> = {};
let optionNameMap: Map<CommandLineOption> = {};
forEach(optionDeclarations, option => {
optionNameMap[option.name.toLowerCase()] = option;
if (option.shortName) {
@ -313,7 +313,7 @@ namespace ts {
}
function parseResponseFile(fileName: string) {
let text = sys.readFile(fileName);
let text = readFile ? readFile(fileName): sys.readFile(fileName);
if (!text) {
errors.push(createCompilerDiagnostic(Diagnostics.File_0_not_found, fileName));

View File

@ -425,6 +425,9 @@ module Harness {
listFiles(path: string, filter: RegExp, options?: { recursive?: boolean }): string[];
log(text: string): void;
getMemoryUsage?(): number;
args(): string[];
getExecutingFilePath(): string;
exit(exitCode?: number): void;
}
export var IO: IO;
@ -446,7 +449,10 @@ module Harness {
} else {
fso = {};
}
export const args = () => ts.sys.args;
export const getExecutingFilePath = () => ts.sys.getExecutingFilePath();
export const exit = (exitCode: number) => ts.sys.exit(exitCode);
export const resolvePath = (path: string) => ts.sys.resolvePath(path);
export const getCurrentDirectory = () => ts.sys.getCurrentDirectory();
export const newLine = () => harnessNewLine;
@ -517,6 +523,9 @@ module Harness {
export const getCurrentDirectory = () => ts.sys.getCurrentDirectory();
export const newLine = () => harnessNewLine;
export const useCaseSensitiveFileNames = () => ts.sys.useCaseSensitiveFileNames;
export const args = () => ts.sys.args;
export const getExecutingFilePath = () => ts.sys.getExecutingFilePath();
export const exit = (exitCode: number) => ts.sys.exit(exitCode);
export const readFile: typeof IO.readFile = path => ts.sys.readFile(path);
export const writeFile: typeof IO.writeFile = (path, content) => ts.sys.writeFile(path, content);
@ -589,6 +598,10 @@ module Harness {
export const newLine = () => harnessNewLine;
export const useCaseSensitiveFileNames = () => false;
export const getCurrentDirectory = () => "";
export const args = () => <string[]>[];
export const getExecutingFilePath = () => "";
export const exit = (exitCode: number) => {};
let supportsCodePage = () => false;
module Http {
@ -1804,7 +1817,7 @@ module Harness {
}
export function getDefaultLibraryFile(): { unitName: string, content: string } {
let libFile = Harness.userSpecifiedRoot + Harness.libFolder + "/" + "lib.d.ts";
let libFile = Harness.userSpecifiedRoot + Harness.libFolder + "lib.d.ts";
return {
unitName: libFile,
content: IO.readFile(libFile)

View File

@ -93,7 +93,7 @@ module Playback {
return run;
}
export interface PlaybackSystem extends ts.System, PlaybackControl { }
export interface PlaybackIO extends Harness.IO, PlaybackControl { }
function createEmptyLog(): IOLog {
return {
@ -223,8 +223,8 @@ module Playback {
// console.log("Swallowed write operation during replay: " + name);
}
export function wrapSystem(underlying: ts.System): PlaybackSystem {
let wrapper: PlaybackSystem = <any>{};
export function wrapIO(underlying: Harness.IO): PlaybackIO {
let wrapper: PlaybackIO = <any>{};
initWrapper(wrapper, underlying);
wrapper.startReplayFromFile = logFn => {
@ -239,18 +239,24 @@ module Playback {
recordLog = undefined;
}
};
Object.defineProperty(wrapper, "args", {
get() {
if (replayLog !== undefined) {
return replayLog.arguments;
} else if (recordLog !== undefined) {
recordLog.arguments = underlying.args;
}
return underlying.args;
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.directoryExists = (path): boolean => { throw new Error("NotSupported"); };
wrapper.deleteFile = path => { 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 }),

View File

@ -5,17 +5,17 @@
module RWC {
function runWithIOLog(ioLog: IOLog, fn: () => void) {
let oldSys = ts.sys;
let oldIO = Harness.IO;
let wrappedSys = Playback.wrapSystem(ts.sys);
wrappedSys.startReplayFromData(ioLog);
ts.sys = wrappedSys;
let wrappedIO = Playback.wrapIO(oldIO);
wrappedIO.startReplayFromData(ioLog);
Harness.IO = wrappedIO;
try {
fn();
} finally {
wrappedSys.endReplay();
ts.sys = oldSys;
wrappedIO.endReplay();
Harness.IO = oldIO;
}
}
@ -32,7 +32,9 @@ module RWC {
let baseName = /(.*)\/(.*).json/.exec(ts.normalizeSlashes(jsonPath))[2];
let currentDirectory: string;
let useCustomLibraryFile: boolean;
const defaultLibraryFile = Harness.getDefaultLibraryFile();
after(() => {
// Mocha holds onto the closure environment of the describe callback even after the test is done.
// Therefore we have to clean out large objects after the test is done.
@ -57,7 +59,7 @@ module RWC {
currentDirectory = ioLog.currentDirectory;
useCustomLibraryFile = ioLog.useCustomLibraryFile;
runWithIOLog(ioLog, () => {
opts = ts.parseCommandLine(ioLog.arguments);
opts = ts.parseCommandLine(ioLog.arguments, fileName => Harness.IO.readFile(fileName));
assert.equal(opts.errors.length, 0);
// To provide test coverage of output javascript file,
@ -75,9 +77,10 @@ module RWC {
// Add files to compilation
let isInInputList = (resolvedPath: string) => (inputFile: { unitName: string; content: string; }) => inputFile.unitName === resolvedPath;
let prependDefaultLib = false;
for (let fileRead of ioLog.filesRead) {
// Check if the file is already added into the set of input files.
const resolvedPath = ts.normalizeSlashes(ts.sys.resolvePath(fileRead.path));
const resolvedPath = ts.normalizeSlashes(Harness.IO.resolvePath(fileRead.path));
let inInputList = ts.forEach(inputFiles, isInInputList(resolvedPath));
if (!Harness.isLibraryFile(fileRead.path)) {
@ -97,11 +100,16 @@ module RWC {
inputFiles.push(getHarnessCompilerInputUnit(fileRead.path));
}
else {
inputFiles.push(Harness.getDefaultLibraryFile());
// set the flag to put default library to the beginning of the list
prependDefaultLib = true;
}
}
}
}
if (prependDefaultLib) {
inputFiles.unshift(defaultLibraryFile);
}
// do not use lib since we already read it in above
opts.options.noLib = true;
@ -118,13 +126,13 @@ module RWC {
});
function getHarnessCompilerInputUnit(fileName: string) {
let unitName = ts.normalizeSlashes(ts.sys.resolvePath(fileName));
let unitName = ts.normalizeSlashes(Harness.IO.resolvePath(fileName));
let content: string = null;
try {
content = ts.sys.readFile(unitName);
content = Harness.IO.readFile(unitName);
}
catch (e) {
content = ts.sys.readFile(fileName);
content = Harness.IO.readFile(fileName);
}
return { unitName, content };
}
@ -187,7 +195,7 @@ module RWC {
}
return Harness.Compiler.minimalDiagnosticsToString(declFileCompilationResult.declResult.errors) +
ts.sys.newLine + ts.sys.newLine +
Harness.IO.newLine() + Harness.IO.newLine() +
Harness.Compiler.getErrorBaseline(declFileCompilationResult.declInputFiles.concat(declFileCompilationResult.declOtherFiles), declFileCompilationResult.declResult.errors);
}, false, baselineOpts);
}