Skip to content

Commit

Permalink
mypy: xmlreport.py
Browse files Browse the repository at this point in the history
  • Loading branch information
nedbat committed Jan 2, 2023
1 parent 0c9b5e0 commit 5580cf8
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 27 deletions.
73 changes: 47 additions & 26 deletions coverage/xmlreport.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,57 @@

"""XML reporting for coverage.py"""

from __future__ import annotations

import os
import os.path
import sys
import time
import xml.dom.minidom

from dataclasses import dataclass
from typing import Dict, IO, Iterable, Optional, TYPE_CHECKING, cast

from coverage import __url__, __version__, files
from coverage.misc import isolate_module, human_sorted, human_sorted_items
from coverage.plugin import FileReporter
from coverage.report import get_analysis_to_report
from coverage.results import Analysis
from coverage.types import TMorf

if TYPE_CHECKING:
from coverage import Coverage

os = isolate_module(os)


DTD_URL = 'https://raw.githubusercontent.com/cobertura/web/master/htdocs/xml/coverage-04.dtd'


def rate(hit, num):
def rate(hit: int, num: int) -> str:
"""Return the fraction of `hit`/`num`, as a string."""
if num == 0:
return "1"
else:
return "%.4g" % (float(hit) / num)
return "%.4g" % (hit / num)


@dataclass
class PackageData:
"""Data we keep about each "package" (in Java terms)."""
elements: Dict[str, xml.dom.minidom.Element]
hits: int
lines: int
br_hits: int
branches: int


class XmlReporter:
"""A reporter for writing Cobertura-style XML coverage results."""

report_type = "XML report"

def __init__(self, coverage):
def __init__(self, coverage: Coverage) -> None:
self.coverage = coverage
self.config = self.coverage.config

Expand All @@ -43,10 +64,10 @@ def __init__(self, coverage):
if not self.config.relative_files:
src = files.canonical_filename(src)
self.source_paths.add(src)
self.packages = {}
self.xml_out = None
self.packages: Dict[str, PackageData] = {}
self.xml_out: xml.dom.minidom.Document

def report(self, morfs, outfile=None):
def report(self, morfs: Optional[Iterable[TMorf]], outfile: Optional[IO[str]]=None) -> float:
"""Generate a Cobertura-compatible XML report for `morfs`.
`morfs` is a list of modules or file names.
Expand All @@ -60,6 +81,7 @@ def report(self, morfs, outfile=None):

# Create the DOM that will store the data.
impl = xml.dom.minidom.getDOMImplementation()
assert impl is not None
self.xml_out = impl.createDocument(None, "coverage", None)

# Write header stuff.
Expand Down Expand Up @@ -93,26 +115,25 @@ def report(self, morfs, outfile=None):

# Populate the XML DOM with the package info.
for pkg_name, pkg_data in human_sorted_items(self.packages.items()):
class_elts, lhits, lnum, bhits, bnum = pkg_data
xpackage = self.xml_out.createElement("package")
xpackages.appendChild(xpackage)
xclasses = self.xml_out.createElement("classes")
xpackage.appendChild(xclasses)
for _, class_elt in human_sorted_items(class_elts.items()):
for _, class_elt in human_sorted_items(pkg_data.elements.items()):
xclasses.appendChild(class_elt)
xpackage.setAttribute("name", pkg_name.replace(os.sep, '.'))
xpackage.setAttribute("line-rate", rate(lhits, lnum))
xpackage.setAttribute("line-rate", rate(pkg_data.hits, pkg_data.lines))
if has_arcs:
branch_rate = rate(bhits, bnum)
branch_rate = rate(pkg_data.br_hits, pkg_data.branches)
else:
branch_rate = "0"
xpackage.setAttribute("branch-rate", branch_rate)
xpackage.setAttribute("complexity", "0")

lnum_tot += lnum
lhits_tot += lhits
bnum_tot += bnum
bhits_tot += bhits
lhits_tot += pkg_data.hits
lnum_tot += pkg_data.lines
bhits_tot += pkg_data.br_hits
bnum_tot += pkg_data.branches

xcoverage.setAttribute("lines-valid", str(lnum_tot))
xcoverage.setAttribute("lines-covered", str(lhits_tot))
Expand All @@ -138,7 +159,7 @@ def report(self, morfs, outfile=None):
pct = 100.0 * (lhits_tot + bhits_tot) / denom
return pct

def xml_file(self, fr, analysis, has_arcs):
def xml_file(self, fr: FileReporter, analysis: Analysis, has_arcs: bool) -> None:
"""Add to the XML report for a single file."""

if self.config.skip_empty:
Expand All @@ -162,9 +183,9 @@ def xml_file(self, fr, analysis, has_arcs):
dirname = "/".join(dirname.split("/")[:self.config.xml_package_depth])
package_name = dirname.replace("/", ".")

package = self.packages.setdefault(package_name, [{}, 0, 0, 0, 0])
package = self.packages.setdefault(package_name, PackageData({}, 0, 0, 0, 0))

xclass = self.xml_out.createElement("class")
xclass: xml.dom.minidom.Element = self.xml_out.createElement("class")

xclass.appendChild(self.xml_out.createElement("methods"))

Expand Down Expand Up @@ -208,8 +229,8 @@ def xml_file(self, fr, analysis, has_arcs):
missing_branches = sum(t - k for t, k in branch_stats.values())
class_br_hits = class_branches - missing_branches
else:
class_branches = 0.0
class_br_hits = 0.0
class_branches = 0
class_br_hits = 0

# Finalize the statistics that are collected in the XML DOM.
xclass.setAttribute("line-rate", rate(class_hits, class_lines))
Expand All @@ -219,13 +240,13 @@ def xml_file(self, fr, analysis, has_arcs):
branch_rate = "0"
xclass.setAttribute("branch-rate", branch_rate)

package[0][rel_name] = xclass
package[1] += class_hits
package[2] += class_lines
package[3] += class_br_hits
package[4] += class_branches
package.elements[rel_name] = xclass
package.hits += class_hits
package.lines += class_lines
package.br_hits += class_br_hits
package.branches += class_branches


def serialize_xml(dom):
def serialize_xml(dom: xml.dom.minidom.Document) -> str:
"""Serialize a minidom node to XML."""
return dom.toprettyxml()
return cast(str, dom.toprettyxml())
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ setenv =
C_DE=coverage/data.py coverage/disposition.py coverage/env.py coverage/exceptions.py
C_FN=coverage/files.py coverage/inorout.py coverage/jsonreport.py coverage/lcovreport.py coverage/multiproc.py coverage/numbits.py
C_OP=coverage/parser.py coverage/phystokens.py coverage/plugin.py coverage/plugin_support.py coverage/python.py
C_QZ=coverage/report.py coverage/results.py coverage/sqldata.py coverage/tomlconfig.py coverage/types.py coverage/version.py
C_QZ=coverage/report.py coverage/results.py coverage/sqldata.py coverage/tomlconfig.py coverage/types.py coverage/version.py coverage/xmlreport.py
T_AN=tests/test_api.py tests/test_cmdline.py tests/goldtest.py tests/helpers.py tests/test_html.py
TYPEABLE={env:C__B} {env:C_CC} {env:C_DE} {env:C_FN} {env:C_OP} {env:C_QZ} {env:T_AN}

Expand Down

0 comments on commit 5580cf8

Please sign in to comment.