1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244 | 1
1
15
15
15
15
1
1
15
13
13
13
2
15
1
15
1
15
1
15
1
1
15
5
15
15
15
1
28
28
28
26
23
26
1
2
1
2
28
1
1
26
26
2
1
2
26
26
26
7
26
26
35
26
26
26
1
18
239
239
30
30
1
29
30
30
209
18
209
18
18
1
10
3
3
10
10
10
10
10
10
11
1
10
10
10
10
10
8
2
2
2
2
2
2
1
1
1
11
11
11
11
1
11
3
3
3
2
3
3
11
11
19
19
19
19
19
19
6
11
11
4
4
11
3
11
7
7
7
7
11
7
7
11
10
| var exec = require('child_process').exec,
util = require('./index'),
spawn = require('child_process').spawn,
fs = require('fs'),
path = require('path'),
Stack = require('./stack').Stack,
cover = require('../lib/coverage'),
mkdirp = require('mkdirp'),
log = require('./log'),
wrapper = path.join(__dirname, './wrapper/wrapper.js');
exports.init = function(options, callback) {
options = options || {};
options.paths = options.paths || [];
exports.check(options, function(version) {
if (!version) {
log.error('Please install the phantomjs binary in your path!');
util.exit(1);
}
if (!options.silent && !options.quiet) {
util.log('Starting Grover on ' + options.paths.length + ' files with PhantomJS@' + version);
util.log(' Running ' + options.concurrent + ' concurrent tests at a time.');
if (options.timeout) {
util.log(' Using a ' + options.timeout + ' second timeout per test.');
}
}
if (options.server) {
require('../lib/server').start(options);
}
if (options.exitOnFail) {
util.log('--will exit on first test error');
}
callback(version);
});
};
exports.check = function(options, callback) {
if (typeof options === 'function') {
callback = options;
options = {};
}
if (!('phantom-bin' in options)) {
options['phantom-bin'] = 'phantomjs';
}
exec(options['phantom-bin'] + ' --version', function(stdin, stdout) {
var version = stdout.replace('\n', '');
callback(version);
});
};
var parseResponse = function(options, file, stdout) {
var err = null,
results;
try {
results = JSON.parse(stdout);
if (util.canPrint(options, results)) {
util.status(results);
}
if (options.exitOnFail && results.failed) {
util.exit(1);
}
} catch (e) {
if (options.debug) {
util.error('Failed to parse response for ' + file + ', readding to queue');
}
err = file;
}
return [err, results];
};
exports.parseResponse = parseResponse;
exports.test = function(options, file, callback) {
var args = [
wrapper,
file
],
stdout = '', child;
if (!util.existsSync(file) && !file.match(/^https?:\/\//) && !options.server) {
if (util.canPrint(options)) {
util.error(':( Can not find file: ' + file);
}
util.exit(1);
}
args.push(options.timeout);
args.push(options.console);
if (!('phantom-bin' in options)) {
options['phantom-bin'] = 'phantomjs';
}
child = spawn(options['phantom-bin'], args);
child.stdout.on('data', function(data) {
stdout += data;
});
child.on('exit', function() {
var res = parseResponse(options, file, stdout);
callback(res[0], res[1]);
});
};
exports.dispatch = function(options, callback) {
var stack = new Stack(),
testResults = [],
i,
noop = function() {},
run = function(cb) {
var file = options.paths.shift();
if (file) {
exports.test(options, file, function(err, json) {
if (err) {
options.paths.unshift(file);
} else {
testResults.push(json);
}
run(stack.add(noop));
cb();
});
} else {
cb();
}
};
for (i = 1; i < options.concurrent; i++) {
run(stack.add(noop));
}
stack.done(function() {
callback(null, testResults);
});
};
exports.process = function(args, callback) {
if (typeof args === 'function') {
callback = args;
args = null;
}
args = args || process.argv.slice(2);
var options = require('./options').parse(args),
done = false;
util.init(options);
exports.init(options, function() {
cover.init();
var runTests = function () {
if (done) {
return; //Don't execute more tests
}
options.START = (new Date()).getTime();
exports.dispatch(options, function(err, results) {
done = true;
exports.done(options, results, callback);
});
};
if (options.run){
runTests();
} else {
util.log('not running tests, just serving them.');
process.on('SIGCONT', function() {
util.log('Received parent message, continuing test execution');
runTests();
});
process.on('message', function (msg) {
if (msg['continue']) {
util.log('Received parent message, continuing test execution');
//Reset start timer because we were paused
runTests();
}
});
}
});
};
exports.done = function(options, testResults, callback) {
options = options || {};
testResults = testResults || [];
var res = {
name: 'Total',
passed: 0,
failed: 0,
ignored: 0,
total: 0,
duration: 0
}, proc, output, END;
if (options.server) {
options.httpd.close();
}
if (options.outfile && options.outtype) {
proc = require(path.join(__dirname, '../lib/process'));
output = proc(testResults, options.outtype);
if (util.canPrint(options)) {
util.log('Writing files in ' + options.outtype + ' format to: ' + options.outfile);
}
mkdirp.sync(path.dirname(options.outfile));
fs.writeFileSync(options.outfile, output, 'utf8');
}
cover.options(options);
testResults.forEach(function(json) {
res.passed += json.passed;
res.failed += json.failed;
res.total += json.total;
res.ignored += json.ignored;
res.duration += json.duration;
if (json.coverage) {
cover.set(json.coverage);
}
});
END = (new Date()).getTime();
if (!options.silent && !options.quiet && !options.coverage) {
log.log('----------------------------------------------------------------');
util.status(res, options.START, END);
}
if (res.failed) {
util.exit(1);
}
if (options.coverage) {
cover.report(options);
log.log('----------------------------------------------------------------');
util.status(res, options.START, END);
cover.save(options);
}
if (process.send) {
process.send({ done: true });
util.exit(0);
}
if (callback) {
callback(null, testResults);
}
};
|