forked from VeryGoodOpenSource/very_good_coverage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
73 lines (62 loc) · 2.14 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
const core = require('@actions/core');
const minimatch = require('minimatch');
const parse = require('lcov-parse');
function run() {
const lcovPath = core.getInput('path');
const minCoverage = core.getInput('min_coverage');
const excluded = core.getInput('exclude');
const excludedFiles = excluded.split(' ');
parse(lcovPath, (_, data) => {
if (typeof data === 'undefined') {
core.setFailed('parsing error!');
return;
}
const linesMissingCoverage = [];
let totalFinds = 0;
let totalHits = 0;
data.forEach((element) => {
if (shouldCalculateCoverageForFile(element['file'], excludedFiles)) {
totalFinds += element['lines']['found'];
totalHits += element['lines']['hit'];
for (const lineDetails of element['lines']['details']) {
const hits = lineDetails['hit'];
if (hits === 0) {
const fileName = element['file'];
const lineNumber = lineDetails['line'];
linesMissingCoverage[fileName] =
linesMissingCoverage[fileName] || [];
linesMissingCoverage[fileName].push(lineNumber);
}
}
}
});
const coverage = (totalHits / totalFinds) * 100;
const isValidBuild = coverage >= minCoverage;
core.setOutput('coverage', coverage);
core.setOutput('is_valid_build', isValidBuild);
core.setOutput('lines_missing_coverage', linesMissingCoverage.join('\n'));
if (!isValidBuild) {
const linesMissingCoverageByFile = Object.entries(
linesMissingCoverage
).map(([file, lines]) => {
return `${file}: ${lines.join(', ')}`;
});
core.setFailed(
`${coverage} is less than min_coverage ${minCoverage}\n\n` +
'Lines not covered:\n' +
linesMissingCoverageByFile.map((line) => ` ${line}`).join('\n')
);
}
});
}
function shouldCalculateCoverageForFile(fileName, excludedFiles) {
for (let i = 0; i < excludedFiles.length; i++) {
const isExcluded = minimatch(fileName, excludedFiles[i]);
if (isExcluded) {
core.debug(`Excluding ${fileName} from coverage`);
return false;
}
}
return true;
}
run();