Skip to content
This repository has been archived by the owner on Sep 6, 2024. It is now read-only.

Commit

Permalink
Merge pull request #1 from MathieuLamiot/enhancement/270-expand-cover…
Browse files Browse the repository at this point in the history
…age-report-for-missing-lines

Enhancement/270 expand coverage report for missing lines
  • Loading branch information
MathieuLamiot committed Aug 26, 2024
2 parents c3ae7dd + 02237c6 commit b0f1062
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
15 changes: 14 additions & 1 deletion diff_cover/diff_cover_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
)
QUIET_HELP = "Only print errors and failures"
SHOW_UNCOVERED = "Show uncovered lines on the console"
EXPAND_COVERAGE_REPORT = (
"Append missing lines in coverage reports based on the hits of the previous line."
)
INCLUDE_UNTRACKED_HELP = "Include untracked files"
CONFIG_FILE_HELP = "The configuration file to use"
DIFF_FILE_HELP = "The diff file to use"
Expand Down Expand Up @@ -93,6 +96,13 @@ def parse_coverage_args(argv):
"--show-uncovered", action="store_true", default=None, help=SHOW_UNCOVERED
)

parser.add_argument(
"--expand_coverage_report",
action="store_true",
default=None,
help=EXPAND_COVERAGE_REPORT,
)

parser.add_argument(
"--external-css-file",
metavar="FILENAME",
Expand Down Expand Up @@ -183,6 +193,7 @@ def parse_coverage_args(argv):
"ignore_whitespace": False,
"diff_range_notation": "...",
"quiet": False,
"expand_coverage_report": False,
}

return get_config(parser=parser, argv=argv, defaults=defaults, tool=Tool.DIFF_COVER)
Expand All @@ -204,6 +215,7 @@ def generate_coverage_report(
src_roots=None,
quiet=False,
show_uncovered=False,
expand_coverage_report=False,
):
"""
Generate the diff coverage report, using kwargs from `parse_args()`.
Expand Down Expand Up @@ -231,7 +243,7 @@ def generate_coverage_report(
if len(xml_roots) > 0 and len(lcov_roots) > 0:
raise ValueError(f"Mixing LCov and XML reports is not supported yet")
elif len(xml_roots) > 0:
coverage = XmlCoverageReporter(xml_roots, src_roots)
coverage = XmlCoverageReporter(xml_roots, src_roots, expand_coverage_report)
else:
coverage = LcovCoverageReporter(lcov_roots, src_roots)

Expand Down Expand Up @@ -308,6 +320,7 @@ def main(argv=None, directory=None):
src_roots=arg_dict["src_roots"],
quiet=quiet,
show_uncovered=arg_dict["show_uncovered"],
expand_coverage_report=arg_dict["expand_coverage_report"],
)

if percent_covered >= fail_under:
Expand Down
22 changes: 21 additions & 1 deletion diff_cover/violationsreporters/violations_reporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class XmlCoverageReporter(BaseViolationReporter):
Query information from a Cobertura|Clover|JaCoCo XML coverage report.
"""

def __init__(self, xml_roots, src_roots=None):
def __init__(self, xml_roots, src_roots=None, expand_coverage_report=False):
"""
Load the XML coverage report represented
by the cElementTree with root element `xml_root`.
Expand All @@ -41,6 +41,7 @@ def __init__(self, xml_roots, src_roots=None):
self._xml_cache = [{} for i in range(len(xml_roots))]

self._src_roots = src_roots or [""]
self._expand_coverage_report = expand_coverage_report

def _get_xml_classes(self, xml_document):
"""
Expand Down Expand Up @@ -216,6 +217,25 @@ def _cache_file(self, src_path):
if line_nodes is None:
continue

# Expand coverage report with not reported lines
if self._expand_coverage_report:
reported_line_hits = {}
for line in line_nodes:
reported_line_hits[int(line.get(_number))] = int(
line.get(_hits, 0)
)
last_hit_number = 0
for line_number in range(
min(reported_line_hits.keys()), max(reported_line_hits.keys())
):
if line_number in reported_line_hits:
last_hit_number = reported_line_hits[line_number]
else:
# This is an unreported line. We add it with the previous line hit score
line_nodes.append(
{_hits: last_hit_number, _number: line_number}
)

# First case, need to define violations initially
if violations is None:
violations = {
Expand Down

0 comments on commit b0f1062

Please sign in to comment.