-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcoverage.js
125 lines (95 loc) · 4.04 KB
/
coverage.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
const convert = require('xml-js');
const fs = require('fs');
const glob = require('@actions/glob');
const { fail } = require('./core');
const { fetchBaseCoverage, fetchBaseDetailedCoverages, fetchHistory } = require('./remote');
const { retrieveDetailedFilesElements, retrieveGlobalMetricsElement, retrieveMetricsElement } = require('./xml');
const compareDetailedCoverages = (oldCoverages, newCoverages) => {
const out = {
degraded: [],
improved: []
};
for (const filename of Object.keys(oldCoverages)) {
if (
typeof newCoverages[filename] === 'undefined' ||
oldCoverages[filename].coverage === null ||
newCoverages[filename].coverage === oldCoverages[filename].coverage
) {
continue;
}
const oldCoverage = Number(oldCoverages[filename].coverage).valueOf();
const newCoverage = Number(newCoverages[filename].coverage).valueOf();
out[newCoverage < oldCoverage ? 'degraded' : 'improved'].push({
filename,
old: `${oldCoverages[filename].covered} / ${oldCoverages[filename].total} (${oldCoverages[filename].coverage}%)`,
new: `${newCoverages[filename].covered} / ${newCoverages[filename].total} (${newCoverages[filename].coverage}%)`
});
}
return out.degraded.length === 0 && out.improved.length === 0 ? null : out;
}
const extractCoverageFromMetricsElement = (metrics) => {
const total = parseInt(metrics.attributes.elements, 10);
const covered = parseInt(metrics.attributes.coveredelements, 10);
const coverage = parseFloat((100 * covered / total).toFixed(3));
return { total, covered, coverage };
}
const extractDetailedCoverages = (json) => {
const out = {};
for (const fileElement of retrieveDetailedFilesElements(json)) {
out[fileElement.attributes.name.replace('/home/runner/work/', '')] = extractCoverageFromMetricsElement(retrieveMetricsElement(fileElement));
}
return out;
}
const parseCoverage = async (file) => {
const globber = await glob.create(file);
const files = await globber.glob();
if (files.length === 0) {
fail('Coverage file not found :/');
}
const options = {ignoreComment: true, alwaysChildren: true};
const json = convert.xml2js(fs.readFileSync(files[0], {encoding: 'utf8'}), options);
return {
overall: extractCoverageFromMetricsElement(retrieveGlobalMetricsElement(json)),
detailed: extractDetailedCoverages(json)
};
}
const parseCoverages = async (coverageFiles) => {
const reports = {};
for (const file of coverageFiles) {
console.log(`Parsing ${file.coverage}...`);
reports[file.summary] = await parseCoverage(file.coverage);
console.log(`Parsed ${file.coverage}`);
}
return reports;
};
const retrieveBaseCoverage = async (summaryFile, coverageBranch) => {
const baseCoverageResult = await fetchBaseCoverage(summaryFile, coverageBranch);
if (baseCoverageResult.status === 404) {
return null;
}
return await baseCoverageResult.json();
}
const retrieveBaseDetailedCoverages = async (summaryFile, coverageBranch) => {
const baseDetailedCoveragesResult = await fetchBaseDetailedCoverages(summaryFile, coverageBranch);
if (baseDetailedCoveragesResult.status === 404) {
return null;
}
return await baseDetailedCoveragesResult.json();
}
const retrieveHistory = async (coverageBranch, historyFilename) => {
const historyFile = await fetchHistory(coverageBranch, historyFilename);
return historyFile.status === 200 ? (await historyFile.json()) : {};
};
const sumCoverages = coverages => {
const out = {
total: 0,
covered: 0
};
for (const coverage of Object.values(coverages)) {
out.total += coverage.total;
out.covered += coverage.covered;
}
out.coverage = parseFloat((100 * out.covered / out.total).toFixed(3));
return out;
}
export { compareDetailedCoverages, parseCoverages, retrieveBaseCoverage, retrieveBaseDetailedCoverages, retrieveHistory, sumCoverages };