Skip to content

fix: min_coverage parsing with default to 100 #290

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

Merged
19 changes: 17 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ 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 = parseMinCoverage(minCoverageInput);

if (!canParse(lcovPath)) {
if (minCoverage === null || !canParse(lcovPath)) {
return;
}

Expand Down Expand Up @@ -107,4 +108,18 @@ you have no test files or your tests are not generating any coverage data.
return true;
}

function parseMinCoverage(input) {
if (input === '') {
return 100;
}

const asNumber = Number(input);
if (isNaN(asNumber)) {
core.setFailed('❌ Failed to parse min_coverage. Make sure to enter a valid number.');
return null;
}

return asNumber;
}

run();
19 changes: 17 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,17 @@ 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%';
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. Make sure to enter a valid number.',
);
}
});