Skip to content

Commit 3760b98

Browse files
committed
fix: lcov command didn't report a total, so --fail-under didn't work
1 parent 5867263 commit 3760b98

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

CHANGES.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ Unreleased
3030

3131
- Fix: adjusted how decorators are traced on PyPy 7.3.10, fixing `issue 1515`_.
3232

33+
- Fix: the ``coverage lcov`` report did not properly implement the
34+
``--fail-under=MIN`` option. This has been fixed.
35+
3336
- Refactor: a number of refactorings internally due to adding type annotations.
3437
This should not affect outward behavior, but they were a bit invasive in some
3538
places.

coverage/lcovreport.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from hashlib import md5
99

1010
from coverage.report import get_analysis_to_report
11+
from coverage.results import Analysis, Numbers
1112

1213

1314
class LcovReporter:
@@ -17,7 +18,7 @@ class LcovReporter:
1718

1819
def __init__(self, coverage):
1920
self.coverage = coverage
20-
self.config = self.coverage.config
21+
self.total = Numbers(self.coverage.config.precision)
2122

2223
def report(self, morfs, outfile=None):
2324
"""Renders the full lcov report.
@@ -33,12 +34,16 @@ def report(self, morfs, outfile=None):
3334
for fr, analysis in get_analysis_to_report(self.coverage, morfs):
3435
self.get_lcov(fr, analysis, outfile)
3536

37+
return self.total.n_statements and self.total.pc_covered
38+
3639
def get_lcov(self, fr, analysis, outfile=None):
3740
"""Produces the lcov data for a single file.
3841
3942
This currently supports both line and branch coverage,
4043
however function coverage is not supported.
4144
"""
45+
self.total += analysis.numbers
46+
4247
outfile.write("TN:\n")
4348
outfile.write(f"SF:{fr.relative_filename()}\n")
4449
source_lines = fr.source().splitlines()

tests/test_lcov.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
"""Test LCOV-based summary reporting for coverage.py."""
55

6+
import math
67
import textwrap
78

89
from tests.coveragetest import CoverageTest
@@ -75,7 +76,8 @@ def IsItTrue():
7576
self.assert_doesnt_exist(".coverage")
7677
cov = coverage.Coverage(source=["."])
7778
self.start_import_stop(cov, "main_file")
78-
cov.lcov_report()
79+
pct = cov.lcov_report()
80+
assert pct == 50.0
7981
actual_result = self.get_lcov_report_content()
8082
assert expected_result == actual_result
8183

@@ -87,7 +89,8 @@ def test_simple_line_coverage_two_files(self):
8789
self.make_file(".coveragerc", "[lcov]\noutput = data.lcov\n")
8890
cov = coverage.Coverage(source=".")
8991
self.start_import_stop(cov, "test_file")
90-
cov.lcov_report()
92+
pct = cov.lcov_report()
93+
assert pct == 50.0
9194
self.assert_exists("data.lcov")
9295
expected_result = textwrap.dedent("""\
9396
TN:
@@ -130,7 +133,8 @@ def is_it_x(x):
130133
self.assert_doesnt_exist(".coverage")
131134
cov = coverage.Coverage(branch=True, source=".")
132135
self.start_import_stop(cov, "main_file")
133-
cov.lcov_report()
136+
pct = cov.lcov_report()
137+
assert math.isclose(pct, 16.666666666666668)
134138
self.assert_exists("coverage.lcov")
135139
expected_result = textwrap.dedent("""\
136140
TN:
@@ -177,7 +181,8 @@ def test_is_it_x(self):
177181
self.assert_doesnt_exist(".coverage")
178182
cov = coverage.Coverage(branch=True, source=".")
179183
self.start_import_stop(cov, "test_file")
180-
cov.lcov_report()
184+
pct = cov.lcov_report()
185+
assert math.isclose(pct, 41.666666666666664)
181186
self.assert_exists("coverage.lcov")
182187
expected_result = textwrap.dedent("""\
183188
TN:
@@ -225,7 +230,8 @@ def test_half_covered_branch(self):
225230
self.assert_doesnt_exist(".coverage")
226231
cov = coverage.Coverage(branch=True, source=".")
227232
self.start_import_stop(cov, "main_file")
228-
cov.lcov_report()
233+
pct = cov.lcov_report()
234+
assert math.isclose(pct, 66.66666666666667)
229235
self.assert_exists("coverage.lcov")
230236
expected_result = textwrap.dedent("""\
231237
TN:
@@ -259,7 +265,8 @@ def test_empty_init_files(self):
259265
self.assert_doesnt_exist(".coverage")
260266
cov = coverage.Coverage(branch=True, source=".")
261267
self.start_import_stop(cov, "__init__")
262-
cov.lcov_report()
268+
pct = cov.lcov_report()
269+
assert pct == 0.0
263270
self.assert_exists("coverage.lcov")
264271
# Newer Pythons have truly empty empty files.
265272
if env.PYBEHAVIOR.empty_is_empty:

0 commit comments

Comments
 (0)