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
3231var _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
405412module 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