26
26
27
27
// Block scoped definitions work poorly for global variables, temporarily enable var
28
28
/* tslint:disable:no-var-keyword */
29
- var Buffer : BufferConstructor = require ( "buffer" ) . Buffer ;
30
29
31
30
// this will work in the browser via browserify
32
31
var _chai : typeof chai = require ( "chai" ) ;
@@ -55,9 +54,17 @@ module Utils {
55
54
return ExecutionEnvironment . Node ;
56
55
}
57
56
}
58
-
57
+
59
58
export let currentExecutionEnvironment = getExecutionEnvironment ( ) ;
60
59
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
+
61
68
export function evalFile ( fileContents : string , fileName : string , nodeContext ?: any ) {
62
69
let environment = getExecutionEnvironment ( ) ;
63
70
switch ( environment ) {
@@ -102,7 +109,7 @@ module Utils {
102
109
103
110
let content : string = undefined ;
104
111
try {
105
- content = ts . sys . readFile ( Harness . userSpecifiedRoot + path ) ;
112
+ content = Harness . IO . readFile ( Harness . userSpecifiedRoot + path ) ;
106
113
}
107
114
catch ( err ) {
108
115
return undefined ;
@@ -190,7 +197,7 @@ module Utils {
190
197
return {
191
198
start : diagnostic . start ,
192
199
length : diagnostic . length ,
193
- messageText : ts . flattenDiagnosticMessageText ( diagnostic . messageText , ts . sys . newLine ) ,
200
+ messageText : ts . flattenDiagnosticMessageText ( diagnostic . messageText , Harness . IO . newLine ( ) ) ,
194
201
category : ( < any > ts ) . DiagnosticCategory [ diagnostic . category ] ,
195
202
code : diagnostic . code
196
203
} ;
@@ -323,8 +330,8 @@ module Utils {
323
330
assert . equal ( d1 . start , d2 . start , "d1.start !== d2.start" ) ;
324
331
assert . equal ( d1 . length , d2 . length , "d1.length !== d2.length" ) ;
325
332
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" ) ;
328
335
assert . equal ( d1 . category , d2 . category , "d1.category !== d2.category" ) ;
329
336
assert . equal ( d1 . code , d2 . code , "d1.code !== d2.code" ) ;
330
337
}
@@ -404,6 +411,10 @@ module Harness.Path {
404
411
405
412
module Harness {
406
413
export interface IO {
414
+ newLine ( ) : string ;
415
+ getCurrentDirectory ( ) : string ;
416
+ useCaseSensitiveFileNames ( ) : boolean ;
417
+ resolvePath ( path : string ) : string ;
407
418
readFile ( path : string ) : string ;
408
419
writeFile ( path : string , contents : string ) : void ;
409
420
directoryName ( path : string ) : string ;
@@ -416,7 +427,10 @@ module Harness {
416
427
getMemoryUsage ?( ) : number ;
417
428
}
418
429
export var IO : IO ;
419
-
430
+
431
+ // harness always uses one kind of new line
432
+ const harnessNewLine = "\r\n" ;
433
+
420
434
module IOImpl {
421
435
declare class Enumerator {
422
436
public atEnd ( ) : boolean ;
@@ -433,12 +447,17 @@ module Harness {
433
447
fso = { } ;
434
448
}
435
449
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 ;
442
461
443
462
export function createDirectory ( path : string ) {
444
463
if ( directoryExists ( path ) ) {
@@ -493,11 +512,16 @@ module Harness {
493
512
} else {
494
513
fs = pathModule = { } ;
495
514
}
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 ;
496
520
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 ) ;
501
525
502
526
export function createDirectory ( path : string ) {
503
527
if ( ! directoryExists ( path ) ) {
@@ -562,9 +586,9 @@ module Harness {
562
586
export module Network {
563
587
let serverRoot = "http://localhost:8888/" ;
564
588
565
- // Unused?
566
- let newLine = "\r\n" ;
567
- let currentDirectory = ( ) => "" ;
589
+ export const newLine = ( ) => harnessNewLine ;
590
+ export const useCaseSensitiveFileNames = ( ) => false ;
591
+ export const getCurrentDirectory = ( ) => "" ;
568
592
let supportsCodePage = ( ) => false ;
569
593
570
594
module Http {
@@ -616,6 +640,7 @@ module Harness {
616
640
xhr . send ( contents ) ;
617
641
}
618
642
catch ( e ) {
643
+ log ( `XHR Error: ${ e } ` ) ;
619
644
return { status : 500 , responseText : null } ;
620
645
}
621
646
@@ -655,6 +680,7 @@ module Harness {
655
680
return dirPath ;
656
681
}
657
682
export let directoryName : typeof IO . directoryName = Utils . memoize ( directoryNameImpl ) ;
683
+ export const resolvePath = ( path : string ) => directoryName ( path ) ;
658
684
659
685
export function fileExists ( path : string ) : boolean {
660
686
let response = Http . getFileFromServerSync ( serverRoot + path ) ;
@@ -840,7 +866,7 @@ module Harness {
840
866
export let fourslashSourceFile : ts . SourceFile ;
841
867
842
868
export function getCanonicalFileName ( fileName : string ) : string {
843
- return ts . sys . useCaseSensitiveFileNames ? fileName : fileName . toLowerCase ( ) ;
869
+ return Harness . IO . useCaseSensitiveFileNames ( ) ? fileName : fileName . toLowerCase ( ) ;
844
870
}
845
871
846
872
export function createCompilerHost (
@@ -858,7 +884,7 @@ module Harness {
858
884
}
859
885
860
886
let filemap : { [ fileName : string ] : ts . SourceFile ; } = { } ;
861
- let getCurrentDirectory = currentDirectory === undefined ? ts . sys . getCurrentDirectory : ( ) => currentDirectory ;
887
+ let getCurrentDirectory = currentDirectory === undefined ? Harness . IO . getCurrentDirectory : ( ) => currentDirectory ;
862
888
863
889
// Register input files
864
890
function register ( file : { unitName : string ; content : string ; } ) {
@@ -895,7 +921,7 @@ module Harness {
895
921
let newLine =
896
922
newLineKind === ts . NewLineKind . CarriageReturnLineFeed ? carriageReturnLineFeed :
897
923
newLineKind === ts . NewLineKind . LineFeed ? lineFeed :
898
- ts . sys . newLine ;
924
+ Harness . IO . newLine ( ) ;
899
925
900
926
return {
901
927
getCurrentDirectory,
@@ -988,7 +1014,7 @@ module Harness {
988
1014
// Treat them as library files, so include them in build, but not in baselines.
989
1015
let includeBuiltFiles : { unitName : string ; content : string } [ ] = [ ] ;
990
1016
991
- let useCaseSensitiveFileNames = ts . sys . useCaseSensitiveFileNames ;
1017
+ let useCaseSensitiveFileNames = Harness . IO . useCaseSensitiveFileNames ( ) ;
992
1018
this . settings . forEach ( setCompilerOptionForSetting ) ;
993
1019
994
1020
let fileOutputs : GeneratedFile [ ] = [ ] ;
@@ -1006,11 +1032,9 @@ module Harness {
1006
1032
let errors = ts . getPreEmitDiagnostics ( program ) . concat ( emitResult . diagnostics ) ;
1007
1033
this . lastErrors = errors ;
1008
1034
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 ) ;
1010
1036
onComplete ( result , program ) ;
1011
1037
1012
- // reset what newline means in case the last test changed it
1013
- ts . sys . newLine = newLine ;
1014
1038
return options ;
1015
1039
1016
1040
function setCompilerOptionForSetting ( setting : Harness . TestCaseParser . CompilerSetting ) {
@@ -1278,7 +1302,7 @@ module Harness {
1278
1302
errorOutput += diagnostic . file . fileName + "(" + ( lineAndCharacter . line + 1 ) + "," + ( lineAndCharacter . character + 1 ) + "): " ;
1279
1303
}
1280
1304
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 ( ) ;
1282
1306
} ) ;
1283
1307
1284
1308
return errorOutput ;
@@ -1291,7 +1315,7 @@ module Harness {
1291
1315
let totalErrorsReported = 0 ;
1292
1316
1293
1317
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 ( ) ) ;
1295
1319
1296
1320
let errLines = RunnerBase . removeFullPaths ( message )
1297
1321
. split ( "\n" )
@@ -1388,7 +1412,7 @@ module Harness {
1388
1412
assert . equal ( totalErrorsReported + numLibraryDiagnostics + numTest262HarnessDiagnostics , diagnostics . length , "total number of errors" ) ;
1389
1413
1390
1414
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" ) ;
1392
1416
}
1393
1417
1394
1418
export function collateOutputs ( outputFiles : Harness . Compiler . GeneratedFile [ ] ) : string {
@@ -1724,7 +1748,7 @@ module Harness {
1724
1748
}
1725
1749
1726
1750
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 ) ;
1728
1752
if ( expected != encoded_actual ) {
1729
1753
// Overwrite & issue error
1730
1754
let errMsg = "The baseline file " + relativeFileName + " has changed" ;
0 commit comments