From 68e2494cb2c17a95051f5d6e92fd4b03bb6b4145 Mon Sep 17 00:00:00 2001 From: Katerina Pilatova Date: Wed, 29 May 2024 17:36:25 +0200 Subject: [PATCH] chore(plugin-coverage): skip missing coverage for empty-report function --- .../__snapshots__/collect.e2e.test.ts.snap | 16 ++------- .../src/lib/runner/constants.ts | 2 ++ .../src/lib/runner/lcov/transform.ts | 35 ++++++++++++++++--- .../lib/runner/lcov/transform.unit.test.ts | 21 +++++++++++ 4 files changed, 56 insertions(+), 18 deletions(-) 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 1140514fa..9694fcd9c 100644 --- a/e2e/cli-e2e/tests/__snapshots__/collect.e2e.test.ts.snap +++ b/e2e/cli-e2e/tests/__snapshots__/collect.e2e.test.ts.snap @@ -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", @@ -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.", diff --git a/packages/plugin-coverage/src/lib/runner/constants.ts b/packages/plugin-coverage/src/lib/runner/constants.ts index 45490e44a..3b6a925e0 100644 --- a/packages/plugin-coverage/src/lib/runner/constants.ts +++ b/packages/plugin-coverage/src/lib/runner/constants.ts @@ -8,3 +8,5 @@ export const PLUGIN_CONFIG_PATH = join( WORKDIR, 'plugin-config.json', ); + +export const INVALID_FUNCTION_NAME = '(empty-report)'; diff --git a/packages/plugin-coverage/src/lib/runner/lcov/transform.ts b/packages/plugin-coverage/src/lib/runner/lcov/transform.ts index 6fcd1e0ea..7cb0f4634 100644 --- a/packages/plugin-coverage/src/lib/runner/lcov/transform.ts +++ b/packages/plugin-coverage/src/lib/runner/lcov/transform.ts @@ -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 }, }, }), @@ -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; + } + + return { + ...record, + functions: { + details: validFunctions, + found: validFunctions.length, + hit: validFunctions.reduce( + (acc, fn) => acc + (fn.hit != null && fn.hit > 0 ? 1 : 0), + 0, + ), + }, + }; +} + export function lcovReportToLineStat(record: LCOVRecord): LCOVStat { const missingCoverage = record.lines.hit < record.lines.found; const lines = missingCoverage diff --git a/packages/plugin-coverage/src/lib/runner/lcov/transform.unit.test.ts b/packages/plugin-coverage/src/lib/runner/lcov/transform.unit.test.ts index 3312489c4..9dbeafe30 100644 --- a/packages/plugin-coverage/src/lib/runner/lcov/transform.unit.test.ts +++ b/packages/plugin-coverage/src/lib/runner/lcov/transform.unit.test.ts @@ -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, @@ -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({ + totalFound: 1, + totalHit: 1, + issues: [], + }); + }); }); describe('lcovReportToLineStat', () => {