5
5
/// <reference path="..\..\src\harness\typeWriter.ts" />
6
6
7
7
interface FileInformation {
8
- contents : string ;
8
+ contents ?: string ;
9
+ contentsPath ?: string ;
9
10
codepage : number ;
11
+ bom ?: string ;
10
12
}
11
13
12
14
interface FindFileResult {
@@ -27,13 +29,15 @@ interface IOLog {
27
29
filesRead : IOLogFile [ ] ;
28
30
filesWritten : {
29
31
path : string ;
30
- contents : string ;
32
+ contents ?: string ;
33
+ contentsPath ?: string ;
31
34
bom : boolean ;
32
35
} [ ] ;
33
36
filesDeleted : string [ ] ;
34
37
filesAppended : {
35
38
path : string ;
36
- contents : string ;
39
+ contents ?: string ;
40
+ contentsPath ?: string ;
37
41
} [ ] ;
38
42
fileExists : {
39
43
path : string ;
@@ -129,6 +133,72 @@ namespace Playback {
129
133
} ;
130
134
}
131
135
136
+ export function newStyleLogIntoOldStyleLog ( log : IOLog , host : ts . System | Harness . IO , baseName : string ) {
137
+ for ( const file of log . filesAppended ) {
138
+ if ( file . contentsPath ) {
139
+ file . contents = host . readFile ( ts . combinePaths ( baseName , file . contentsPath ) ) ;
140
+ delete file . contentsPath ;
141
+ }
142
+ }
143
+ for ( const file of log . filesWritten ) {
144
+ if ( file . contentsPath ) {
145
+ file . contents = host . readFile ( ts . combinePaths ( baseName , file . contentsPath ) ) ;
146
+ delete file . contentsPath ;
147
+ }
148
+ }
149
+ for ( const file of log . filesRead ) {
150
+ if ( file . result . contentsPath ) {
151
+ // `readFile` strips away a BOM (and actually reinerprets the file contents according to the correct encoding)
152
+ // - but this has the unfortunate sideeffect of removing the BOM from any outputs based on the file, so we readd it here.
153
+ file . result . contents = ( file . result . bom || "" ) + host . readFile ( ts . combinePaths ( baseName , file . result . contentsPath ) ) ;
154
+ delete file . result . contentsPath ;
155
+ }
156
+ }
157
+ return log ;
158
+ }
159
+
160
+ export function oldStyleLogIntoNewStyleLog ( log : IOLog , writeFile : typeof Harness . IO . writeFile , baseTestName : string ) {
161
+ if ( log . filesAppended ) {
162
+ for ( const file of log . filesAppended ) {
163
+ if ( file . contents !== undefined ) {
164
+ file . contentsPath = ts . combinePaths ( "appended" , Harness . Compiler . sanitizeTestFilePath ( file . path ) ) ;
165
+ writeFile ( ts . combinePaths ( baseTestName , file . contentsPath ) , file . contents ) ;
166
+ delete file . contents ;
167
+ }
168
+ }
169
+ }
170
+ if ( log . filesWritten ) {
171
+ for ( const file of log . filesWritten ) {
172
+ if ( file . contents !== undefined ) {
173
+ file . contentsPath = ts . combinePaths ( "written" , Harness . Compiler . sanitizeTestFilePath ( file . path ) ) ;
174
+ writeFile ( ts . combinePaths ( baseTestName , file . contentsPath ) , file . contents ) ;
175
+ delete file . contents ;
176
+ }
177
+ }
178
+ }
179
+ if ( log . filesRead ) {
180
+ for ( const file of log . filesRead ) {
181
+ const { contents } = file . result ;
182
+ if ( contents !== undefined ) {
183
+ file . result . contentsPath = ts . combinePaths ( "read" , Harness . Compiler . sanitizeTestFilePath ( file . path ) ) ;
184
+ writeFile ( ts . combinePaths ( baseTestName , file . result . contentsPath ) , contents ) ;
185
+ const len = contents . length ;
186
+ if ( len >= 2 && contents . charCodeAt ( 0 ) === 0xfeff ) {
187
+ file . result . bom = "\ufeff" ;
188
+ }
189
+ if ( len >= 2 && contents . charCodeAt ( 0 ) === 0xfffe ) {
190
+ file . result . bom = "\ufffe" ;
191
+ }
192
+ if ( len >= 3 && contents . charCodeAt ( 0 ) === 0xefbb && contents . charCodeAt ( 1 ) === 0xbf ) {
193
+ file . result . bom = "\uefbb\xbf" ;
194
+ }
195
+ delete file . result . contents ;
196
+ }
197
+ }
198
+ }
199
+ return log ;
200
+ }
201
+
132
202
function initWrapper ( wrapper : PlaybackSystem , underlying : ts . System ) : void ;
133
203
function initWrapper ( wrapper : PlaybackIO , underlying : Harness . IO ) : void ;
134
204
function initWrapper ( wrapper : PlaybackSystem | PlaybackIO , underlying : ts . System | Harness . IO ) : void {
@@ -164,9 +234,9 @@ namespace Playback {
164
234
wrapper . endRecord = ( ) => {
165
235
if ( recordLog !== undefined ) {
166
236
let i = 0 ;
167
- const fn = ( ) => recordLogFileNameBase + i + ".json" ;
168
- while ( underlying . fileExists ( fn ( ) ) ) i ++ ;
169
- underlying . writeFile ( fn ( ) , JSON . stringify ( recordLog ) ) ;
237
+ const fn = ( ) => recordLogFileNameBase + i ;
238
+ while ( underlying . fileExists ( fn ( ) + ".json" ) ) i ++ ;
239
+ underlying . writeFile ( ts . combinePaths ( fn ( ) , "test.json" ) , JSON . stringify ( oldStyleLogIntoNewStyleLog ( recordLog , ( path , string ) => underlying . writeFile ( ts . combinePaths ( fn ( ) , path ) , string ) , fn ( ) ) , null , 4 ) ) ; // tslint:disable-line:no-null-keyword
170
240
recordLog = undefined ;
171
241
}
172
242
} ;
0 commit comments