generated from actions/javascript-action
-
Notifications
You must be signed in to change notification settings - Fork 18
/
index.js
132 lines (114 loc) · 3.58 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
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
const core = require('@actions/core');
const minimatch = require('minimatch');
const parse = require('lcov-parse');
const fs = require('fs');
function run() {
const lcovPath = core.getInput('path');
const minCoverageInput = core.getInput('min_coverage');
const excluded = core.getInput('exclude');
const excludedFiles = excluded.split(' ');
const minCoverage = tryParseMinCoverage(minCoverageInput);
if (minCoverage === null) {
core.setFailed(
'❌ Failed to parse min_coverage. Make sure to enter a valid number between 0 and 100.',
);
return;
}
if (!canParse(lcovPath)) {
return;
}
parse(lcovPath, (err, 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 = totalFinds === 0 ? 0 : (totalHits / totalFinds) * 100;
const isValidBuild = coverage >= minCoverage;
const linesMissingCoverageByFile = Object.entries(linesMissingCoverage).map(
([file, lines]) => {
return `- ${file}: ${lines.join(', ')}`;
},
);
let linesMissingCoverageMessage =
`Lines not covered:\n` +
linesMissingCoverageByFile.map((line) => ` ${line}`).join('\n');
if (!isValidBuild) {
core.setFailed(
`${coverage} is less than min_coverage ${minCoverage}\n\n` +
linesMissingCoverageMessage,
);
} else {
var resultMessage = `Coverage: ${coverage}%.\n`;
if (coverage < 100) {
resultMessage += `${coverage} is greater than or equal to min_coverage ${minCoverage}.\n\n`;
resultMessage += linesMissingCoverageMessage;
}
core.info(resultMessage);
}
});
}
function shouldCalculateCoverageForFile(fileName, excludedFiles) {
for (let i = 0; i < excludedFiles.length; i++) {
const isExcluded = minimatch.minimatch(fileName, excludedFiles[i]);
if (isExcluded) {
core.debug(`Excluding ${fileName} from coverage`);
return false;
}
}
return true;
}
function canParse(path) {
if (!fs.existsSync(path)) {
core.setFailed(
`❌ Failed to find an lcov file at ${path}.
Make sure to generate an lcov file before running VeryGoodCoverage and set the \
path input to the correct location.
For example:
uses: VeryGoodOpenSource/very_good_coverage@v2
with:
path: 'my_project/coverage/lcov.info'
`,
);
return false;
}
if (fs.readFileSync(path).length === 0) {
core.setFailed(
`❌ Found an empty lcov file at "${path}".
An empty lcov file was found but with no coverage data. This might be because \
you have no test files or your tests are not generating any coverage data.
`,
);
return false;
}
return true;
}
function tryParseMinCoverage(input) {
if (input === '') {
return 100;
}
const minCoverage = Number(input);
if (isNaN(minCoverage) || minCoverage < 0 || minCoverage > 100) {
return null;
}
return minCoverage;
}
run();