Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,5 @@
"fs": false,
"os": false,
"path": false
}
}
}
28 changes: 14 additions & 14 deletions src/harness/fourslash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ module FourSlash {
this.formatCodeOptions = {
IndentSize: 4,
TabSize: 4,
NewLineCharacter: ts.sys.newLine,
NewLineCharacter: Harness.IO.newLine(),
ConvertTabsToSpaces: true,
InsertSpaceAfterCommaDelimiter: true,
InsertSpaceAfterSemicolonInForStatements: true,
Expand Down Expand Up @@ -551,7 +551,7 @@ module FourSlash {
errors.forEach(function (error: ts.Diagnostic) {
Harness.IO.log(" minChar: " + error.start +
", limChar: " + (error.start + error.length) +
", message: " + ts.flattenDiagnosticMessageText(error.messageText, ts.sys.newLine) + "\n");
", message: " + ts.flattenDiagnosticMessageText(error.messageText, Harness.IO.newLine()) + "\n");
});
}

Expand Down Expand Up @@ -1262,21 +1262,21 @@ module FourSlash {
emitFiles.forEach(emitFile => {
let emitOutput = this.languageService.getEmitOutput(emitFile.fileName);
// Print emitOutputStatus in readable format
resultString += "EmitSkipped: " + emitOutput.emitSkipped + ts.sys.newLine;
resultString += "EmitSkipped: " + emitOutput.emitSkipped + Harness.IO.newLine();

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

emitOutput.outputFiles.forEach((outputFile, idx, array) => {
let fileName = "FileName : " + outputFile.name + ts.sys.newLine;
let fileName = "FileName : " + outputFile.name + Harness.IO.newLine();
resultString = resultString + fileName + outputFile.text;
});
resultString += ts.sys.newLine;
resultString += Harness.IO.newLine();
});

return resultString;
Expand Down Expand Up @@ -1313,7 +1313,7 @@ module FourSlash {
Harness.IO.log(
"start: " + err.start +
", length: " + err.length +
", message: " + ts.flattenDiagnosticMessageText(err.messageText, ts.sys.newLine));
", message: " + ts.flattenDiagnosticMessageText(err.messageText, Harness.IO.newLine()));
});
}
}
Expand Down Expand Up @@ -1887,9 +1887,9 @@ module FourSlash {
}

function jsonMismatchString() {
return ts.sys.newLine +
"expected: '" + ts.sys.newLine + JSON.stringify(expected, (k, v) => v, 2) + "'" + ts.sys.newLine +
"actual: '" + ts.sys.newLine + JSON.stringify(actual, (k, v) => v, 2) + "'";
return Harness.IO.newLine() +
"expected: '" + Harness.IO.newLine() + JSON.stringify(expected, (k, v) => v, 2) + "'" + Harness.IO.newLine() +
"actual: '" + Harness.IO.newLine() + JSON.stringify(actual, (k, v) => v, 2) + "'";
}
}

Expand Down Expand Up @@ -2398,7 +2398,7 @@ module FourSlash {
let host = Harness.Compiler.createCompilerHost([{ unitName: Harness.Compiler.fourslashFileName, content: undefined }],
(fn, contents) => fourslashJsOutput = contents,
ts.ScriptTarget.Latest,
ts.sys.useCaseSensitiveFileNames);
Harness.IO.useCaseSensitiveFileNames());

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

Expand All @@ -2420,7 +2420,7 @@ module FourSlash {
],
(fn, contents) => result = contents,
ts.ScriptTarget.Latest,
ts.sys.useCaseSensitiveFileNames);
Harness.IO.useCaseSensitiveFileNames());

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

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

program.emit(sourceFile);
Expand Down
86 changes: 55 additions & 31 deletions src/harness/harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

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

// this will work in the browser via browserify
var _chai: typeof chai = require("chai");
Expand Down Expand Up @@ -55,9 +54,17 @@ module Utils {
return ExecutionEnvironment.Node;
}
}

export let currentExecutionEnvironment = getExecutionEnvironment();

const Buffer: BufferConstructor = currentExecutionEnvironment !== ExecutionEnvironment.Browser
? require("buffer").Buffer
: undefined;

export function encodeString(s: string): string {
return Buffer ? (new Buffer(s)).toString("utf8") : s;
}

export function evalFile(fileContents: string, fileName: string, nodeContext?: any) {
let environment = getExecutionEnvironment();
switch (environment) {
Expand Down Expand Up @@ -102,7 +109,7 @@ module Utils {

let content: string = undefined;
try {
content = ts.sys.readFile(Harness.userSpecifiedRoot + path);
content = Harness.IO.readFile(Harness.userSpecifiedRoot + path);
}
catch (err) {
return undefined;
Expand Down Expand Up @@ -190,7 +197,7 @@ module Utils {
return {
start: diagnostic.start,
length: diagnostic.length,
messageText: ts.flattenDiagnosticMessageText(diagnostic.messageText, ts.sys.newLine),
messageText: ts.flattenDiagnosticMessageText(diagnostic.messageText, Harness.IO.newLine()),
category: (<any>ts).DiagnosticCategory[diagnostic.category],
code: diagnostic.code
};
Expand Down Expand Up @@ -323,8 +330,8 @@ module Utils {
assert.equal(d1.start, d2.start, "d1.start !== d2.start");
assert.equal(d1.length, d2.length, "d1.length !== d2.length");
assert.equal(
ts.flattenDiagnosticMessageText(d1.messageText, ts.sys.newLine),
ts.flattenDiagnosticMessageText(d2.messageText, ts.sys.newLine), "d1.messageText !== d2.messageText");
ts.flattenDiagnosticMessageText(d1.messageText, Harness.IO.newLine()),
ts.flattenDiagnosticMessageText(d2.messageText, Harness.IO.newLine()), "d1.messageText !== d2.messageText");
assert.equal(d1.category, d2.category, "d1.category !== d2.category");
assert.equal(d1.code, d2.code, "d1.code !== d2.code");
}
Expand Down Expand Up @@ -404,6 +411,10 @@ module Harness.Path {

module Harness {
export interface IO {
newLine(): string;
getCurrentDirectory(): string;
useCaseSensitiveFileNames(): boolean;
resolvePath(path: string): string;
readFile(path: string): string;
writeFile(path: string, contents: string): void;
directoryName(path: string): string;
Expand All @@ -416,7 +427,10 @@ module Harness {
getMemoryUsage?(): number;
}
export var IO: IO;


// harness always uses one kind of new line
const harnessNewLine = "\r\n";

module IOImpl {
declare class Enumerator {
public atEnd(): boolean;
Expand All @@ -433,12 +447,17 @@ module Harness {
fso = {};
}

export let readFile: typeof IO.readFile = ts.sys.readFile;
export let writeFile: typeof IO.writeFile = ts.sys.writeFile;
export let directoryName: typeof IO.directoryName = fso.GetParentFolderName;
export let directoryExists: typeof IO.directoryExists = fso.FolderExists;
export let fileExists: typeof IO.fileExists = fso.FileExists;
export let log: typeof IO.log = global.WScript && global.WScript.StdOut.WriteLine;
export const resolvePath = (path: string) => ts.sys.resolvePath(path);
export const getCurrentDirectory = () => ts.sys.getCurrentDirectory();
export const newLine = () => harnessNewLine;
export const useCaseSensitiveFileNames = () => ts.sys.useCaseSensitiveFileNames;

export const readFile: typeof IO.readFile = path => ts.sys.readFile(path);
export const writeFile: typeof IO.writeFile = (path, content) => ts.sys.writeFile(path, content);
export const directoryName: typeof IO.directoryName = fso.GetParentFolderName;
export const directoryExists: typeof IO.directoryExists = fso.FolderExists;
export const fileExists: typeof IO.fileExists = fso.FileExists;
export const log: typeof IO.log = global.WScript && global.WScript.StdOut.WriteLine;

export function createDirectory(path: string) {
if (directoryExists(path)) {
Expand Down Expand Up @@ -493,11 +512,16 @@ module Harness {
} else {
fs = pathModule = {};
}

export const resolvePath = (path: string) => ts.sys.resolvePath(path);
export const getCurrentDirectory = () => ts.sys.getCurrentDirectory();
export const newLine = () => harnessNewLine;
export const useCaseSensitiveFileNames = () => ts.sys.useCaseSensitiveFileNames;

export let readFile: typeof IO.readFile = ts.sys.readFile;
export let writeFile: typeof IO.writeFile = ts.sys.writeFile;
export let fileExists: typeof IO.fileExists = fs.existsSync;
export let log: typeof IO.log = s => console.log(s);
export const readFile: typeof IO.readFile = path => ts.sys.readFile(path);
export const writeFile: typeof IO.writeFile = (path, content) => ts.sys.writeFile(path, content);
export const fileExists: typeof IO.fileExists = fs.existsSync;
export const log: typeof IO.log = s => console.log(s);

export function createDirectory(path: string) {
if (!directoryExists(path)) {
Expand Down Expand Up @@ -562,9 +586,9 @@ module Harness {
export module Network {
let serverRoot = "http://localhost:8888/";

// Unused?
let newLine = "\r\n";
let currentDirectory = () => "";
export const newLine = () => harnessNewLine;
export const useCaseSensitiveFileNames = () => false;
export const getCurrentDirectory = () => "";
let supportsCodePage = () => false;

module Http {
Expand Down Expand Up @@ -616,6 +640,7 @@ module Harness {
xhr.send(contents);
}
catch (e) {
log(`XHR Error: ${e}`);
return { status: 500, responseText: null };
}

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

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

export function getCanonicalFileName(fileName: string): string {
return ts.sys.useCaseSensitiveFileNames ? fileName : fileName.toLowerCase();
return Harness.IO.useCaseSensitiveFileNames() ? fileName : fileName.toLowerCase();
}

export function createCompilerHost(
Expand All @@ -858,7 +884,7 @@ module Harness {
}

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

// Register input files
function register(file: { unitName: string; content: string; }) {
Expand Down Expand Up @@ -895,7 +921,7 @@ module Harness {
let newLine =
newLineKind === ts.NewLineKind.CarriageReturnLineFeed ? carriageReturnLineFeed :
newLineKind === ts.NewLineKind.LineFeed ? lineFeed :
ts.sys.newLine;
Harness.IO.newLine();

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

let useCaseSensitiveFileNames = ts.sys.useCaseSensitiveFileNames;
let useCaseSensitiveFileNames = Harness.IO.useCaseSensitiveFileNames();
this.settings.forEach(setCompilerOptionForSetting);

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

let result = new CompilerResult(fileOutputs, errors, program, ts.sys.getCurrentDirectory(), emitResult.sourceMaps);
let result = new CompilerResult(fileOutputs, errors, program, Harness.IO.getCurrentDirectory(), emitResult.sourceMaps);
onComplete(result, program);

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

function setCompilerOptionForSetting(setting: Harness.TestCaseParser.CompilerSetting) {
Expand Down Expand Up @@ -1273,7 +1297,7 @@ module Harness {
errorOutput += diagnostic.file.fileName + "(" + (lineAndCharacter.line + 1) + "," + (lineAndCharacter.character + 1) + "): ";
}

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

return errorOutput;
Expand All @@ -1286,7 +1310,7 @@ module Harness {
let totalErrorsReported = 0;

function outputErrorText(error: ts.Diagnostic) {
let message = ts.flattenDiagnosticMessageText(error.messageText, ts.sys.newLine);
let message = ts.flattenDiagnosticMessageText(error.messageText, Harness.IO.newLine());

let errLines = RunnerBase.removeFullPaths(message)
.split("\n")
Expand Down Expand Up @@ -1383,7 +1407,7 @@ module Harness {
assert.equal(totalErrorsReported + numLibraryDiagnostics + numTest262HarnessDiagnostics, diagnostics.length, "total number of errors");

return minimalDiagnosticsToString(diagnostics) +
ts.sys.newLine + ts.sys.newLine + outputLines.join("\r\n");
Harness.IO.newLine() + Harness.IO.newLine() + outputLines.join("\r\n");
}

export function collateOutputs(outputFiles: Harness.Compiler.GeneratedFile[]): string {
Expand Down Expand Up @@ -1719,7 +1743,7 @@ module Harness {
}

function writeComparison(expected: string, actual: string, relativeFileName: string, actualFileName: string, descriptionForDescribe: string) {
let encoded_actual = (new Buffer(actual)).toString("utf8");
let encoded_actual = Utils.encodeString(actual);
if (expected != encoded_actual) {
// Overwrite & issue error
let errMsg = "The baseline file " + relativeFileName + " has changed";
Expand Down
Loading