Skip to content

Commit

Permalink
fix: don't assume 'no branches' means 'not executed' #1896
Browse files Browse the repository at this point in the history
  • Loading branch information
nedbat committed Nov 23, 2024
1 parent 3ed5915 commit 2ace7a2
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
7 changes: 6 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ upgrading your version of coverage.py.
Unreleased
----------

Nothing yet.
- fix: the LCOV report code assumed that a branch line that took no branches
meant that the entire line was unexecuted. This isn't true in a few cases:
the line might always raise an exception, or might have been optimized away.
Fixes `issue 1896`_.

.. _issue 1896: https://github.com/nedbat/coveragepy/issues/1896


.. start-releases
Expand Down
4 changes: 1 addition & 3 deletions coverage/lcovreport.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,8 @@ def lcov_arcs(

if taken == 0:
# When _none_ of the out arcs from 'line' were executed,
# this probably means 'line' was never executed at all.
# Cross-check with the line stats.
# it can mean the line always raised an exception.
assert len(executed_arcs[line]) == 0
assert line in analysis.missing
destinations = [
(dst, "-") for dst in missing_arcs[line]
]
Expand Down
32 changes: 32 additions & 0 deletions tests/test_lcov.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,3 +526,35 @@ def foo(a):
""")
actual_result = self.get_lcov_report_content()
assert expected_result == actual_result

def test_always_raise(self) -> None:
self.make_file("always_raise.py", """\
try:
if not_defined:
print("Yes")
else:
print("No")
except Exception:
pass
""")
cov = coverage.Coverage(source=".", branch=True)
self.start_import_stop(cov, "always_raise")
cov.lcov_report()
expected_result = textwrap.dedent("""\
SF:always_raise.py
DA:1,1
DA:2,1
DA:3,0
DA:5,0
DA:6,1
DA:7,1
LF:6
LH:4
BRDA:2,0,jump to line 3,-
BRDA:2,0,jump to line 5,-
BRF:2
BRH:0
end_of_record
""")
actual_result = self.get_lcov_report_content()
assert expected_result == actual_result

0 comments on commit 2ace7a2

Please sign in to comment.