22
33// Module Requirements
44var _ = require ( 'lodash' ) ,
5- proc = require ( 'child_process' ) ,
6- join = require ( 'path' ) . join ,
7- async = require ( 'async' ) ,
8- util = require ( 'util' ) ,
9- EventEmitter = require ( 'events' ) . EventEmitter ,
10- streamBuffers = require ( "stream-buffers" ) ,
11- through = require ( 'through' ) ,
12- split = require ( 'split' ) ,
13- fs = require ( 'fs' ) ;
5+ proc = require ( 'child_process' ) ,
6+ join = require ( 'path' ) . join ,
7+ async = require ( 'async' ) ,
8+ util = require ( 'util' ) ,
9+ EventEmitter = require ( 'events' ) . EventEmitter ,
10+ streamBuffers = require ( "stream-buffers" ) ,
11+ through = require ( 'through' ) ,
12+ split = require ( 'split' ) ,
13+ fs = require ( 'fs' ) ;
1414
1515require ( 'colors' ) ;
1616
1717function newStreamBuffer ( ) {
1818 var stream = new streamBuffers . WritableStreamBuffer ( {
19- initialSize : ( 25 * 1024 ) ,
20- incrementAmount : ( 10 * 1024 )
19+ initialSize : ( 25 * 1024 ) ,
20+ incrementAmount : ( 10 * 1024 )
2121 } ) ;
2222 return stream ;
2323}
@@ -30,15 +30,20 @@ var SpawnMocha = function (opts) {
3030 } ) ;
3131 var queue = async . queue ( function ( task , done ) {
3232 // Setup
33+ var _opts = opts ;
34+ //merge iterationOpts, if any
35+ if ( task . iterationOpts ) {
36+ _opts = _ . merge ( opts , task . iterationOpts ) ;
37+ }
3338 var bin = _ . isFunction ( opts . bin ) ? opts . bin ( ) : opts . bin ||
34- join ( __dirname , '..' , 'node_modules' , '.bin' , 'mocha' ) ;
35- var env = _ . isFunction ( opts . env ) ? opts . env ( ) : opts . env || process . env ;
39+ join ( __dirname , '..' , 'node_modules' , '.bin' , 'mocha' ) ;
40+ var env = _ . isFunction ( _opts . env ) ? _opts . env ( ) : _opts . env || process . env ;
3641 env = _ . clone ( env ) ;
3742
3843 // Generate arguments
3944 var args = [ ] ;
40- _ ( opts . flags ) . each ( function ( val , key ) {
41- if ( _ . isFunction ( val ) ) val = val ( ) ;
45+ _ ( _opts . flags ) . each ( function ( val , key ) {
46+ if ( _ . isFunction ( val ) ) val = val ( ) ;
4247 args . push ( ( key . length > 1 ? '--' : '-' ) + key ) ;
4348 if ( _ . isString ( val ) || _ . isNumber ( val ) ) {
4449 args . push ( val ) ;
@@ -47,27 +52,26 @@ var SpawnMocha = function (opts) {
4752
4853 var stdout = newStreamBuffer ( ) ;
4954 var stderr = newStreamBuffer ( ) ;
50- var fsStream = opts . fileOutput ?
51- fs . createWriteStream ( opts . fileOutput , { flags : 'a' , encoding : 'utf8' } ) : null ;
55+ var fsStream = _opts . fileOutput ?
56+ fs . createWriteStream ( _opts . fileOutput , { flags : 'a' , encoding : 'utf8' } ) : null ;
5257
5358 // Split xunit test report in several files if required
54- if ( env . JUNIT_REPORT_PATH ) {
55- env . JUNIT_REPORT_PATH = env . JUNIT_REPORT_PATH + '.' + task . taskNum ;
59+ if ( env . JUNIT_REPORT_PATH ) {
60+ env . JUNIT_REPORT_PATH = env . JUNIT_REPORT_PATH . replace ( / x m l $ / , task . taskNum + '.xml' ) ;
5661 }
57- console . log ( 'haoooooo' , task . files ) ;
5862 // Execute Mocha
5963 var child = proc . spawn ( bin , args . concat ( task . files ) , { env : env } ) ;
6064
6165 if ( opts . liveOutput ) {
6266 child . stdout . pipe ( split ( ) )
6367 . on ( 'data' , function ( line ) {
64- console . log ( ( opts . liveOutputPrepend || '' ) + line ) ;
68+ console . log ( ( _opts . liveOutputPrepend || '' ) + line ) ;
6569 } ) ;
6670 child . stderr . pipe ( split ( ) )
6771 . on ( 'data' , function ( line ) {
68- console . error ( ( opts . liveOutputPrepend || '' ) + line ) ;
72+ console . error ( ( _opts . liveOutputPrepend || '' ) + line ) ;
6973 } ) ;
70- if ( fsStream ) {
74+ if ( fsStream ) {
7175 child . stdout . pipe ( fsStream ) ;
7276 child . stderr . pipe ( fsStream ) ;
7377 }
@@ -77,25 +81,27 @@ var SpawnMocha = function (opts) {
7781 }
7882
7983 // When done...
80- child . on ( 'close' , function ( errCode ) {
81- if ( stdout . size ( ) ) {
84+ child . on ( 'close' , function ( errCode ) {
85+ if ( stdout . size ( ) ) {
8286 var contentOut = stdout . getContentsAsString ( "utf8" ) ;
8387 console . log ( contentOut ) ;
84- if ( fsStream ) {
88+ if ( fsStream ) {
8589 fsStream . write ( contentOut + '\n' , 'utf8' ) ;
8690 }
8791 }
88- if ( stderr . size ( ) ) {
92+ if ( stderr . size ( ) ) {
8993 var contentErr = stdout . getContentsAsString ( "utf8" ) ;
9094 console . error ( contentErr ) ;
91- if ( fsStream ) {
95+ if ( fsStream ) {
9296 fsStream . write ( contentErr + '\n' , 'utf8' ) ;
9397 }
9498 }
95- if ( fsStream ) { fsStream . close ( ) ; }
99+ if ( fsStream ) {
100+ fsStream . close ( ) ;
101+ }
96102
97103 var err = null ;
98- if ( errCode && opts . errorSummary ) {
104+ if ( errCode && opts . errorSummary ) {
99105 err = new Error ( 'Error for files: ' + task . files . join ( ', ' ) ) ;
100106 err . files = task . files ;
101107 err . stderr = stderr . size ( ) ? stderr . getContentsAsString ( "utf8" ) : '' ;
@@ -105,19 +111,19 @@ var SpawnMocha = function (opts) {
105111 } ) ;
106112 } , opts . concurrency || 1 ) ;
107113
108- queue . drain = function ( ) {
114+ queue . drain = function ( ) {
109115 _this . emit ( 'end' ) ;
110116 } ;
111117
112118 var taskNum = 0 ;
113- this . add = function ( files ) {
114- taskNum ++ ;
119+ this . add = function ( files , iterationOpts ) {
120+ taskNum ++ ;
115121 if ( ! _ . isArray ( files ) ) {
116122 files = [ files ] ;
117123 }
118- var task = { taskNum : taskNum , files : files } ;
119- queue . push ( task , function ( err ) {
120- if ( err ) {
124+ var task = { taskNum : taskNum , files : files , iterationOpts : iterationOpts || { } } ;
125+ queue . push ( task , function ( err ) {
126+ if ( err ) {
121127 _this . emit ( 'error' , err , files ) ;
122128 }
123129 } ) ;
@@ -128,46 +134,25 @@ util.inherits(SpawnMocha, EventEmitter);
128134
129135var mochaStream = function mocha ( opts ) {
130136 opts = opts || { } ;
131- var spawnMocha = new SpawnMocha ( opts ) ;
132- var stream = through ( function write ( file ) {
133- spawnMocha . add ( file . path ) ;
134- } , function ( ) { } ) ;
135- var errors = [ ] ;
136- spawnMocha . on ( 'error' , function ( err ) {
137- console . error ( err . toString ( ) ) ;
138- errors . push ( err ) ;
139- } ) . on ( 'end' , function ( ) {
140- if ( errors . length > 0 ) {
141- console . error ( 'ERROR SUMMARY: ' ) ;
142- _ ( errors ) . each ( function ( err ) {
143- console . error ( err ) ;
144- console . error ( err . stack ) ;
145- } ) . value ( ) ;
146- stream . emit ( 'error' , "Some tests failed." ) ;
147- }
148- stream . emit ( 'end' ) ;
149- } ) ;
150- return stream ;
151- } ;
152-
153- var mochaOptsStream = function mocha ( opts ) {
154137 var files = [ ] ;
155- opts = opts || { } ;
156138 var spawnMocha = new SpawnMocha ( opts ) ;
157139 var stream = through ( function write ( file ) {
158- //spawnMocha.add(file.path);
159- files . push ( file . path ) ;
160- } , function ( ) {
161- spawnMocha . add ( files ) ;
140+ ( opts . iterations ) ? files . push ( file . path ) : spawnMocha . add ( file . path ) ;
141+ } , function ( ) {
142+ if ( opts . iterations ) {
143+ opts . iterations . forEach ( function ( iteration ) {
144+ spawnMocha . add ( files , iteration ) ;
145+ } ) ;
146+ }
162147 } ) ;
163148 var errors = [ ] ;
164- spawnMocha . on ( 'error' , function ( err ) {
149+ spawnMocha . on ( 'error' , function ( err ) {
165150 console . error ( err . toString ( ) ) ;
166151 errors . push ( err ) ;
167- } ) . on ( 'end' , function ( ) {
168- if ( errors . length > 0 ) {
152+ } ) . on ( 'end' , function ( ) {
153+ if ( errors . length > 0 ) {
169154 console . error ( 'ERROR SUMMARY: ' ) ;
170- _ ( errors ) . each ( function ( err ) {
155+ _ ( errors ) . each ( function ( err ) {
171156 console . error ( err ) ;
172157 console . error ( err . stack ) ;
173158 } ) . value ( ) ;
@@ -177,8 +162,8 @@ var mochaOptsStream = function mocha(opts) {
177162 } ) ;
178163 return stream ;
179164} ;
165+
180166module . exports = {
181167 SpawnMocha : SpawnMocha ,
182- mochaStream : mochaStream ,
183- mochaOptsStream : mochaOptsStream
168+ mochaStream : mochaStream
184169} ;
0 commit comments