import { assert } from "chai" ; import * as fs from "fs" ; import * as path from "path" ; import * as webpack from "webpack" ; import { LoaderOptions } from "../../src/LoaderOptions" ; import { WebpackAny } from "../../src/Plugin" ; import { EntryTypes , config as basicConfig } from "../configs/basicConfig" ; import { BLOCK_FIXTURES_DIRECTORY , DIST_DIRECTORY , WEBPACK_DEV_SERVER_PATH } from "./testPaths" ; const CR = /\r/g ; export function execTest ( testId , options ? , entryFormat = "string" ) { const entryPath = path . join ( BLOCK_FIXTURES_DIRECTORY , testId + ".block.css" ) ; let entry = entryPath ; if ( entryFormat === "array" ) { entry = [ entryPath ] ; } else if ( entryFormat === "object" ) { entry = { main : entryPath } ; } else if ( entryFormat === "dev-server" ) { entry = [ WEBPACK_DEV_SERVER_PATH , "webpack/hot/dev-server" , entryPath ] ; } return runWebpackAsPromise ( basicConfig ( entry , options ) ) . then ( ( ) => { const actualCss = readBundle ( "bundle.block.css.js" ) ; const expectedCss = readCss ( testId ) ; assert . deepEqual ( actualCss , expectedCss ) ; } ) ; } export function readCss ( id ) { let css = fs . readFileSync ( path . join ( BLOCK_FIXTURES_DIRECTORY , id + ".css" ) , "utf8" ) ; css = css . replace ( CR , "" ) ; css = css . replace ( "FIXTURES_DIRECTORY" , BLOCK_FIXTURES_DIRECTORY ) ; return css ; } export function readCssSourceMap ( id ) { let json = fs . readFileSync ( path . join ( BLOCK_FIXTURES_DIRECTORY , id + ".css.map" ) , "utf8" ) ; json = json . replace ( CR , "" ) ; json = json . replace ( "FIXTURES_DIRECTORY" , BLOCK_FIXTURES_DIRECTORY ) ; return JSON . parse ( json ) ; } export function readAsset ( filename ) { const outputLocation = path . resolve ( DIST_DIRECTORY , `./test_output/${ filename }` ) ; let content = fs . readFileSync ( outputLocation , "utf8" ) ; content = content . replace ( CR , "" ) ; return content ; } export function runWebpackAsPromise ( webpackConfig ) { return new Promise ( ( resolve , reject ) => { runWebpack ( webpackConfig , ( err ) => err ? reject ( err ) : resolve ( ) ) ; } ) ; } function runWebpack ( webpackConfig , done ) { webpack ( webpackConfig , ( webpackErr , stats ) => { const err = webpackErr || ( stats . hasErrors ( ) && ( < WebpackAny > stats ) . compilation . errors [ 0 ] ) || ( stats . hasWarnings ( ) && ( < WebpackAny > stats ) . compilation . warnings [ 0 ] ) ; done ( err || null ) ; } ) ; } export function readBundle ( filename ) { const outputLocation = path . resolve ( DIST_DIRECTORY , `./test_output/${ filename }` ) ; delete require . cache [ outputLocation ] ; return require ( outputLocation ) ; }