-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
87 lines (77 loc) · 2.09 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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
const path = require('path');
const util = require('util');
const cwd = process.cwd();
function p() {
console.log(' ' + util.format.apply(null, arguments));
}
module.exports = CovSummary;
function CovSummary(runner) {
runner.on('end', report);
}
function report() {
var cov = global._$jscoverage || {};
var stmts = 0;
var miss = 0;
p('Coverage Summary:');
var keys = Object.keys(cov);
var longest = keys.length && Math.max.apply(null, keys.map(function(k) {
return path.relative(cwd, k).length;
}));
var header = pad('Name', longest) + ' Stmts Miss Cover Missing';
p(header);
p(repeat('-', header.length));
keys.sort();
keys.forEach(function(filename) {
var data = stats(cov[filename]);
p('%s %s %s %s% %s', pad(path.relative(cwd, filename), longest),
pad(data.stmts, 6), pad(data.miss.length, 6), pad(data.cover, 3),
data.miss);
stmts += data.stmts;
miss += data.miss.length;
});
p(repeat('=', header.length));
p('%s %s %s %s%', pad('TOTAL', longest), pad(stmts, 6), pad(miss, 6),
pad(percent(stmts - miss, stmts), 3));
p('');
}
function stats(data) {
var stmts = 0;
var missed = [];
data.source.forEach(function(line, num) {
num++;
if (data[num] !== undefined) {
stmts++;
if (data[num] === 0) {
missed.push(num);
}
}
});
return {
stmts: stmts,
miss: missed,
cover: percent(stmts - missed.length, stmts)
};
}
function pad(_str, _size) {
var padLeft = _size > 0;
var size = Math.abs(Number(_size));
var str = String(_str);
if (str.length < size) {
var padding = repeat(' ', size - str.length);
if (padLeft) {
str = padding + str;
} else {
str += padding;
}
}
return str;
}
function repeat(str, num) {
return new Array(num + 1).join(str);
}
function percent(part, whole) {
return whole ? Math.round(part / whole * 100) : 100;
}