c155643843e6d2bbe72a6a93bc3b7262ecc81f07.svn-base
7.04 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
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
278
279
280
281
282
283
284
285
/*
Copyright 2012-2015, Yahoo Inc.
Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
*/
'use strict';
const util = require('util');
const coverage = require('istanbul-lib-coverage');
const Path = require('./path');
const tree = require('./tree');
const BaseNode = tree.Node;
const BaseTree = tree.Tree;
function ReportNode(path, fileCoverage) {
this.path = path;
this.parent = null;
this.fileCoverage = fileCoverage;
this.children = [];
}
util.inherits(ReportNode, BaseNode);
ReportNode.prototype.addChild = function(child) {
child.parent = this;
this.children.push(child);
};
ReportNode.prototype.asRelative = function(p) {
/* istanbul ignore if */
if (p.substring(0, 1) === '/') {
return p.substring(1);
}
return p;
};
ReportNode.prototype.getQualifiedName = function() {
return this.asRelative(this.path.toString());
};
ReportNode.prototype.getRelativeName = function() {
const parent = this.getParent();
const myPath = this.path;
let relPath;
let i;
const parentPath = parent ? parent.path : new Path([]);
if (parentPath.ancestorOf(myPath)) {
relPath = new Path(myPath.elements());
for (i = 0; i < parentPath.length; i += 1) {
relPath.shift();
}
return this.asRelative(relPath.toString());
}
return this.asRelative(this.path.toString());
};
ReportNode.prototype.getParent = function() {
return this.parent;
};
ReportNode.prototype.getChildren = function() {
return this.children;
};
ReportNode.prototype.isSummary = function() {
return !this.fileCoverage;
};
ReportNode.prototype.getFileCoverage = function() {
return this.fileCoverage;
};
ReportNode.prototype.getCoverageSummary = function(filesOnly) {
const cacheProp = 'c_' + (filesOnly ? 'files' : 'full');
let summary;
if (this.hasOwnProperty(cacheProp)) {
return this[cacheProp];
}
if (!this.isSummary()) {
summary = this.getFileCoverage().toSummary();
} else {
let count = 0;
summary = coverage.createCoverageSummary();
this.getChildren().forEach(child => {
if (filesOnly && child.isSummary()) {
return;
}
count += 1;
summary.merge(child.getCoverageSummary(filesOnly));
});
if (count === 0 && filesOnly) {
summary = null;
}
}
this[cacheProp] = summary;
return summary;
};
function treeFor(root, childPrefix) {
const tree = new BaseTree();
const maybePrefix = function(node) {
if (childPrefix && !node.isRoot()) {
node.path.unshift(childPrefix);
}
};
tree.getRoot = function() {
return root;
};
const visitor = {
onDetail(node) {
maybePrefix(node);
},
onSummary(node) {
maybePrefix(node);
node.children.sort((a, b) => {
const astr = a.path.toString();
const bstr = b.path.toString();
return astr < bstr
? -1
: astr > bstr
? 1
: /* istanbul ignore next */ 0;
});
}
};
tree.visit(visitor);
return tree;
}
function findCommonParent(paths) {
if (paths.length === 0) {
return new Path([]);
}
let common = paths[0];
let i;
for (i = 1; i < paths.length; i += 1) {
common = common.commonPrefixPath(paths[i]);
if (common.length === 0) {
break;
}
}
return common;
}
function toInitialList(coverageMap) {
const ret = [];
coverageMap.files().forEach(filePath => {
const p = new Path(filePath);
const coverage = coverageMap.fileCoverageFor(filePath);
ret.push({
filePath,
path: p,
fileCoverage: coverage
});
});
const commonParent = findCommonParent(ret.map(o => o.path.parent()));
if (commonParent.length > 0) {
ret.forEach(o => {
o.path.splice(0, commonParent.length);
});
}
return {
list: ret,
commonParent
};
}
function toDirParents(list) {
const nodeMap = Object.create(null);
const parentNodeList = [];
list.forEach(o => {
const node = new ReportNode(o.path, o.fileCoverage);
const parentPath = o.path.parent();
let parent = nodeMap[parentPath.toString()];
if (!parent) {
parent = new ReportNode(parentPath);
nodeMap[parentPath.toString()] = parent;
parentNodeList.push(parent);
}
parent.addChild(node);
});
return parentNodeList;
}
function foldIntoParents(nodeList) {
const ret = [];
let i;
let j;
// sort by longest length first
nodeList.sort((a, b) => -1 * Path.compare(a.path, b.path));
for (i = 0; i < nodeList.length; i += 1) {
const first = nodeList[i];
let inserted = false;
for (j = i + 1; j < nodeList.length; j += 1) {
const second = nodeList[j];
if (second.path.ancestorOf(first.path)) {
second.addChild(first);
inserted = true;
break;
}
}
if (!inserted) {
ret.push(first);
}
}
return ret;
}
function createRoot() {
return new ReportNode(new Path([]));
}
function createNestedSummary(coverageMap) {
const flattened = toInitialList(coverageMap);
const dirParents = toDirParents(flattened.list);
const topNodes = foldIntoParents(dirParents);
if (topNodes.length === 0) {
return treeFor(new ReportNode(new Path([])));
}
if (topNodes.length === 1) {
return treeFor(topNodes[0]);
}
const root = createRoot();
topNodes.forEach(node => {
root.addChild(node);
});
return treeFor(root);
}
function createPackageSummary(coverageMap) {
const flattened = toInitialList(coverageMap);
const dirParents = toDirParents(flattened.list);
const common = flattened.commonParent;
let prefix;
let root;
if (dirParents.length === 1) {
root = dirParents[0];
} else {
root = createRoot();
// if one of the dirs is itself the root,
// then we need to create a top-level dir
dirParents.forEach(dp => {
if (dp.path.length === 0) {
prefix = 'root';
}
});
if (prefix && common.length > 0) {
prefix = common.elements()[common.elements().length - 1];
}
dirParents.forEach(node => {
root.addChild(node);
});
}
return treeFor(root, prefix);
}
function createFlatSummary(coverageMap) {
const flattened = toInitialList(coverageMap);
const list = flattened.list;
const root = createRoot();
list.forEach(o => {
const node = new ReportNode(o.path, o.fileCoverage);
root.addChild(node);
});
return treeFor(root);
}
module.exports = {
createNestedSummary,
createPackageSummary,
createFlatSummary
};