Skip to content

Commit d17dc51

Browse files
authored
fix: min_coverage parsing with default to 100 (#290)
1 parent 5e6aa77 commit d17dc51

File tree

5 files changed

+88
-11
lines changed

5 files changed

+88
-11
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Very Good Coverage accepts the following configuration inputs:
1919
| Input name | Description | Default value | Optional |
2020
| ------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------- | -------- |
2121
| path | The absolute path to the lcov.info file. | `"/coverage/lcov.info"` ||
22-
| min_coverage | The minimum coverage percentage allowed. | `100` ||
22+
| min_coverage | The minimum coverage percentage allowed. Must be a number between 0 and 100. | `100` ||
2323
| exclude | List of paths to exclude from the coverage report, separated by an empty space. Supports [globs](<https://en.wikipedia.org/wiki/Glob_(programming)>) to describe file patterns. | `""` ||
2424

2525
## Example usage

dist/index.js

+20-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.js

+27-5
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,17 @@ const fs = require('fs');
55

66
function run() {
77
const lcovPath = core.getInput('path');
8-
const minCoverage = core.getInput('min_coverage');
8+
const minCoverageInput = core.getInput('min_coverage');
99
const excluded = core.getInput('exclude');
1010
const excludedFiles = excluded.split(' ');
11+
const minCoverage = tryParseMinCoverage(minCoverageInput);
12+
13+
if (minCoverage === null) {
14+
core.setFailed(
15+
'❌ Failed to parse min_coverage. Make sure to enter a valid number between 0 and 100.',
16+
);
17+
return;
18+
}
1119

1220
if (!canParse(lcovPath)) {
1321
return;
@@ -46,15 +54,15 @@ function run() {
4654
const linesMissingCoverageByFile = Object.entries(linesMissingCoverage).map(
4755
([file, lines]) => {
4856
return `- ${file}: ${lines.join(', ')}`;
49-
}
57+
},
5058
);
5159
let linesMissingCoverageMessage =
5260
`Lines not covered:\n` +
5361
linesMissingCoverageByFile.map((line) => ` ${line}`).join('\n');
5462
if (!isValidBuild) {
5563
core.setFailed(
5664
`${coverage} is less than min_coverage ${minCoverage}\n\n` +
57-
linesMissingCoverageMessage
65+
linesMissingCoverageMessage,
5866
);
5967
} else {
6068
var resultMessage = `Coverage: ${coverage}%.\n`;
@@ -89,7 +97,7 @@ For example:
8997
uses: VeryGoodOpenSource/very_good_coverage@v2
9098
with:
9199
path: 'my_project/coverage/lcov.info'
92-
`
100+
`,
93101
);
94102
return false;
95103
}
@@ -99,12 +107,26 @@ For example:
99107
`❌ Found an empty lcov file at "${path}".
100108
An empty lcov file was found but with no coverage data. This might be because \
101109
you have no test files or your tests are not generating any coverage data.
102-
`
110+
`,
103111
);
104112
return false;
105113
}
106114

107115
return true;
108116
}
109117

118+
function tryParseMinCoverage(input) {
119+
if (input === '') {
120+
return 100;
121+
}
122+
123+
const minCoverage = Number(input);
124+
125+
if (isNaN(minCoverage) || minCoverage < 0 || minCoverage > 100) {
126+
return null;
127+
}
128+
129+
return minCoverage;
130+
}
131+
110132
run();

index.test.js

+39-2
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,11 @@ test('fails when the coverage is not 100 and min_coverage is not provided', () =
107107
process.env['INPUT_PATH'] = lcovPath;
108108
const ip = path.join(__dirname, 'index.js');
109109
try {
110-
cp.execSync(`node ${ip}`, { env: process.env }).toString();
110+
cp.execSync(`node ${ip}`, { env: process.env });
111111
fail('this code should fail');
112112
} catch (err) {
113-
expect(err).toBeDefined();
113+
const output = getErrorOutput(err);
114+
expect(output).toContain('95 is less than min_coverage 100');
114115
}
115116
});
116117

@@ -210,3 +211,39 @@ test('reports 0 coverage when no lines are found ', () => {
210211
expect(errorMessage).toContain('0 is less than min_coverage 100');
211212
}
212213
});
214+
215+
test('fails when min_coverage is not a number', () => {
216+
process.env['INPUT_MIN_COVERAGE'] = '10%';
217+
const ip = path.join(__dirname, 'index.js');
218+
try {
219+
cp.execSync(`node ${ip}`, { env: process.env });
220+
fail('this code should fail');
221+
} catch (err) {
222+
const output = getErrorOutput(err);
223+
expect(output).toContain('❌ Failed to parse min_coverage.');
224+
}
225+
});
226+
227+
test('fails when min_coverage is lower than 0', () => {
228+
process.env['INPUT_MIN_COVERAGE'] = -1;
229+
const ip = path.join(__dirname, 'index.js');
230+
try {
231+
cp.execSync(`node ${ip}`, { env: process.env });
232+
fail('this code should fail');
233+
} catch (err) {
234+
const output = getErrorOutput(err);
235+
expect(output).toContain('❌ Failed to parse min_coverage.');
236+
}
237+
});
238+
239+
test('fails when min_coverage is greater than 100', () => {
240+
process.env['INPUT_MIN_COVERAGE'] = 101;
241+
const ip = path.join(__dirname, 'index.js');
242+
try {
243+
cp.execSync(`node ${ip}`, { env: process.env });
244+
fail('this code should fail');
245+
} catch (err) {
246+
const output = getErrorOutput(err);
247+
expect(output).toContain('❌ Failed to parse min_coverage.');
248+
}
249+
});

0 commit comments

Comments
 (0)