Skip to content

Commit

Permalink
fix: lcov command didn't report a total, so --fail-under didn't work
Browse files Browse the repository at this point in the history
  • Loading branch information
nedbat committed Jan 1, 2023
1 parent 5867263 commit 3760b98
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ Unreleased

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

- Fix: the ``coverage lcov`` report did not properly implement the
``--fail-under=MIN`` option. This has been fixed.

- Refactor: a number of refactorings internally due to adding type annotations.
This should not affect outward behavior, but they were a bit invasive in some
places.
Expand Down
7 changes: 6 additions & 1 deletion coverage/lcovreport.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from hashlib import md5

from coverage.report import get_analysis_to_report
from coverage.results import Analysis, Numbers


class LcovReporter:
Expand All @@ -17,7 +18,7 @@ class LcovReporter:

def __init__(self, coverage):
self.coverage = coverage
self.config = self.coverage.config
self.total = Numbers(self.coverage.config.precision)

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

return self.total.n_statements and self.total.pc_covered

def get_lcov(self, fr, analysis, outfile=None):
"""Produces the lcov data for a single file.
This currently supports both line and branch coverage,
however function coverage is not supported.
"""
self.total += analysis.numbers

outfile.write("TN:\n")
outfile.write(f"SF:{fr.relative_filename()}\n")
source_lines = fr.source().splitlines()
Expand Down
19 changes: 13 additions & 6 deletions tests/test_lcov.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

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

import math
import textwrap

from tests.coveragetest import CoverageTest
Expand Down Expand Up @@ -75,7 +76,8 @@ def IsItTrue():
self.assert_doesnt_exist(".coverage")
cov = coverage.Coverage(source=["."])
self.start_import_stop(cov, "main_file")
cov.lcov_report()
pct = cov.lcov_report()
assert pct == 50.0
actual_result = self.get_lcov_report_content()
assert expected_result == actual_result

Expand All @@ -87,7 +89,8 @@ def test_simple_line_coverage_two_files(self):
self.make_file(".coveragerc", "[lcov]\noutput = data.lcov\n")
cov = coverage.Coverage(source=".")
self.start_import_stop(cov, "test_file")
cov.lcov_report()
pct = cov.lcov_report()
assert pct == 50.0
self.assert_exists("data.lcov")
expected_result = textwrap.dedent("""\
TN:
Expand Down Expand Up @@ -130,7 +133,8 @@ def is_it_x(x):
self.assert_doesnt_exist(".coverage")
cov = coverage.Coverage(branch=True, source=".")
self.start_import_stop(cov, "main_file")
cov.lcov_report()
pct = cov.lcov_report()
assert math.isclose(pct, 16.666666666666668)
self.assert_exists("coverage.lcov")
expected_result = textwrap.dedent("""\
TN:
Expand Down Expand Up @@ -177,7 +181,8 @@ def test_is_it_x(self):
self.assert_doesnt_exist(".coverage")
cov = coverage.Coverage(branch=True, source=".")
self.start_import_stop(cov, "test_file")
cov.lcov_report()
pct = cov.lcov_report()
assert math.isclose(pct, 41.666666666666664)
self.assert_exists("coverage.lcov")
expected_result = textwrap.dedent("""\
TN:
Expand Down Expand Up @@ -225,7 +230,8 @@ def test_half_covered_branch(self):
self.assert_doesnt_exist(".coverage")
cov = coverage.Coverage(branch=True, source=".")
self.start_import_stop(cov, "main_file")
cov.lcov_report()
pct = cov.lcov_report()
assert math.isclose(pct, 66.66666666666667)
self.assert_exists("coverage.lcov")
expected_result = textwrap.dedent("""\
TN:
Expand Down Expand Up @@ -259,7 +265,8 @@ def test_empty_init_files(self):
self.assert_doesnt_exist(".coverage")
cov = coverage.Coverage(branch=True, source=".")
self.start_import_stop(cov, "__init__")
cov.lcov_report()
pct = cov.lcov_report()
assert pct == 0.0
self.assert_exists("coverage.lcov")
# Newer Pythons have truly empty empty files.
if env.PYBEHAVIOR.empty_is_empty:
Expand Down

0 comments on commit 3760b98

Please sign in to comment.