-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathtestrunner.js
230 lines (188 loc) · 5.67 KB
/
testrunner.js
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
var fs = require('fs'),
path = require('path'),
coverage = require('./coverage'),
cp = require('child_process'),
_ = require('underscore'),
freeport = require('freeport'),
log = exports.log = require('./log');
var options,
noop = function() {};
options = exports.options = {
// logging options
log: {
// log assertions overview
assertions: true,
// log expected and actual values for failed tests
errors: true,
// log tests overview
tests: true,
// log summary
summary: true,
// log global summary (all files)
globalSummary: true,
// log coverage
coverage: true,
// log global coverage (all files)
globalCoverage: true,
// log currently testing code file
testing: true
},
// run test coverage tool
coverage: false,
// define dependencies, which are required then before code
deps: null,
// define namespace your code will be attached to on global['your namespace']
namespace: null,
// max amount of ms child can be blocked, after that we assume running an infinite loop
maxBlockDuration: 2000,
// port that should be used for debugging
debug: null
};
/**
* Run one spawned instance with tests
* @param {Object} opts
* @param {Function} callback
*/
function runOne(opts, callback) {
var child;
var pingCheckTimeoutId;
var argv = process.argv.slice();
var debug = opts.debug;
argv.push(JSON.stringify(opts));
if (debug) {
if (typeof debug === 'boolean') {
freeport(function (er, port) {
process.execArgv.push('--debug-brk=' + port);
fork();
});
} else {
process.execArgv.push('--debug-brk=' + debug);
fork();
}
} else {
fork();
}
function kill() {
process.removeListener('exit', kill);
child.kill();
}
function complete(err, data) {
kill();
clearTimeout(pingCheckTimeoutId);
callback(err, data)
}
function fork() {
child = cp.fork(__dirname + '/child.js', argv, {env: process.env});
child.on('message', function (msg) {
switch (msg.event) {
case 'ping':
clearTimeout(pingCheckTimeoutId);
pingCheckTimeoutId = setTimeout(function () {
complete(new Error('Process blocked for too long'));
}, opts.maxBlockDuration);
break;
case 'assertionDone':
log.add('assertions', msg.data);
break;
case 'testDone':
log.add('tests', msg.data);
break;
case 'done':
clearTimeout(pingCheckTimeoutId);
msg.data.code = opts.code.path;
log.add('summaries', msg.data);
if (opts.coverage) {
coverage.add(msg.data.coverage);
msg.data.coverage = coverage.get();
msg.data.coverage.code = msg.data.code;
log.add('coverages', msg.data.coverage);
}
if (opts.log.testing) {
console.log('done');
}
complete(null, msg.data);
break;
case 'uncaughtException':
complete(_.extend(new Error(), msg.data));
break;
}
});
}
process.on('exit', kill);
if (opts.log.testing) {
console.log('\nTesting ', opts.code.path + ' ... ');
}
}
/**
* Make an absolute path from relative
* @param {string|Object} file
* @return {Object}
*/
function absPath(file) {
if (typeof file === 'string') {
file = {path: file};
}
if (file.path.charAt(0) != '/') {
file.path = path.resolve(process.cwd(), file.path);
}
return file;
}
/**
* Convert path or array of paths to array of abs paths
* @param {Array|string} files
* @return {Array}
*/
function absPaths(files) {
var ret = [];
if (Array.isArray(files)) {
files.forEach(function(file) {
ret.push(absPath(file));
});
} else if (files) {
ret.push(absPath(files));
}
return ret;
}
/**
* Run tests in spawned node instance async for every test.
* @param {Object|Array} files
* @param {Function} callback optional
*/
exports.run = function(files, callback) {
var filesCount = 0;
callback || (callback = noop);
if (!Array.isArray(files)) {
files = [files];
}
if (options.coverage || files[0].coverage) coverage.setup(options.coverage);
files.forEach(function(file) {
var opts = _.extend({}, options, file);
!opts.log && (opts.log = {});
opts.deps = absPaths(opts.deps);
opts.code = absPath(opts.code);
opts.tests = absPaths(opts.tests);
runOne(opts, function(err, stat) {
if (err) {
return callback(err, log.stats());
}
filesCount++;
if (filesCount >= files.length) {
_.each(opts.log, function(val, name) {
if (val && log.print[name]) {
log.print[name]();
}
});
// Write coverage report.
if (opts.coverage) coverage.report();
callback(null, log.stats());
}
});
});
};
/**
* Set options
* @param {Object}
*/
exports.setup = function(opts) {
_.extend(options, opts);
};