Skip to content

Commit 2ace7a2

Browse files
committed
fix: don't assume 'no branches' means 'not executed' #1896
1 parent 3ed5915 commit 2ace7a2

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

CHANGES.rst

+6-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,12 @@ upgrading your version of coverage.py.
2323
Unreleased
2424
----------
2525

26-
Nothing yet.
26+
- fix: the LCOV report code assumed that a branch line that took no branches
27+
meant that the entire line was unexecuted. This isn't true in a few cases:
28+
the line might always raise an exception, or might have been optimized away.
29+
Fixes `issue 1896`_.
30+
31+
.. _issue 1896: https://github.com/nedbat/coveragepy/issues/1896
2732

2833

2934
.. start-releases

coverage/lcovreport.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,8 @@ def lcov_arcs(
118118

119119
if taken == 0:
120120
# When _none_ of the out arcs from 'line' were executed,
121-
# this probably means 'line' was never executed at all.
122-
# Cross-check with the line stats.
121+
# it can mean the line always raised an exception.
123122
assert len(executed_arcs[line]) == 0
124-
assert line in analysis.missing
125123
destinations = [
126124
(dst, "-") for dst in missing_arcs[line]
127125
]

tests/test_lcov.py

+32
Original file line numberDiff line numberDiff line change
@@ -526,3 +526,35 @@ def foo(a):
526526
""")
527527
actual_result = self.get_lcov_report_content()
528528
assert expected_result == actual_result
529+
530+
def test_always_raise(self) -> None:
531+
self.make_file("always_raise.py", """\
532+
try:
533+
if not_defined:
534+
print("Yes")
535+
else:
536+
print("No")
537+
except Exception:
538+
pass
539+
""")
540+
cov = coverage.Coverage(source=".", branch=True)
541+
self.start_import_stop(cov, "always_raise")
542+
cov.lcov_report()
543+
expected_result = textwrap.dedent("""\
544+
SF:always_raise.py
545+
DA:1,1
546+
DA:2,1
547+
DA:3,0
548+
DA:5,0
549+
DA:6,1
550+
DA:7,1
551+
LF:6
552+
LH:4
553+
BRDA:2,0,jump to line 3,-
554+
BRDA:2,0,jump to line 5,-
555+
BRF:2
556+
BRH:0
557+
end_of_record
558+
""")
559+
actual_result = self.get_lcov_report_content()
560+
assert expected_result == actual_result

0 commit comments

Comments
 (0)