Skip to content

Commit af2a499

Browse files
committed
Move RWC runner to use Harness.IO
1 parent 1d94653 commit af2a499

File tree

4 files changed

+60
-33
lines changed

4 files changed

+60
-33
lines changed

src/compiler/commandLineParser.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,13 +237,13 @@ namespace ts {
237237
}
238238
];
239239

240-
export function parseCommandLine(commandLine: string[]): ParsedCommandLine {
240+
export function parseCommandLine(commandLine: string[], readFile?: (fileName: string) => string): ParsedCommandLine {
241241
let options: CompilerOptions = {};
242242
let fileNames: string[] = [];
243243
let errors: Diagnostic[] = [];
244244
let shortOptionNames: Map<string> = {};
245245
let optionNameMap: Map<CommandLineOption> = {};
246-
246+
247247
forEach(optionDeclarations, option => {
248248
optionNameMap[option.name.toLowerCase()] = option;
249249
if (option.shortName) {
@@ -313,7 +313,7 @@ namespace ts {
313313
}
314314

315315
function parseResponseFile(fileName: string) {
316-
let text = sys.readFile(fileName);
316+
let text = readFile ? readFile(fileName): sys.readFile(fileName);
317317

318318
if (!text) {
319319
errors.push(createCompilerDiagnostic(Diagnostics.File_0_not_found, fileName));

src/harness/harness.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,9 @@ module Harness {
425425
listFiles(path: string, filter: RegExp, options?: { recursive?: boolean }): string[];
426426
log(text: string): void;
427427
getMemoryUsage?(): number;
428+
args(): string[];
429+
getExecutingFilePath(): string;
430+
exit(exitCode?: number): void;
428431
}
429432
export var IO: IO;
430433

@@ -446,7 +449,10 @@ module Harness {
446449
} else {
447450
fso = {};
448451
}
449-
452+
453+
export const args = () => ts.sys.args;
454+
export const getExecutingFilePath = () => ts.sys.getExecutingFilePath();
455+
export const exit = (exitCode: number) => ts.sys.exit(exitCode);
450456
export const resolvePath = (path: string) => ts.sys.resolvePath(path);
451457
export const getCurrentDirectory = () => ts.sys.getCurrentDirectory();
452458
export const newLine = () => harnessNewLine;
@@ -517,6 +523,9 @@ module Harness {
517523
export const getCurrentDirectory = () => ts.sys.getCurrentDirectory();
518524
export const newLine = () => harnessNewLine;
519525
export const useCaseSensitiveFileNames = () => ts.sys.useCaseSensitiveFileNames;
526+
export const args = () => ts.sys.args;
527+
export const getExecutingFilePath = () => ts.sys.getExecutingFilePath();
528+
export const exit = (exitCode: number) => ts.sys.exit(exitCode);
520529

521530
export const readFile: typeof IO.readFile = path => ts.sys.readFile(path);
522531
export const writeFile: typeof IO.writeFile = (path, content) => ts.sys.writeFile(path, content);
@@ -589,6 +598,10 @@ module Harness {
589598
export const newLine = () => harnessNewLine;
590599
export const useCaseSensitiveFileNames = () => false;
591600
export const getCurrentDirectory = () => "";
601+
export const args = () => <string[]>[];
602+
export const getExecutingFilePath = () => "";
603+
export const exit = (exitCode: number) => {};
604+
592605
let supportsCodePage = () => false;
593606

594607
module Http {
@@ -1804,7 +1817,7 @@ module Harness {
18041817
}
18051818

18061819
export function getDefaultLibraryFile(): { unitName: string, content: string } {
1807-
let libFile = Harness.userSpecifiedRoot + Harness.libFolder + "/" + "lib.d.ts";
1820+
let libFile = Harness.userSpecifiedRoot + Harness.libFolder + "lib.d.ts";
18081821
return {
18091822
unitName: libFile,
18101823
content: IO.readFile(libFile)

src/harness/loggedIO.ts

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ module Playback {
9393
return run;
9494
}
9595

96-
export interface PlaybackSystem extends ts.System, PlaybackControl { }
96+
export interface PlaybackIO extends Harness.IO, PlaybackControl { }
9797

9898
function createEmptyLog(): IOLog {
9999
return {
@@ -223,8 +223,8 @@ module Playback {
223223
// console.log("Swallowed write operation during replay: " + name);
224224
}
225225

226-
export function wrapSystem(underlying: ts.System): PlaybackSystem {
227-
let wrapper: PlaybackSystem = <any>{};
226+
export function wrapIO(underlying: Harness.IO): PlaybackIO {
227+
let wrapper: PlaybackIO = <any>{};
228228
initWrapper(wrapper, underlying);
229229

230230
wrapper.startReplayFromFile = logFn => {
@@ -239,18 +239,24 @@ module Playback {
239239
recordLog = undefined;
240240
}
241241
};
242-
243-
Object.defineProperty(wrapper, "args", {
244-
get() {
245-
if (replayLog !== undefined) {
246-
return replayLog.arguments;
247-
} else if (recordLog !== undefined) {
248-
recordLog.arguments = underlying.args;
249-
}
250-
return underlying.args;
242+
243+
wrapper.args = () => {
244+
if (replayLog !== undefined) {
245+
return replayLog.arguments;
246+
} else if (recordLog !== undefined) {
247+
recordLog.arguments = underlying.args();
251248
}
252-
});
253-
249+
return underlying.args();
250+
}
251+
252+
wrapper.newLine = () => underlying.newLine();
253+
wrapper.useCaseSensitiveFileNames = () => underlying.useCaseSensitiveFileNames();
254+
wrapper.directoryName = (path): string => { throw new Error("NotSupported"); };
255+
wrapper.createDirectory = path => { throw new Error("NotSupported"); };
256+
wrapper.directoryExists = (path): boolean => { throw new Error("NotSupported"); };
257+
wrapper.deleteFile = path => { throw new Error("NotSupported"); };
258+
wrapper.listFiles = (path, filter, options): string[] => { throw new Error("NotSupported"); };
259+
wrapper.log = text => underlying.log(text);
254260

255261
wrapper.fileExists = recordReplay(wrapper.fileExists, underlying)(
256262
(path) => callAndRecord(underlying.fileExists(path), recordLog.fileExists, { path: path }),

src/harness/rwcRunner.ts

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@
55

66
module RWC {
77
function runWithIOLog(ioLog: IOLog, fn: () => void) {
8-
let oldSys = ts.sys;
8+
let oldIO = Harness.IO;
99

10-
let wrappedSys = Playback.wrapSystem(ts.sys);
11-
wrappedSys.startReplayFromData(ioLog);
12-
ts.sys = wrappedSys;
10+
let wrappedIO = Playback.wrapIO(oldIO);
11+
wrappedIO.startReplayFromData(ioLog);
12+
Harness.IO = wrappedIO;
1313

1414
try {
1515
fn();
1616
} finally {
17-
wrappedSys.endReplay();
18-
ts.sys = oldSys;
17+
wrappedIO.endReplay();
18+
Harness.IO = oldIO;
1919
}
2020
}
2121

@@ -32,7 +32,9 @@ module RWC {
3232
let baseName = /(.*)\/(.*).json/.exec(ts.normalizeSlashes(jsonPath))[2];
3333
let currentDirectory: string;
3434
let useCustomLibraryFile: boolean;
35-
35+
36+
const defaultLibraryFile = Harness.getDefaultLibraryFile();
37+
3638
after(() => {
3739
// Mocha holds onto the closure environment of the describe callback even after the test is done.
3840
// Therefore we have to clean out large objects after the test is done.
@@ -57,7 +59,7 @@ module RWC {
5759
currentDirectory = ioLog.currentDirectory;
5860
useCustomLibraryFile = ioLog.useCustomLibraryFile;
5961
runWithIOLog(ioLog, () => {
60-
opts = ts.parseCommandLine(ioLog.arguments);
62+
opts = ts.parseCommandLine(ioLog.arguments, fileName => Harness.IO.readFile(fileName));
6163
assert.equal(opts.errors.length, 0);
6264

6365
// To provide test coverage of output javascript file,
@@ -75,9 +77,10 @@ module RWC {
7577

7678
// Add files to compilation
7779
let isInInputList = (resolvedPath: string) => (inputFile: { unitName: string; content: string; }) => inputFile.unitName === resolvedPath;
80+
let prependDefaultLib = false;
7881
for (let fileRead of ioLog.filesRead) {
7982
// Check if the file is already added into the set of input files.
80-
const resolvedPath = ts.normalizeSlashes(ts.sys.resolvePath(fileRead.path));
83+
const resolvedPath = ts.normalizeSlashes(Harness.IO.resolvePath(fileRead.path));
8184
let inInputList = ts.forEach(inputFiles, isInInputList(resolvedPath));
8285

8386
if (!Harness.isLibraryFile(fileRead.path)) {
@@ -97,11 +100,16 @@ module RWC {
97100
inputFiles.push(getHarnessCompilerInputUnit(fileRead.path));
98101
}
99102
else {
100-
inputFiles.push(Harness.getDefaultLibraryFile());
103+
// set the flag to put default library to the beginning of the list
104+
prependDefaultLib = true;
101105
}
102106
}
103107
}
104108
}
109+
110+
if (prependDefaultLib) {
111+
inputFiles.unshift(defaultLibraryFile);
112+
}
105113

106114
// do not use lib since we already read it in above
107115
opts.options.noLib = true;
@@ -118,13 +126,13 @@ module RWC {
118126
});
119127

120128
function getHarnessCompilerInputUnit(fileName: string) {
121-
let unitName = ts.normalizeSlashes(ts.sys.resolvePath(fileName));
129+
let unitName = ts.normalizeSlashes(Harness.IO.resolvePath(fileName));
122130
let content: string = null;
123131
try {
124-
content = ts.sys.readFile(unitName);
132+
content = Harness.IO.readFile(unitName);
125133
}
126134
catch (e) {
127-
content = ts.sys.readFile(fileName);
135+
content = Harness.IO.readFile(fileName);
128136
}
129137
return { unitName, content };
130138
}
@@ -187,7 +195,7 @@ module RWC {
187195
}
188196

189197
return Harness.Compiler.minimalDiagnosticsToString(declFileCompilationResult.declResult.errors) +
190-
ts.sys.newLine + ts.sys.newLine +
198+
Harness.IO.newLine() + Harness.IO.newLine() +
191199
Harness.Compiler.getErrorBaseline(declFileCompilationResult.declInputFiles.concat(declFileCompilationResult.declOtherFiles), declFileCompilationResult.declResult.errors);
192200
}, false, baselineOpts);
193201
}

0 commit comments

Comments
 (0)