55/// <reference path="..\..\src\harness\typeWriter.ts" />
66
77interface FileInformation {
8- contents : string ;
8+ contents ?: string ;
9+ contentsPath ?: string ;
910 codepage : number ;
11+ bom ?: string ;
1012}
1113
1214interface FindFileResult {
@@ -27,13 +29,15 @@ interface IOLog {
2729 filesRead : IOLogFile [ ] ;
2830 filesWritten : {
2931 path : string ;
30- contents : string ;
32+ contents ?: string ;
33+ contentsPath ?: string ;
3134 bom : boolean ;
3235 } [ ] ;
3336 filesDeleted : string [ ] ;
3437 filesAppended : {
3538 path : string ;
36- contents : string ;
39+ contents ?: string ;
40+ contentsPath ?: string ;
3741 } [ ] ;
3842 fileExists : {
3943 path : string ;
@@ -129,6 +133,72 @@ namespace Playback {
129133 } ;
130134 }
131135
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+
132202 function initWrapper ( wrapper : PlaybackSystem , underlying : ts . System ) : void ;
133203 function initWrapper ( wrapper : PlaybackIO , underlying : Harness . IO ) : void ;
134204 function initWrapper ( wrapper : PlaybackSystem | PlaybackIO , underlying : ts . System | Harness . IO ) : void {
@@ -164,9 +234,9 @@ namespace Playback {
164234 wrapper . endRecord = ( ) => {
165235 if ( recordLog !== undefined ) {
166236 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
170240 recordLog = undefined ;
171241 }
172242 } ;
0 commit comments