Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: min_coverage parsing with default to 100 #290

Merged
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Very Good Coverage accepts the following configuration inputs:
| Input name | Description | Default value | Optional |
| ------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------- | -------- |
| path | The absolute path to the lcov.info file. | `"/coverage/lcov.info"` | ✅ |
| min_coverage | The minimum coverage percentage allowed. | `100` | ✅ |
| min_coverage | The minimum coverage percentage allowed. Must be a number between 0 and 100. | `100` | ✅ |
| 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. | `""` | ✅ |

## Example usage
Expand Down
22 changes: 20 additions & 2 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

32 changes: 27 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,17 @@ const fs = require('fs');

function run() {
const lcovPath = core.getInput('path');
const minCoverage = core.getInput('min_coverage');
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;
Expand Down Expand Up @@ -46,15 +54,15 @@ function run() {
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
linesMissingCoverageMessage,
);
} else {
var resultMessage = `Coverage: ${coverage}%.\n`;
Expand Down Expand Up @@ -89,7 +97,7 @@ For example:
uses: VeryGoodOpenSource/very_good_coverage@v2
with:
path: 'my_project/coverage/lcov.info'
`
`,
);
return false;
}
Expand All @@ -99,12 +107,26 @@ For example:
`❌ 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) {
alestiago marked this conversation as resolved.
Show resolved Hide resolved
return null;
}

return minCoverage;
}

run();
41 changes: 39 additions & 2 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,11 @@ test('fails when the coverage is not 100 and min_coverage is not provided', () =
process.env['INPUT_PATH'] = lcovPath;
const ip = path.join(__dirname, 'index.js');
try {
cp.execSync(`node ${ip}`, { env: process.env }).toString();
cp.execSync(`node ${ip}`, { env: process.env });
fail('this code should fail');
} catch (err) {
expect(err).toBeDefined();
const output = getErrorOutput(err);
expect(output).toContain('95 is less than min_coverage 100');
}
});

Expand Down Expand Up @@ -210,3 +211,39 @@ test('reports 0 coverage when no lines are found ', () => {
expect(errorMessage).toContain('0 is less than min_coverage 100');
}
});

test('fails when min_coverage is not a number', () => {
process.env['INPUT_MIN_COVERAGE'] = '10%';
alestiago marked this conversation as resolved.
Show resolved Hide resolved
const ip = path.join(__dirname, 'index.js');
try {
cp.execSync(`node ${ip}`, { env: process.env });
fail('this code should fail');
} catch (err) {
const output = getErrorOutput(err);
expect(output).toContain('❌ Failed to parse min_coverage.');
}
});

test('fails when min_coverage is lower than 0', () => {
process.env['INPUT_MIN_COVERAGE'] = -1;
const ip = path.join(__dirname, 'index.js');
try {
cp.execSync(`node ${ip}`, { env: process.env });
fail('this code should fail');
} catch (err) {
const output = getErrorOutput(err);
expect(output).toContain('❌ Failed to parse min_coverage.');
}
});

test('fails when min_coverage is greater than 100', () => {
process.env['INPUT_MIN_COVERAGE'] = 101;
const ip = path.join(__dirname, 'index.js');
try {
cp.execSync(`node ${ip}`, { env: process.env });
fail('this code should fail');
} catch (err) {
const output = getErrorOutput(err);
expect(output).toContain('❌ Failed to parse min_coverage.');
}
});