-
Notifications
You must be signed in to change notification settings - Fork 11
/
runner.js
123 lines (101 loc) · 3.17 KB
/
runner.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
var vow = require('vow'),
Mocha = require('mocha'),
istanbul = require('istanbul'),
minimatch = require('minimatch'),
_ = require('lodash'),
unmapCoverageObject = require('./unmap-coverage');
function Runner(opts) {
this._opts = opts;
this._mocha = new Mocha({
ui: 'bdd',
reporter: runReporters(opts.diffOpts),
grep: parseRe(opts.grep),
timeout: opts.timeout
});
}
Runner.prototype.run = function (files, params) {
params || (params = {});
var defer = vow.defer(),
mocha = this._mocha,
opts = this._opts;
mocha.files = files;
mocha.run(function (failures) {
function resolvePromise() {
failures ? defer.reject(new Error('tmpl-specs: ' + failures + ' failing')) : defer.resolve();
}
if (params.needCoverage) {
processCoverage(opts.coverage)
.then(resolvePromise)
.fail(function (err) {
defer.reject(err);
});
} else {
resolvePromise();
}
});
return defer.promise();
};
module.exports = Runner;
// Helper allow you to run the multiple reporters
function runReporters(diffOpts) {
var reporters = getReporters();
return function (runner) {
reporters.forEach(function (Reporter) {
Reporter && new Reporter(runner, diffOpts);
});
};
}
function getReporters() {
// TMPL_SPECS_REPORTERS=spec,html,json
var reporters = process.env.BEM_TMPL_SPECS_REPORTERS || 'spec';
reporters = reporters
.split(',')
.map(function (reporter) {
var mock = { options: {} },
_reporter;
try { _reporter = require('./reporters/' + reporter); } catch (err) {}
if (!_reporter) _reporter = Mocha.prototype.reporter.apply(mock, [reporter])._reporter;
return _reporter;
});
return reporters;
}
function processCoverage(coverageOpts) {
var coverage = global.__coverage__;
return coverage ? unmapCoverageObject(coverage)
.then(function (unmapedCoverage) {
return filterAndSaveCoverage(unmapedCoverage, coverageOpts);
}) : vow.resolve();
}
function filterAndSaveCoverage(coverage, opts) {
coverage = filterCoverage(coverage, opts.exclude);
return saveCoverageReport(coverage, opts);
}
function filterCoverage(coverage, exclude) {
return _.omitBy(coverage, function (value, fileName) {
return exclude.some(function (pattern) {
return minimatch(fileName, pattern, { matchBase: true });
});
});
}
function saveCoverageReport(coverage, opts) {
var defer = vow.defer(),
reporter = new istanbul.Reporter(null, opts.reportDirectory);
reporter.addAll(opts.reporters);
var collector = new istanbul.Collector();
collector.add(coverage);
reporter.write(collector, false, function () {
defer.resolve();
});
return defer.promise();
}
var regexpRe = /^\/(.+)\/(i?)$/;
function parseRe(str) {
if (typeof str !== 'string') {
return str;
}
var m = regexpRe.exec(str);
if (!m) {
return str;
}
return new RegExp(m[1], m[2]);
}