Skip to content

Commit 969808a

Browse files
committed
chore(plugin-coverage): skip missing coverage for empty-report function
1 parent abe0cd2 commit 969808a

4 files changed

Lines changed: 56 additions & 18 deletions

File tree

e2e/cli-e2e/tests/__snapshots__/collect.e2e.test.ts.snap

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,6 @@ exports[`CLI collect > should run Code coverage plugin that runs coverage tool a
2424
"description": "Measures how many functions were called in at least one test.",
2525
"details": {
2626
"issues": [
27-
{
28-
"message": "Function (empty-report) is not called in any test case.",
29-
"severity": "error",
30-
"source": {
31-
"file": "examples/react-todos-app/src/index.jsx",
32-
"position": {
33-
"startLine": 1,
34-
},
35-
},
36-
},
3727
{
3828
"message": "Function onSubmit is not called in any test case.",
3929
"severity": "error",
@@ -76,11 +66,11 @@ exports[`CLI collect > should run Code coverage plugin that runs coverage tool a
7666
},
7767
],
7868
},
79-
"displayValue": "50 %",
80-
"score": 0.5,
69+
"displayValue": "56 %",
70+
"score": 0.5556,
8171
"slug": "function-coverage",
8272
"title": "Function coverage",
83-
"value": 50,
73+
"value": 56,
8474
},
8575
{
8676
"description": "Measures how many branches were executed after conditional statements in at least one test.",

packages/plugin-coverage/src/lib/runner/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@ export const PLUGIN_CONFIG_PATH = join(
88
WORKDIR,
99
'plugin-config.json',
1010
);
11+
12+
export const INVALID_FUNCTION_NAME = '(empty-report)';

packages/plugin-coverage/src/lib/runner/lcov/transform.ts

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,26 @@ import { LCOVRecord } from 'parse-lcov';
22
import { AuditOutput, Issue } from '@code-pushup/models';
33
import { toNumberPrecision, toOrdinal } from '@code-pushup/utils';
44
import { CoverageType } from '../../config';
5+
import { INVALID_FUNCTION_NAME } from '../constants';
56
import { LCOVStat } from './types';
67
import { calculateCoverage, mergeConsecutiveNumbers } from './utils';
78

89
export function lcovReportToFunctionStat(record: LCOVRecord): LCOVStat {
10+
const validRecord = removeEmptyReport(record);
11+
912
return {
10-
totalFound: record.functions.found,
11-
totalHit: record.functions.hit,
13+
totalFound: validRecord.functions.found,
14+
totalHit: validRecord.functions.hit,
1215
issues:
13-
record.functions.hit < record.functions.found
14-
? record.functions.details
16+
validRecord.functions.hit < validRecord.functions.found
17+
? validRecord.functions.details
1518
.filter(detail => !detail.hit)
1619
.map(
1720
(detail): Issue => ({
1821
message: `Function ${detail.name} is not called in any test case.`,
1922
severity: 'error',
2023
source: {
21-
file: record.file,
24+
file: validRecord.file,
2225
position: { startLine: detail.line },
2326
},
2427
}),
@@ -27,6 +30,28 @@ export function lcovReportToFunctionStat(record: LCOVRecord): LCOVStat {
2730
};
2831
}
2932

33+
function removeEmptyReport(record: LCOVRecord): LCOVRecord {
34+
const validFunctions = record.functions.details.filter(
35+
detail => detail.name !== INVALID_FUNCTION_NAME,
36+
);
37+
38+
if (validFunctions.length === record.functions.found) {
39+
return record;
40+
}
41+
42+
return {
43+
...record,
44+
functions: {
45+
details: validFunctions,
46+
found: validFunctions.length,
47+
hit: validFunctions.reduce(
48+
(acc, fn) => acc + (fn.hit != null && fn.hit > 0 ? 1 : 0),
49+
0,
50+
),
51+
},
52+
};
53+
}
54+
3055
export function lcovReportToLineStat(record: LCOVRecord): LCOVStat {
3156
const missingCoverage = record.lines.hit < record.lines.found;
3257
const lines = missingCoverage

packages/plugin-coverage/src/lib/runner/lcov/transform.unit.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { LCOVRecord } from 'parse-lcov';
22
import { describe, it } from 'vitest';
33
import type { AuditOutput, Issue } from '@code-pushup/models';
4+
import { INVALID_FUNCTION_NAME } from '../constants';
45
import {
56
lcovCoverageToAuditOutput,
67
lcovReportToBranchStat,
@@ -92,6 +93,26 @@ describe('lcovReportToFunctionStat', () => {
9293
}),
9394
);
9495
});
96+
97+
it('should skip a record of uncovered invalid function called (empty-report)', () => {
98+
expect(
99+
lcovReportToFunctionStat({
100+
...lcovRecordMock,
101+
functions: {
102+
hit: 1,
103+
found: 2,
104+
details: [
105+
{ line: 1, name: INVALID_FUNCTION_NAME, hit: 0 },
106+
{ line: 5, name: 'transform', hit: 4 },
107+
],
108+
},
109+
}),
110+
).toStrictEqual<LCOVStat>({
111+
totalFound: 1,
112+
totalHit: 1,
113+
issues: [],
114+
});
115+
});
95116
});
96117

97118
describe('lcovReportToLineStat', () => {

0 commit comments

Comments
 (0)