@@ -67,17 +67,16 @@ namespace Utils {
67
67
68
68
export let currentExecutionEnvironment = getExecutionEnvironment ( ) ;
69
69
70
- const Buffer : typeof global . Buffer = currentExecutionEnvironment !== ExecutionEnvironment . Browser
71
- ? require ( "buffer" ) . Buffer
72
- : undefined ;
70
+ // Thanks to browserify, Buffer is always available nowadays
71
+ const Buffer : typeof global . Buffer = require ( "buffer" ) . Buffer ;
73
72
74
73
export function encodeString ( s : string ) : string {
75
- return Buffer ? ( new Buffer ( s ) ) . toString ( "utf8" ) : s ;
74
+ return Buffer . from ( s ) . toString ( "utf8" ) ;
76
75
}
77
76
78
77
export function byteLength ( s : string , encoding ?: string ) : number {
79
78
// stub implementation if Buffer is not available (in-browser case)
80
- return Buffer ? Buffer . byteLength ( s , encoding ) : s . length ;
79
+ return Buffer . byteLength ( s , encoding ) ;
81
80
}
82
81
83
82
export function evalFile ( fileContents : string , fileName : string , nodeContext ?: any ) {
@@ -133,17 +132,18 @@ namespace Utils {
133
132
return content ;
134
133
}
135
134
136
- export function memoize < T extends Function > ( f : T ) : T {
137
- const cache : { [ idx : string ] : any } = { } ;
135
+ export function memoize < T extends Function > ( f : T , memoKey : ( ... anything : any [ ] ) => string ) : T {
136
+ const cache = ts . createMap < any > ( ) ;
138
137
139
- return < any > ( function ( this : any ) {
140
- const key = Array . prototype . join . call ( arguments ) ;
141
- const cachedResult = cache [ key ] ;
142
- if ( cachedResult ) {
143
- return cachedResult ;
138
+ return < any > ( function ( this : any , ...args : any [ ] ) {
139
+ const key = memoKey ( ...args ) ;
140
+ if ( cache . has ( key ) ) {
141
+ return cache . get ( key ) ;
144
142
}
145
143
else {
146
- return cache [ key ] = f . apply ( this , arguments ) ;
144
+ const value = f . apply ( this , args ) ;
145
+ cache . set ( key , value ) ;
146
+ return value ;
147
147
}
148
148
} ) ;
149
149
}
@@ -686,7 +686,7 @@ namespace Harness {
686
686
687
687
return dirPath ;
688
688
}
689
- export let directoryName : typeof IO . directoryName = Utils . memoize ( directoryNameImpl ) ;
689
+ export let directoryName : typeof IO . directoryName = Utils . memoize ( directoryNameImpl , path => path ) ;
690
690
691
691
export function resolvePath ( path : string ) {
692
692
const response = Http . getFileFromServerSync ( serverRoot + path + "?resolve=true" ) ;
@@ -703,21 +703,22 @@ namespace Harness {
703
703
return response . status === 200 ;
704
704
}
705
705
706
- export let listFiles = Utils . memoize ( ( path : string , spec ?: RegExp ) : string [ ] => {
706
+ export const listFiles = Utils . memoize ( ( path : string , spec ?: RegExp , options ?: { recursive ?: boolean } ) : string [ ] => {
707
707
const response = Http . getFileFromServerSync ( serverRoot + path ) ;
708
708
if ( response . status === 200 ) {
709
- const results = response . responseText . split ( "," ) ;
709
+ let results = response . responseText . split ( "," ) ;
710
710
if ( spec ) {
711
- return results . filter ( file => spec . test ( file ) ) ;
711
+ results = results . filter ( file => spec . test ( file ) ) ;
712
712
}
713
- else {
714
- return results ;
713
+ if ( options && ! options . recursive ) {
714
+ results = results . filter ( file => ( ts . getDirectoryPath ( ts . normalizeSlashes ( file ) ) === path ) ) ;
715
715
}
716
+ return results ;
716
717
}
717
718
else {
718
719
return [ "" ] ;
719
720
}
720
- } ) ;
721
+ } , ( path : string , spec ?: RegExp , options ?: { recursive ?: boolean } ) => ` ${ path } | ${ spec } | ${ options ? options . recursive : undefined } ` ) ;
721
722
722
723
export function readFile ( file : string ) : string | undefined {
723
724
const response = Http . getFileFromServerSync ( serverRoot + file ) ;
@@ -1989,7 +1990,7 @@ namespace Harness {
1989
1990
IO . writeFile ( actualFileName + ".delete" , "" ) ;
1990
1991
}
1991
1992
else {
1992
- IO . writeFile ( actualFileName , actual ) ;
1993
+ IO . writeFile ( actualFileName , encoded_actual ) ;
1993
1994
}
1994
1995
throw new Error ( `The baseline file ${ relativeFileName } has changed.` ) ;
1995
1996
}
0 commit comments