-
Notifications
You must be signed in to change notification settings - Fork 66
/
cobertura.js
199 lines (181 loc) · 5.32 KB
/
cobertura.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
const fs = require("fs").promises;
const xml2js = require("xml2js");
const util = require("util");
const glob = require("glob-promise");
const parseString = util.promisify(xml2js.parseString);
/**
* generate the report for the given file
*
* @param path: string
* @param options: object
* @return {Promise<{total: number, line: number, files: T[], branch: number}>}
*/
async function readCoverageFromFile(path, options) {
const xml = await fs.readFile(path, "utf-8");
const { coverage } = await parseString(xml, {
explicitArray: false,
mergeAttrs: true,
});
const { packages } = coverage;
const classes = processPackages(packages);
const files = classes
.filter(Boolean)
.map((klass) => {
return {
...calculateRates(klass),
filename: klass["filename"],
name: klass["name"],
missing: missingLines(klass),
};
})
.filter((file) => options.skipCovered === false || file.total < 100);
return {
...calculateRates(coverage),
files,
};
}
function trimFolder(path, positionOfFirstDiff) {
const lastFolder = path.lastIndexOf("/") + 1;
if (positionOfFirstDiff >= lastFolder) {
return path.substr(lastFolder);
} else {
const startOffset = Math.min(positionOfFirstDiff - 1, lastFolder);
const length = path.length - startOffset - lastFolder - 2; // remove filename
return path.substr(startOffset, length);
}
}
/**
*
* @param path: string
* @param options: {}
* @returns {Promise<{total: number, folder: string, line: number, files: T[], branch: number}[]>}
*/
async function processCoverage(path, options) {
options = options || { skipCovered: false };
const paths = glob.hasMagic(path) ? await glob(path) : [path];
const positionOfFirstDiff = longestCommonPrefix(paths);
return await Promise.all(
paths.map(async (path) => {
const report = await readCoverageFromFile(path, options);
const folder = trimFolder(path, positionOfFirstDiff);
return {
...report,
folder,
};
}),
);
}
function processPackages(packages) {
if (packages.package instanceof Array) {
return packages.package.map((p) => processPackage(p)).flat();
} else if (packages.package) {
return processPackage(packages.package);
} else {
return processPackage(packages);
}
}
function processPackage(packageObj) {
if (packageObj.classes && packageObj.classes.class instanceof Array) {
return packageObj.classes.class;
} else if (packageObj.classes && packageObj.classes.class) {
return [packageObj.classes.class];
} else if (packageObj.class && packageObj.class instanceof Array) {
return packageObj.class;
} else {
return [packageObj.class];
}
}
/**
* returns coverage rates
*
* @param element: object
* @returns {{total: number, line: number, branch: number}}
*/
function calculateRates(element) {
const line = parseFloat(element["line-rate"]) * 100;
const branch = parseFloat(element["branch-rate"]) * 100;
const total = line && branch ? (line + branch) / 2 : line;
return {
total,
line,
branch,
};
}
function getLines(klass) {
if (klass.lines && klass.lines.line instanceof Array) {
return klass.lines.line;
} else if (klass.lines && klass.lines.line) {
return [klass.lines.line];
} else {
return [];
}
}
function missingLines(klass) {
// Bail if line-rate says fully covered
if (parseFloat(klass["line-rate"]) >= 1.0) return "";
const lines = getLines(klass).sort(
(a, b) => parseInt(a.number) - parseInt(b.number),
);
const statements = lines.map((line) => line.number);
const misses = lines
.filter((line) => parseInt(line.hits) < 1)
.map((line) => line.number);
return partitionLines(statements, misses);
}
function partitionLines(statements, lines) {
/*
* Detect sequences, with gaps according to 'statements',
* in 'lines' and compress them in to a range format.
*
* Example:
*
* statements = [1,2,3,4,5,10,11,12,13,14,15,16]
* lines = [1,2, 5,10,11, 13,14, ,16]
* Returns: [[1, 2], [5, 11], [13, 14], [16, 16]]
*/
const ranges = [];
let start = null;
let linesCursor = 0;
let end;
for (const statement of statements) {
if (linesCursor >= lines.length) break;
if (statement === lines[linesCursor]) {
// (Consecutive) element from 'statements' matches
// element from 'lines' at 'linesCursor'
linesCursor += 1;
if (start === null) start = statement;
end = statement;
} else if (start !== null) {
// Consecutive elements are broken, an element from
// 'statements' is missing from 'lines'
ranges.push([start, end]);
start = null;
}
}
// (Eventually) close range running last iteration
if (start !== null) ranges.push([start, end]);
return ranges;
}
/**
*
* @param paths: [string]
* @returns number
*/
function longestCommonPrefix(paths) {
let prefix = "";
if (paths === null || paths.length === 0) return 0;
for (let i = 0; i < paths[0].length; i++) {
const char = paths[0][i]; // loop through all characters of the very first string.
for (let j = 1; j < paths.length; j++) {
// loop through all other strings in the array
if (paths[j][i] !== char) return prefix.length;
}
prefix = prefix + char;
}
return prefix.length;
}
module.exports = {
processCoverage,
trimFolder,
longestCommonPrefix,
};