Skip to content

Commit

Permalink
chore(plugin-coverage): skip missing coverage for empty-report function
Browse files Browse the repository at this point in the history
  • Loading branch information
Tlacenka committed May 29, 2024
1 parent 37b3283 commit 68e2494
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 18 deletions.
16 changes: 3 additions & 13 deletions e2e/cli-e2e/tests/__snapshots__/collect.e2e.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,6 @@ exports[`CLI collect > should run Code coverage plugin that runs coverage tool a
"description": "Measures how many functions were called in at least one test.",
"details": {
"issues": [
{
"message": "Function (empty-report) is not called in any test case.",
"severity": "error",
"source": {
"file": "examples/react-todos-app/src/index.jsx",
"position": {
"startLine": 1,
},
},
},
{
"message": "Function onSubmit is not called in any test case.",
"severity": "error",
Expand Down Expand Up @@ -76,11 +66,11 @@ exports[`CLI collect > should run Code coverage plugin that runs coverage tool a
},
],
},
"displayValue": "50 %",
"score": 0.5,
"displayValue": "56 %",
"score": 0.5556,
"slug": "function-coverage",
"title": "Function coverage",
"value": 50,
"value": 56,
},
{
"description": "Measures how many branches were executed after conditional statements in at least one test.",
Expand Down
2 changes: 2 additions & 0 deletions packages/plugin-coverage/src/lib/runner/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ export const PLUGIN_CONFIG_PATH = join(
WORKDIR,
'plugin-config.json',
);

export const INVALID_FUNCTION_NAME = '(empty-report)';
35 changes: 30 additions & 5 deletions packages/plugin-coverage/src/lib/runner/lcov/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,26 @@ import { LCOVRecord } from 'parse-lcov';
import { AuditOutput, Issue } from '@code-pushup/models';
import { toNumberPrecision, toOrdinal } from '@code-pushup/utils';
import { CoverageType } from '../../config';
import { INVALID_FUNCTION_NAME } from '../constants';
import { LCOVStat } from './types';
import { calculateCoverage, mergeConsecutiveNumbers } from './utils';

export function lcovReportToFunctionStat(record: LCOVRecord): LCOVStat {
const validRecord = removeEmptyReport(record);

return {
totalFound: record.functions.found,
totalHit: record.functions.hit,
totalFound: validRecord.functions.found,
totalHit: validRecord.functions.hit,
issues:
record.functions.hit < record.functions.found
? record.functions.details
validRecord.functions.hit < validRecord.functions.found
? validRecord.functions.details
.filter(detail => !detail.hit)
.map(
(detail): Issue => ({
message: `Function ${detail.name} is not called in any test case.`,
severity: 'error',
source: {
file: record.file,
file: validRecord.file,
position: { startLine: detail.line },
},
}),
Expand All @@ -27,6 +30,28 @@ export function lcovReportToFunctionStat(record: LCOVRecord): LCOVStat {
};
}

function removeEmptyReport(record: LCOVRecord): LCOVRecord {
const validFunctions = record.functions.details.filter(
detail => detail.name !== INVALID_FUNCTION_NAME,
);

if (validFunctions.length === record.functions.found) {
return record;
}

Check failure on line 40 in packages/plugin-coverage/src/lib/runner/lcov/transform.ts

View workflow job for this annotation

GitHub Actions / Code PushUp

<↗> Code coverage | Branch coverage

1st branch is not taken in any test case.

return {
...record,
functions: {
details: validFunctions,
found: validFunctions.length,
hit: validFunctions.reduce(
(acc, fn) => acc + (fn.hit != null && fn.hit > 0 ? 1 : 0),

Check failure on line 48 in packages/plugin-coverage/src/lib/runner/lcov/transform.ts

View workflow job for this annotation

GitHub Actions / Code PushUp

<↗> Code coverage | Branch coverage

1st branch is not taken in any test case.
0,
),
},
};
}

Check warning on line 53 in packages/plugin-coverage/src/lib/runner/lcov/transform.ts

View workflow job for this annotation

GitHub Actions / Code PushUp

<↗> Code coverage | Line coverage

Lines 41-53 are not covered in any test case.

export function lcovReportToLineStat(record: LCOVRecord): LCOVStat {
const missingCoverage = record.lines.hit < record.lines.found;
const lines = missingCoverage
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { LCOVRecord } from 'parse-lcov';
import { describe, it } from 'vitest';
import type { AuditOutput, Issue } from '@code-pushup/models';
import { INVALID_FUNCTION_NAME } from '../constants';
import {
lcovCoverageToAuditOutput,
lcovReportToBranchStat,
Expand Down Expand Up @@ -92,6 +93,26 @@ describe('lcovReportToFunctionStat', () => {
}),
);
});

it('should skip a record of uncovered invalid function called (empty-report)', () => {
expect(
lcovReportToFunctionStat({
...lcovRecordMock,
functions: {
hit: 1,
found: 2,
details: [
{ line: 1, name: INVALID_FUNCTION_NAME, hit: 0 },
{ line: 5, name: 'transform', hit: 4 },
],
},
}),
).toStrictEqual<LCOVStat>({
totalFound: 1,
totalHit: 1,
issues: [],
});
});
});

describe('lcovReportToLineStat', () => {
Expand Down

0 comments on commit 68e2494

Please sign in to comment.