1+ var should = require ( "should" ) ;
2+ var path = require ( "path" ) ;
3+ var fs = require ( "fs" ) ;
4+ var vm = require ( "vm" ) ;
5+ var Test = require ( "mocha/lib/test" ) ;
6+ var checkArrayExpectation = require ( "./checkArrayExpectation" ) ;
7+
8+ var webpack = require ( "../lib/webpack" ) ;
9+
10+ describe ( "ConfigTestCases" , function ( ) {
11+ var casesPath = path . join ( __dirname , "configCases" ) ;
12+ var categories = fs . readdirSync ( casesPath ) ;
13+ categories = categories . map ( function ( cat ) {
14+ return {
15+ name : cat ,
16+ tests : fs . readdirSync ( path . join ( casesPath , cat ) ) . filter ( function ( folder ) {
17+ return folder . indexOf ( "_" ) < 0 ;
18+ } )
19+ } ;
20+ } ) ;
21+ categories . forEach ( function ( category ) {
22+ describe ( category . name , function ( ) {
23+ category . tests . forEach ( function ( testName ) {
24+ var suite = describe ( testName , function ( ) { } ) ;
25+ it ( testName + " should compile" , function ( done ) {
26+ this . timeout ( 10000 ) ;
27+ var testDirectory = path . join ( casesPath , category . name , testName ) ;
28+ var outputDirectory = path . join ( __dirname , "js" , "config" , category . name , testName ) ;
29+ var options = require ( path . join ( testDirectory , "webpack.config.js" ) ) ;
30+ if ( ! options . context ) options . context = testDirectory ;
31+ if ( ! options . entry ) options . entry = "./index.js" ;
32+ if ( ! options . target ) options . target = "async-node" ;
33+ if ( ! options . output ) options . output = { } ;
34+ if ( ! options . output . path ) options . output . path = outputDirectory ;
35+ if ( ! options . output . filename ) options . output . filename = "bundle.js" ;
36+ webpack ( options , function ( err , stats ) {
37+ if ( err ) return done ( err ) ;
38+ var jsonStats = stats . toJson ( {
39+ errorDetails : true
40+ } ) ;
41+ if ( checkArrayExpectation ( testDirectory , jsonStats , "error" , "Error" , done ) ) return ;
42+ if ( checkArrayExpectation ( testDirectory , jsonStats , "warning" , "Warning" , done ) ) return ;
43+ var exportedTest = 0 ;
44+ function _it ( title , fn ) {
45+ var test = new Test ( title , fn ) ;
46+ suite . addTest ( test ) ;
47+ exportedTest ++ ;
48+ return test ;
49+ }
50+ function _require ( module ) {
51+ if ( module . substr ( 0 , 2 ) === "./" ) {
52+ var p = path . join ( outputDirectory , module ) ;
53+ var fn = vm . runInThisContext ( "(function(require, module, exports, __dirname, it) {" + fs . readFileSync ( p , "utf-8" ) + "\n})" , p ) ;
54+ var module = { exports : { } } ;
55+ fn . call ( module . exports , _require , module , module . exports , outputDirectory , _it ) ;
56+ return module . exports ;
57+ } else return require ( module ) ;
58+ }
59+ _require ( "./bundle.js" ) ;
60+ if ( exportedTest === 0 ) return done ( new Error ( "No tests exported by test case" ) ) ;
61+ done ( ) ;
62+ } ) ;
63+ } ) ;
64+ } ) ;
65+ } ) ;
66+ } ) ;
67+ } ) ;
0 commit comments