/
vows
executable file
·643 lines (565 loc) · 20.1 KB
/
vows
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
#!/usr/bin/env node
var path = require('path'),
fs = require('fs'),
util = require('util'),
glob = require('glob'),
NopStream = require('../lib/utils/nopstream').NopStream,
events = require('events');
//
// Attempt to load Coffee-Script. If it's not available, continue on our
// merry way, if it is available, start searching for `*.coffee` scripts too.
//
var fileExt, specFileExt;
try {
var coffee = require('coffee-script');
if(coffee.register) coffee.register();
fileExt = /\.(js|coffee)$/;
specFileExt = /[.(-|_)]((t|T)est|(s|S)pec)\.(js|coffee)$/;
} catch (_) {
fileExt = /\.js$/;
specFileExt = /[.(-|_)]((t|T)est|(s|S)pec)\.js$/;
}
var inspect = require('eyes').inspector({
stream: null,
styles: { string: 'grey', regexp: 'grey' }
});
var vows = require('../lib/vows');
var cutils = require('../lib/vows/console');
var stylize = require('../lib/vows/console').stylize;
var _reporter = require('../lib/vows/reporters/dot-matrix'), reporter = {
name: _reporter.name
};
var _coverage;
var nodeMinorVersion = parseInt(process.version.split('.')[1], 10);
var help = [
"usage: vows [FILE, ...] [options]",
"",
"options:",
" -v, --verbose Enable verbose output",
" -w, --watch Watch mode",
" -s, --silent Don't report",
" -i, --isolate Run each test in its own vows process",
" -m PATTERN Only run tests matching the PATTERN string",
" -r PATTERN Only run tests matching the PATTERN regexp",
" --json Use JSON reporter",
" --spec Use Spec reporter",
" --tap Use TAP reporter",
" --dot-matrix Use Dot-Matrix reporter",
" --xunit Use xUnit reporter",
" --cover-plain Print plain coverage map if detected",
" --cover-html Write coverage map to \"coverage.html\"",
" --cover-json Write unified coverage map to \"coverage.json\"",
" --cover-xml Write coverage map to \"coverage.xml\" in Emma xml",
" --no-color Don't use terminal colors",
" --version Show version",
" -h, --help You're staring at it"
].join('\n');
var options = {
reporter: reporter,
matcher: /.*/,
watch: false,
coverage: false,
isolate: false,
shuffle: false,
nocolor: !process.stdout.isTTY
};
var files = [];
var wildcardFiles = [];
// Get rid of process runner
// ('node' in most cases)
var arg, args = [], argv = process.argv.slice(2);
// Current directory index,
// and path of test folder.
var root, testFolder;
//
// Parse command-line parameters
//
while (arg = argv.shift()) {
if (arg === __filename) { continue }
if (arg[0] !== '-') {
args.push(arg);
} else {
arg = arg.match(/^--?(.+)/)[1];
if (arg[0] === 'r') {
options.matcher = new(RegExp)(argv.shift());
} else if (arg[0] === 'm') {
options.matcher = (function (str) { // Create an escaped RegExp
var specials = '. * + ? | ( ) [ ] { } \\ ^ ? ! = : $'.split(' ').join('|\\'),
regex = new(RegExp)('(\\' + specials + ')', 'g');
return new(RegExp)(str.replace(regex, '\\$1'));
})(argv.shift());
} else if (arg in options) {
options[arg] = true;
} else {
switch (arg) {
case 'json':
_reporter = require('../lib/vows/reporters/json');
break;
case 'spec':
_reporter = require('../lib/vows/reporters/spec');
break;
case 'tap':
_reporter = require('../lib/vows/reporters/tap');
break;
case 'dot-matrix':
_reporter = require('../lib/vows/reporters/dot-matrix');
break;
case 'silent':
case 's':
_reporter = require('../lib/vows/reporters/silent');
break;
case 'xunit':
_reporter = require('../lib/vows/reporters/xunit');
break;
case 'cover-plain':
options.coverage = true;
_coverage = require('../lib/vows/coverage/report-plain');
break;
case 'cover-html':
options.coverage = true;
_coverage = require('../lib/vows/coverage/report-html');
break;
case 'cover-json':
options.coverage = true;
_coverage = require('../lib/vows/coverage/report-json');
break;
case 'cover-xml':
options.coverage = true;
_coverage = require('../lib/vows/coverage/report-xml');
break;
case 'verbose':
case 'v':
options.verbose = true;
break;
case 'watch':
case 'w':
options.watch = true;
break;
case 'supress-stdout':
options.supressStdout = true;
break;
case 'shuffle':
options.shuffle = true;
break;
case 'isolate':
case 'i':
options.isolate = true;
break;
case 'no-color':
options.nocolor = true;
break;
case 'color':
options.nocolor = false;
break;
case 'no-error':
options.error = false;
break;
case 'version':
console.log('vows ' + vows.version);
process.exit(0);
case 'help':
case 'h':
console.log(help);
process.exit(0);
break;
}
}
}
}
if (options.nocolor) {
cutils.nocolor = true;
inspect = require('eyes').inspector({ stream: null, styles: false });
}
if (options.supressStdout) {
_reporter.setStream && _reporter.setStream(process.stdout);
var devNullStream = null;
if(process.platform === 'win32'){
devNullStream = new NopStream ();
} else {
devNullStream = fs.createWriteStream('/dev/null');
}
process.__defineGetter__('stdout', function () {
return devNullStream;
});
}
if (options.watch) {
options.reporter = reporter = require('../lib/vows/reporters/watch');
}
msg('bin', 'argv', args);
msg('bin', 'options', { reporter: options.reporter.name, matcher: options.matcher });
if (args.length === 0 || options.watch) {
msg('bin', 'discovering', 'folder structure');
root = fs.readdirSync('.');
if (root.indexOf('test') !== -1) {
testFolder = 'test';
} else if (root.indexOf('spec') !== -1) {
testFolder = 'spec';
} else {
abort("runner", "couldn't find test folder");
}
msg('bin', 'discovered', "./" + testFolder);
if (args.length === 0) {
args = paths(testFolder).filter(function (f) {
return specFileExt.test(f);
});
if (options.watch) {
args = args.concat(paths('lib'), paths('src'));
}
}
}
if (! options.watch) {
reporter.report = function (data, filename) {
switch (data[0]) {
case 'subject':
case 'vow':
case 'context':
case 'error':
_reporter.report(data, filename);
break;
case 'end':
(options.verbose || _reporter.name === 'json') &&
_reporter.report(data);
break;
case 'finish':
options.verbose ?
_reporter.print('\n')
:
_reporter.print(' ');
break;
}
};
reporter.reset = function () { _reporter.reset && _reporter.reset() };
reporter.print = _reporter.print;
// add full path if necessary
files = args.map(function (a) {
if(process.platform === 'win32') {
// check if path starts with a valid windows drive name
return (!a.match(/^[a-z]:/i))? path.join(process.cwd(), a) : a;
} else {
// check if path starts at root
return (!a.match(/^\//))? path.join(process.cwd(), a) : a;
}
});
// preprocess the list of files for any wildcards. win32 does not handle wildcards before calling vows
// any paths not containing wildcards are simple returned by wildcard()
files.forEach(function(a) {
wildcardFiles = wildcardFiles.concat(glob.sync(a));
});
// now set up the file list for vows including all the wildcard files
files = wildcardFiles.map(function (a) {
return a.replace(fileExt, '');
});
if (options.shuffle) {
var source = files.slice(0);
files.length = 0;
while (source.length) {
files.push(source.splice(Math.floor(Math.random() * source.length), 1)[0]);
}
}
runSuites(importSuites(files), function (results) {
var status = results.errored ? 2 : (results.broken ? 1 : 0);
!options.verbose && _reporter.print('\n');
msg('runner', 'finish');
_reporter.report(['finish', results], {
write: function (str) {
process.stdout.write(str.replace(/^\n\n/, '\n'));
}
});
try {
if (options.coverage === true && _$jscoverage !== undefined) {
_coverage.report(_$jscoverage);
}
} catch (err) {
// ignore the undefined jscoverage
}
if (process.stdout.write('')) { // Check if stdout is drained
process.exit(status);
} else {
process.stdout.on('drain', function () {
process.exit(status);
});
}
});
} else {
//
// Watch mode
//
(function () {
var pendulum = [
'. ', '.. ', '... ', ' ...',
' ..', ' .', ' .', ' ..',
'... ', '.. ', '. '
];
var strobe = ['.', ' '];
var status,
cue,
current = 0,
running = 0,
lastRun,
colors = ['32m', '33m', '31m'],
timer = setInterval(tick, 100);
process.on('uncaughtException', exception);
process.on('exit', cleanup);
process.on('SIGINT', function () {
process.exit(0);
});
process.on('SIGQUIT', function () {
changed();
});
cursorHide();
// Run every 100ms
function tick() {
if (running && (cue !== strobe)) {
cue = strobe, current = 0;
} else if (!running && (cue !== pendulum)) {
cue = pendulum, current = 0;
}
eraseLine();
lastRun && !running && esc(colors[status.errored ? 2 : (status.broken ? 1 : 0)]);
print(cue[current]);
if (current == cue.length - 1) { current = -1 }
current ++;
esc('39m');
cursorRestore();
}
//
// Utility functions
//
function print(str) { process.stdout.write(str) }
function esc(str) { print("\x1b[" + str) }
function eraseLine() { esc("0K") }
function cursorRestore() { esc("0G") }
function cursorHide() { esc("?25l") }
function cursorShow() { esc("?25h") }
function cleanup() { eraseLine(), cursorShow(), clearInterval(timer), print('\n') }
function exception(err) { print(err.stack || err.message || JSON.stringify(err)), running = 0}
//
// Get a matching test for a given file
//
function getMatchingTest(file, join) {
join || (join = '-');
var testFile;
if (specFileExt.test(file)) {
testFile = path.join(testFolder, file);
}
else {
var root, extension;
_s = file.split('.'), root = _s[0], extension = _s[1];
testFile = path.join(testFolder, root + join + testFolder + "." + extension);
}
try {
fs.statSync(testFile);
} catch (e) {
if (join == '-') {
return getMatchingTest(file, '_');
}
else {
msg('watcher', 'no equivalence found, running all tests.');
testFile = null;
}
}
return testFile;
}
//
// Called when a file has been modified.
// Run the matching tests and change the status.
//
function changed(file) {
status = { honored: 0, broken: 0, errored: 0, pending: 0 };
msg('watcher', 'detected change in', file);
file = getMatchingTest(file);
var files = (specFileExt.test(file) ? [file] : paths(testFolder)).map(function (p) {
return path.join(process.cwd(), p);
}).filter(function (p) {
return specFileExt.test(p);
}).map(function (p) {
var cache = require.main.moduleCache || require.cache;
if (cache[p]) { delete(cache[p]) }
return p;
}).map(function (p) {
return p.replace(fileExt, '');
});
running ++;
runSuites(importSuites(files), function (results) {
delete(results.time);
print(cutils.result(results).join('') + '\n\n');
lastRun = new(Date);
status = results;
running --;
});
}
msg('watcher', 'watching', args);
//
// Watch all relevant files,
// and call `changed()` on change.
//
args.forEach(function (p) {
fs.watchFile(p, function (current, previous) {
if (new(Date)(current.mtime).valueOf() ===
new(Date)(previous.mtime).valueOf()) { return }
else {
changed(p);
}
});
});
})();
}
function runSuites(suites, callback) {
var results = {
honored: 0,
broken: 0,
errored: 0,
pending: 0,
total: 0,
time: 0
};
reporter.reset();
(function run(suites, callback) {
var suite = suites.shift();
if (suite) {
msg('runner', "running", suite.subject + ' ', options.watch ? false : true);
suite.run(options, function (result) {
Object.keys(result).forEach(function (k) {
results[k] += result[k];
});
run(suites, callback);
});
} else {
callback(results);
}
})(suites, callback);
}
function importSuites(files) {
msg(options.watcher ? 'watcher' : 'runner', 'loading', files);
var spawn = require('child_process').spawn;
function cwdname(f) {
return f.replace(process.cwd() + '/', '') + '.js';
}
function wrapSpawn(f) {
f = cwdname(f);
return function (options, callback) {
var args = [process.argv[1], '--json', f],
result;
// --supress-stdout stops the stream too early on win32
if(process.platform !== 'win32') {
args.push( '--supress-stdout');
}
var p = spawn(process.execPath, args);
//
// AvianFlu, you broke the API! OH NOEZ.
// Anyway.
// Since node 0.7.something, semantics of child process events
// changed - `exit` event is emitted when child process exits
// and `close` event is emitted when child's streams stdio streams
// are closed. `exit` event is emitted before `close` event, and
// since we use child's stdio streams, we shouldn't rely on `exit`
// event.
//
p.on(nodeMinorVersion >= 7 ? 'close' : 'exit', function (code) {
callback(
!result ?
{errored: 1, total: 1}
:
result
);
});
var buffer = [];
p.stdout.on('data', function (data) {
data = data.toString().split(/\n/g);
if (data.length == 1) {
buffer.push(data[0]);
} else {
data[0] = buffer.concat(data[0]).join('');
buffer = [data.pop()];
data.forEach(function (data) {
if (data && data !== 'undefined') {
try {
data = JSON.parse(data);
// on win32 we may get back console.log strings which break JSON.parse
if (data && data[0] === 'finish') {
result = data[1];
} else {
reporter.report(data);
}
} catch(e) {
}
}
});
}
});
p.stderr.pipe(process.stderr);
}
}
return files.reduce(options.isolate ? function (suites, f) {
return suites.concat({
run: wrapSpawn(f)
});
} : function (suites, f) {
//f = path.join(process.cwd(), path.relative(process.cwd(),f));
var obj = require(f);
return suites.concat(Object.keys(obj).map(function (s) {
try {
obj[s]._filename = cwdname(f);
} catch (e) {
if (e instanceof TypeError && typeof(obj[s]) === 'undefined') {
abort("runner",
"Caught a TypeError while trying to import " +
"suites: a suite is undefined." +
"Check your exports; are you doing something like " +
"exports.suite = vows.describe('foo')." +
"addBatch({}).run()? If so, remove '.run()'");
} else {
throw e;
}
}
return obj[s];
}));
}, [])
}
//
// Recursively traverse a hierarchy, returning
// a list of all relevant .js files.
//
function paths(dir) {
var paths = [];
try { fs.statSync(dir) }
catch (e) { return [] }
(function traverse(dir, stack) {
stack.push(dir);
var dirPath = stack.join('/');
fs.readdirSync(stack.join('/')).forEach(function (file) {
//
// Skip dotfiles and `vendor` directory before `fs.stat()`ing them.
// Not doing so causes race conditions with Emacs buffer files
// (`.#filename.js`).
//
if (file[0] == '.' || file === 'vendor') {
return;
}
var path = stack.concat([file]).join('/'),
stat = fs.statSync(path);
if (stat.isFile() && fileExt.test(file)) {
paths.push(path);
} else if (stat.isDirectory()) {
traverse(file, stack);
}
});
stack.pop();
})(dir || '.', []);
return paths;
}
function msg(cmd, subject, str, p) {
if (options.verbose) {
var func = p ? process.stdout.write.bind(process.stdout) : console.log;
func( stylize('vows ', 'green')
+ stylize(cmd, 'bold')
+ ' ' + subject + ' '
+ (str ? (typeof(str) === 'string' ? str : inspect(str)) : '')
);
}
}
function abort(cmd, str) {
console.log(stylize('vows ', 'red') + stylize(cmd, 'bold') + ' ' + str);
console.log(stylize('vows ', 'red') + stylize(cmd, 'bold') + ' exiting');
process.exit(-1);
}