Skip to content

Commit

Permalink
Revise lcovreport.lcov_arcs using the new arc_description API.
Browse files Browse the repository at this point in the history
Doesn’t quite work yet, see discussion in #1850.
  • Loading branch information
zackw committed Sep 24, 2024
1 parent 228854a commit d767721
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 39 deletions.
38 changes: 17 additions & 21 deletions coverage/lcovreport.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ def lcov_functions(


def lcov_arcs(
fr: FileReporter,
analysis: Analysis,
lines: list[int],
outfile: IO[str],
Expand All @@ -116,39 +117,34 @@ def lcov_arcs(

if taken == 0:
# When _none_ of the out arcs from 'line' were executed,
# this probably means means 'line' was never executed at all.
# this probably means 'line' was never executed at all.
# Cross-check with the line stats.
assert len(executed_arcs[line]) == 0
if line in analysis.missing:
hit = "-" # indeed, never reached
else:
hit = "0" # I am not prepared to swear this is impossible
assert line in analysis.missing
destinations = [
(int(dst < 0), abs(dst), hit) for dst in missing_arcs[line]
(dst, "-") for dst in missing_arcs[line]
]
else:
# Q: can we get counts of the number of times each arc was executed?
# branch_stats has "total" and "taken" counts for each branch,
# but it doesn't have "taken" broken down by destination.
destinations = [
(int(dst < 0), abs(dst), "1") for dst in executed_arcs[line]
(dst, "1") for dst in executed_arcs[line]
]
destinations.extend(
(int(dst < 0), abs(dst), "0") for dst in missing_arcs[line]
(dst, "0") for dst in missing_arcs[line]
)

destinations.sort()
n_exits = sum(
1 for is_exit, _, _ in destinations if is_exit
)
for is_exit, dst, hit in destinations:
if is_exit:
if n_exits == 1:
branch = "to exit"
else:
branch = f"to exit {dst}"
else:
branch = f"to line {dst}"
# Sort exit arcs after normal arcs. Exit arcs typically come from
# an if statement, at the end of a function, with no else clause.
# This structure reads like you're jumping to the end of the function
# when the conditional expression is false, so it should be presented
# as the second alternative for the branch, after the alternative that
# enters the if clause.
destinations.sort(key=lambda d: (d[0] < 0, d))

for dst, hit in destinations:
branch = fr.arc_description(line, dst)
outfile.write(f"BRDA:{line},0,{branch},{hit}\n")

# Summary of the branch coverage.
Expand Down Expand Up @@ -221,6 +217,6 @@ def lcov_file(
lcov_lines(analysis, lines, source_lines, outfile)
lcov_functions(fr, analysis, outfile)
if analysis.has_arcs:
lcov_arcs(analysis, lines, outfile)
lcov_arcs(fr, analysis, lines, outfile)

outfile.write("end_of_record\n")
36 changes: 18 additions & 18 deletions tests/test_lcov.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,8 @@ def is_it_x(x):
FNH:0
"""),
textwrap.dedent("""\
BRDA:2,0,to line 3,-
BRDA:2,0,to line 5,-
BRDA:2,0,jump to line 3,-
BRDA:2,0,jump to line 5,-
BRF:2
BRH:0
end_of_record
Expand Down Expand Up @@ -264,8 +264,8 @@ def test_is_it_x(self):
FNH:0
"""),
textwrap.dedent("""\
BRDA:2,0,to line 3,-
BRDA:2,0,to line 5,-
BRDA:2,0,jump to line 3,-
BRDA:2,0,jump to line 5,-
BRF:2
BRH:0
end_of_record
Expand Down Expand Up @@ -315,8 +315,8 @@ def test_half_covered_branch(self) -> None:
DA:6,0
LF:4
LH:3
BRDA:3,0,to line 4,1
BRDA:3,0,to line 6,0
BRDA:3,0,jump to line 4,1
BRDA:3,0,jump to line 6,0
BRF:2
BRH:1
end_of_record
Expand Down Expand Up @@ -383,8 +383,8 @@ def test_excluded_lines(self) -> None:
DA:6,1
LF:4
LH:3
BRDA:3,0,to line 4,0
BRDA:3,0,to line 6,1
BRDA:3,0,jump to line 4,0
BRDA:3,0,jump to line 6,1
BRF:2
BRH:1
end_of_record
Expand Down Expand Up @@ -425,8 +425,8 @@ def foo(a):
FNH:1
"""),
textwrap.dedent("""\
BRDA:2,0,to line 3,1
BRDA:2,0,to exit,1
BRDA:2,0,jump to line 3,1
BRDA:2,0,return from function 'foo',1
BRF:2
BRH:2
end_of_record
Expand Down Expand Up @@ -468,8 +468,8 @@ def foo(a):
FNH:1
"""),
textwrap.dedent("""\
BRDA:2,0,to line 3,1
BRDA:2,0,to exit,1
BRDA:2,0,jump to line 3,1
BRDA:2,0,return from function 'foo',1
BRF:2
BRH:2
end_of_record
Expand Down Expand Up @@ -507,8 +507,8 @@ def foo(a):
FNH:1
"""),
textwrap.dedent("""\
BRDA:2,0,to line 3,0
BRDA:2,0,to exit,1
BRDA:2,0,jump to line 3,0
BRDA:2,0,return from function 'foo',1
BRF:2
BRH:1
end_of_record
Expand Down Expand Up @@ -546,8 +546,8 @@ def foo(a):
FNH:1
"""),
textwrap.dedent("""\
BRDA:2,0,to line 3,1
BRDA:2,0,to exit,0
BRDA:2,0,jump to line 3,1
BRDA:2,0,return from function 'foo',0
BRF:2
BRH:1
end_of_record
Expand Down Expand Up @@ -581,8 +581,8 @@ def foo(a):
FNH:0
"""),
textwrap.dedent("""\
BRDA:2,0,to line 3,-
BRDA:2,0,to exit,-
BRDA:2,0,jump to line 3,-
BRDA:2,0,return from function 'foo',-
BRF:2
BRH:0
end_of_record
Expand Down

0 comments on commit d767721

Please sign in to comment.