Skip to content

Commit 6bc746b

Browse files
committed
fix(plugin-coverage): sort lines numerically to merge consecutive ranges
1 parent 357b1fa commit 6bc746b

File tree

5 files changed

+105
-22
lines changed

5 files changed

+105
-22
lines changed

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

+1-12
Original file line numberDiff line numberDiff line change
@@ -164,23 +164,12 @@ exports[`CLI collect > should run Code coverage plugin that runs coverage tool a
164164
},
165165
},
166166
{
167-
"message": "Lines 10-28 are not covered in any test case.",
167+
"message": "Lines 7-28 are not covered in any test case.",
168168
"severity": "warning",
169169
"source": {
170170
"file": "examples/react-todos-app/src/components/TodoList.jsx",
171171
"position": {
172172
"endLine": 28,
173-
"startLine": 10,
174-
},
175-
},
176-
},
177-
{
178-
"message": "Lines 7-9 are not covered in any test case.",
179-
"severity": "warning",
180-
"source": {
181-
"file": "examples/react-todos-app/src/components/TodoList.jsx",
182-
"position": {
183-
"endLine": 9,
184173
"startLine": 7,
185174
},
186175
},
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
TN:
2+
SF:src\lib\index.ts
3+
FN:1
4+
FNF:1
5+
FNH:0
6+
FNDA:0
7+
DA:1,0
8+
DA:2,0
9+
DA:3,0
10+
DA:4,0
11+
DA:5,0
12+
DA:6,0
13+
DA:7,0
14+
DA:8,0
15+
DA:9,0
16+
DA:10,0
17+
DA:11,0
18+
DA:12,0
19+
DA:13,0
20+
DA:14,0
21+
DA:15,0
22+
DA:16,0
23+
DA:17,0
24+
DA:18,0
25+
DA:19,0
26+
DA:20,0
27+
DA:21,0
28+
DA:22,0
29+
DA:23,0
30+
DA:24,0
31+
DA:25,0
32+
DA:26,0
33+
DA:27,0
34+
DA:28,0
35+
DA:29,0
36+
DA:30,0
37+
LF:30
38+
LH:0
39+
BRDA:1,0,0,0
40+
BRF:1
41+
BRH:0
42+
end_of_record

packages/plugin-coverage/src/lib/runner/lcov/__snapshots__/lcov-runner.integration.test.ts.snap

+26
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,29 @@ exports[`lcovResultsToAuditOutputs > should correctly convert lcov results to Au
6161
},
6262
]
6363
`;
64+
65+
exports[`lcovResultsToAuditOutputs > should correctly merge all lines for coverage 1`] = `
66+
[
67+
{
68+
"details": {
69+
"issues": [
70+
{
71+
"message": "Lines 1-30 are not covered in any test case.",
72+
"severity": "warning",
73+
"source": {
74+
"file": "packages/cli/src/lib/index.ts",
75+
"position": {
76+
"endLine": 30,
77+
"startLine": 1,
78+
},
79+
},
80+
},
81+
],
82+
},
83+
"displayValue": "0 %",
84+
"score": 0,
85+
"slug": "line-coverage",
86+
"value": 0,
87+
},
88+
]
89+
`;

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

+21
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,25 @@ describe('lcovResultsToAuditOutputs', () => {
3131
);
3232
expect(osAgnosticAuditOutputs(results)).toMatchSnapshot();
3333
});
34+
35+
it('should correctly merge all lines for coverage', async () => {
36+
const results = await lcovResultsToAuditOutputs(
37+
[
38+
{
39+
resultsPath: join(
40+
fileURLToPath(dirname(import.meta.url)),
41+
'..',
42+
'..',
43+
'..',
44+
'..',
45+
'mocks',
46+
'no-coverage-lcov.info',
47+
),
48+
pathToProject: 'packages/cli',
49+
},
50+
],
51+
['line'],
52+
);
53+
expect(osAgnosticAuditOutputs(results)).toMatchSnapshot();
54+
});
3455
});

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

+15-10
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,19 @@ export function calculateCoverage(hit: number, found: number): number {
1111
}
1212

1313
export function mergeConsecutiveNumbers(numberArr: number[]): NumberRange[] {
14-
return [...numberArr].sort().reduce<NumberRange[]>((acc, currValue) => {
15-
const prevValue = acc.at(-1);
16-
if (
17-
prevValue != null &&
18-
(prevValue.start === currValue - 1 || prevValue.end === currValue - 1)
19-
) {
20-
return [...acc.slice(0, -1), { start: prevValue.start, end: currValue }];
21-
}
22-
return [...acc, { start: currValue }];
23-
}, []);
14+
return [...numberArr]
15+
.sort((a, b) => a - b)
16+
.reduce<NumberRange[]>((acc, currValue) => {
17+
const prevValue = acc.at(-1);
18+
if (
19+
prevValue != null &&
20+
(prevValue.start === currValue - 1 || prevValue.end === currValue - 1)
21+
) {
22+
return [
23+
...acc.slice(0, -1),
24+
{ start: prevValue.start, end: currValue },
25+
];
26+
}
27+
return [...acc, { start: currValue }];
28+
}, []);
2429
}

0 commit comments

Comments
 (0)