From 6bc746b4b4da638bc8790bca610b8df225ce4a8b Mon Sep 17 00:00:00 2001 From: Katerina Pilatova Date: Mon, 29 Apr 2024 10:53:29 +0200 Subject: [PATCH] fix(plugin-coverage): sort lines numerically to merge consecutive ranges --- .../__snapshots__/collect.e2e.test.ts.snap | 13 +----- .../mocks/no-coverage-lcov.info | 42 +++++++++++++++++++ .../lcov-runner.integration.test.ts.snap | 26 ++++++++++++ .../lcov/lcov-runner.integration.test.ts | 21 ++++++++++ .../src/lib/runner/lcov/utils.ts | 25 ++++++----- 5 files changed, 105 insertions(+), 22 deletions(-) create mode 100644 packages/plugin-coverage/mocks/no-coverage-lcov.info diff --git a/e2e/cli-e2e/tests/__snapshots__/collect.e2e.test.ts.snap b/e2e/cli-e2e/tests/__snapshots__/collect.e2e.test.ts.snap index 036436584..1140514fa 100644 --- a/e2e/cli-e2e/tests/__snapshots__/collect.e2e.test.ts.snap +++ b/e2e/cli-e2e/tests/__snapshots__/collect.e2e.test.ts.snap @@ -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, }, }, diff --git a/packages/plugin-coverage/mocks/no-coverage-lcov.info b/packages/plugin-coverage/mocks/no-coverage-lcov.info new file mode 100644 index 000000000..a063f7e33 --- /dev/null +++ b/packages/plugin-coverage/mocks/no-coverage-lcov.info @@ -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 diff --git a/packages/plugin-coverage/src/lib/runner/lcov/__snapshots__/lcov-runner.integration.test.ts.snap b/packages/plugin-coverage/src/lib/runner/lcov/__snapshots__/lcov-runner.integration.test.ts.snap index d5f456560..59ed1953d 100644 --- a/packages/plugin-coverage/src/lib/runner/lcov/__snapshots__/lcov-runner.integration.test.ts.snap +++ b/packages/plugin-coverage/src/lib/runner/lcov/__snapshots__/lcov-runner.integration.test.ts.snap @@ -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, + }, +] +`; diff --git a/packages/plugin-coverage/src/lib/runner/lcov/lcov-runner.integration.test.ts b/packages/plugin-coverage/src/lib/runner/lcov/lcov-runner.integration.test.ts index 0b328ea00..f1d26282e 100644 --- a/packages/plugin-coverage/src/lib/runner/lcov/lcov-runner.integration.test.ts +++ b/packages/plugin-coverage/src/lib/runner/lcov/lcov-runner.integration.test.ts @@ -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(); + }); }); diff --git a/packages/plugin-coverage/src/lib/runner/lcov/utils.ts b/packages/plugin-coverage/src/lib/runner/lcov/utils.ts index 6bf4bc5e6..f52688379 100644 --- a/packages/plugin-coverage/src/lib/runner/lcov/utils.ts +++ b/packages/plugin-coverage/src/lib/runner/lcov/utils.ts @@ -11,14 +11,19 @@ export function calculateCoverage(hit: number, found: number): number { } export function mergeConsecutiveNumbers(numberArr: number[]): NumberRange[] { - return [...numberArr].sort().reduce((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((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 }]; + }, []); }