forked from dylanb/gulp-coverage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
190 lines (172 loc) · 6.49 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
* Copyright (C) 2014 Dylan Barrell, all rights reserved
*
* Licensed under the MIT license
*
*/
var path = require('path');
var fs = require('fs');
var cover = require('./contrib/cover');
var through2 = require('through2');
var gutil = require('gulp-util');
var chalk = require('chalk')
var coverInst;
module.exports.instrument = function (options) {
options = options || {};
cover.cleanup();
cover.init();
if (coverInst) {
coverInst.release();
}
coverInst = cover.cover(options.pattern, options.debugDirectory);
return through2.obj(function (file, encoding, cb) {
if (!file.path) {
this.emit('error', new gutil.PluginError('gulp-coverage', 'Streaming not supported'));
return cb();
}
this.push(file);
cb();
},
function (cb) {
cb();
});
};
module.exports.report = function (options) {
options = options || {};
var reporter = options.reporter || 'html';
var output = options.output || false;
return through2.obj(
function (file, encoding, cb) {
if (!file.path) {
this.emit('error', new gutil.PluginError('gulp-coverage', 'Streaming not supported'));
return cb();
}
cb();
}, function (cb) {
var stats;
if (!coverInst) {
throw new Error('Must call instrument before calling report');
}
stats = coverInst.allStats();
if (output) {
var lines = stats.coverage;
var statements = stats.statements;
var blocks = stats.blocks;
var _format = function(n) {
n = n.toFixed(2);
if (n < 60) {
n = chalk.red(n + '%');
} else if (n < 80) {
n = chalk.yellow(n + '%');
} else {
n = chalk.green(n + '%');
}
return n;
};
console.log(chalk.underline.bold(' Coverage report'));
console.log(' Line coverage: ' + _format(lines));
console.log(' Statement coverage: ' + _format(statements));
console.log(' Block coverage: ' + _format(blocks));
if (stats.uncovered.length > 0) {
console.log();
console.log(chalk.red(' Uncovered files:'));
for (var i = 0; i < stats.uncovered.length; i++) {
console.log(' ' + chalk.red(stats.uncovered[i]));
}
} else {
console.log();
console.log(chalk.green(' All files covered'));
}
console.log();
}
cover.reporters[reporter](stats, options.outFile ? options.outFile : undefined);
this.push({ coverage: stats });
cb();
});
};
module.exports.gather = function () {
return through2.obj(
function (file, encoding, cb) {
if (!file.path) {
this.emit('error', new gutil.PluginError('gulp-coverage', 'Streaming not supported'));
return cb();
}
cb();
}, function (cb) {
var stats;
if (!coverInst) {
throw new Error('Must call instrument before calling report');
}
stats = coverInst.allStats();
this.push({ coverage: stats });
cb();
});
};
module.exports.enforce = function (options) {
options = options || {};
var statements = options.statements || 100,
blocks = options.blocks || 100,
lines = options.lines || 100,
uncovered = options.uncovered;
return through2.obj(
function (data, encoding, cb) {
if (!data.coverage) {
this.emit('error', new gutil.PluginError('gulp-coverage',
'Must call gather or report before calling enforce'));
return cb();
}
if (data.coverage.statements < statements) {
this.emit('error', new gutil.PluginError('gulp-coverage',
'statement coverage of ' + data.coverage.statements +
' does not meet the threshold of ' + statements));
}
if (data.coverage.coverage < lines) {
this.emit('error', new gutil.PluginError('gulp-coverage',
'line coverage of ' + data.coverage.coverage +
' does not meet the threshold of ' + lines));
}
if (data.coverage.blocks < blocks) {
this.emit('error', new gutil.PluginError('gulp-coverage',
'block coverage of ' + data.coverage.blocks +
' does not meet the threshold of ' + blocks));
}
if (data.coverage.uncovered && uncovered !== undefined && data.coverage.uncovered.length > uncovered) {
this.emit('error', new gutil.PluginError('gulp-coverage',
'uncovered files of ' + data.coverage.uncovered.length +
' does not meet the threshold of ' + uncovered));
}
cb();
}, function (cb) {
cb();
});
};
module.exports.format = function (options) {
var reporters = options || [{}];
if (!Array.isArray(reporters)) reporters = [reporters];
return through2.obj(
function (data, encoding, cb) {
var file;
if (!data.coverage) {
this.emit('error', new gutil.PluginError('gulp-coverage',
'Must call gather before calling enforce'));
cb();
return;
}
reporters.forEach(function(opts) {
if (typeof opts === 'string') opts = { reporter: opts };
var reporter = opts.reporter || 'html';
var outfile = opts.outFile || 'coverage.' + reporter;
file = new gutil.File({
base: path.join(__dirname, './'),
cwd: __dirname,
path: path.join(__dirname, './', outfile),
contents: new Buffer(cover.reporters[reporter](data.coverage))
});
file.coverage = data.coverage;
this.push(file);
}, this);
cb();
}, function (cb) {
cb();
});
};