forked from chrisdickinson/node-runforcover
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
278 lines (231 loc) · 7.88 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
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
var bunker = require('bunker'),
Module = require('module').Module,
path = require('path'),
fs = require('fs'),
vm = require('vm'),
html_formatter = require('./formatters/html'),
plain_formatter = require('./formatters/plain'),
json_formatter = require('./formatters/json');
function CoverageData (filename, bunker) {
this.bunker = bunker;
this.filename = filename;
this.nodes = {};
};
CoverageData.prototype.visit = function(node) {
++(this.nodes[node.id] = this.nodes[node.id] || {node:node, count:0}).count;
};
CoverageData.prototype.missing = function() {
var nodes = this.nodes,
missing = this.bunker.nodes.filter(function(node) {
return !nodes[node.id];
});
return missing;
};
CoverageData.prototype.seen = function() {
var nodes = this.nodes,
seen = this.bunker.nodes.filter(function(node) {
return !!nodes[node.id];
});
return seen;
};
// Explode all multi-line nodes into single-line ones.
var explodeNodes = function(coverageData, fileData) {
var missing = coverageData.missing();
var newNodes = [];
// Get only the multi-line nodes.
var multiLineNodes = missing.filter(function(node) {
return (node.node[0].start.line < node.node[0].end.line);
});
for(var i = 0; i < multiLineNodes.length; i++) {
// Get the current node and delta
var node = multiLineNodes[i];
var lineDelta = node.node[0].end.line - node.node[0].start.line + 1;
for(var j = 0; j < lineDelta; j++) {
// For each line in the multi-line node, we'll create a
// new node, and we set the start and end columns
// to the correct vlaues.
var curLine = node.node[0].start.line + j;
var startCol = 0;
var endCol = fileData[curLine].length;;
if (curLine === node.node[0].start.line) {
startCol = node.node[0].start.col;
}
else if (curLine === node.node[0].end.line) {
startCol = 0;
endCol = node.node[0].end.col;
}
var newNode = {
node: [
{
start: {
line: curLine,
col: startCol
},
end: {
line: curLine,
col: endCol
}
}
]
};
newNodes.push(newNode);
}
}
return newNodes;
}
CoverageData.prototype.coverage = function() {
var missingLines = this.missing(),
fileData = fs.readFileSync(this.filename, 'utf8').split('\n');
// Get a dictionary of all the lines we did observe being at least
// partially covered
seen = {};
this.seen().forEach(function(node) {
seen[node.node[0].start.line] = true;
});
// Add all the new multi-line nodes.
missingLines = missingLines.concat(explodeNodes(this, fileData));
var seenNodes = {};
missingLines = missingLines.sort(
function(lhs, rhs) {
var lhsNode = lhs.node[0];
var rhsNode = rhs.node[0];
// First try to sort based on line
return lhsNode.start.line < rhsNode.start.line ? -1 : // first try line
lhsNode.start.line > rhsNode.start.line ? 1 :
lhsNode.start.col < rhsNode.start.col ? -1 : // then try start col
lhsNode.start.col > rhsNode.start.col ? 1 :
lhsNode.end.col < rhsNode.end.col ? -1 : // then try end col
lhsNode.end.col > rhsNode.end.col ? 1 :
0; // then just give up and say they are equal
}).filter(
function(node) {
// If it is a multi-line node, we can just ignore it
if (node.node[0].start.line < node.node[0].end.line) {
return false;
}
// We allow multiple nodes per line, but only one node per
// start column (due to how bunker works)
var okay = false;
if (seenNodes.hasOwnProperty(node.node[0].start.line)) {
var isNew = (seenNodes[node.node[0].start.line].indexOf(node.node[0].start.col) < 0);
if (isNew) {
seenNodes[node.node[0].start.line].push(node.node[0].start.col);
okay = true;
}
}
else {
seenNodes[node.node[0].start.line] = [node.node[0].start.col];
okay = true;
}
return okay;
});
var coverage = {};
missingLines.forEach(function(node) {
var line = node.node[0].start.line + 1;
var startCol = node.node[0].start.col;
var endCol = node.node[0].end.col;
var source = fileData[line - 1];
var partial = seen.hasOwnProperty(line - 1) && seen[line - 1];
if (coverage.hasOwnProperty(line)) {
coverage[line].missing.push({startCol: startCol, endCol: endCol});
}
else {
coverage[line] = {
partial: partial,
source: source,
missing: [{startCol: startCol, endCol: endCol}]
};
}
});
return coverage;
};
CoverageData.prototype.stats = function() {
var missing = this.missing(),
filedata = fs.readFileSync(this.filename, 'utf8').split('\n');
var seenLines = [],
lines =
missing.sort(function(lhs, rhs) {
return lhs.node[0].start.line < rhs.node[0].start.line ? -1 :
lhs.node[0].start.line > rhs.node[0].start.line ? 1 :
0;
}).filter(function(node) {
var okay = (seenLines.indexOf(node.node[0].start.line) < 0);
if(okay)
seenLines.push(node.node[0].start.line);
return okay;
}).map(function(node, idx, all) {
return {
lineno:node.node[0].start.line + 1,
source:function() { return filedata[node.node[0].start.line]; }
};
});
return {
percentage:(filedata.length-seenLines.length)/filedata.length,
lines:lines,
missing:seenLines.length,
seen:(filedata.length-seenLines.length),
coverage: this.coverage()
};
};
module.exports.createEnvironment = function(module, filename) {
var req = function(path) {
return Module._load(path, module);
};
req.resolve = function(request) {
return Module._resolveFilename(request, module)[1];
}
req.paths = Module._paths;
req.main = process.mainModule;
req.extensions = Module._extensions;
req.registerExtension = function() {
throw new Error('require.registerExtension() removed. Use ' +
'require.extensions instead.');
}
require.cache = Module._cache;
var ctxt = {};
for(var k in global)
ctxt[k] = global[k];
ctxt.require = req;
ctxt.exports = module.exports;
ctxt.__filename = filename;
ctxt.__dirname = path.dirname(filename);
ctxt.process = process;
ctxt.console = console;
ctxt.module = module;
ctxt.global = ctxt;
return ctxt;
};
module.exports.formatters = {
html: html_formatter,
plain: plain_formatter,
json: json_formatter
};
module.exports.cover = function(fileRegex) {
var originalRequire = require.extensions['.js'],
coverageData = {},
match = fileRegex instanceof RegExp ?
fileRegex : new RegExp(
fileRegex ? fileRegex.replace(/\//g, '\\/').replace(/\./g, '\\.') : '.*'
, ''),
target = this;
require.extensions['.js'] = function(module, filename) {
if(!match.test(filename)) return originalRequire(module, filename);
var context = target.createEnvironment(module, filename),
data = fs.readFileSync(filename, 'utf8'),
bunkerized = bunker(data),
coverage = coverageData[filename] = new CoverageData(filename, bunkerized);
bunkerized.on('node', coverage.visit.bind(coverage));
bunkerized.assign(context);
var wrapper = '(function(ctxt) { with(ctxt) { return '+Module.wrap(bunkerized.compile())+'; } })',
compiledWrapper = vm.runInThisContext(wrapper, filename, true)(context);
var args = [context.exports, context.require, module, filename, context.__dirname];
return compiledWrapper.apply(module.exports, args);
};
var retval = function(ready) {
ready(coverageData);
};
retval.release = function() {
require.extensions['.js'] = originalRequire;
};
return retval;
};