forked from SBoudrias/gulp-istanbul
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
167 lines (134 loc) · 5.05 KB
/
index.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
'use strict';
var through = require('through2').obj;
var path = require('path');
var checker = require('istanbul-threshold-checker');
// Make sure istanbul is `require`d after the istanbul-threshold-checker to use the istanbul version
// defined in this package.json instead of the one defined in istanbul-threshold-checker.
var istanbul = require('istanbul');
var gutil = require('gulp-util');
var _ = require('lodash');
var Report = istanbul.Report;
var Collector = istanbul.Collector;
var PluginError = gutil.PluginError;
var PLUGIN_NAME = 'gulp-istanbul';
var COVERAGE_VARIABLE = '$$cov_' + new Date().getTime() + '$$';
var plugin = module.exports = function (opts) {
opts = opts || {};
_.defaults(opts, {
coverageVariable: COVERAGE_VARIABLE,
instrumenter: istanbul.Instrumenter
});
opts.includeUntested = opts.includeUntested === true;
var instrumenter = new opts.instrumenter(opts);
return through(function (file, enc, cb) {
cb = _.once(cb);
if (!(file.contents instanceof Buffer)) {
return cb(new PluginError(PLUGIN_NAME, 'streams not supported'));
}
instrumenter.instrument(file.contents.toString(), file.path, function (err, code) {
if (err) {
return cb(new PluginError(
PLUGIN_NAME,
'Unable to parse ' + file.path + '\n\n' + err.message + '\n'
));
}
file.contents = new Buffer(code);
// Parse the blank coverage object from the instrumented file and save it
// to the global coverage variable to enable reporting on non-required
// files, a workaround for
// https://github.com/gotwarlost/istanbul/issues/112
if (opts.includeUntested) {
var instrumentedSrc = file.contents.toString();
var covStubRE = /\{.*"path".*"fnMap".*"statementMap".*"branchMap".*\}/g;
var covStubMatch = covStubRE.exec(instrumentedSrc);
if (covStubMatch !== null) {
var covStub = JSON.parse(covStubMatch[0]);
global[opts.coverageVariable] = global[opts.coverageVariable] || {};
global[opts.coverageVariable][file.path] = covStub;
}
}
return cb(err, file);
});
});
};
plugin.hookRequire = function (options) {
var fileMap = {};
istanbul.hook.unhookRequire();
istanbul.hook.hookRequire(function (path) {
return !!fileMap[path];
}, function (code, path) {
return fileMap[path];
}, options);
return through(function (file, enc, cb) {
// If the file is already required, delete it from the cache otherwise the covered
// version will be ignored.
delete require.cache[path.resolve(file.path)];
fileMap[file.path] = file.contents.toString();
return cb();
});
};
plugin.summarizeCoverage = function (opts) {
opts = opts || {};
if (!opts.coverageVariable) opts.coverageVariable = COVERAGE_VARIABLE;
if (!global[opts.coverageVariable]) throw new Error('no coverage data found, run tests before calling `summarizeCoverage`');
var collector = new Collector();
collector.add(global[opts.coverageVariable]);
return istanbul.utils.summarizeCoverage(collector.getFinalCoverage());
};
plugin.writeReports = function (opts) {
if (typeof opts === 'string') opts = { dir: opts };
opts = opts || {};
var defaultDir = path.join(process.cwd(), 'coverage');
opts = _.defaults(opts, {
coverageVariable: COVERAGE_VARIABLE,
dir: defaultDir,
reporters: [ 'lcov', 'json', 'text', 'text-summary' ],
reportOpts: { dir: opts.dir || defaultDir }
});
var reporters = opts.reporters.map(function(reporter) {
if (reporter.TYPE) Report.register(reporter);
return reporter.TYPE || reporter;
});
var invalid = _.difference(reporters, Report.getReportList());
if (invalid.length) {
// throw before we start -- fail fast
throw new PluginError(PLUGIN_NAME, 'Invalid reporters: ' + invalid.join(', '));
}
reporters = reporters.map(function (r) {
var reportOpts = opts.reportOpts[r] || opts.reportOpts;
return Report.create(r, _.clone(reportOpts));
});
var cover = through();
cover.on('end', function () {
var collector = new Collector();
// Revert to an object if there are no matching source files.
collector.add(global[opts.coverageVariable] || {});
reporters.forEach(function (report) {
report.writeReport(collector, true);
});
}).resume();
return cover;
};
plugin.enforceThresholds = function (opts) {
opts = opts || {};
opts = _.defaults(opts, {
coverageVariable: COVERAGE_VARIABLE
});
var cover = through();
cover.on('end', function () {
var collector = new Collector();
// Revert to an object if there are no macthing source files.
collector.add(global[opts.coverageVariable] || {});
var results = checker.checkFailures(opts.thresholds, collector.getFinalCoverage());
var criteria = function(type) {
return (type.global && type.global.failed) || (type.each && type.each.failed);
};
if (_.some(results, criteria)) {
this.emit('error', new PluginError({
plugin: PLUGIN_NAME,
message: 'Coverage failed'
}));
}
}).resume();
return cover;
};