Skip to content

Commit

Permalink
fix(plugin-coverage): sort lines numerically to merge consecutive ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
Tlacenka committed Apr 29, 2024
1 parent 357b1fa commit 6bc746b
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 22 deletions.
13 changes: 1 addition & 12 deletions e2e/cli-e2e/tests/__snapshots__/collect.e2e.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -164,23 +164,12 @@ exports[`CLI collect > should run Code coverage plugin that runs coverage tool a
},
},
{
"message": "Lines 10-28 are not covered in any test case.",
"message": "Lines 7-28 are not covered in any test case.",
"severity": "warning",
"source": {
"file": "examples/react-todos-app/src/components/TodoList.jsx",
"position": {
"endLine": 28,
"startLine": 10,
},
},
},
{
"message": "Lines 7-9 are not covered in any test case.",
"severity": "warning",
"source": {
"file": "examples/react-todos-app/src/components/TodoList.jsx",
"position": {
"endLine": 9,
"startLine": 7,
},
},
Expand Down
42 changes: 42 additions & 0 deletions packages/plugin-coverage/mocks/no-coverage-lcov.info
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
TN:
SF:src\lib\index.ts
FN:1
FNF:1
FNH:0
FNDA:0
DA:1,0
DA:2,0
DA:3,0
DA:4,0
DA:5,0
DA:6,0
DA:7,0
DA:8,0
DA:9,0
DA:10,0
DA:11,0
DA:12,0
DA:13,0
DA:14,0
DA:15,0
DA:16,0
DA:17,0
DA:18,0
DA:19,0
DA:20,0
DA:21,0
DA:22,0
DA:23,0
DA:24,0
DA:25,0
DA:26,0
DA:27,0
DA:28,0
DA:29,0
DA:30,0
LF:30
LH:0
BRDA:1,0,0,0
BRF:1
BRH:0
end_of_record
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,29 @@ exports[`lcovResultsToAuditOutputs > should correctly convert lcov results to Au
},
]
`;

exports[`lcovResultsToAuditOutputs > should correctly merge all lines for coverage 1`] = `
[
{
"details": {
"issues": [
{
"message": "Lines 1-30 are not covered in any test case.",
"severity": "warning",
"source": {
"file": "packages/cli/src/lib/index.ts",
"position": {
"endLine": 30,
"startLine": 1,
},
},
},
],
},
"displayValue": "0 %",
"score": 0,
"slug": "line-coverage",
"value": 0,
},
]
`;
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,25 @@ describe('lcovResultsToAuditOutputs', () => {
);
expect(osAgnosticAuditOutputs(results)).toMatchSnapshot();
});

it('should correctly merge all lines for coverage', async () => {
const results = await lcovResultsToAuditOutputs(
[
{
resultsPath: join(
fileURLToPath(dirname(import.meta.url)),
'..',
'..',
'..',
'..',
'mocks',
'no-coverage-lcov.info',
),
pathToProject: 'packages/cli',
},
],
['line'],
);
expect(osAgnosticAuditOutputs(results)).toMatchSnapshot();
});
});
25 changes: 15 additions & 10 deletions packages/plugin-coverage/src/lib/runner/lcov/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,19 @@ export function calculateCoverage(hit: number, found: number): number {
}

export function mergeConsecutiveNumbers(numberArr: number[]): NumberRange[] {
return [...numberArr].sort().reduce<NumberRange[]>((acc, currValue) => {
const prevValue = acc.at(-1);
if (
prevValue != null &&
(prevValue.start === currValue - 1 || prevValue.end === currValue - 1)
) {
return [...acc.slice(0, -1), { start: prevValue.start, end: currValue }];
}
return [...acc, { start: currValue }];
}, []);
return [...numberArr]
.sort((a, b) => a - b)
.reduce<NumberRange[]>((acc, currValue) => {
const prevValue = acc.at(-1);
if (
prevValue != null &&
(prevValue.start === currValue - 1 || prevValue.end === currValue - 1)
) {
return [
...acc.slice(0, -1),
{ start: prevValue.start, end: currValue },
];
}
return [...acc, { start: currValue }];
}, []);
}

0 comments on commit 6bc746b

Please sign in to comment.