Skip to content

Commit

Permalink
fix: throw "lcov is empty" instead of "parsing error!" (#104)
Browse files Browse the repository at this point in the history
  • Loading branch information
alestiago committed Feb 8, 2022
1 parent ff39e7f commit 49de719
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
Empty file added fixtures/lcov.empty.info
Empty file.
16 changes: 15 additions & 1 deletion index.js
@@ -1,14 +1,19 @@
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 minCoverage = core.getInput('min_coverage');
const excluded = core.getInput('exclude');
const excludedFiles = excluded.split(' ');

parse(lcovPath, (_, data) => {
if (!canParse(lcovPath)) {
return;
}

parse(lcovPath, (err, data) => {
if (typeof data === 'undefined') {
core.setFailed('parsing error!');
return;
Expand Down Expand Up @@ -65,4 +70,13 @@ function shouldCalculateCoverageForFile(fileName, excludedFiles) {
return true;
}

function canParse(path) {
if (fs.existsSync(path) && fs.readFileSync(path).length === 0) {
core.setFailed('lcov is empty!');
return false;
}

return true;
}

run();
18 changes: 18 additions & 0 deletions index.test.js
Expand Up @@ -12,6 +12,21 @@ const getErrorOutput = (error) => {
return output;
};

test('empty LCOV throws an error', () => {
const lcovPath = './fixtures/lcov.empty.info';
process.env['INPUT_PATH'] = lcovPath;
const ip = path.join(__dirname, 'index.js');
try {
cp.execSync(`node ${ip}`, { env: process.env });
fail('this code should fail');
} catch (err) {
expect(err).toBeDefined();

const errorMessage = err.stdout.toString();
expect(errorMessage).toContain('lcov is empty!');
}
});

test('invalid LCOV format throws an error', () => {
const lcovPath = './fixtures/lcov.error.info';
process.env['INPUT_PATH'] = lcovPath;
Expand All @@ -21,6 +36,9 @@ test('invalid LCOV format throws an error', () => {
fail('this code should fail');
} catch (err) {
expect(err).toBeDefined();

const errorMessage = err.stdout.toString();
expect(errorMessage).toContain('parsing error!');
}
});

Expand Down

0 comments on commit 49de719

Please sign in to comment.