Skip to content

Commit 4aec447

Browse files
committed
Merge pull request #4488 from Microsoft/fixRWC
Move RWC runner to use Harness.IO instead of sys
2 parents e047025 + 24a65f4 commit 4aec447

File tree

4 files changed

+73
-52
lines changed

4 files changed

+73
-52
lines changed

src/compiler/commandLineParser.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ namespace ts {
274274
return optionNameMapCache;
275275
}
276276

277-
export function parseCommandLine(commandLine: string[]): ParsedCommandLine {
277+
export function parseCommandLine(commandLine: string[], readFile?: (path: string) => string): ParsedCommandLine {
278278
let options: CompilerOptions = {};
279279
let fileNames: string[] = [];
280280
let errors: Diagnostic[] = [];
@@ -343,7 +343,7 @@ namespace ts {
343343
}
344344

345345
function parseResponseFile(fileName: string) {
346-
let text = sys.readFile(fileName);
346+
let text = readFile ? readFile(fileName) : sys.readFile(fileName);
347347

348348
if (!text) {
349349
errors.push(createCompilerDiagnostic(Diagnostics.File_0_not_found, fileName));
@@ -496,4 +496,4 @@ namespace ts {
496496
return fileNames;
497497
}
498498
}
499-
}
499+
}

src/harness/harness.ts

Lines changed: 31 additions & 9 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 {
@@ -1331,8 +1344,8 @@ module Harness {
13311344
export function getErrorBaseline(inputFiles: { unitName: string; content: string }[], diagnostics: ts.Diagnostic[]) {
13321345
diagnostics.sort(ts.compareDiagnostics);
13331346
let outputLines: string[] = [];
1334-
// Count up all the errors we find so we don't miss any
1335-
let totalErrorsReported = 0;
1347+
// Count up all errors that were found in files other than lib.d.ts so we don't miss any
1348+
let totalErrorsReportedInNonLibraryFiles = 0;
13361349

13371350
function outputErrorText(error: ts.Diagnostic) {
13381351
let message = ts.flattenDiagnosticMessageText(error.messageText, Harness.IO.newLine());
@@ -1343,8 +1356,15 @@ module Harness {
13431356
.filter(s => s.length > 0)
13441357
.map(s => "!!! " + ts.DiagnosticCategory[error.category].toLowerCase() + " TS" + error.code + ": " + s);
13451358
errLines.forEach(e => outputLines.push(e));
1346-
1347-
totalErrorsReported++;
1359+
1360+
// do not count errors from lib.d.ts here, they are computed separately as numLibraryDiagnostics
1361+
// if lib.d.ts is explicitly included in input files and there are some errors in it (i.e. because of duplicate identifiers)
1362+
// then they will be added twice thus triggering 'total errors' assertion with condition
1363+
// 'totalErrorsReportedInNonLibraryFiles + numLibraryDiagnostics + numTest262HarnessDiagnostics, diagnostics.length
1364+
1365+
if (!error.file || !isLibraryFile(error.file.fileName)) {
1366+
totalErrorsReportedInNonLibraryFiles++;
1367+
}
13481368
}
13491369

13501370
// Report global errors
@@ -1428,7 +1448,9 @@ module Harness {
14281448
return diagnostic.file && diagnostic.file.fileName.indexOf("test262-harness") >= 0;
14291449
});
14301450

1431-
assert.equal(totalErrorsReported + numLibraryDiagnostics + numTest262HarnessDiagnostics, diagnostics.length, "total number of errors");
1451+
// Verify we didn't miss any errors in total
1452+
assert.equal(totalErrorsReportedInNonLibraryFiles + numLibraryDiagnostics + numTest262HarnessDiagnostics, diagnostics.length, "total number of errors");
1453+
14321454
return minimalDiagnosticsToString(diagnostics) +
14331455
Harness.IO.newLine() + Harness.IO.newLine() + outputLines.join("\r\n");
14341456
}
@@ -1805,11 +1827,11 @@ module Harness {
18051827
return filePath.indexOf(Harness.libFolder) === 0;
18061828
}
18071829

1808-
export function getDefaultLibraryFile(): { unitName: string, content: string } {
1809-
let libFile = Harness.userSpecifiedRoot + Harness.libFolder + "/" + "lib.d.ts";
1830+
export function getDefaultLibraryFile(io: Harness.IO): { unitName: string, content: string } {
1831+
let libFile = Harness.userSpecifiedRoot + Harness.libFolder + "lib.d.ts";
18101832
return {
18111833
unitName: libFile,
1812-
content: IO.readFile(libFile)
1834+
content: io.readFile(libFile)
18131835
};
18141836
}
18151837

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: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,21 @@
44
/// <reference path="..\compiler\commandLineParser.ts"/>
55

66
module RWC {
7-
function runWithIOLog(ioLog: IOLog, fn: () => void) {
8-
let oldSys = ts.sys;
7+
function runWithIOLog(ioLog: IOLog, fn: (oldIO: Harness.IO) => void) {
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 {
15-
fn();
15+
fn(oldIO);
1616
} finally {
17-
wrappedSys.endReplay();
18-
ts.sys = oldSys;
17+
wrappedIO.endReplay();
18+
Harness.IO = oldIO;
1919
}
2020
}
2121

22-
let defaultLibPath = ts.sys.resolvePath("built/local/lib.d.ts");
23-
let defaultLib = {
24-
unitName: ts.normalizePath(defaultLibPath),
25-
content: Harness.IO.readFile(defaultLibPath)
26-
};
27-
2822
export function runRWCTest(jsonPath: string) {
2923
describe("Testing a RWC project: " + jsonPath, () => {
3024
let inputFiles: { unitName: string; content: string; }[] = [];
@@ -38,7 +32,6 @@ module RWC {
3832
let baseName = /(.*)\/(.*).json/.exec(ts.normalizeSlashes(jsonPath))[2];
3933
let currentDirectory: string;
4034
let useCustomLibraryFile: boolean;
41-
4235
after(() => {
4336
// Mocha holds onto the closure environment of the describe callback even after the test is done.
4437
// Therefore we have to clean out large objects after the test is done.
@@ -63,19 +56,15 @@ module RWC {
6356
currentDirectory = ioLog.currentDirectory;
6457
useCustomLibraryFile = ioLog.useCustomLibraryFile;
6558
runWithIOLog(ioLog, () => {
66-
opts = ts.parseCommandLine(ioLog.arguments);
59+
opts = ts.parseCommandLine(ioLog.arguments, fileName => Harness.IO.readFile(fileName));
6760
assert.equal(opts.errors.length, 0);
6861

6962
// To provide test coverage of output javascript file,
7063
// we will set noEmitOnError flag to be false.
7164
opts.options.noEmitOnError = false;
7265
});
7366

74-
if (!useCustomLibraryFile) {
75-
inputFiles.push(defaultLib);
76-
}
77-
78-
runWithIOLog(ioLog, () => {
67+
runWithIOLog(ioLog, oldIO => {
7968
harnessCompiler.reset();
8069

8170
// Load the files
@@ -87,7 +76,7 @@ module RWC {
8776
let isInInputList = (resolvedPath: string) => (inputFile: { unitName: string; content: string; }) => inputFile.unitName === resolvedPath;
8877
for (let fileRead of ioLog.filesRead) {
8978
// Check if the file is already added into the set of input files.
90-
const resolvedPath = ts.normalizeSlashes(ts.sys.resolvePath(fileRead.path));
79+
const resolvedPath = ts.normalizeSlashes(Harness.IO.resolvePath(fileRead.path));
9180
let inInputList = ts.forEach(inputFiles, isInInputList(resolvedPath));
9281

9382
if (!Harness.isLibraryFile(fileRead.path)) {
@@ -106,6 +95,10 @@ module RWC {
10695
if (useCustomLibraryFile) {
10796
inputFiles.push(getHarnessCompilerInputUnit(fileRead.path));
10897
}
98+
else {
99+
// set the flag to put default library to the beginning of the list
100+
inputFiles.unshift(Harness.getDefaultLibraryFile(oldIO));
101+
}
109102
}
110103
}
111104
}
@@ -125,13 +118,13 @@ module RWC {
125118
});
126119

127120
function getHarnessCompilerInputUnit(fileName: string) {
128-
let unitName = ts.normalizeSlashes(ts.sys.resolvePath(fileName));
121+
let unitName = ts.normalizeSlashes(Harness.IO.resolvePath(fileName));
129122
let content: string = null;
130123
try {
131-
content = ts.sys.readFile(unitName);
124+
content = Harness.IO.readFile(unitName);
132125
}
133126
catch (e) {
134-
content = ts.sys.readFile(fileName);
127+
content = Harness.IO.readFile(fileName);
135128
}
136129
return { unitName, content };
137130
}
@@ -194,7 +187,7 @@ module RWC {
194187
}
195188

196189
return Harness.Compiler.minimalDiagnosticsToString(declFileCompilationResult.declResult.errors) +
197-
ts.sys.newLine + ts.sys.newLine +
190+
Harness.IO.newLine() + Harness.IO.newLine() +
198191
Harness.Compiler.getErrorBaseline(declFileCompilationResult.declInputFiles.concat(declFileCompilationResult.declOtherFiles), declFileCompilationResult.declResult.errors);
199192
}, false, baselineOpts);
200193
}

0 commit comments

Comments
 (0)