Skip to content

Commit ea1512e

Browse files
committed
Merge pull request #4394 from Microsoft/browserifyFix
use Harness.IO instead of sys in harness
2 parents 65f4ac6 + 64fd41d commit ea1512e

File tree

10 files changed

+109
-80
lines changed

10 files changed

+109
-80
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,5 @@
4949
"fs": false,
5050
"os": false,
5151
"path": false
52-
}
52+
}
5353
}

src/harness/fourslash.ts

+14-14
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ module FourSlash {
377377
this.formatCodeOptions = {
378378
IndentSize: 4,
379379
TabSize: 4,
380-
NewLineCharacter: ts.sys.newLine,
380+
NewLineCharacter: Harness.IO.newLine(),
381381
ConvertTabsToSpaces: true,
382382
InsertSpaceAfterCommaDelimiter: true,
383383
InsertSpaceAfterSemicolonInForStatements: true,
@@ -555,7 +555,7 @@ module FourSlash {
555555
errors.forEach(function (error: ts.Diagnostic) {
556556
Harness.IO.log(" minChar: " + error.start +
557557
", limChar: " + (error.start + error.length) +
558-
", message: " + ts.flattenDiagnosticMessageText(error.messageText, ts.sys.newLine) + "\n");
558+
", message: " + ts.flattenDiagnosticMessageText(error.messageText, Harness.IO.newLine()) + "\n");
559559
});
560560
}
561561

@@ -1266,21 +1266,21 @@ module FourSlash {
12661266
emitFiles.forEach(emitFile => {
12671267
let emitOutput = this.languageService.getEmitOutput(emitFile.fileName);
12681268
// Print emitOutputStatus in readable format
1269-
resultString += "EmitSkipped: " + emitOutput.emitSkipped + ts.sys.newLine;
1269+
resultString += "EmitSkipped: " + emitOutput.emitSkipped + Harness.IO.newLine();
12701270

12711271
if (emitOutput.emitSkipped) {
1272-
resultString += "Diagnostics:" + ts.sys.newLine;
1272+
resultString += "Diagnostics:" + Harness.IO.newLine();
12731273
let diagnostics = ts.getPreEmitDiagnostics(this.languageService.getProgram());
12741274
for (let i = 0, n = diagnostics.length; i < n; i++) {
1275-
resultString += " " + diagnostics[0].messageText + ts.sys.newLine;
1275+
resultString += " " + diagnostics[0].messageText + Harness.IO.newLine();
12761276
}
12771277
}
12781278

12791279
emitOutput.outputFiles.forEach((outputFile, idx, array) => {
1280-
let fileName = "FileName : " + outputFile.name + ts.sys.newLine;
1280+
let fileName = "FileName : " + outputFile.name + Harness.IO.newLine();
12811281
resultString = resultString + fileName + outputFile.text;
12821282
});
1283-
resultString += ts.sys.newLine;
1283+
resultString += Harness.IO.newLine();
12841284
});
12851285

12861286
return resultString;
@@ -1317,7 +1317,7 @@ module FourSlash {
13171317
Harness.IO.log(
13181318
"start: " + err.start +
13191319
", length: " + err.length +
1320-
", message: " + ts.flattenDiagnosticMessageText(err.messageText, ts.sys.newLine));
1320+
", message: " + ts.flattenDiagnosticMessageText(err.messageText, Harness.IO.newLine()));
13211321
});
13221322
}
13231323
}
@@ -1891,9 +1891,9 @@ module FourSlash {
18911891
}
18921892

18931893
function jsonMismatchString() {
1894-
return ts.sys.newLine +
1895-
"expected: '" + ts.sys.newLine + JSON.stringify(expected, (k, v) => v, 2) + "'" + ts.sys.newLine +
1896-
"actual: '" + ts.sys.newLine + JSON.stringify(actual, (k, v) => v, 2) + "'";
1894+
return Harness.IO.newLine() +
1895+
"expected: '" + Harness.IO.newLine() + JSON.stringify(expected, (k, v) => v, 2) + "'" + Harness.IO.newLine() +
1896+
"actual: '" + Harness.IO.newLine() + JSON.stringify(actual, (k, v) => v, 2) + "'";
18971897
}
18981898
}
18991899

@@ -2402,7 +2402,7 @@ module FourSlash {
24022402
let host = Harness.Compiler.createCompilerHost([{ unitName: Harness.Compiler.fourslashFileName, content: undefined }],
24032403
(fn, contents) => fourslashJsOutput = contents,
24042404
ts.ScriptTarget.Latest,
2405-
ts.sys.useCaseSensitiveFileNames);
2405+
Harness.IO.useCaseSensitiveFileNames());
24062406

24072407
let program = ts.createProgram([Harness.Compiler.fourslashFileName], { noResolve: true, target: ts.ScriptTarget.ES3 }, host);
24082408

@@ -2424,7 +2424,7 @@ module FourSlash {
24242424
],
24252425
(fn, contents) => result = contents,
24262426
ts.ScriptTarget.Latest,
2427-
ts.sys.useCaseSensitiveFileNames);
2427+
Harness.IO.useCaseSensitiveFileNames());
24282428

24292429
let program = ts.createProgram([Harness.Compiler.fourslashFileName, fileName], { outFile: "fourslashTestOutput.js", noResolve: true, target: ts.ScriptTarget.ES3 }, host);
24302430

@@ -2433,7 +2433,7 @@ module FourSlash {
24332433
let diagnostics = ts.getPreEmitDiagnostics(program, sourceFile);
24342434
if (diagnostics.length > 0) {
24352435
throw new Error(`Error compiling ${fileName}: ` +
2436-
diagnostics.map(e => ts.flattenDiagnosticMessageText(e.messageText, ts.sys.newLine)).join("\r\n"));
2436+
diagnostics.map(e => ts.flattenDiagnosticMessageText(e.messageText, Harness.IO.newLine())).join("\r\n"));
24372437
}
24382438

24392439
program.emit(sourceFile);

src/harness/harness.ts

+55-31
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626

2727
// Block scoped definitions work poorly for global variables, temporarily enable var
2828
/* tslint:disable:no-var-keyword */
29-
var Buffer: BufferConstructor = require("buffer").Buffer;
3029

3130
// this will work in the browser via browserify
3231
var _chai: typeof chai = require("chai");
@@ -55,9 +54,17 @@ module Utils {
5554
return ExecutionEnvironment.Node;
5655
}
5756
}
58-
57+
5958
export let currentExecutionEnvironment = getExecutionEnvironment();
6059

60+
const Buffer: BufferConstructor = currentExecutionEnvironment !== ExecutionEnvironment.Browser
61+
? require("buffer").Buffer
62+
: undefined;
63+
64+
export function encodeString(s: string): string {
65+
return Buffer ? (new Buffer(s)).toString("utf8") : s;
66+
}
67+
6168
export function evalFile(fileContents: string, fileName: string, nodeContext?: any) {
6269
let environment = getExecutionEnvironment();
6370
switch (environment) {
@@ -102,7 +109,7 @@ module Utils {
102109

103110
let content: string = undefined;
104111
try {
105-
content = ts.sys.readFile(Harness.userSpecifiedRoot + path);
112+
content = Harness.IO.readFile(Harness.userSpecifiedRoot + path);
106113
}
107114
catch (err) {
108115
return undefined;
@@ -190,7 +197,7 @@ module Utils {
190197
return {
191198
start: diagnostic.start,
192199
length: diagnostic.length,
193-
messageText: ts.flattenDiagnosticMessageText(diagnostic.messageText, ts.sys.newLine),
200+
messageText: ts.flattenDiagnosticMessageText(diagnostic.messageText, Harness.IO.newLine()),
194201
category: (<any>ts).DiagnosticCategory[diagnostic.category],
195202
code: diagnostic.code
196203
};
@@ -323,8 +330,8 @@ module Utils {
323330
assert.equal(d1.start, d2.start, "d1.start !== d2.start");
324331
assert.equal(d1.length, d2.length, "d1.length !== d2.length");
325332
assert.equal(
326-
ts.flattenDiagnosticMessageText(d1.messageText, ts.sys.newLine),
327-
ts.flattenDiagnosticMessageText(d2.messageText, ts.sys.newLine), "d1.messageText !== d2.messageText");
333+
ts.flattenDiagnosticMessageText(d1.messageText, Harness.IO.newLine()),
334+
ts.flattenDiagnosticMessageText(d2.messageText, Harness.IO.newLine()), "d1.messageText !== d2.messageText");
328335
assert.equal(d1.category, d2.category, "d1.category !== d2.category");
329336
assert.equal(d1.code, d2.code, "d1.code !== d2.code");
330337
}
@@ -404,6 +411,10 @@ module Harness.Path {
404411

405412
module Harness {
406413
export interface IO {
414+
newLine(): string;
415+
getCurrentDirectory(): string;
416+
useCaseSensitiveFileNames(): boolean;
417+
resolvePath(path: string): string;
407418
readFile(path: string): string;
408419
writeFile(path: string, contents: string): void;
409420
directoryName(path: string): string;
@@ -416,7 +427,10 @@ module Harness {
416427
getMemoryUsage?(): number;
417428
}
418429
export var IO: IO;
419-
430+
431+
// harness always uses one kind of new line
432+
const harnessNewLine = "\r\n";
433+
420434
module IOImpl {
421435
declare class Enumerator {
422436
public atEnd(): boolean;
@@ -433,12 +447,17 @@ module Harness {
433447
fso = {};
434448
}
435449

436-
export let readFile: typeof IO.readFile = ts.sys.readFile;
437-
export let writeFile: typeof IO.writeFile = ts.sys.writeFile;
438-
export let directoryName: typeof IO.directoryName = fso.GetParentFolderName;
439-
export let directoryExists: typeof IO.directoryExists = fso.FolderExists;
440-
export let fileExists: typeof IO.fileExists = fso.FileExists;
441-
export let log: typeof IO.log = global.WScript && global.WScript.StdOut.WriteLine;
450+
export const resolvePath = (path: string) => ts.sys.resolvePath(path);
451+
export const getCurrentDirectory = () => ts.sys.getCurrentDirectory();
452+
export const newLine = () => harnessNewLine;
453+
export const useCaseSensitiveFileNames = () => ts.sys.useCaseSensitiveFileNames;
454+
455+
export const readFile: typeof IO.readFile = path => ts.sys.readFile(path);
456+
export const writeFile: typeof IO.writeFile = (path, content) => ts.sys.writeFile(path, content);
457+
export const directoryName: typeof IO.directoryName = fso.GetParentFolderName;
458+
export const directoryExists: typeof IO.directoryExists = fso.FolderExists;
459+
export const fileExists: typeof IO.fileExists = fso.FileExists;
460+
export const log: typeof IO.log = global.WScript && global.WScript.StdOut.WriteLine;
442461

443462
export function createDirectory(path: string) {
444463
if (directoryExists(path)) {
@@ -493,11 +512,16 @@ module Harness {
493512
} else {
494513
fs = pathModule = {};
495514
}
515+
516+
export const resolvePath = (path: string) => ts.sys.resolvePath(path);
517+
export const getCurrentDirectory = () => ts.sys.getCurrentDirectory();
518+
export const newLine = () => harnessNewLine;
519+
export const useCaseSensitiveFileNames = () => ts.sys.useCaseSensitiveFileNames;
496520

497-
export let readFile: typeof IO.readFile = ts.sys.readFile;
498-
export let writeFile: typeof IO.writeFile = ts.sys.writeFile;
499-
export let fileExists: typeof IO.fileExists = fs.existsSync;
500-
export let log: typeof IO.log = s => console.log(s);
521+
export const readFile: typeof IO.readFile = path => ts.sys.readFile(path);
522+
export const writeFile: typeof IO.writeFile = (path, content) => ts.sys.writeFile(path, content);
523+
export const fileExists: typeof IO.fileExists = fs.existsSync;
524+
export const log: typeof IO.log = s => console.log(s);
501525

502526
export function createDirectory(path: string) {
503527
if (!directoryExists(path)) {
@@ -562,9 +586,9 @@ module Harness {
562586
export module Network {
563587
let serverRoot = "http://localhost:8888/";
564588

565-
// Unused?
566-
let newLine = "\r\n";
567-
let currentDirectory = () => "";
589+
export const newLine = () => harnessNewLine;
590+
export const useCaseSensitiveFileNames = () => false;
591+
export const getCurrentDirectory = () => "";
568592
let supportsCodePage = () => false;
569593

570594
module Http {
@@ -616,6 +640,7 @@ module Harness {
616640
xhr.send(contents);
617641
}
618642
catch (e) {
643+
log(`XHR Error: ${e}`);
619644
return { status: 500, responseText: null };
620645
}
621646

@@ -655,6 +680,7 @@ module Harness {
655680
return dirPath;
656681
}
657682
export let directoryName: typeof IO.directoryName = Utils.memoize(directoryNameImpl);
683+
export const resolvePath = (path: string) => directoryName(path);
658684

659685
export function fileExists(path: string): boolean {
660686
let response = Http.getFileFromServerSync(serverRoot + path);
@@ -840,7 +866,7 @@ module Harness {
840866
export let fourslashSourceFile: ts.SourceFile;
841867

842868
export function getCanonicalFileName(fileName: string): string {
843-
return ts.sys.useCaseSensitiveFileNames ? fileName : fileName.toLowerCase();
869+
return Harness.IO.useCaseSensitiveFileNames() ? fileName : fileName.toLowerCase();
844870
}
845871

846872
export function createCompilerHost(
@@ -858,7 +884,7 @@ module Harness {
858884
}
859885

860886
let filemap: { [fileName: string]: ts.SourceFile; } = {};
861-
let getCurrentDirectory = currentDirectory === undefined ? ts.sys.getCurrentDirectory : () => currentDirectory;
887+
let getCurrentDirectory = currentDirectory === undefined ? Harness.IO.getCurrentDirectory : () => currentDirectory;
862888

863889
// Register input files
864890
function register(file: { unitName: string; content: string; }) {
@@ -895,7 +921,7 @@ module Harness {
895921
let newLine =
896922
newLineKind === ts.NewLineKind.CarriageReturnLineFeed ? carriageReturnLineFeed :
897923
newLineKind === ts.NewLineKind.LineFeed ? lineFeed :
898-
ts.sys.newLine;
924+
Harness.IO.newLine();
899925

900926
return {
901927
getCurrentDirectory,
@@ -988,7 +1014,7 @@ module Harness {
9881014
// Treat them as library files, so include them in build, but not in baselines.
9891015
let includeBuiltFiles: { unitName: string; content: string }[] = [];
9901016

991-
let useCaseSensitiveFileNames = ts.sys.useCaseSensitiveFileNames;
1017+
let useCaseSensitiveFileNames = Harness.IO.useCaseSensitiveFileNames();
9921018
this.settings.forEach(setCompilerOptionForSetting);
9931019

9941020
let fileOutputs: GeneratedFile[] = [];
@@ -1006,11 +1032,9 @@ module Harness {
10061032
let errors = ts.getPreEmitDiagnostics(program).concat(emitResult.diagnostics);
10071033
this.lastErrors = errors;
10081034

1009-
let result = new CompilerResult(fileOutputs, errors, program, ts.sys.getCurrentDirectory(), emitResult.sourceMaps);
1035+
let result = new CompilerResult(fileOutputs, errors, program, Harness.IO.getCurrentDirectory(), emitResult.sourceMaps);
10101036
onComplete(result, program);
10111037

1012-
// reset what newline means in case the last test changed it
1013-
ts.sys.newLine = newLine;
10141038
return options;
10151039

10161040
function setCompilerOptionForSetting(setting: Harness.TestCaseParser.CompilerSetting) {
@@ -1278,7 +1302,7 @@ module Harness {
12781302
errorOutput += diagnostic.file.fileName + "(" + (lineAndCharacter.line + 1) + "," + (lineAndCharacter.character + 1) + "): ";
12791303
}
12801304

1281-
errorOutput += ts.DiagnosticCategory[diagnostic.category].toLowerCase() + " TS" + diagnostic.code + ": " + ts.flattenDiagnosticMessageText(diagnostic.messageText, ts.sys.newLine) + ts.sys.newLine;
1305+
errorOutput += ts.DiagnosticCategory[diagnostic.category].toLowerCase() + " TS" + diagnostic.code + ": " + ts.flattenDiagnosticMessageText(diagnostic.messageText, Harness.IO.newLine()) + Harness.IO.newLine();
12821306
});
12831307

12841308
return errorOutput;
@@ -1291,7 +1315,7 @@ module Harness {
12911315
let totalErrorsReported = 0;
12921316

12931317
function outputErrorText(error: ts.Diagnostic) {
1294-
let message = ts.flattenDiagnosticMessageText(error.messageText, ts.sys.newLine);
1318+
let message = ts.flattenDiagnosticMessageText(error.messageText, Harness.IO.newLine());
12951319

12961320
let errLines = RunnerBase.removeFullPaths(message)
12971321
.split("\n")
@@ -1388,7 +1412,7 @@ module Harness {
13881412
assert.equal(totalErrorsReported + numLibraryDiagnostics + numTest262HarnessDiagnostics, diagnostics.length, "total number of errors");
13891413

13901414
return minimalDiagnosticsToString(diagnostics) +
1391-
ts.sys.newLine + ts.sys.newLine + outputLines.join("\r\n");
1415+
Harness.IO.newLine() + Harness.IO.newLine() + outputLines.join("\r\n");
13921416
}
13931417

13941418
export function collateOutputs(outputFiles: Harness.Compiler.GeneratedFile[]): string {
@@ -1724,7 +1748,7 @@ module Harness {
17241748
}
17251749

17261750
function writeComparison(expected: string, actual: string, relativeFileName: string, actualFileName: string, descriptionForDescribe: string) {
1727-
let encoded_actual = (new Buffer(actual)).toString("utf8");
1751+
let encoded_actual = Utils.encodeString(actual);
17281752
if (expected != encoded_actual) {
17291753
// Overwrite & issue error
17301754
let errMsg = "The baseline file " + relativeFileName + " has changed";

0 commit comments

Comments
 (0)