import * as fs from 'fs' ; import * as path from 'path' ; import "mocha" ; import * as chai from "chai" ; import * as util from "../common/testUtils" ; const casesDir = path . join ( process . cwd ( ) , "tests" , "format-test" , "cases" ) ; const baselineDir = path . join ( process . cwd ( ) , "tests" , "format-test" , "baselines" ) ; function initGlobals ( ) { let g = global as any ; g . pxt = pxt ; g . ts = ts ; g . pxtc = pxtc ; g . btoa = ( str ) => Buffer . from ( str , "binary" ) . toString ( "base64" ) ; g . atob = ( str ) => Buffer . from ( str , "base64" ) . toString ( "binary" ) ; } initGlobals ( ) ; pxt . setAppTarget ( util . testAppTarget ) ; describe ( "ts formatting" , ( ) => { const filenames = [ ] ; for ( const file of fs . readdirSync ( casesDir ) ) { if ( file [ 0 ] == "." ) { continue ; } const filename = path . join ( casesDir , file ) ; if ( file . substr ( - 3 ) === ".ts" ) { filenames . push ( filename ) ; } } ; filenames . forEach ( filename => { it ( "should correctly format " + path . basename ( filename ) , ( ) => { formatTest ( filename ) ; } ) ; } ) ; } ) ; function formatTest ( filename ) { const input = fs . readFileSync ( filename , "utf8" ) ; const baselinePath = path . join ( baselineDir , path . basename ( filename ) ) ; let baseline ; let hasBaseline = false ; try { baseline = fs . readFileSync ( baselinePath , "utf8" ) ; hasBaseline = true ; } catch ( e ) { } baseline = baseline || input ; const { formatted } = pxtc . format ( input , 0 ) ; chai . assert ( formatted === baseline , hasBaseline ? "Formatted code did not match baseline" : "No baseline found and formatted code did not preserve input" ) ; }