This repository was archived by the owner on Mar 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.js
150 lines (139 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
/*
* 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 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';
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();
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();
});
};