923167922e9daa3b563e2f8830906b0f6b9ddeed.svn-base
6.14 KB
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
/*
Copyright 2012-2015, Yahoo Inc.
Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
*/
'use strict';
const PCT_COLS = 9;
const MISSING_COL = 18;
const TAB_SIZE = 1;
const DELIM = ' |';
const COL_DELIM = '-|';
function padding(num, ch) {
let str = '';
let i;
ch = ch || ' ';
for (i = 0; i < num; i += 1) {
str += ch;
}
return str;
}
function fill(str, width, right, tabs) {
tabs = tabs || 0;
str = String(str);
const leadingSpaces = tabs * TAB_SIZE;
const remaining = width - leadingSpaces;
const leader = padding(leadingSpaces);
let fmtStr = '';
let fillStr;
const strlen = str.length;
if (remaining > 0) {
if (remaining >= strlen) {
fillStr = padding(remaining - strlen);
fmtStr = right ? fillStr + str : str + fillStr;
} else {
fmtStr = str.substring(strlen - remaining);
fmtStr = '... ' + fmtStr.substring(4);
}
}
return leader + fmtStr;
}
function formatName(name, maxCols, level) {
return fill(name, maxCols, false, level);
}
function formatPct(pct, width) {
return fill(pct, width || PCT_COLS, true, 0);
}
function nodeName(node) {
return node.getRelativeName() || 'All files';
}
function depthFor(node) {
let ret = 0;
node = node.getParent();
while (node) {
ret += 1;
node = node.getParent();
}
return ret;
}
function findNameWidth(node, context) {
let last = 0;
const compareWidth = function(node) {
const depth = depthFor(node);
const idealWidth = TAB_SIZE * depth + nodeName(node).length;
if (idealWidth > last) {
last = idealWidth;
}
};
const visitor = {
onSummary(node) {
compareWidth(node);
},
onDetail(node) {
compareWidth(node);
}
};
node.visit(context.getVisitor(visitor));
return last;
}
function makeLine(nameWidth) {
const name = padding(nameWidth, '-');
const pct = padding(PCT_COLS, '-');
const elements = [];
elements.push(name);
elements.push(pct);
elements.push(pct);
elements.push(pct);
elements.push(pct);
elements.push(padding(MISSING_COL, '-'));
return elements.join(COL_DELIM) + COL_DELIM;
}
function tableHeader(maxNameCols) {
const elements = [];
elements.push(formatName('File', maxNameCols, 0));
elements.push(formatPct('% Stmts'));
elements.push(formatPct('% Branch'));
elements.push(formatPct('% Funcs'));
elements.push(formatPct('% Lines'));
elements.push(formatPct('Uncovered Line #s', MISSING_COL));
return elements.join(' |') + ' |';
}
function missingLines(node, colorizer) {
const missingLines = node.isSummary()
? []
: node.getFileCoverage().getUncoveredLines();
return colorizer(formatPct(missingLines.join(','), MISSING_COL), 'low');
}
function missingBranches(node, colorizer) {
const branches = node.isSummary()
? {}
: node.getFileCoverage().getBranchCoverageByLine();
const missingLines = Object.keys(branches)
.filter(key => branches[key].coverage < 100)
.map(key => key);
return colorizer(formatPct(missingLines.join(','), MISSING_COL), 'medium');
}
function isFull(metrics) {
return (
metrics.statements.pct === 100 &&
metrics.branches.pct === 100 &&
metrics.functions.pct === 100 &&
metrics.lines.pct === 100
);
}
function tableRow(
node,
context,
colorizer,
maxNameCols,
level,
skipEmpty,
skipFull
) {
const name = nodeName(node);
const metrics = node.getCoverageSummary();
const isEmpty = metrics.isEmpty();
if (skipEmpty && isEmpty) {
return '';
}
if (skipFull && isFull(metrics)) {
return '';
}
const mm = {
statements: isEmpty ? 0 : metrics.statements.pct,
branches: isEmpty ? 0 : metrics.branches.pct,
functions: isEmpty ? 0 : metrics.functions.pct,
lines: isEmpty ? 0 : metrics.lines.pct
};
const colorize = isEmpty
? function(str) {
return str;
}
: function(str, key) {
return colorizer(str, context.classForPercent(key, mm[key]));
};
const elements = [];
elements.push(colorize(formatName(name, maxNameCols, level), 'statements'));
elements.push(colorize(formatPct(mm.statements), 'statements'));
elements.push(colorize(formatPct(mm.branches), 'branches'));
elements.push(colorize(formatPct(mm.functions), 'functions'));
elements.push(colorize(formatPct(mm.lines), 'lines'));
if (mm.lines === 100) {
elements.push(missingBranches(node, colorizer));
} else {
elements.push(missingLines(node, colorizer));
}
return elements.join(DELIM) + DELIM;
}
function TextReport(opts) {
opts = opts || {};
this.file = opts.file || null;
this.maxCols = opts.maxCols || 0;
this.cw = null;
this.skipEmpty = opts.skipEmpty;
this.skipFull = opts.skipFull;
}
TextReport.prototype.onStart = function(root, context) {
const statsWidth = 4 * (PCT_COLS + 2) + MISSING_COL;
this.cw = context.writer.writeFile(this.file);
this.nameWidth = findNameWidth(root, context);
if (this.maxCols > 0) {
const maxRemaining = this.maxCols - statsWidth - 2;
if (this.nameWidth > maxRemaining) {
this.nameWidth = maxRemaining;
}
}
const line = makeLine(this.nameWidth);
this.cw.println(line);
this.cw.println(tableHeader(this.nameWidth));
this.cw.println(line);
};
TextReport.prototype.onSummary = function(node, context) {
const nodeDepth = depthFor(node);
const row = tableRow(
node,
context,
this.cw.colorize.bind(this.cw),
this.nameWidth,
nodeDepth,
this.skipEmpty,
this.skipFull
);
if (row) {
this.cw.println(row);
}
};
TextReport.prototype.onDetail = function(node, context) {
return this.onSummary(node, context);
};
TextReport.prototype.onEnd = function() {
this.cw.println(makeLine(this.nameWidth));
this.cw.close();
};
module.exports = TextReport;