summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2012-04-19 19:05:22 -0400
committerNed Batchelder <ned@nedbatchelder.com>2012-04-19 19:05:22 -0400
commit43bf01f860dff446b30d5ca00bcd0dd1e41e9a09 (patch)
tree2319287a5689f212305c29cef148a26bde799a33
parent9840d2de631dbfb99306c9b2930bcf40a624bbfb (diff)
downloadpython-coveragepy-git-43bf01f860dff446b30d5ca00bcd0dd1e41e9a09.tar.gz
Refactor reporters so the config is part of construction, and is then available everywhere.
-rw-r--r--coverage/annotate.py8
-rw-r--r--coverage/control.py18
-rw-r--r--coverage/html.py17
-rw-r--r--coverage/report.py31
-rw-r--r--coverage/summary.py20
-rw-r--r--coverage/xmlreport.py11
6 files changed, 48 insertions, 57 deletions
diff --git a/coverage/annotate.py b/coverage/annotate.py
index a556d853..b7f32c1c 100644
--- a/coverage/annotate.py
+++ b/coverage/annotate.py
@@ -26,20 +26,20 @@ class AnnotateReporter(Reporter):
"""
- def __init__(self, coverage, ignore_errors=False):
- super(AnnotateReporter, self).__init__(coverage, ignore_errors)
+ def __init__(self, coverage, config):
+ super(AnnotateReporter, self).__init__(coverage, config)
self.directory = None
blank_re = re.compile(r"\s*(#|$)")
else_re = re.compile(r"\s*else\s*:\s*(#|$)")
- def report(self, morfs, config, directory=None):
+ def report(self, morfs, directory=None):
"""Run the report.
See `coverage.report()` for arguments.
"""
- self.report_files(self.annotate_file, morfs, config, directory)
+ self.report_files(self.annotate_file, morfs, directory)
def annotate_file(self, cu, analysis):
"""Annotate a single file.
diff --git a/coverage/control.py b/coverage/control.py
index ca2a51ab..7373b316 100644
--- a/coverage/control.py
+++ b/coverage/control.py
@@ -560,10 +560,8 @@ class coverage(object):
ignore_errors=ignore_errors, omit=omit, include=include,
show_missing=show_missing,
)
- reporter = SummaryReporter(
- self, self.config.show_missing, self.config.ignore_errors
- )
- reporter.report(morfs, outfile=file, config=self.config)
+ reporter = SummaryReporter(self, self.config)
+ reporter.report(morfs, outfile=file)
def annotate(self, morfs=None, directory=None, ignore_errors=None,
omit=None, include=None):
@@ -580,8 +578,8 @@ class coverage(object):
self.config.from_args(
ignore_errors=ignore_errors, omit=omit, include=include
)
- reporter = AnnotateReporter(self, self.config.ignore_errors)
- reporter.report(morfs, config=self.config, directory=directory)
+ reporter = AnnotateReporter(self, self.config)
+ reporter.report(morfs, directory=directory)
def html_report(self, morfs=None, directory=None, ignore_errors=None,
omit=None, include=None):
@@ -594,8 +592,8 @@ class coverage(object):
ignore_errors=ignore_errors, omit=omit, include=include,
html_dir=directory,
)
- reporter = HtmlReporter(self, self.config.ignore_errors)
- reporter.report(morfs, config=self.config)
+ reporter = HtmlReporter(self, self.config)
+ reporter.report(morfs)
def xml_report(self, morfs=None, outfile=None, ignore_errors=None,
omit=None, include=None):
@@ -621,8 +619,8 @@ class coverage(object):
outfile = open(self.config.xml_output, "w")
file_to_close = outfile
try:
- reporter = XmlReporter(self, self.config.ignore_errors)
- reporter.report(morfs, outfile=outfile, config=self.config)
+ reporter = XmlReporter(self, self.config)
+ reporter.report(morfs, outfile=outfile)
finally:
if file_to_close:
file_to_close.close()
diff --git a/coverage/html.py b/coverage/html.py
index 09683ad8..af27edfa 100644
--- a/coverage/html.py
+++ b/coverage/html.py
@@ -41,8 +41,8 @@ class HtmlReporter(Reporter):
"keybd_open.png",
]
- def __init__(self, cov, ignore_errors=False):
- super(HtmlReporter, self).__init__(cov, ignore_errors)
+ def __init__(self, cov, config):
+ super(HtmlReporter, self).__init__(cov, config)
self.directory = None
self.template_globals = {
'escape': escape,
@@ -59,28 +59,27 @@ class HtmlReporter(Reporter):
self.arcs = self.coverage.data.has_arcs()
self.status = HtmlStatus()
- def report(self, morfs, config=None):
+ def report(self, morfs):
"""Generate an HTML report for `morfs`.
- `morfs` is a list of modules or filenames. `config` is a
- CoverageConfig instance.
+ `morfs` is a list of modules or filenames.
"""
- assert config.html_dir, "must provide a directory for html reporting"
+ assert self.config.html_dir, "must give a directory for html reporting"
# Read the status data.
- self.status.read(config.html_dir)
+ self.status.read(self.config.html_dir)
# Check that this run used the same settings as the last run.
m = Hasher()
- m.update(config)
+ m.update(self.config)
these_settings = m.digest()
if self.status.settings_hash() != these_settings:
self.status.reset()
self.status.set_settings_hash(these_settings)
# Process all the files.
- self.report_files(self.html_file, morfs, config, config.html_dir)
+ self.report_files(self.html_file, morfs, self.config.html_dir)
if not self.files:
raise CoverageException("No data to report.")
diff --git a/coverage/report.py b/coverage/report.py
index 5d187996..f6a9c2a8 100644
--- a/coverage/report.py
+++ b/coverage/report.py
@@ -7,15 +7,15 @@ from coverage.misc import CoverageException, NoSource, NotPython
class Reporter(object):
"""A base class for all reporters."""
- def __init__(self, coverage, ignore_errors=False):
+ def __init__(self, coverage, config):
"""Create a reporter.
- `coverage` is the coverage instance. `ignore_errors` controls how
- skittish the reporter will be during file processing.
+ `coverage` is the coverage instance. `config` is an instance of
+ CoverageConfig, for controlling all sorts of behavior.
"""
self.coverage = coverage
- self.ignore_errors = ignore_errors
+ self.config = config
# The code units to report on. Set by find_code_units.
self.code_units = []
@@ -24,19 +24,18 @@ class Reporter(object):
# classes.
self.directory = None
- def find_code_units(self, morfs, config):
+ def find_code_units(self, morfs):
"""Find the code units we'll report on.
- `morfs` is a list of modules or filenames. `config` is a
- CoverageConfig instance.
+ `morfs` is a list of modules or filenames.
"""
morfs = morfs or self.coverage.data.measured_files()
file_locator = self.coverage.file_locator
self.code_units = code_unit_factory(morfs, file_locator)
- if config.include:
- patterns = [file_locator.abs_file(p) for p in config.include]
+ if self.config.include:
+ patterns = [file_locator.abs_file(p) for p in self.config.include]
filtered = []
for cu in self.code_units:
for pattern in patterns:
@@ -45,8 +44,8 @@ class Reporter(object):
break
self.code_units = filtered
- if config.omit:
- patterns = [file_locator.abs_file(p) for p in config.omit]
+ if self.config.omit:
+ patterns = [file_locator.abs_file(p) for p in self.config.omit]
filtered = []
for cu in self.code_units:
for pattern in patterns:
@@ -58,7 +57,7 @@ class Reporter(object):
self.code_units.sort()
- def report_files(self, report_fn, morfs, config, directory=None):
+ def report_files(self, report_fn, morfs, directory=None):
"""Run a reporting function on a number of morfs.
`report_fn` is called for each relative morf in `morfs`. It is called
@@ -69,10 +68,8 @@ class Reporter(object):
where `code_unit` is the `CodeUnit` for the morf, and `analysis` is
the `Analysis` for the morf.
- `config` is a CoverageConfig instance.
-
"""
- self.find_code_units(morfs, config)
+ self.find_code_units(morfs)
if not self.code_units:
raise CoverageException("No data to report.")
@@ -85,10 +82,10 @@ class Reporter(object):
try:
report_fn(cu, self.coverage._analyze(cu))
except NoSource:
- if not self.ignore_errors:
+ if not self.config.ignore_errors:
raise
except NotPython:
# Only report errors for .py files, and only if we didn't
# explicitly suppress those errors.
- if cu.should_be_python(".py") and not self.ignore_errors:
+ if cu.should_be_python(".py") and not self.config.ignore_errors:
raise
diff --git a/coverage/summary.py b/coverage/summary.py
index dcd5b159..fec94810 100644
--- a/coverage/summary.py
+++ b/coverage/summary.py
@@ -10,19 +10,17 @@ from coverage.misc import NotPython
class SummaryReporter(Reporter):
"""A reporter for writing the summary report."""
- def __init__(self, coverage, show_missing=True, ignore_errors=False):
- super(SummaryReporter, self).__init__(coverage, ignore_errors)
- self.show_missing = show_missing
+ def __init__(self, coverage, config):
+ super(SummaryReporter, self).__init__(coverage, config)
self.branches = coverage.data.has_arcs()
- def report(self, morfs, outfile=None, config=None):
+ def report(self, morfs, outfile=None):
"""Writes a report summarizing coverage statistics per module.
- `outfile` is a file object to write the summary to. `config` is a
- CoverageConfig instance.
+ `outfile` is a file object to write the summary to.
"""
- self.find_code_units(morfs, config)
+ self.find_code_units(morfs)
# Prepare the formatting strings
max_name = max([len(cu.name) for cu in self.code_units] + [5])
@@ -36,7 +34,7 @@ class SummaryReporter(Reporter):
width100 = Numbers.pc_str_width()
header += "%*s" % (width100+4, "Cover")
fmt_coverage += "%%%ds%%%%" % (width100+3,)
- if self.show_missing:
+ if self.config.show_missing:
header += " Missing"
fmt_coverage += " %s"
rule = "-" * len(header) + "\n"
@@ -60,14 +58,14 @@ class SummaryReporter(Reporter):
if self.branches:
args += (nums.n_branches, nums.n_missing_branches)
args += (nums.pc_covered_str,)
- if self.show_missing:
+ if self.config.show_missing:
args += (analysis.missing_formatted(),)
outfile.write(fmt_coverage % args)
total += nums
except KeyboardInterrupt: # pragma: no cover
raise
except:
- report_it = not self.ignore_errors
+ report_it = not self.config.ignore_errors
if report_it:
typ, msg = sys.exc_info()[:2]
if typ is NotPython and not cu.should_be_python(".py"):
@@ -81,6 +79,6 @@ class SummaryReporter(Reporter):
if self.branches:
args += (total.n_branches, total.n_missing_branches)
args += (total.pc_covered_str,)
- if self.show_missing:
+ if self.config.show_missing:
args += ("",)
outfile.write(fmt_coverage % args)
diff --git a/coverage/xmlreport.py b/coverage/xmlreport.py
index 5f6cc87e..a65d5a6d 100644
--- a/coverage/xmlreport.py
+++ b/coverage/xmlreport.py
@@ -15,20 +15,19 @@ def rate(hit, num):
class XmlReporter(Reporter):
"""A reporter for writing Cobertura-style XML coverage results."""
- def __init__(self, coverage, ignore_errors=False):
- super(XmlReporter, self).__init__(coverage, ignore_errors)
+ def __init__(self, coverage, config):
+ super(XmlReporter, self).__init__(coverage, config)
self.packages = None
self.xml_out = None
self.arcs = coverage.data.has_arcs()
- def report(self, morfs, outfile=None, config=None):
+ def report(self, morfs, outfile=None):
"""Generate a Cobertura-compatible XML report for `morfs`.
`morfs` is a list of modules or filenames.
- `outfile` is a file object to write the XML to. `config` is a
- CoverageConfig instance.
+ `outfile` is a file object to write the XML to.
"""
# Initial setup.
@@ -54,7 +53,7 @@ class XmlReporter(Reporter):
# Call xml_file for each file in the data.
self.packages = {}
- self.report_files(self.xml_file, morfs, config)
+ self.report_files(self.xml_file, morfs)
lnum_tot, lhits_tot = 0, 0
bnum_tot, bhits_tot = 0, 0