6
6
7
7
var util = require ( 'util' ) ,
8
8
path = require ( 'path' ) ,
9
- fs = require ( 'fs' ) ,
10
- glob = require ( 'glob' ) ,
11
9
child = require ( 'child_process' ) ,
12
10
ConfigParser = require ( './configParser' ) ;
13
11
@@ -21,20 +19,20 @@ var noLineLog_ = function(stuff) {
21
19
process . stdout . write ( launcherPrefix + stuff ) ;
22
20
}
23
21
24
- var reportHeader_ = function ( childFork ) {
25
- var capability = childFork . capability ;
22
+ var reportHeader_ = function ( driverInstance ) {
23
+ var capability = driverInstance . capability ;
26
24
var eol = require ( 'os' ) . EOL ;
27
25
28
26
var outputHeader = eol + '------------------------------------' + eol ;
29
- outputHeader += 'PID: ' + childFork . process . pid + ' (capability: ' ;
27
+ outputHeader += 'PID: ' + driverInstance . process . pid + ' (capability: ' ;
30
28
outputHeader += ( capability . browserName ) ?
31
29
capability . browserName : '' ;
32
30
outputHeader += ( capability . version ) ?
33
31
capability . version : '' ;
34
32
outputHeader += ( capability . platform ) ?
35
33
capability . platform : '' ;
36
- outputHeader += ( childFork . runNumber ) ?
37
- ' #' + childFork . runNumber : '' ;
34
+ outputHeader += ( driverInstance . runNumber ) ?
35
+ ' #' + driverInstance . runNumber : '' ;
38
36
outputHeader += ')' + eol ;
39
37
outputHeader += '------------------------------------' + eol ;
40
38
@@ -51,7 +49,7 @@ var reportHeader_ = function(childFork) {
51
49
var init = function ( configFile , additionalConfig ) {
52
50
53
51
var capabilityRunCount ,
54
- childForks = [ ] ,
52
+ driverInstances = [ ] ,
55
53
launcherExitCode = 0 ;
56
54
57
55
var configParser = new ConfigParser ( ) ;
@@ -65,8 +63,8 @@ var init = function(configFile, additionalConfig) {
65
63
66
64
var listRemainingForks = function ( ) {
67
65
var remaining = 0 ;
68
- childForks . forEach ( function ( childFork ) {
69
- if ( ! childFork . done ) {
66
+ driverInstances . forEach ( function ( driverInstance ) {
67
+ if ( ! driverInstance . done ) {
70
68
remaining ++ ;
71
69
}
72
70
} ) ;
@@ -76,11 +74,11 @@ var init = function(configFile, additionalConfig) {
76
74
} ;
77
75
78
76
var logSummary = function ( ) {
79
- childForks . forEach ( function ( childFork ) {
80
- var shortChildName = childFork . capability . browserName +
81
- ( childFork . runNumber ? ' #' + childFork . runNumber : '' ) ;
82
- if ( childFork . failedCount ) {
83
- log_ ( shortChildName + ' failed ' + childFork . failedCount + ' test(s)' ) ;
77
+ driverInstances . forEach ( function ( driverInstance ) {
78
+ var shortChildName = driverInstance . capability . browserName +
79
+ ( driverInstance . runNumber ? ' #' + driverInstance . runNumber : '' ) ;
80
+ if ( driverInstance . failedCount ) {
81
+ log_ ( shortChildName + ' failed ' + driverInstance . failedCount + ' test(s)' ) ;
84
82
} else {
85
83
log_ ( shortChildName + ' passed' ) ;
86
84
}
@@ -95,12 +93,12 @@ var init = function(configFile, additionalConfig) {
95
93
'config.capabilities will be ignored' ) ;
96
94
}
97
95
98
- // Merge ' capabilities' and ' multiCapabilities', if applicable .
96
+ // Use capabilities if multiCapabilities is empty .
99
97
if ( ! config . multiCapabilities . length ) {
100
98
config . multiCapabilities = [ config . capabilities ] ;
101
99
}
102
100
103
- // Loop through capabilities and launch forks of runner.js
101
+ // Loop through capabilities and set up forks of runner.js
104
102
for ( var i = 0 ; i < config . multiCapabilities . length ; i ++ ) {
105
103
106
104
// Determine how many times to run the capability
@@ -109,7 +107,7 @@ var init = function(configFile, additionalConfig) {
109
107
110
108
// Fork the child runners.
111
109
for ( var j = 0 ; j < capabilityRunCount ; j ++ ) {
112
- childForks . push ( {
110
+ driverInstances . push ( {
113
111
configFile : configFile ,
114
112
additionalConfig : additionalConfig ,
115
113
capability : config . multiCapabilities [ i ] ,
@@ -118,67 +116,52 @@ var init = function(configFile, additionalConfig) {
118
116
}
119
117
}
120
118
121
- // If we're launching multiple runners, aggregate output until completion.
122
- // Otherwise, there is a single runner, let's pipe the output straight
123
- // through to maintain realtime reporting.
124
- if ( childForks . length === 1 ) {
125
- var childFork = childForks [ 0 ] ;
126
- childFork . process = child . fork (
127
- __dirname + "/runFromLauncher.js" ,
128
- process . argv . slice ( 2 ) ,
129
- { cwd : process . cwd ( ) } ) ;
130
- reportHeader_ ( childFork ) ;
131
-
132
- childFork . process . send ( {
133
- command : 'run' ,
134
- configFile : childFork . configFile ,
135
- additionalConfig : childFork . additionalConfig ,
136
- capability : childFork . capability
119
+ // If there is a single runner, avoid starting a separate process
120
+ // and print output directly.
121
+ // Otherwise, if we're launching multiple runners, aggregate output until
122
+ // completion.
123
+ if ( driverInstances . length === 1 ) {
124
+ var driverInstance = driverInstances [ 0 ] ;
125
+
126
+ var Runner = require ( './runner' ) ;
127
+ config . capabilities = driverInstance . capability ;
128
+
129
+ var runner = new Runner ( config ) ;
130
+ runner . run ( ) . then ( function ( exitCode ) {
131
+ process . exit ( exitCode ) ;
132
+ } ) . catch ( function ( err ) {
133
+ log_ ( 'Error: ' + err . message ) ;
134
+ process . exit ( 1 ) ;
137
135
} ) ;
138
136
139
- childFork . process . on ( 'error' , function ( err ) {
140
- log_ ( 'Runner Process(' + childFork . process . pid + ') Error: ' + err ) ;
141
- } ) ;
142
-
143
-
144
- childFork . process . on ( 'message' , function ( m ) {
145
- switch ( m . event ) {
146
- case 'testsDone' :
147
- childFork . failedCount = m . failedCount ;
148
- break ;
149
- }
150
- } ) ;
151
-
152
- childFork . process . on ( 'exit' , function ( code , signal ) {
153
- if ( code ) {
154
- log_ ( 'Runner Process Exited With Error Code: ' + code ) ;
155
- launcherExitCode = 1 ;
156
- }
137
+ runner . on ( 'testsDone' , function ( failedCount ) {
138
+ driverInstance . failedCount = failedCount ;
157
139
} ) ;
158
140
} else {
159
- noLineLog_ ( 'Running ' + childForks . length +
141
+ noLineLog_ ( 'Running ' + driverInstances . length +
160
142
' instances of WebDriver' ) ;
161
143
162
144
// Launch each fork and set up listeners
163
- childForks . forEach ( function ( childFork ) {
145
+ driverInstances . forEach ( function ( driverInstance ) {
164
146
165
- childFork . process = child . fork (
166
- __dirname + "/runFromLauncher.js" , process . argv . slice ( 2 ) ,
147
+ driverInstance . process = child . fork (
148
+ __dirname + "/runFromLauncher.js" ,
149
+ process . argv . slice ( 2 ) ,
167
150
{ silent : true , cwd : process . cwd ( ) } ) ;
168
151
169
- childFork . output = '' ;
152
+ driverInstance . output = '' ;
170
153
171
154
// stdin pipe
172
- childFork . process . stdout . on ( 'data' , function ( chunk ) {
173
- childFork . output += chunk ;
155
+ driverInstance . process . stdout . on ( 'data' , function ( chunk ) {
156
+ driverInstance . output += chunk ;
174
157
} ) ;
175
158
176
159
// stderr pipe
177
- childFork . process . stderr . on ( 'data' , function ( chunk ) {
178
- childFork . output += chunk ;
160
+ driverInstance . process . stderr . on ( 'data' , function ( chunk ) {
161
+ driverInstance . output += chunk ;
179
162
} ) ;
180
163
181
- childFork . process . on ( 'message' , function ( m ) {
164
+ driverInstance . process . on ( 'message' , function ( m ) {
182
165
switch ( m . event ) {
183
166
case 'testPass' :
184
167
process . stdout . write ( '.' ) ;
@@ -187,41 +170,44 @@ var init = function(configFile, additionalConfig) {
187
170
process . stdout . write ( 'F' ) ;
188
171
break ;
189
172
case 'testsDone' :
190
- childFork . failedCount = m . failedCount ;
173
+ driverInstance . failedCount = m . failedCount ;
191
174
break ;
192
175
}
193
176
} ) ;
194
177
195
178
// err handler
196
- childFork . process . on ( 'error' , function ( err ) {
197
- log_ ( 'Runner Process(' + childFork . process . pid + ') Error: ' + err ) ;
179
+ driverInstance . process . on ( 'error' , function ( err ) {
180
+ log_ ( 'Runner Process(' + driverInstance . process . pid + ') Error: ' + err ) ;
198
181
} ) ;
199
182
200
183
// exit handlers
201
- childFork . process . on ( 'exit' , function ( code , signal ) {
184
+ driverInstance . process . on ( 'exit' , function ( code , signal ) {
202
185
if ( code ) {
203
186
log_ ( 'Runner Process Exited With Error Code: ' + code ) ;
204
187
launcherExitCode = 1 ;
205
188
}
206
- reportHeader_ ( childFork ) ;
207
- util . puts ( childFork . output ) ;
208
- childFork . done = true ;
189
+ reportHeader_ ( driverInstance ) ;
190
+ util . puts ( driverInstance . output ) ;
191
+ driverInstance . done = true ;
209
192
listRemainingForks ( ) ;
210
193
} ) ;
211
194
212
- childFork . process . send ( {
195
+ driverInstance . process . send ( {
213
196
command : 'run' ,
214
- configFile : childFork . configFile ,
215
- additionalConfig : childFork . additionalConfig ,
216
- capability : childFork . capability
197
+ configFile : driverInstance . configFile ,
198
+ additionalConfig : driverInstance . additionalConfig ,
199
+ capability : driverInstance . capability
217
200
} ) ;
218
201
} ) ;
219
- }
220
202
221
- process . on ( 'exit' , function ( code ) {
222
- logSummary ( ) ;
223
- process . exit ( launcherExitCode ) ;
224
- } ) ;
203
+ process . on ( 'exit' , function ( code ) {
204
+ if ( code ) {
205
+ launcherExitCode = code ;
206
+ }
207
+ logSummary ( ) ;
208
+ process . exit ( launcherExitCode ) ;
209
+ } ) ;
210
+ }
225
211
} ;
226
212
227
213
exports . init = init ;
0 commit comments