Skip to content

Commit 2425a46

Browse files
karthiknadigCopilot
andcommitted
fix: reject negative coverage counters (PR #505)
Treat negative LCOV summary counts as invalid snapshots and cover the failure contract directly. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 1767650 commit 2425a46

2 files changed

Lines changed: 11 additions & 0 deletions

File tree

scripts/quality_snapshot.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,8 @@ def parse_lcov(path: Path) -> tuple[int, int, int, int]:
200200
functions_hit += int(line[4:])
201201
except ValueError as error:
202202
raise SnapshotError(f'Coverage file has a malformed summary count: {path}') from error
203+
if min(lines_hit, lines_found, functions_hit, functions_found) < 0:
204+
raise SnapshotError(f'Coverage file has negative summary counts: {path}')
203205
if lines_found == 0 or functions_found == 0:
204206
raise SnapshotError(f'Coverage file has no line/function summary data: {path}')
205207
if lines_hit > lines_found or functions_hit > functions_found:

scripts/tests/test_quality_snapshot.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,15 @@ def test_invalid_lcov_is_rejected(self):
154154
with self.assertRaises(SnapshotError):
155155
compare_coverage(current, baseline)
156156

157+
def test_negative_lcov_count_is_rejected(self):
158+
with tempfile.TemporaryDirectory() as directory:
159+
current = Path(directory) / 'current.info'
160+
baseline = Path(directory) / 'baseline.info'
161+
current.write_text('SF:example.rs\nLF:-1\nLH:-1\nFNF:1\nFNH:1\n', encoding='utf-8')
162+
write_lcov(baseline, lines_hit=1, lines_found=1, functions_hit=1, functions_found=1)
163+
with self.assertRaisesRegex(SnapshotError, 'negative summary counts'):
164+
compare_coverage(current, baseline)
165+
157166
def test_malformed_lcov_count_is_rejected(self):
158167
with tempfile.TemporaryDirectory() as directory:
159168
current = Path(directory) / 'current.info'

0 commit comments

Comments
 (0)