Skip to content

Commit

Permalink
fix: total values when ignore regex used (#176)
Browse files Browse the repository at this point in the history
  • Loading branch information
danctorres authored Feb 17, 2024
1 parent faf1a6e commit 283147c
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 19 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Release Notes

## Unreleased
* Fix total stmts, miss and cover values in the coverage report when an ignore
regex is passed. Thanks @danctorres

## 3.3.0 (2024-01-14)

Expand Down
45 changes: 29 additions & 16 deletions pycobertura/cobertura.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,21 +102,23 @@ def version(self):
"""Return the version number of the coverage report."""
return self.xml.get("version")

def line_rate(self, filename=None):
def line_rate(self, filename=None, ignore_regex=None):
"""
Return the global line rate of the coverage report. If the
`filename` file is given, return the line rate of the file.
"""

if filename is None:
if filename is None and ignore_regex is None:
return float(self.xml.get("line-rate"))

elements = self._class_elements_by_file_name[filename]
if len(elements) == 1:
return float(elements[0].get("line-rate"))
else:
total = self.total_statements(filename)
return float(self.total_hits(filename) / total) if total != 0 else 0
if ignore_regex is None:
elements = self._class_elements_by_file_name[filename]
if len(elements) == 1:
return float(elements[0].get("line-rate"))
total = self.total_statements(filename, ignore_regex)
return (
float(self.total_hits(filename, ignore_regex) / total) if total != 0 else 0
)

def branch_rate(self, filename=None):
"""
Expand Down Expand Up @@ -209,7 +211,7 @@ def file_source(self, filename):

return lines

def total_misses(self, filename=None):
def total_misses(self, filename=None, ignore_regex=None):
"""
Return the total number of uncovered statements for the file
`filename`. If `filename` is not given, return the total
Expand All @@ -218,30 +220,41 @@ def total_misses(self, filename=None):
if filename is not None:
return len(self.missed_statements(filename))

return sum([len(self.missed_statements(filename)) for filename in self.files()])
return sum(
[
len(self.missed_statements(filename))
for filename in self.files(ignore_regex)
]
)

def total_hits(self, filename=None):
def total_hits(self, filename=None, ignore_regex=None):
"""
Return the total number of covered statements for the file
`filename`. If `filename` is not given, return the total
number of covered statements for all files.
"""
if filename is not None:
return len(self.hit_statements(filename))
return sum(
[
len(self.hit_statements(filename))
for filename in self.files(ignore_regex)
]
)

return sum([len(self.hit_statements(filename)) for filename in self.files()])

def total_statements(self, filename=None):
def total_statements(self, filename=None, ignore_regex=None):
"""
Return the total number of statements for the file
`filename`. If `filename` is not given, return the total
number of statements for all files.
"""
if filename is not None:
return len(self._get_lines_by_filename(filename))

return sum(
[len(self._get_lines_by_filename(filename)) for filename in self.files()]
[
len(self._get_lines_by_filename(filename))
for filename in self.files(ignore_regex)
]
)

@memoize
Expand Down
12 changes: 9 additions & 3 deletions pycobertura/reporters.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,15 @@ def get_report_lines(self):
],
}
lines["Filename"].append("TOTAL")
lines["Stmts"] += [self.cobertura.total_statements()]
lines["Miss"] += [self.cobertura.total_misses()]
lines["Cover"] += [self.format_line_rate(self.cobertura.line_rate())]
lines["Stmts"] += [
self.cobertura.total_statements(ignore_regex=self.ignore_regex)
]
lines["Miss"] += [self.cobertura.total_misses(ignore_regex=self.ignore_regex)]
lines["Cover"] += [
self.format_line_rate(
self.cobertura.line_rate(ignore_regex=self.ignore_regex)
)
]
lines["Missing"].append("")

return lines
Expand Down
14 changes: 14 additions & 0 deletions tests/test_reporters.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,20 @@ def test_text_report__with_missing_range():
TOTAL 6 2 66.67%"""


def test_text_report__with_ignore_regex():
from pycobertura.reporters import TextReporter

cobertura = make_cobertura()
report = TextReporter(cobertura, ".*Main.java|.*ISortedArraySearch.java")

assert report.generate() == """\
Filename Stmts Miss Cover Missing
------------------------ ------- ------ ------- ---------
search/BinarySearch.java 12 1 91.67% 24
search/LinearSearch.java 7 2 71.43% 19-24
TOTAL 19 3 84.21%"""


def test_text_report_delta__no_diff():
from pycobertura.reporters import TextReporterDelta

Expand Down

0 comments on commit 283147c

Please sign in to comment.