From 663d8708075436da00acf77d9881bae09c164697 Mon Sep 17 00:00:00 2001 From: Ahbong Chang Date: Fri, 11 Oct 2024 21:17:22 +0800 Subject: [PATCH] fix(coverage): Do not emit error on BRDA:0 entries Coverage report generators in Coverage.py would generate BRDA:0 for exit branches. Ref: https://github.com/nedbat/coveragepy/blob/5467e1f/coverage/lcovreport.py#L108-L112 Also small string fixes for error messages in BRDA. Improves #362. --- src/test-explorer/lcov_parser.ts | 14 +++++++++----- test/lcov_parser.test.ts | 4 ++++ 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/test-explorer/lcov_parser.ts b/src/test-explorer/lcov_parser.ts index 8c7afa7c..d98863b9 100644 --- a/src/test-explorer/lcov_parser.ts +++ b/src/test-explorer/lcov_parser.ts @@ -330,23 +330,27 @@ export async function parseLcov( if (!match) { throw new Error(`Invalid FNDA entry`); } - const lineNumber = Number.parseInt(match[1], 10) - 1; - if (lineNumber < 0) { - throw new Error("Negative line number in DA entry"); + const lineNumber1 = Number.parseInt(match[1], 10); + if (lineNumber1 < 0) { + throw new Error("Negative line number in BRDA entry"); } + // There might be BRDA:0 for exiting branch in report generated by + // Coverage.py. Simply drop these entries. + if (lineNumber1 === 0) break; + const lineNumber = lineNumber1 - 1; const isException = match[2] === "e"; const blockId = Number.parseInt(match[3], 10); const rest = match[4]; const commaOffset = rest.lastIndexOf(","); if (commaOffset === undefined) { - throw new Error(`Invalid FNDA entry`); + throw new Error(`Invalid BRDA entry`); } const label = rest.substring(0, commaOffset); const hitCountStr = rest.substring(commaOffset + 1); const hitCount = hitCountStr === "-" ? 0 : Number.parseInt(hitCountStr, 10); if (hitCount < 0) { - throw new Error("Negative hit count in DA entry"); + throw new Error("Negative hit count in BRDA entry"); } if (info === undefined) { diff --git a/test/lcov_parser.test.ts b/test/lcov_parser.test.ts index 65f45b21..895bedc0 100644 --- a/test/lcov_parser.test.ts +++ b/test/lcov_parser.test.ts @@ -71,6 +71,10 @@ describe("The lcov parser", () => { assert.equal(coveredFiles[0].declarationCoverage.total, 1); }); + it("ignores invalid line numbers", async () => { + assert.deepEqual(await parseTestLcov("BRDA:0,0,0,b"), []); + }); + describe("parses Java coverage data:", () => { let fileCov: BazelFileCoverage; before(async () => {