summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CHANGES.txt14
-rw-r--r--Makefile5
-rw-r--r--coverage/annotate.py10
-rw-r--r--coverage/bytecode.py16
-rw-r--r--coverage/codeunit.py38
-rw-r--r--coverage/collector.py8
-rw-r--r--coverage/control.py61
-rw-r--r--coverage/html.py18
-rw-r--r--coverage/htmlfiles/pyfile.html4
-rw-r--r--coverage/phystokens.py5
-rw-r--r--coverage/python.py41
-rw-r--r--coverage/report.py44
-rw-r--r--coverage/results.py6
-rw-r--r--coverage/summary.py14
-rw-r--r--coverage/tracer.c311
-rw-r--r--coverage/version.py4
-rw-r--r--coverage/xmlreport.py11
-rw-r--r--doc/install.rst4
-rw-r--r--doc/trouble.rst6
-rw-r--r--howto.txt4
-rw-r--r--lab/dataflow.txt2
-rw-r--r--lab/show_pyc.py2
-rw-r--r--requirements.txt2
-rw-r--r--tests/coveragetest.py16
-rw-r--r--tests/plugin2.py8
-rw-r--r--tests/test_arcs.py44
-rw-r--r--tests/test_concurrency.py7
-rw-r--r--tests/test_filereporter.py (renamed from tests/test_codeunit.py)48
-rw-r--r--tests/test_html.py10
-rw-r--r--tests/test_oddball.py54
-rw-r--r--tests/test_plugins.py211
-rw-r--r--tox.ini5
32 files changed, 701 insertions, 332 deletions
diff --git a/CHANGES.txt b/CHANGES.txt
index f96db6e..45c279c 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -3,6 +3,20 @@ Change history for Coverage.py
------------------------------
+Latest
+------
+
+- Branch coverage couldn't properly handle certain extremely long files. This
+ is now fixed (`issue 359`_).
+
+- HTML reports were truncated at formfeed characters. This is now fixed
+ (`issue 360`_). It's always fun when the problem is due to a `bug in the
+ Python standard library <http://bugs.python.org/issue19035>`_.
+
+.. _issue 359: https://bitbucket.org/ned/coveragepy/issue/359/xml-report-chunk-error
+.. _issue 360: https://bitbucket.org/ned/coveragepy/issue/360/html-reports-get-confused-by-l-in-the-code
+
+
Version 4.0a5 --- 16 February 2015
----------------------------------
diff --git a/Makefile b/Makefile
index 9c25cd6..b755d07 100644
--- a/Makefile
+++ b/Makefile
@@ -6,7 +6,7 @@ default:
clean:
-rm -f *.pyd */*.pyd
-rm -f *.so */*.so
- PYTHONPATH=. python tests/test_farm.py clean
+ -PYTHONPATH=. python tests/test_farm.py clean
-rm -rf build coverage.egg-info dist htmlcov
-rm -f *.pyc */*.pyc */*/*.pyc */*/*/*.pyc */*/*/*/*.pyc */*/*/*/*/*.pyc
-rm -f *.pyo */*.pyo */*/*.pyo */*/*/*.pyo */*/*/*/*.pyo */*/*/*/*/*.pyo
@@ -58,6 +58,9 @@ kit:
kit_upload:
$(SDIST_CMD) upload
+kit_local:
+ cp -v dist/* `awk -F "=" '/find-links/ {print $$2}' ~/.pip/pip.conf`
+
pypi:
python setup.py register
diff --git a/coverage/annotate.py b/coverage/annotate.py
index 3487feb..b77df4e 100644
--- a/coverage/annotate.py
+++ b/coverage/annotate.py
@@ -41,10 +41,10 @@ class AnnotateReporter(Reporter):
"""
self.report_files(self.annotate_file, morfs, directory)
- def annotate_file(self, cu, analysis):
+ def annotate_file(self, fr, analysis):
"""Annotate a single file.
- `cu` is the CodeUnit for the file to annotate.
+ `fr` is the FileReporter for the file to annotate.
"""
statements = sorted(analysis.statements)
@@ -52,18 +52,18 @@ class AnnotateReporter(Reporter):
excluded = sorted(analysis.excluded)
if self.directory:
- dest_file = os.path.join(self.directory, cu.flat_rootname())
+ dest_file = os.path.join(self.directory, fr.flat_rootname())
if dest_file.endswith("_py"):
dest_file = dest_file[:-3] + ".py"
dest_file += ",cover"
else:
- dest_file = cu.filename + ",cover"
+ dest_file = fr.filename + ",cover"
with open(dest_file, 'w') as dest:
i = 0
j = 0
covered = True
- source = cu.source()
+ source = fr.source()
for lineno, line in enumerate(source.splitlines(True), start=1):
while i < len(statements) and statements[i] < lineno:
i += 1
diff --git a/coverage/bytecode.py b/coverage/bytecode.py
index 3f62dfa..d730493 100644
--- a/coverage/bytecode.py
+++ b/coverage/bytecode.py
@@ -1,9 +1,11 @@
"""Bytecode manipulation for coverage.py"""
-import opcode, types
+import opcode
+import types
from coverage.backward import byte_to_int
+
class ByteCode(object):
"""A single bytecode."""
def __init__(self):
@@ -26,6 +28,9 @@ class ByteCode(object):
class ByteCodes(object):
"""Iterator over byte codes in `code`.
+ This handles the logic of EXTENDED_ARG byte codes internally. Those byte
+ codes are not returned by this iterator.
+
Returns `ByteCode` objects.
"""
@@ -37,6 +42,7 @@ class ByteCodes(object):
def __iter__(self):
offset = 0
+ ext_arg = 0
while offset < len(self.code):
bc = ByteCode()
bc.op = self[offset]
@@ -44,7 +50,7 @@ class ByteCodes(object):
next_offset = offset+1
if bc.op >= opcode.HAVE_ARGUMENT:
- bc.arg = self[offset+1] + 256*self[offset+2]
+ bc.arg = ext_arg + self[offset+1] + 256*self[offset+2]
next_offset += 2
label = -1
@@ -55,7 +61,11 @@ class ByteCodes(object):
bc.jump_to = label
bc.next_offset = offset = next_offset
- yield bc
+ if bc.op == opcode.EXTENDED_ARG:
+ ext_arg = bc.arg * 256*256
+ else:
+ ext_arg = 0
+ yield bc
class CodeObjects(object):
diff --git a/coverage/codeunit.py b/coverage/codeunit.py
deleted file mode 100644
index ef7e848..0000000
--- a/coverage/codeunit.py
+++ /dev/null
@@ -1,38 +0,0 @@
-"""Code unit (module) handling for Coverage."""
-
-import os
-
-from coverage.files import FileLocator
-from coverage.plugin import FileReporter
-
-
-class CodeUnit(FileReporter):
- """Code unit: a filename or module.
-
- Instance attributes:
-
- `name` is a human-readable name for this code unit.
- `filename` is the os path from which we can read the source.
-
- """
-
- def __init__(self, morf, file_locator=None):
- self.file_locator = file_locator or FileLocator()
-
- if hasattr(morf, '__file__'):
- filename = morf.__file__
- else:
- filename = morf
- filename = self._adjust_filename(filename)
- self.filename = self.file_locator.canonical_filename(filename)
-
- if hasattr(morf, '__name__'):
- name = morf.__name__
- name = name.replace(".", os.sep) + ".py"
- else:
- name = self.file_locator.relative_filename(filename)
- self.name = name
-
- def _adjust_filename(self, f):
- # TODO: This shouldn't be in the base class, right?
- return f
diff --git a/coverage/collector.py b/coverage/collector.py
index c156090..948cbbb 100644
--- a/coverage/collector.py
+++ b/coverage/collector.py
@@ -291,10 +291,14 @@ class Collector(object):
"""
if self.branch:
# If we were measuring branches, then we have to re-build the dict
- # to show line data.
+ # to show line data. We'll use the first lines of all the arcs,
+ # if they are actual lines. We don't need the second lines, because
+ # the second lines will also be first lines, sometimes to exits.
line_data = {}
for f, arcs in self.data.items():
- line_data[f] = dict((l1, None) for l1, _ in arcs.keys() if l1)
+ line_data[f] = dict(
+ (l1, None) for l1, _ in arcs.keys() if l1 > 0
+ )
return line_data
else:
return self.data
diff --git a/coverage/control.py b/coverage/control.py
index 0bbe301..563925e 100644
--- a/coverage/control.py
+++ b/coverage/control.py
@@ -18,13 +18,13 @@ from coverage.data import CoverageData
from coverage.debug import DebugControl
from coverage.files import FileLocator, TreeMatcher, FnmatchMatcher
from coverage.files import PathAliases, find_python_files, prep_patterns
-from coverage.files import ModuleMatcher
+from coverage.files import ModuleMatcher, abs_file
from coverage.html import HtmlReporter
from coverage.misc import CoverageException, bool_or_none, join_regex
from coverage.misc import file_be_gone, overrides
from coverage.monkey import patch_multiprocessing
from coverage.plugin import CoveragePlugin, FileReporter
-from coverage.python import PythonCodeUnit
+from coverage.python import PythonFileReporter
from coverage.results import Analysis, Numbers
from coverage.summary import SummaryReporter
from coverage.xmlreport import XmlReporter
@@ -168,7 +168,7 @@ class Coverage(object):
self.omit = self.include = self.source = None
self.source_pkgs = self.file_locator = None
self.data = self.collector = None
- self.plugins = self.file_tracers = None
+ self.plugins = self.file_tracing_plugins = None
self.pylib_dirs = self.cover_dir = None
self.data_suffix = self.run_suffix = None
self._exclude_re = None
@@ -203,10 +203,10 @@ class Coverage(object):
# Load plugins
self.plugins = Plugins.load_plugins(self.config.plugins, self.config)
- self.file_tracers = []
+ self.file_tracing_plugins = []
for plugin in self.plugins:
if overrides(plugin, "file_tracer", CoveragePlugin):
- self.file_tracers.append(plugin)
+ self.file_tracing_plugins.append(plugin)
# _exclude_re is a dict that maps exclusion list names to compiled
# regexes.
@@ -242,15 +242,18 @@ class Coverage(object):
)
# Early warning if we aren't going to be able to support plugins.
- if self.file_tracers and not self.collector.supports_plugins:
- raise CoverageException(
+ if self.file_tracing_plugins and not self.collector.supports_plugins:
+ self._warn(
"Plugin file tracers (%s) aren't supported with %s" % (
", ".join(
- ft._coverage_plugin_name for ft in self.file_tracers
+ plugin._coverage_plugin_name
+ for plugin in self.file_tracing_plugins
),
self.collector.tracer_name(),
)
)
+ for plugin in self.file_tracing_plugins:
+ plugin._coverage_enabled = False
# Suffixes are a bit tricky. We want to use the data suffix only when
# collecting data, not when combining data. So we save it as
@@ -341,7 +344,7 @@ class Coverage(object):
def _canonical_dir(self, morf):
"""Return the canonical directory of the module or file `morf`."""
- morf_filename = PythonCodeUnit(morf, self).filename
+ morf_filename = PythonFileReporter(morf, self).filename
return os.path.split(morf_filename)[0]
def _source_for_file(self, filename):
@@ -462,15 +465,14 @@ class Coverage(object):
# Try the plugins, see if they have an opinion about the file.
plugin = None
- for plugin in self.file_tracers:
+ for plugin in self.file_tracing_plugins:
if not plugin._coverage_enabled:
continue
try:
file_tracer = plugin.file_tracer(canonical)
if file_tracer is not None:
- file_tracer._coverage_plugin_name = \
- plugin._coverage_plugin_name
+ file_tracer._coverage_plugin = plugin
disp.trace = True
disp.file_tracer = file_tracer
if file_tracer.has_dynamic_source_filename():
@@ -481,7 +483,7 @@ class Coverage(object):
file_tracer.source_filename()
)
break
- except Exception as e:
+ except Exception:
self._warn(
"Disabling plugin %r due to an exception:" % (
plugin._coverage_plugin_name
@@ -835,12 +837,13 @@ class Coverage(object):
plugin = None
if isinstance(morf, string_class):
- plugin_name = self.data.plugin_data().get(morf)
+ abs_morf = abs_file(morf)
+ plugin_name = self.data.plugin_data().get(abs_morf)
if plugin_name:
plugin = self.plugins.get(plugin_name)
if plugin:
- file_reporter = plugin.file_reporter(morf)
+ file_reporter = plugin.file_reporter(abs_morf)
if file_reporter is None:
raise CoverageException(
"Plugin %r did not provide a file reporter for %r." % (
@@ -848,7 +851,14 @@ class Coverage(object):
)
)
else:
- file_reporter = PythonCodeUnit(morf, self)
+ file_reporter = PythonFileReporter(morf, self)
+
+ # The FileReporter can have a name attribute, but if it doesn't, we'll
+ # supply it as the relative path to self.filename.
+ if not hasattr(file_reporter, "name"):
+ file_reporter.name = self.file_locator.relative_filename(
+ file_reporter.filename
+ )
return file_reporter
@@ -887,9 +897,9 @@ class Coverage(object):
Each module in `morfs` is listed, with counts of statements, executed
statements, missing statements, and a list of lines missed.
- `include` is a list of filename patterns. Modules whose filenames
- match those patterns will be included in the report. Modules matching
- `omit` will not be included in the report.
+ `include` is a list of filename patterns. Files that match will be
+ included in the report. Files matching `omit` will not be included in
+ the report.
Returns a float, the total percentage covered.
@@ -987,7 +997,7 @@ class Coverage(object):
outfile = open(self.config.xml_output, "w")
file_to_close = outfile
try:
- reporter = XmlReporter(self, self.config)
+ reporter = XmlReporter(self, self.config, self.file_locator)
return reporter.report(morfs, outfile=outfile)
except CoverageException:
delete_file = True
@@ -1009,15 +1019,20 @@ class Coverage(object):
except AttributeError:
implementation = "unknown"
+ ft_plugins = []
+ for ft in self.file_tracing_plugins:
+ ft_name = ft._coverage_plugin_name
+ if not ft._coverage_enabled:
+ ft_name += " (disabled)"
+ ft_plugins.append(ft_name)
+
info = [
('version', covmod.__version__),
('coverage', covmod.__file__),
('cover_dir', self.cover_dir),
('pylib_dirs', self.pylib_dirs),
('tracer', self.collector.tracer_name()),
- ('file_tracers', [
- ft._coverage_plugin_name for ft in self.file_tracers
- ]),
+ ('file_tracing_plugins', ft_plugins),
('config_files', self.config.attempted_config_files),
('configs_read', self.config.config_files),
('data_path', self.data.filename),
diff --git a/coverage/html.py b/coverage/html.py
index d4a2229..8ed085b 100644
--- a/coverage/html.py
+++ b/coverage/html.py
@@ -150,20 +150,20 @@ class HtmlReporter(Reporter):
with open(fname, "wb") as fout:
fout.write(html.encode('ascii', 'xmlcharrefreplace'))
- def file_hash(self, source, cu):
+ def file_hash(self, source, fr):
"""Compute a hash that changes if the file needs to be re-reported."""
m = Hasher()
m.update(source)
- self.coverage.data.add_to_hash(cu.filename, m)
+ self.coverage.data.add_to_hash(fr.filename, m)
return m.hexdigest()
- def html_file(self, cu, analysis):
+ def html_file(self, fr, analysis):
"""Generate an HTML file for one source file."""
- source = cu.source()
+ source = fr.source()
# Find out if the file on disk is already correct.
- flat_rootname = cu.flat_rootname()
- this_hash = self.file_hash(source.encode('utf-8'), cu)
+ flat_rootname = fr.flat_rootname()
+ this_hash = self.file_hash(source.encode('utf-8'), fr)
that_hash = self.status.file_hash(flat_rootname)
if this_hash == that_hash:
# Nothing has changed to require the file to be reported again.
@@ -186,7 +186,7 @@ class HtmlReporter(Reporter):
lines = []
- for lineno, line in enumerate(cu.source_token_lines(), start=1):
+ for lineno, line in enumerate(fr.source_token_lines(), start=1):
# Figure out how to mark this line.
line_class = []
annotate_html = ""
@@ -236,7 +236,7 @@ class HtmlReporter(Reporter):
template_values = {
'c_exc': c_exc, 'c_mis': c_mis, 'c_par': c_par, 'c_run': c_run,
'arcs': self.arcs, 'extra_css': self.extra_css,
- 'cu': cu, 'nums': nums, 'lines': lines,
+ 'fr': fr, 'nums': nums, 'lines': lines,
}
html = spaceless(self.source_tmpl.render(template_values))
@@ -248,7 +248,7 @@ class HtmlReporter(Reporter):
index_info = {
'nums': nums,
'html_filename': html_filename,
- 'name': cu.name,
+ 'name': fr.name,
}
self.files.append(index_info)
self.status.set_index_info(flat_rootname, index_info)
diff --git a/coverage/htmlfiles/pyfile.html b/coverage/htmlfiles/pyfile.html
index 1219107..72b6928 100644
--- a/coverage/htmlfiles/pyfile.html
+++ b/coverage/htmlfiles/pyfile.html
@@ -5,7 +5,7 @@
{# IE8 rounds line-height incorrectly, and adding this emulateIE7 line makes it right! #}
{# http://social.msdn.microsoft.com/Forums/en-US/iewebdevelopment/thread/7684445e-f080-4d8f-8529-132763348e21 #}
<meta http-equiv="X-UA-Compatible" content="IE=emulateIE7" />
- <title>Coverage for {{cu.name|escape}}: {{nums.pc_covered_str}}%</title>
+ <title>Coverage for {{fr.name|escape}}: {{nums.pc_covered_str}}%</title>
<link rel="stylesheet" href="style.css" type="text/css">
{% if extra_css %}
<link rel="stylesheet" href="{{ extra_css }}" type="text/css">
@@ -22,7 +22,7 @@
<div id="header">
<div class="content">
- <h1>Coverage for <b>{{cu.name|escape}}</b> :
+ <h1>Coverage for <b>{{fr.name|escape}}</b> :
<span class="pc_cov">{{nums.pc_covered_str}}%</span>
</h1>
diff --git a/coverage/phystokens.py b/coverage/phystokens.py
index b3b0870..ed6bd23 100644
--- a/coverage/phystokens.py
+++ b/coverage/phystokens.py
@@ -85,8 +85,11 @@ def source_token_lines(source):
ws_tokens = set([token.INDENT, token.DEDENT, token.NEWLINE, tokenize.NL])
line = []
col = 0
- source = source.expandtabs(8).replace('\r\n', '\n')
+
+ # The \f is because of http://bugs.python.org/issue19035
+ source = source.expandtabs(8).replace('\r\n', '\n').replace('\f', ' ')
tokgen = generate_tokens(source)
+
for ttype, ttext, (_, scol), (_, ecol), _ in phys_tokens(tokgen):
mark_start = True
for part in re.split('(\n)', ttext):
diff --git a/coverage/python.py b/coverage/python.py
index 53da561..19212a5 100644
--- a/coverage/python.py
+++ b/coverage/python.py
@@ -7,10 +7,11 @@ import zipimport
from coverage import env
from coverage.backward import unicode_class
-from coverage.codeunit import CodeUnit
+from coverage.files import FileLocator
from coverage.misc import NoSource, join_regex
from coverage.parser import PythonParser
from coverage.phystokens import source_token_lines, source_encoding
+from coverage.plugin import FileReporter
def read_python_source(filename):
@@ -86,13 +87,35 @@ def get_zip_bytes(filename):
return None
-class PythonCodeUnit(CodeUnit):
- """Represents a Python file."""
+class PythonFileReporter(FileReporter):
+ """Report support for a Python file."""
def __init__(self, morf, coverage=None):
self.coverage = coverage
- file_locator = coverage.file_locator if coverage else None
- super(PythonCodeUnit, self).__init__(morf, file_locator)
+ file_locator = coverage.file_locator if coverage else FileLocator()
+
+ if hasattr(morf, '__file__'):
+ filename = morf.__file__
+ else:
+ filename = morf
+
+ # .pyc files should always refer to a .py instead.
+ if filename.endswith(('.pyc', '.pyo')):
+ filename = filename[:-1]
+ elif filename.endswith('$py.class'): # Jython
+ filename = filename[:-9] + ".py"
+
+ super(PythonFileReporter, self).__init__(
+ file_locator.canonical_filename(filename)
+ )
+
+ if hasattr(morf, '__name__'):
+ name = morf.__name__
+ name = name.replace(".", os.sep) + ".py"
+ else:
+ name = file_locator.relative_filename(filename)
+ self.name = name
+
self._source = None
self._parser = None
self._statements = None
@@ -138,14 +161,6 @@ class PythonCodeUnit(CodeUnit):
def exit_counts(self):
return self.parser.exit_counts()
- def _adjust_filename(self, fname):
- # .pyc files should always refer to a .py instead.
- if fname.endswith(('.pyc', '.pyo')):
- fname = fname[:-1]
- elif fname.endswith('$py.class'): # Jython
- fname = fname[:-9] + ".py"
- return fname
-
def source(self):
if self._source is None:
self._source = get_python_source(self.filename)
diff --git a/coverage/report.py b/coverage/report.py
index 93b6c92..33a4607 100644
--- a/coverage/report.py
+++ b/coverage/report.py
@@ -19,40 +19,40 @@ class Reporter(object):
self.coverage = coverage
self.config = config
- # The code units to report on. Set by find_code_units.
- self.code_units = []
+ # The FileReporters to report on. Set by find_file_reporters.
+ self.file_reporters = []
# The directory into which to place the report, used by some derived
# classes.
self.directory = None
- def find_code_units(self, morfs):
- """Find the code units we'll report on.
+ def find_file_reporters(self, morfs):
+ """Find the FileReporters we'll report on.
`morfs` is a list of modules or filenames.
"""
- self.code_units = self.coverage._get_file_reporters(morfs)
+ self.file_reporters = self.coverage._get_file_reporters(morfs)
if self.config.include:
patterns = prep_patterns(self.config.include)
matcher = FnmatchMatcher(patterns)
filtered = []
- for cu in self.code_units:
- if matcher.match(cu.filename):
- filtered.append(cu)
- self.code_units = filtered
+ for fr in self.file_reporters:
+ if matcher.match(fr.filename):
+ filtered.append(fr)
+ self.file_reporters = filtered
if self.config.omit:
patterns = prep_patterns(self.config.omit)
matcher = FnmatchMatcher(patterns)
filtered = []
- for cu in self.code_units:
- if not matcher.match(cu.filename):
- filtered.append(cu)
- self.code_units = filtered
+ for fr in self.file_reporters:
+ if not matcher.match(fr.filename):
+ filtered.append(fr)
+ self.file_reporters = filtered
- self.code_units.sort()
+ self.file_reporters.sort()
def report_files(self, report_fn, morfs, directory=None):
"""Run a reporting function on a number of morfs.
@@ -60,29 +60,29 @@ class Reporter(object):
`report_fn` is called for each relative morf in `morfs`. It is called
as::
- report_fn(code_unit, analysis)
+ report_fn(file_reporter, analysis)
- where `code_unit` is the `CodeUnit` for the morf, and `analysis` is
- the `Analysis` for the morf.
+ where `file_reporter` is the `FileReporter` for the morf, and
+ `analysis` is the `Analysis` for the morf.
"""
- self.find_code_units(morfs)
+ self.find_file_reporters(morfs)
- if not self.code_units:
+ if not self.file_reporters:
raise CoverageException("No data to report.")
self.directory = directory
if self.directory and not os.path.exists(self.directory):
os.makedirs(self.directory)
- for cu in self.code_units:
+ for fr in self.file_reporters:
try:
- report_fn(cu, self.coverage._analyze(cu))
+ report_fn(fr, self.coverage._analyze(fr))
except NoSource:
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() and not self.config.ignore_errors:
+ if fr.should_be_python() and not self.config.ignore_errors:
raise
diff --git a/coverage/results.py b/coverage/results.py
index 0b27971..c1718d4 100644
--- a/coverage/results.py
+++ b/coverage/results.py
@@ -7,11 +7,11 @@ from coverage.misc import format_lines
class Analysis(object):
- """The results of analyzing a code unit."""
+ """The results of analyzing a FileReporter."""
- def __init__(self, cov, code_unit):
+ def __init__(self, cov, file_reporters):
self.coverage = cov
- self.file_reporter = code_unit
+ self.file_reporter = file_reporters
self.filename = self.file_reporter.filename
self.statements = self.file_reporter.statements()
self.excluded = self.file_reporter.excluded_statements()
diff --git a/coverage/summary.py b/coverage/summary.py
index 10ac7e2..5b8c903 100644
--- a/coverage/summary.py
+++ b/coverage/summary.py
@@ -20,10 +20,10 @@ class SummaryReporter(Reporter):
`outfile` is a file object to write the summary to.
"""
- self.find_code_units(morfs)
+ self.find_file_reporters(morfs)
# Prepare the formatting strings
- max_name = max([len(cu.name) for cu in self.code_units] + [5])
+ max_name = max([len(fr.name) for fr in self.file_reporters] + [5])
fmt_name = "%%- %ds " % max_name
fmt_err = "%s %s: %s\n"
header = (fmt_name % "Name") + " Stmts Miss"
@@ -50,9 +50,9 @@ class SummaryReporter(Reporter):
total = Numbers()
- for cu in self.code_units:
+ for fr in self.file_reporters:
try:
- analysis = self.coverage._analyze(cu)
+ analysis = self.coverage._analyze(fr)
nums = analysis.numbers
if self.config.skip_covered:
@@ -65,7 +65,7 @@ class SummaryReporter(Reporter):
if no_missing_lines and no_missing_branches:
continue
- args = (cu.name, nums.n_statements, nums.n_missing)
+ args = (fr.name, nums.n_statements, nums.n_missing)
if self.branches:
args += (nums.n_branches, nums.n_partial_branches)
args += (nums.pc_covered_str,)
@@ -84,10 +84,10 @@ class SummaryReporter(Reporter):
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():
+ if typ is NotPython and not fr.should_be_python():
report_it = False
if report_it:
- outfile.write(fmt_err % (cu.name, typ.__name__, msg))
+ outfile.write(fmt_err % (fr.name, typ.__name__, msg))
if total.n_files > 1:
outfile.write(rule)
diff --git a/coverage/tracer.c b/coverage/tracer.c
index 43ecd18..52543b8 100644
--- a/coverage/tracer.c
+++ b/coverage/tracer.c
@@ -21,7 +21,9 @@
#define MyText_Type PyUnicode_Type
#define MyText_AS_BYTES(o) PyUnicode_AsASCIIString(o)
-#define MyText_AS_STRING(o) PyBytes_AS_STRING(o)
+#define MyBytes_AS_STRING(o) PyBytes_AS_STRING(o)
+#define MyText_AsString(o) PyUnicode_AsUTF8(o)
+#define MyText_FromFormat PyUnicode_FromFormat
#define MyInt_FromInt(i) PyLong_FromLong((long)i)
#define MyInt_AsInt(o) (int)PyLong_AsLong(o)
@@ -31,7 +33,9 @@
#define MyText_Type PyString_Type
#define MyText_AS_BYTES(o) (Py_INCREF(o), o)
-#define MyText_AS_STRING(o) PyString_AS_STRING(o)
+#define MyBytes_AS_STRING(o) PyString_AS_STRING(o)
+#define MyText_AsString(o) PyString_AsString(o)
+#define MyText_FromFormat PyUnicode_FromFormat
#define MyInt_FromInt(i) PyInt_FromLong((long)i)
#define MyInt_AsInt(o) (int)PyInt_AsLong(o)
@@ -43,14 +47,32 @@
#define RET_OK 0
#define RET_ERROR -1
-/* An entry on the data stack. For each call frame, we need to record the
- dictionary to capture data, and the last line number executed in that
- frame.
-*/
+/* Python C API helpers. */
+
+static int
+pyint_as_int(PyObject * pyint, int *pint)
+{
+ int the_int = MyInt_AsInt(pyint);
+ if (the_int == -1 && PyErr_Occurred()) {
+ return RET_ERROR;
+ }
+
+ *pint = the_int;
+ return RET_OK;
+}
+
+
+/* An entry on the data stack. For each call frame, we need to record all
+ * the information needed for CTracer_handle_line to operate as quickly as
+ * possible.
+ */
typedef struct {
/* The current file_data dictionary. Borrowed, owned by self->data. */
PyObject * file_data;
+ /* The disposition object for this frame. */
+ PyObject * disposition;
+
/* The FileTracer handling this frame, or None if it's Python. */
PyObject * file_tracer;
@@ -169,64 +191,33 @@ DataStack_grow(CTracer *self, DataStack *pdata_stack)
}
+static void CTracer_disable_plugin(CTracer *self, PyObject * disposition);
+
static int
CTracer_init(CTracer *self, PyObject *args_unused, PyObject *kwds_unused)
{
int ret = RET_ERROR;
PyObject * weakref = NULL;
-#if COLLECT_STATS
- self->stats.calls = 0;
- self->stats.lines = 0;
- self->stats.returns = 0;
- self->stats.exceptions = 0;
- self->stats.others = 0;
- self->stats.new_files = 0;
- self->stats.missed_returns = 0;
- self->stats.stack_reallocs = 0;
- self->stats.errors = 0;
-#endif /* COLLECT_STATS */
-
- self->should_trace = NULL;
- self->check_include = NULL;
- self->warn = NULL;
- self->concur_id_func = NULL;
- self->data = NULL;
- self->plugin_data = NULL;
- self->should_trace_cache = NULL;
- self->arcs = NULL;
-
- self->started = 0;
- self->tracing_arcs = 0;
-
- if (DataStack_init(self, &self->data_stack)) {
+ if (DataStack_init(self, &self->data_stack) < 0) {
goto error;
}
weakref = PyImport_ImportModule("weakref");
if (weakref == NULL) {
- STATS( self->stats.errors++; )
goto error;
}
self->data_stack_index = PyObject_CallMethod(weakref, "WeakKeyDictionary", NULL);
Py_XDECREF(weakref);
if (self->data_stack_index == NULL) {
- STATS( self->stats.errors++; )
goto error;
}
- self->data_stacks = NULL;
- self->data_stacks_alloc = 0;
- self->data_stacks_used = 0;
-
self->pdata_stack = &self->data_stack;
- self->cur_entry.file_data = NULL;
self->cur_entry.last_line = -1;
- self->last_exc_back = NULL;
-
ret = RET_OK;
goto ok;
@@ -298,7 +289,7 @@ showlog(int depth, int lineno, PyObject * filename, const char * msg)
}
if (filename) {
PyObject *ascii = MyText_AS_BYTES(filename);
- printf(" %s", MyText_AS_STRING(ascii));
+ printf(" %s", MyBytes_AS_STRING(ascii));
Py_DECREF(ascii);
}
if (msg) {
@@ -365,6 +356,9 @@ CTracer_set_pdata_stack(CTracer *self)
/* A new concurrency object. Make a new data stack. */
the_index = self->data_stacks_used;
stack_index = MyInt_FromInt(the_index);
+ if (stack_index == NULL) {
+ goto error;
+ }
if (PyObject_SetItem(self->data_stack_index, co_obj, stack_index) < 0) {
goto error;
}
@@ -382,7 +376,9 @@ CTracer_set_pdata_stack(CTracer *self)
DataStack_init(self, &self->data_stacks[the_index]);
}
else {
- the_index = MyInt_AsInt(stack_index);
+ if (pyint_as_int(stack_index, &the_index) < 0) {
+ goto error;
+ }
}
self->pdata_stack = &self->data_stacks[the_index];
@@ -423,7 +419,7 @@ CTracer_check_missing_return(CTracer *self, PyFrameObject *frame)
we'll need to keep more of the missed frame's state.
*/
STATS( self->stats.missed_returns++; )
- if (CTracer_set_pdata_stack(self)) {
+ if (CTracer_set_pdata_stack(self) < 0) {
goto error;
}
if (self->pdata_stack->depth >= 0) {
@@ -451,12 +447,15 @@ static int
CTracer_handle_call(CTracer *self, PyFrameObject *frame)
{
int ret = RET_ERROR;
+ int ret2;
/* Owned references that we clean up at the very end of the function. */
PyObject * tracename = NULL;
PyObject * disposition = NULL;
PyObject * disp_trace = NULL;
- PyObject * disp_file_tracer = NULL;
+ PyObject * file_tracer = NULL;
+ PyObject * plugin = NULL;
+ PyObject * plugin_name = NULL;
PyObject * has_dynamic_filename = NULL;
/* Borrowed references. */
@@ -465,10 +464,10 @@ CTracer_handle_call(CTracer *self, PyFrameObject *frame)
STATS( self->stats.calls++; )
/* Grow the stack. */
- if (CTracer_set_pdata_stack(self)) {
+ if (CTracer_set_pdata_stack(self) < 0) {
goto error;
}
- if (DataStack_grow(self, self->pdata_stack)) {
+ if (DataStack_grow(self, self->pdata_stack) < 0) {
goto error;
}
@@ -479,6 +478,9 @@ CTracer_handle_call(CTracer *self, PyFrameObject *frame)
filename = frame->f_code->co_filename;
disposition = PyDict_GetItem(self->should_trace_cache, filename);
if (disposition == NULL) {
+ if (PyErr_Occurred()) {
+ goto error;
+ }
STATS( self->stats.new_files++; )
/* We've never considered this file before. */
/* Ask should_trace about it. */
@@ -506,10 +508,20 @@ CTracer_handle_call(CTracer *self, PyFrameObject *frame)
if (tracename == NULL) {
goto error;
}
- disp_file_tracer = PyObject_GetAttrString(disposition, "file_tracer");
- if (disp_file_tracer == NULL) {
+ file_tracer = PyObject_GetAttrString(disposition, "file_tracer");
+ if (file_tracer == NULL) {
goto error;
}
+ if (file_tracer != Py_None) {
+ plugin = PyObject_GetAttrString(file_tracer, "_coverage_plugin");
+ if (plugin == NULL) {
+ goto error;
+ }
+ plugin_name = PyObject_GetAttrString(plugin, "_coverage_plugin_name");
+ if (plugin_name == NULL) {
+ goto error;
+ }
+ }
has_dynamic_filename = PyObject_GetAttrString(disposition, "has_dynamic_filename");
if (has_dynamic_filename == NULL) {
goto error;
@@ -517,11 +529,16 @@ CTracer_handle_call(CTracer *self, PyFrameObject *frame)
if (has_dynamic_filename == Py_True) {
PyObject * next_tracename = NULL;
next_tracename = PyObject_CallMethod(
- disp_file_tracer, "dynamic_source_filename",
+ file_tracer, "dynamic_source_filename",
"OO", tracename, frame
);
if (next_tracename == NULL) {
- goto error;
+ /* An exception from the function. Alert the user with a
+ * warning and a traceback.
+ */
+ CTracer_disable_plugin(self, disposition);
+ /* Because we handled the error, goto ok. */
+ goto ok;
}
Py_DECREF(tracename);
tracename = next_tracename;
@@ -531,6 +548,9 @@ CTracer_handle_call(CTracer *self, PyFrameObject *frame)
PyObject * included = NULL;
included = PyDict_GetItem(self->should_trace_cache, tracename);
if (included == NULL) {
+ if (PyErr_Occurred()) {
+ goto error;
+ }
STATS( self->stats.new_files++; )
included = PyObject_CallFunctionObjArgs(self->check_include, tracename, frame, NULL);
if (included == NULL) {
@@ -555,35 +575,32 @@ CTracer_handle_call(CTracer *self, PyFrameObject *frame)
if (tracename != Py_None) {
PyObject * file_data = PyDict_GetItem(self->data, tracename);
- PyObject * disp_plugin_name = NULL;
if (file_data == NULL) {
+ if (PyErr_Occurred()) {
+ goto error;
+ }
file_data = PyDict_New();
if (file_data == NULL) {
goto error;
}
- ret = PyDict_SetItem(self->data, tracename, file_data);
+ ret2 = PyDict_SetItem(self->data, tracename, file_data);
Py_DECREF(file_data);
- if (ret < 0) {
+ if (ret2 < 0) {
goto error;
}
/* If the disposition mentions a plugin, record that. */
- if (disp_file_tracer != Py_None) {
- disp_plugin_name = PyObject_GetAttrString(disp_file_tracer, "_coverage_plugin_name");
- if (disp_plugin_name == NULL) {
- goto error;
- }
- ret = PyDict_SetItem(self->plugin_data, tracename, disp_plugin_name);
- Py_DECREF(disp_plugin_name);
- if (ret < 0) {
+ if (file_tracer != Py_None) {
+ ret2 = PyDict_SetItem(self->plugin_data, tracename, plugin_name);
+ if (ret2 < 0) {
goto error;
}
}
}
self->cur_entry.file_data = file_data;
- self->cur_entry.file_tracer = disp_file_tracer;
+ self->cur_entry.file_tracer = file_tracer;
/* Make the frame right in case settrace(gettrace()) happens. */
Py_INCREF(self);
@@ -596,24 +613,128 @@ CTracer_handle_call(CTracer *self, PyFrameObject *frame)
SHOWLOG(self->pdata_stack->depth, frame->f_lineno, filename, "skipped");
}
+ self->cur_entry.disposition = disposition;
self->cur_entry.last_line = -1;
+ok:
ret = RET_OK;
error:
Py_XDECREF(tracename);
Py_XDECREF(disposition);
Py_XDECREF(disp_trace);
- Py_XDECREF(disp_file_tracer);
+ Py_XDECREF(file_tracer);
+ Py_XDECREF(plugin);
+ Py_XDECREF(plugin_name);
Py_XDECREF(has_dynamic_filename);
return ret;
}
+
+static void
+CTracer_disable_plugin(CTracer *self, PyObject * disposition)
+{
+ PyObject * file_tracer = NULL;
+ PyObject * plugin = NULL;
+ PyObject * plugin_name = NULL;
+ PyObject * msg = NULL;
+ PyObject * ignored = NULL;
+
+ file_tracer = PyObject_GetAttrString(disposition, "file_tracer");
+ if (file_tracer == NULL) {
+ goto error;
+ }
+ if (file_tracer == Py_None) {
+ /* This shouldn't happen... */
+ goto ok;
+ }
+ plugin = PyObject_GetAttrString(file_tracer, "_coverage_plugin");
+ if (plugin == NULL) {
+ goto error;
+ }
+ plugin_name = PyObject_GetAttrString(plugin, "_coverage_plugin_name");
+ if (plugin_name == NULL) {
+ goto error;
+ }
+ msg = MyText_FromFormat(
+ "Disabling plugin '%s' due to an exception:",
+ MyText_AsString(plugin_name)
+ );
+ if (msg == NULL) {
+ goto error;
+ }
+ ignored = PyObject_CallFunctionObjArgs(self->warn, msg, NULL);
+ if (ignored == NULL) {
+ goto error;
+ }
+
+ PyErr_Print();
+
+ /* Disable the plugin for future files, and stop tracing this file. */
+ if (PyObject_SetAttrString(plugin, "_coverage_enabled", Py_False) < 0) {
+ goto error;
+ }
+ if (PyObject_SetAttrString(disposition, "trace", Py_False) < 0) {
+ goto error;
+ }
+
+ goto ok;
+
+error:
+ /* This function doesn't return a status, so if an error happens, print it,
+ * but don't interrupt the flow. */
+ /* PySys_WriteStderr is nicer, but is not in the public API. */
+ fprintf(stderr, "Error occurred while disabling plugin:\n");
+ PyErr_Print();
+
+ok:
+ Py_XDECREF(file_tracer);
+ Py_XDECREF(plugin);
+ Py_XDECREF(plugin_name);
+ Py_XDECREF(msg);
+ Py_XDECREF(ignored);
+}
+
+
+static int
+CTracer_unpack_pair(CTracer *self, PyObject *pair, int *p_one, int *p_two)
+{
+ int ret = RET_ERROR;
+ int the_int;
+ PyObject * pyint = NULL;
+ int index;
+
+ if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2) {
+ PyErr_SetString(
+ PyExc_TypeError,
+ "line_number_range must return 2-tuple"
+ );
+ goto error;
+ }
+
+ for (index = 0; index < 2; index++) {
+ pyint = PyTuple_GetItem(pair, index);
+ if (pyint == NULL) {
+ goto error;
+ }
+ if (pyint_as_int(pyint, &the_int) < 0) {
+ goto error;
+ }
+ *(index == 0 ? p_one : p_two) = the_int;
+ }
+
+ ret = RET_OK;
+
+error:
+ return ret;
+}
+
static int
CTracer_handle_line(CTracer *self, PyFrameObject *frame)
{
int ret = RET_ERROR;
+ int ret2;
STATS( self->stats.lines++; )
if (self->pdata_stack->depth >= 0) {
@@ -628,44 +749,46 @@ CTracer_handle_line(CTracer *self, PyFrameObject *frame)
if (from_to == NULL) {
goto error;
}
- /* TODO: error check bad returns. */
- lineno_from = MyInt_AsInt(PyTuple_GetItem(from_to, 0));
- lineno_to = MyInt_AsInt(PyTuple_GetItem(from_to, 1));
+ ret2 = CTracer_unpack_pair(self, from_to, &lineno_from, &lineno_to);
Py_DECREF(from_to);
+ if (ret2 < 0) {
+ CTracer_disable_plugin(self, self->cur_entry.disposition);
+ goto ok;
+ }
}
else {
lineno_from = lineno_to = frame->f_lineno;
}
if (lineno_from != -1) {
- if (self->tracing_arcs) {
- /* Tracing arcs: key is (last_line,this_line). */
- /* TODO: this needs to deal with lineno_to also. */
- if (CTracer_record_pair(self, self->cur_entry.last_line, lineno_from) < 0) {
- goto error;
+ for (; lineno_from <= lineno_to; lineno_from++) {
+ if (self->tracing_arcs) {
+ /* Tracing arcs: key is (last_line,this_line). */
+ if (CTracer_record_pair(self, self->cur_entry.last_line, lineno_from) < 0) {
+ goto error;
+ }
}
- }
- else {
- /* Tracing lines: key is simply this_line. */
- while (lineno_from <= lineno_to) {
+ else {
+ /* Tracing lines: key is simply this_line. */
PyObject * this_line = MyInt_FromInt(lineno_from);
if (this_line == NULL) {
goto error;
}
- ret = PyDict_SetItem(self->cur_entry.file_data, this_line, Py_None);
+
+ ret2 = PyDict_SetItem(self->cur_entry.file_data, this_line, Py_None);
Py_DECREF(this_line);
- if (ret < 0) {
+ if (ret2 < 0) {
goto error;
}
- lineno_from++;
}
+
+ self->cur_entry.last_line = lineno_from;
}
}
-
- self->cur_entry.last_line = lineno_to;
}
}
+ok:
ret = RET_OK;
error:
@@ -680,7 +803,7 @@ CTracer_handle_return(CTracer *self, PyFrameObject *frame)
STATS( self->stats.returns++; )
/* A near-copy of this code is above in the missing-return handler. */
- if (CTracer_set_pdata_stack(self)) {
+ if (CTracer_set_pdata_stack(self) < 0) {
goto error;
}
if (self->pdata_stack->depth >= 0) {
@@ -742,45 +865,45 @@ CTracer_trace(CTracer *self, PyFrameObject *frame, int what, PyObject *arg_unuse
#if WHAT_LOG
if (what <= sizeof(what_sym)/sizeof(const char *)) {
ascii = MyText_AS_BYTES(frame->f_code->co_filename);
- printf("trace: %s @ %s %d\n", what_sym[what], MyText_AS_STRING(ascii), frame->f_lineno);
+ printf("trace: %s @ %s %d\n", what_sym[what], MyBytes_AS_STRING(ascii), frame->f_lineno);
Py_DECREF(ascii);
}
#endif
#if TRACE_LOG
ascii = MyText_AS_BYTES(frame->f_code->co_filename);
- if (strstr(MyText_AS_STRING(ascii), start_file) && frame->f_lineno == start_line) {
+ if (strstr(MyBytes_AS_STRING(ascii), start_file) && frame->f_lineno == start_line) {
logging = 1;
}
Py_DECREF(ascii);
#endif
/* See below for details on missing-return detection. */
- if (CTracer_check_missing_return(self, frame)) {
+ if (CTracer_check_missing_return(self, frame) < 0) {
goto error;
}
switch (what) {
case PyTrace_CALL:
- if (CTracer_handle_call(self, frame)) {
+ if (CTracer_handle_call(self, frame) < 0) {
goto error;
}
break;
case PyTrace_RETURN:
- if (CTracer_handle_return(self, frame)) {
+ if (CTracer_handle_return(self, frame) < 0) {
goto error;
}
break;
case PyTrace_LINE:
- if (CTracer_handle_line(self, frame)) {
+ if (CTracer_handle_line(self, frame) < 0) {
goto error;
}
break;
case PyTrace_EXCEPTION:
- if (CTracer_handle_exception(self, frame)) {
+ if (CTracer_handle_exception(self, frame) < 0) {
goto error;
}
break;
@@ -853,7 +976,7 @@ CTracer_call(CTracer *self, PyObject *args, PyObject *kwds)
for the C function. */
for (what = 0; what_names[what]; what++) {
PyObject *ascii = MyText_AS_BYTES(what_str);
- int should_break = !strcmp(MyText_AS_STRING(ascii), what_names[what]);
+ int should_break = !strcmp(MyBytes_AS_STRING(ascii), what_names[what]);
Py_DECREF(ascii);
if (should_break) {
break;
@@ -900,7 +1023,7 @@ CTracer_stop(CTracer *self, PyObject *args_unused)
self->started = 0;
}
- return Py_BuildValue("");
+ Py_RETURN_NONE;
}
static PyObject *
@@ -921,7 +1044,7 @@ CTracer_get_stats(CTracer *self)
"errors", self->stats.errors
);
#else
- return Py_BuildValue("");
+ Py_RETURN_NONE;
#endif /* COLLECT_STATS */
}
@@ -1045,7 +1168,11 @@ PyInit_tracer(void)
}
Py_INCREF(&CTracerType);
- PyModule_AddObject(mod, "CTracer", (PyObject *)&CTracerType);
+ if (PyModule_AddObject(mod, "CTracer", (PyObject *)&CTracerType) < 0) {
+ Py_DECREF(mod);
+ Py_DECREF(&CTracerType);
+ return NULL;
+ }
return mod;
}
diff --git a/coverage/version.py b/coverage/version.py
index 304a54c..51e1310 100644
--- a/coverage/version.py
+++ b/coverage/version.py
@@ -1,9 +1,9 @@
"""The version and URL for coverage.py"""
# This file is exec'ed in setup.py, don't import anything!
-__version__ = "4.0a5" # see detailed history in CHANGES.txt
+__version__ = "4.0a6" # see detailed history in CHANGES.txt
__url__ = "https://coverage.readthedocs.org"
if max(__version__).isalpha():
# For pre-releases, use a version-specific URL.
- __url__ += "/en/coverage-" + __version__
+ __url__ += "/en/" + __version__
diff --git a/coverage/xmlreport.py b/coverage/xmlreport.py
index f7ad2b8..996f19a 100644
--- a/coverage/xmlreport.py
+++ b/coverage/xmlreport.py
@@ -20,9 +20,10 @@ def rate(hit, num):
class XmlReporter(Reporter):
"""A reporter for writing Cobertura-style XML coverage results."""
- def __init__(self, coverage, config):
+ def __init__(self, coverage, config, file_locator):
super(XmlReporter, self).__init__(coverage, config)
+ self.file_locator = file_locator
self.source_paths = set()
self.packages = {}
self.xml_out = None
@@ -116,20 +117,20 @@ class XmlReporter(Reporter):
pct = 100.0 * (lhits_tot + bhits_tot) / denom
return pct
- def xml_file(self, cu, analysis):
+ def xml_file(self, fr, analysis):
"""Add to the XML report for a single file."""
# Create the 'lines' and 'package' XML elements, which
# are populated later. Note that a package == a directory.
- filename = cu.file_locator.relative_filename(cu.filename)
+ filename = self.file_locator.relative_filename(fr.filename)
filename = filename.replace("\\", "/")
dirname = os.path.dirname(filename) or "."
parts = dirname.split("/")
dirname = "/".join(parts[:self.config.xml_package_depth])
package_name = dirname.replace("/", ".")
- className = cu.name
+ className = fr.name
- self.source_paths.add(cu.file_locator.relative_dir.rstrip('/'))
+ self.source_paths.add(self.file_locator.relative_dir.rstrip('/'))
package = self.packages.setdefault(package_name, [{}, 0, 0, 0, 0])
xclass = self.xml_out.createElement("class")
diff --git a/doc/install.rst b/doc/install.rst
index c3b1329..757b775 100644
--- a/doc/install.rst
+++ b/doc/install.rst
@@ -95,7 +95,7 @@ installed properly:
$ coverage --version
Coverage.py, version |release|.
- Documentation at https://coverage.readthedocs.org/en/coverage-|release|
+ Documentation at https://coverage.readthedocs.org/en/|release|
You can also invoke coverage as a module:
@@ -113,4 +113,4 @@ You can also invoke coverage as a module:
$ python -m coverage --version
Coverage.py, version |release|.
- Documentation at https://coverage.readthedocs.org/en/coverage-|release|
+ Documentation at https://coverage.readthedocs.org/en/|release|
diff --git a/doc/trouble.rst b/doc/trouble.rst
index bfdf12b..c54ab68 100644
--- a/doc/trouble.rst
+++ b/doc/trouble.rst
@@ -34,10 +34,6 @@ coverage.py from working properly:
program that calls execv will not be fully measured. A patch for coverage.py
is in `issue 43`_.
-* `multiprocessing`_ launches processes to provide parallelism. These
- processes don't get measured by coverage.py. Some possible fixes are
- discussed or linked to in `issue 117`_.
-
* `thread`_, in the Python standard library, is the low-level threading
interface. Threads created with this module will not be traced. Use the
higher-level `threading`_ module instead.
@@ -48,12 +44,10 @@ coverage.py from working properly:
measured properly.
.. _execv: http://docs.python.org/library/os#os.execl
-.. _multiprocessing: http://docs.python.org/library/multiprocessing.html
.. _sys.settrace: http://docs.python.org/library/sys.html#sys.settrace
.. _thread: http://docs.python.org/library/thread.html
.. _threading: http://docs.python.org/library/threading.html
.. _issue 43: https://bitbucket.org/ned/coveragepy/issue/43/coverage-measurement-fails-on-code
-.. _issue 117: https://bitbucket.org/ned/coveragepy/issue/117/enable-coverage-measurement-of-code-run-by
Things that require --timid
diff --git a/howto.txt b/howto.txt
index e37a949..9a33d54 100644
--- a/howto.txt
+++ b/howto.txt
@@ -47,11 +47,13 @@
- Visit http://pypi.python.org/pypi?%3Aaction=pkg_edit&name=coverage :
- show/hide the proper versions.
- Tag the tree
- - hg tag -m "Coverage 3.0.1" coverage-3.0.1
+ - hg tag -m "Coverage 3.0.1" 3.0.1
- Update nedbatchelder.com
- Edit webfaction.htaccess to make sure the proper versions are mapped to /beta
- Blog post?
- Update readthedocs
+ - Coverage / versions
+ - find the latest tag in the inactive list, edit it, make it active.
- Update bitbucket:
- Issue tracker should get new version number in picker.
# Note: don't delete old version numbers: it marks changes on the tickets
diff --git a/lab/dataflow.txt b/lab/dataflow.txt
index b2b619a..1f628f1 100644
--- a/lab/dataflow.txt
+++ b/lab/dataflow.txt
@@ -18,7 +18,7 @@ CoverageData.add_line_data( { filename: { lineno: None, .. }, ... } )
CoverageData.measured_files():
returns [filename, ...]
called by:
- Reporter.find_code_units()
+ Reporter.find_file_reporters()
tests
CoverageData.executed_lines():
diff --git a/lab/show_pyc.py b/lab/show_pyc.py
index b2cbb34..d6bbd92 100644
--- a/lab/show_pyc.py
+++ b/lab/show_pyc.py
@@ -4,7 +4,7 @@ def show_pyc_file(fname):
f = open(fname, "rb")
magic = f.read(4)
moddate = f.read(4)
- modtime = time.asctime(time.localtime(struct.unpack('L', moddate)[0]))
+ modtime = time.asctime(time.localtime(struct.unpack('<L', moddate)[0]))
print "magic %s" % (magic.encode('hex'))
print "moddate %s (%s)" % (moddate.encode('hex'), modtime)
code = marshal.load(f)
diff --git a/requirements.txt b/requirements.txt
index 5b19f9c..ab84656 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -2,4 +2,4 @@
nose
mock
pylint
-tox >= 1.8
+tox >= 1.9
diff --git a/tests/coveragetest.py b/tests/coveragetest.py
index d673f6d..0e80f4a 100644
--- a/tests/coveragetest.py
+++ b/tests/coveragetest.py
@@ -149,7 +149,8 @@ class CoverageTest(
def check_coverage(
self, text, lines=None, missing="", report="",
excludes=None, partials="",
- arcz=None, arcz_missing="", arcz_unpredicted=""
+ arcz=None, arcz_missing=None, arcz_unpredicted=None,
+ arcs=None, arcs_missing=None, arcs_unpredicted=None,
):
"""Check the coverage measurement of `text`.
@@ -165,6 +166,8 @@ class CoverageTest(
`arcs_unpredicted` are the arcs executed in the code, but not deducible
from the code.
+ Returns the Coverage object, in case you want to poke at it some more.
+
"""
# We write the code into a file so that we can import it.
# Coverage wants to deal with things as modules with file names.
@@ -172,11 +175,12 @@ class CoverageTest(
self.make_file(modname+".py", text)
- arcs = arcs_missing = arcs_unpredicted = None
- if arcz is not None:
+ if arcs is None and arcz is not None:
arcs = self.arcz_to_arcs(arcz)
- arcs_missing = self.arcz_to_arcs(arcz_missing or "")
- arcs_unpredicted = self.arcz_to_arcs(arcz_unpredicted or "")
+ if arcs_missing is None and arcz_missing is not None:
+ arcs_missing = self.arcz_to_arcs(arcz_missing)
+ if arcs_unpredicted is None and arcz_unpredicted is not None:
+ arcs_unpredicted = self.arcz_to_arcs(arcz_unpredicted)
# Start up Coverage.
cov = coverage.coverage(branch=(arcs_missing is not None))
@@ -246,6 +250,8 @@ class CoverageTest(
rep = " ".join(frep.getvalue().split("\n")[2].split()[1:])
self.assertEqual(report, rep)
+ return cov
+
def nice_file(self, *fparts):
"""Canonicalize the filename composed of the parts in `fparts`."""
fname = os.path.join(*fparts)
diff --git a/tests/plugin2.py b/tests/plugin2.py
index 9d47d26..658ee22 100644
--- a/tests/plugin2.py
+++ b/tests/plugin2.py
@@ -1,5 +1,7 @@
"""A plugin for test_plugins.py to import."""
+import os.path
+
import coverage
# pylint: disable=missing-docstring
@@ -23,16 +25,16 @@ class RenderFileTracer(coverage.plugin.FileTracer):
def dynamic_source_filename(self, filename, frame):
if frame.f_code.co_name != "render":
return None
- return frame.f_locals['filename']
+ return os.path.abspath(frame.f_locals['filename'])
def line_number_range(self, frame):
lineno = frame.f_locals['linenum']
- return lineno,lineno+1
+ return lineno, lineno+1
class FileReporter(coverage.plugin.FileReporter):
def statements(self):
# Goofy test arrangement: claim that the file has as many lines as the
# number in its name.
- num = self.filename.split(".")[0].split("_")[1]
+ num = os.path.basename(self.filename).split(".")[0].split("_")[1]
return set(range(1, int(num)+1))
diff --git a/tests/test_arcs.py b/tests/test_arcs.py
index d3717a8..81fa7e6 100644
--- a/tests/test_arcs.py
+++ b/tests/test_arcs.py
@@ -2,7 +2,9 @@
from tests.coveragetest import CoverageTest
+import coverage
from coverage import env
+from coverage.files import abs_file
class SimpleArcTest(CoverageTest):
@@ -575,6 +577,27 @@ class MiscArcTest(CoverageTest):
""",
arcz=".1 19 9.")
+ def test_pathologically_long_code_object(self):
+ # https://bitbucket.org/ned/coveragepy/issue/359
+ # The structure of this file is such that an EXTENDED_ARG byte code is
+ # needed to encode the jump at the end. We weren't interpreting those
+ # opcodes.
+ code = """\
+ data = [
+ """ + "".join("""\
+ [{i}, {i}, {i}, {i}, {i}, {i}, {i}, {i}, {i}, {i}],
+ """.format(i=i) for i in range(2000)
+ ) + """\
+ ]
+
+ if __name__ == "__main__":
+ print(len(data))
+ """
+ self.check_coverage(
+ code,
+ arcs=[(-1, 1), (1, 2004), (2004, -2), (2004, 2005), (2005, -2)],
+ )
+
class ExcludeTest(CoverageTest):
"""Tests of exclusions to indicate known partial branches."""
@@ -606,3 +629,24 @@ class ExcludeTest(CoverageTest):
[1,2,3,4,5],
partials=["only some"],
arcz=".1 12 23 34 45 25 5.", arcz_missing="")
+
+
+class LineDataTest(CoverageTest):
+ """Tests that line_data gives us what we expect."""
+
+ def test_branch(self):
+ cov = coverage.Coverage(branch=True)
+
+ self.make_file("fun1.py", """\
+ def fun1(x):
+ if x == 1:
+ return
+
+ fun1(3)
+ """)
+
+ self.start_import_stop(cov, "fun1")
+
+ cov._harvest_data()
+ fun1_lines = cov.data.line_data()[abs_file("fun1.py")]
+ self.assertEqual(fun1_lines, [1, 2, 5])
diff --git a/tests/test_concurrency.py b/tests/test_concurrency.py
index 9a82a0c..93809df 100644
--- a/tests/test_concurrency.py
+++ b/tests/test_concurrency.py
@@ -239,7 +239,7 @@ class MultiprocessingTest(CoverageTest):
def func(x):
# Need to pause, or the tasks go too quick, and some processes
# in the pool don't get any work, and then don't record data.
- time.sleep(0.01)
+ time.sleep(0.02)
# Use different lines in different subprocesses.
if x % 2:
y = x*x
@@ -249,7 +249,7 @@ class MultiprocessingTest(CoverageTest):
if __name__ == "__main__":
pool = multiprocessing.Pool(3)
- inputs = range(20)
+ inputs = range(30)
outputs = pool.imap_unordered(func, inputs)
pids = set()
total = 0
@@ -264,8 +264,7 @@ class MultiprocessingTest(CoverageTest):
out = self.run_command(
"coverage run --concurrency=multiprocessing multi.py"
)
- os.system("cp .cov* /tmp")
- total = sum(x*x if x%2 else x*x*x for x in range(20))
+ total = sum(x*x if x%2 else x*x*x for x in range(30))
self.assertEqual(out.rstrip(), "3 pids, total = %d" % total)
self.run_command("coverage combine")
diff --git a/tests/test_codeunit.py b/tests/test_filereporter.py
index ea65d85..9db4c0c 100644
--- a/tests/test_codeunit.py
+++ b/tests/test_filereporter.py
@@ -1,10 +1,10 @@
-"""Tests for coverage.codeunit"""
+"""Tests for FileReporters"""
import os
import sys
-from coverage.codeunit import CodeUnit
-from coverage.python import PythonCodeUnit
+from coverage.plugin import FileReporter
+from coverage.python import PythonFileReporter
from tests.coveragetest import CoverageTest
@@ -17,21 +17,21 @@ def native(filename):
return filename.replace("/", os.sep)
-class CodeUnitTest(CoverageTest):
- """Tests for coverage.codeunit"""
+class FileReporterTest(CoverageTest):
+ """Tests for FileReporter classes."""
run_in_temp_dir = False
def setUp(self):
- super(CodeUnitTest, self).setUp()
+ super(FileReporterTest, self).setUp()
# Parent class saves and restores sys.path, we can just modify it.
testmods = self.nice_file(os.path.dirname(__file__), 'modules')
sys.path.append(testmods)
def test_filenames(self):
- acu = PythonCodeUnit("aa/afile.py")
- bcu = PythonCodeUnit("aa/bb/bfile.py")
- ccu = PythonCodeUnit("aa/bb/cc/cfile.py")
+ acu = PythonFileReporter("aa/afile.py")
+ bcu = PythonFileReporter("aa/bb/bfile.py")
+ ccu = PythonFileReporter("aa/bb/cc/cfile.py")
self.assertEqual(acu.name, "aa/afile.py")
self.assertEqual(bcu.name, "aa/bb/bfile.py")
self.assertEqual(ccu.name, "aa/bb/cc/cfile.py")
@@ -43,9 +43,9 @@ class CodeUnitTest(CoverageTest):
self.assertEqual(ccu.source(), "# cfile.py\n")
def test_odd_filenames(self):
- acu = PythonCodeUnit("aa/afile.odd.py")
- bcu = PythonCodeUnit("aa/bb/bfile.odd.py")
- b2cu = PythonCodeUnit("aa/bb.odd/bfile.py")
+ acu = PythonFileReporter("aa/afile.odd.py")
+ bcu = PythonFileReporter("aa/bb/bfile.odd.py")
+ b2cu = PythonFileReporter("aa/bb.odd/bfile.py")
self.assertEqual(acu.name, "aa/afile.odd.py")
self.assertEqual(bcu.name, "aa/bb/bfile.odd.py")
self.assertEqual(b2cu.name, "aa/bb.odd/bfile.py")
@@ -61,9 +61,9 @@ class CodeUnitTest(CoverageTest):
import aa.bb
import aa.bb.cc
- acu = PythonCodeUnit(aa)
- bcu = PythonCodeUnit(aa.bb)
- ccu = PythonCodeUnit(aa.bb.cc)
+ acu = PythonFileReporter(aa)
+ bcu = PythonFileReporter(aa.bb)
+ ccu = PythonFileReporter(aa.bb.cc)
self.assertEqual(acu.name, native("aa.py"))
self.assertEqual(bcu.name, native("aa/bb.py"))
self.assertEqual(ccu.name, native("aa/bb/cc.py"))
@@ -79,9 +79,9 @@ class CodeUnitTest(CoverageTest):
import aa.bb.bfile
import aa.bb.cc.cfile
- acu = PythonCodeUnit(aa.afile)
- bcu = PythonCodeUnit(aa.bb.bfile)
- ccu = PythonCodeUnit(aa.bb.cc.cfile)
+ acu = PythonFileReporter(aa.afile)
+ bcu = PythonFileReporter(aa.bb.bfile)
+ ccu = PythonFileReporter(aa.bb.cc.cfile)
self.assertEqual(acu.name, native("aa/afile.py"))
self.assertEqual(bcu.name, native("aa/bb/bfile.py"))
self.assertEqual(ccu.name, native("aa/bb/cc/cfile.py"))
@@ -93,10 +93,10 @@ class CodeUnitTest(CoverageTest):
self.assertEqual(ccu.source(), "# cfile.py\n")
def test_comparison(self):
- acu = CodeUnit("aa/afile.py")
- acu2 = CodeUnit("aa/afile.py")
- zcu = CodeUnit("aa/zfile.py")
- bcu = CodeUnit("aa/bb/bfile.py")
+ acu = FileReporter("aa/afile.py")
+ acu2 = FileReporter("aa/afile.py")
+ zcu = FileReporter("aa/zfile.py")
+ bcu = FileReporter("aa/bb/bfile.py")
assert acu == acu2 and acu <= acu2 and acu >= acu2
assert acu < zcu and acu <= zcu and acu != zcu
assert zcu > acu and zcu >= acu and zcu != acu
@@ -114,7 +114,7 @@ class CodeUnitTest(CoverageTest):
# in the path is actually the .egg zip file.
self.assert_doesnt_exist(egg1.__file__)
- ecu = PythonCodeUnit(egg1)
- eecu = PythonCodeUnit(egg1.egg1)
+ ecu = PythonFileReporter(egg1)
+ eecu = PythonFileReporter(egg1.egg1)
self.assertEqual(ecu.source(), u"")
self.assertEqual(eecu.source().split("\n")[0], u"# My egg file!")
diff --git a/tests/test_html.py b/tests/test_html.py
index 6b398c4..004ebbf 100644
--- a/tests/test_html.py
+++ b/tests/test_html.py
@@ -322,6 +322,16 @@ class HtmlWithUnparsableFilesTest(HtmlTestHelpers, CoverageTest):
expected = "# Isn&#39;t this great?&#203;!"
self.assertIn(expected, html_report)
+ def test_formfeeds(self):
+ # https://bitbucket.org/ned/coveragepy/issue/360/html-reports-get-confused-by-l-in-the-code
+ self.make_file("formfeed.py", "line_one = 1\n\f\nline_two = 2\n")
+ cov = coverage.coverage()
+ self.start_import_stop(cov, "formfeed")
+ cov.html_report()
+
+ formfeed_html = self.get_html_report_content("formfeed.py")
+ self.assertIn("line_two", formfeed_html)
+
class HtmlTest(CoverageTest):
"""Moar HTML tests."""
diff --git a/tests/test_oddball.py b/tests/test_oddball.py
index 25f58d3..9fdc654 100644
--- a/tests/test_oddball.py
+++ b/tests/test_oddball.py
@@ -8,6 +8,7 @@ import coverage
from tests.coveragetest import CoverageTest
from tests import osinfo
+
class ThreadingTest(CoverageTest):
"""Tests of the threading support."""
@@ -29,7 +30,7 @@ class ThreadingTest(CoverageTest):
fromMainThread()
other.join()
""",
- [1,3,4,6,7,9,10,12,13,14,15], "10")
+ [1, 3, 4, 6, 7, 9, 10, 12, 13, 14, 15], "10")
def test_thread_run(self):
self.check_coverage("""\
@@ -48,7 +49,7 @@ class ThreadingTest(CoverageTest):
thd.start()
thd.join()
""",
- [1,3,4,5,6,7,9,10,12,13,14], "")
+ [1, 3, 4, 5, 6, 7, 9, 10, 12, 13, 14], "")
class RecursionTest(CoverageTest):
@@ -66,7 +67,7 @@ class RecursionTest(CoverageTest):
recur(495) # We can get at least this many stack frames.
i = 8 # and this line will be traced
""",
- [1,2,3,5,7,8], "")
+ [1, 2, 3, 5, 7, 8], "")
def test_long_recursion(self):
# We can't finish a very deep recursion, but we don't crash.
@@ -80,7 +81,7 @@ class RecursionTest(CoverageTest):
recur(100000) # This is definitely too many frames.
""",
- [1,2,3,5,7], ""
+ [1, 2, 3, 5, 7], ""
)
def test_long_recursion_recovery(self):
@@ -112,10 +113,10 @@ class RecursionTest(CoverageTest):
pytrace = (cov.collector.tracer_name() == "PyTracer")
expected_missing = [3]
if pytrace:
- expected_missing += [9,10,11]
+ expected_missing += [9, 10, 11]
_, statements, missing, _ = cov.analysis("recur.py")
- self.assertEqual(statements, [1,2,3,5,7,8,9,10,11])
+ self.assertEqual(statements, [1, 2, 3, 5, 7, 8, 9, 10, 11])
self.assertEqual(missing, expected_missing)
# Get a warning about the stackoverflow effect on the tracing function.
@@ -179,7 +180,6 @@ class MemoryLeakTest(CoverageTest):
self.fail("RAM grew by %d" % (ram_growth))
-
class PyexpatTest(CoverageTest):
"""Pyexpat screws up tracing. Make sure we've counter-defended properly."""
@@ -216,11 +216,11 @@ class PyexpatTest(CoverageTest):
self.start_import_stop(cov, "outer")
_, statements, missing, _ = cov.analysis("trydom.py")
- self.assertEqual(statements, [1,3,8,9,10,11,13])
+ self.assertEqual(statements, [1, 3, 8, 9, 10, 11, 13])
self.assertEqual(missing, [])
_, statements, missing, _ = cov.analysis("outer.py")
- self.assertEqual(statements, [101,102])
+ self.assertEqual(statements, [101, 102])
self.assertEqual(missing, [])
@@ -281,26 +281,26 @@ class ExceptionTest(CoverageTest):
# combinations of catching exceptions and letting them fly.
runs = [
("doit fly oops", {
- 'doit.py': [302,303,304,305],
- 'fly.py': [102,103],
- 'oops.py': [2,3],
+ 'doit.py': [302, 303, 304, 305],
+ 'fly.py': [102, 103],
+ 'oops.py': [2, 3],
}),
("doit catch oops", {
- 'doit.py': [302,303],
- 'catch.py': [202,203,204,206,207],
- 'oops.py': [2,3],
+ 'doit.py': [302, 303],
+ 'catch.py': [202, 203, 204, 206, 207],
+ 'oops.py': [2, 3],
}),
("doit fly catch oops", {
- 'doit.py': [302,303],
- 'fly.py': [102,103,104],
- 'catch.py': [202,203,204,206,207],
- 'oops.py': [2,3],
+ 'doit.py': [302, 303],
+ 'fly.py': [102, 103, 104],
+ 'catch.py': [202, 203, 204, 206, 207],
+ 'oops.py': [2, 3],
}),
("doit catch fly oops", {
- 'doit.py': [302,303],
- 'catch.py': [202,203,204,206,207],
- 'fly.py': [102,103],
- 'oops.py': [2,3],
+ 'doit.py': [302, 303],
+ 'catch.py': [202, 203, 204, 206, 207],
+ 'fly.py': [102, 103],
+ 'oops.py': [2, 3],
}),
]
@@ -318,7 +318,7 @@ class ExceptionTest(CoverageTest):
# Clean the line data and compare to expected results.
# The filenames are absolute, so keep just the base.
- cov._harvest_data() # private! sshhh...
+ cov._harvest_data() # private! sshhh...
lines = cov.data.line_data()
clean_lines = {}
for f, llist in lines.items():
@@ -366,7 +366,7 @@ class DoctestTest(CoverageTest):
import doctest, sys
doctest.testmod(sys.modules[__name__]) # we're not __main__ :(
''',
- [1,11,12,14,16,17], "")
+ [1, 11, 12, 14, 16, 17], "")
class GettraceTest(CoverageTest):
@@ -382,7 +382,7 @@ class GettraceTest(CoverageTest):
sys.settrace(sys.gettrace())
a = bar(8)
''',
- [1,2,3,4,5,6,7,8], "")
+ [1, 2, 3, 4, 5, 6, 7, 8], "")
def test_multi_layers(self):
self.check_coverage('''\
@@ -399,4 +399,4 @@ class GettraceTest(CoverageTest):
level1()
f = 12
''',
- [1,2,3,4,5,6,7,8,9,10,11,12], "")
+ [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], "")
diff --git a/tests/test_plugins.py b/tests/test_plugins.py
index 74f78cf..69e7b42 100644
--- a/tests/test_plugins.py
+++ b/tests/test_plugins.py
@@ -231,12 +231,17 @@ class PluginWarningOnPyTracer(CoverageTest):
cov = coverage.Coverage()
cov.config["run:plugins"] = ["tests.plugin1"]
- msg = (
- r"Plugin file tracers \(tests.plugin1\) "
- r"aren't supported with PyTracer"
- )
- with self.assertRaisesRegex(CoverageException, msg):
- self.start_import_stop(cov, "simple")
+ warnings = []
+ def capture_warning(msg):
+ warnings.append(msg)
+ cov._warn = capture_warning
+
+ self.start_import_stop(cov, "simple")
+ self.assertIn(
+ "Plugin file tracers (tests.plugin1) "
+ "aren't supported with PyTracer",
+ warnings
+ )
class FileTracerTest(CoverageTest):
@@ -277,7 +282,7 @@ class GoodPluginTest(FileTracerTest):
_, statements, _, _ = cov.analysis(zzfile)
self.assertEqual(statements, [105, 106, 107, 205, 206, 207])
- def test_plugin2(self):
+ def make_render_and_caller(self):
# plugin2 emulates a dynamic tracing plugin: the caller's locals
# are examined to determine the source file and line number.
# The plugin is in tests/plugin2.py.
@@ -295,6 +300,7 @@ class GoodPluginTest(FileTracerTest):
return x+1
""")
self.make_file("caller.py", """\
+ import sys
from render import helper, render
assert render("foo_7.html", 4) == "[foo_7.html @ 4]"
@@ -309,10 +315,22 @@ class GoodPluginTest(FileTracerTest):
assert render("quux_5.html", 3) == "[quux_5.html @ 3]"
# In Python 2, either kind of string should be OK.
- if type("") == type(b""):
- assert render(u"unicode_3.html", 2) == "[unicode_3.html @ 2]"
+ if sys.version_info[0] == 2:
+ assert render(u"uni_3.html", 2) == "[uni_3.html @ 2]"
""")
+ # will try to read the actual source files, so make some
+ # source files.
+ def lines(n):
+ """Make a string with n lines of text."""
+ return "".join("line %d\n" % i for i in range(n))
+
+ self.make_file("bar_4.html", lines(4))
+ self.make_file("foo_7.html", lines(7))
+
+ def test_plugin2(self):
+ self.make_render_and_caller()
+
cov = coverage.Coverage(omit=["*quux*"])
CheckUniqueFilenames.hook(cov, '_should_trace')
CheckUniqueFilenames.hook(cov, '_check_include_omit_etc')
@@ -336,25 +354,118 @@ class GoodPluginTest(FileTracerTest):
self.assertNotIn("quux_5.html", cov.data.summary())
if env.PY2:
- _, statements, missing, _ = cov.analysis("unicode_3.html")
+ _, statements, missing, _ = cov.analysis("uni_3.html")
self.assertEqual(statements, [1, 2, 3])
self.assertEqual(missing, [1])
- self.assertIn("unicode_3.html", cov.data.summary())
+ self.assertIn("uni_3.html", cov.data.summary())
+
+ def test_plugin2_with_branch(self):
+ self.make_render_and_caller()
+
+ cov = coverage.Coverage(branch=True, omit=["*quux*"])
+ CheckUniqueFilenames.hook(cov, '_should_trace')
+ CheckUniqueFilenames.hook(cov, '_check_include_omit_etc')
+ cov.config["run:plugins"] = ["tests.plugin2"]
+
+ self.start_import_stop(cov, "caller")
+
+ # The way plugin2 works, a file named foo_7.html will be claimed to
+ # have 7 lines in it. If render() was called with line number 4,
+ # then the plugin will claim that lines 4 and 5 were executed.
+ analysis = cov._analyze("foo_7.html")
+ self.assertEqual(analysis.statements, set([1, 2, 3, 4, 5, 6, 7]))
+ # Plugins don't do branch coverage yet.
+ self.assertEqual(analysis.has_arcs(), True)
+ self.assertEqual(analysis.arc_possibilities(), [])
+
+ self.assertEqual(analysis.missing, set([1, 2, 3, 6, 7]))
+
+ def test_plugin2_with_text_report(self):
+ self.make_render_and_caller()
+
+ cov = coverage.Coverage(branch=True, omit=["*quux*"])
+ cov.config["run:plugins"] = ["tests.plugin2"]
+
+ self.start_import_stop(cov, "caller")
+
+ repout = StringIO()
+ total = cov.report(file=repout, include=["*.html"], omit=["uni*.html"])
+ report = repout.getvalue().splitlines()
+ expected = [
+ 'Name Stmts Miss Branch BrPart Cover Missing',
+ '--------------------------------------------------------',
+ 'bar_4.html 4 2 0 0 50% 1, 4',
+ 'foo_7.html 7 5 0 0 29% 1-3, 6-7',
+ '--------------------------------------------------------',
+ 'TOTAL 11 7 0 0 36% ',
+ ]
+ self.assertEqual(report, expected)
+ self.assertAlmostEqual(total, 36.36, places=2)
+
+ def test_plugin2_with_html_report(self):
+ self.make_render_and_caller()
+
+ cov = coverage.Coverage(branch=True, omit=["*quux*"])
+ cov.config["run:plugins"] = ["tests.plugin2"]
+
+ self.start_import_stop(cov, "caller")
+
+ total = cov.html_report(include=["*.html"], omit=["uni*.html"])
+ self.assertAlmostEqual(total, 36.36, places=2)
+
+ self.assert_exists("htmlcov/index.html")
+ self.assert_exists("htmlcov/bar_4_html.html")
+ self.assert_exists("htmlcov/foo_7_html.html")
+
+ def test_plugin2_with_xml_report(self):
+ self.make_render_and_caller()
+
+ cov = coverage.Coverage(branch=True, omit=["*quux*"])
+ cov.config["run:plugins"] = ["tests.plugin2"]
+
+ self.start_import_stop(cov, "caller")
+
+ total = cov.xml_report(include=["*.html"], omit=["uni*.html"])
+ self.assertAlmostEqual(total, 36.36, places=2)
+
+ with open("coverage.xml") as fxml:
+ xml = fxml.read()
+
+ for snip in [
+ 'filename="bar_4.html" line-rate="0.5" name="bar_4.html"',
+ 'filename="foo_7.html" line-rate="0.2857" name="foo_7.html"',
+ ]:
+ self.assertIn(snip, xml)
class BadPluginTest(FileTracerTest):
"""Test error handling around plugins."""
def run_bad_plugin(self, plugin_name, our_error=True):
- """Run a file, and see that the plugin failed."""
+ """Run a file, and see that the plugin failed.
+
+ `plugin_name` is the name of the plugin to use.
+
+ `our_error` is True if the error reported to the user will be an
+ explicit error in our test code, marked with an # Oh noes! comment.
+
+ """
self.make_file("simple.py", """\
- import other
- a = 2
- b = 3
+ import other, another
+ a = other.f(2)
+ b = other.f(3)
+ c = another.g(4)
+ d = another.g(5)
""")
+ # The names of these files are important: some plugins apply themselves
+ # to "*other.py".
self.make_file("other.py", """\
- x = 1
- y = 2
+ def f(x):
+ return x+1
+ """)
+ self.make_file("another.py", """\
+ def g(x):
+ return x-1
""")
cov = coverage.Coverage()
@@ -370,9 +481,9 @@ class BadPluginTest(FileTracerTest):
self.assertEqual(errors, 1)
# There should be a warning explaining what's happening, but only one.
- msg = "Disabling plugin '%s' due to an exception:" % plugin_name
- tracebacks = stderr.count(msg)
- self.assertEqual(tracebacks, 1)
+ msg = "Disabling plugin %r due to an exception:" % plugin_name
+ warnings = stderr.count(msg)
+ self.assertEqual(warnings, 1)
def test_file_tracer_fails(self):
self.make_file("bad_plugin.py", """\
@@ -431,19 +542,69 @@ class BadPluginTest(FileTracerTest):
""")
self.run_bad_plugin("bad_plugin", our_error=False)
- # This test currently crashes the C tracer function. I'm working on
- # figuring out why....
- def xxx_dynamic_source_filename_fails(self):
+ def test_dynamic_source_filename_fails(self):
self.make_file("bad_plugin.py", """\
import coverage.plugin
class Plugin(coverage.plugin.CoveragePlugin):
def file_tracer(self, filename):
- return BadFileTracer()
+ if filename.endswith("other.py"):
+ return BadFileTracer()
class BadFileTracer(coverage.plugin.FileTracer):
def has_dynamic_source_filename(self):
return True
def dynamic_source_filename(self, filename, frame):
- 101/0
+ 101/0 # Oh noes!
""")
self.run_bad_plugin("bad_plugin")
+
+ def test_line_number_range_returns_non_tuple(self):
+ self.make_file("bad_plugin.py", """\
+ import coverage.plugin
+ class Plugin(coverage.plugin.CoveragePlugin):
+ def file_tracer(self, filename):
+ if filename.endswith("other.py"):
+ return BadFileTracer()
+
+ class BadFileTracer(coverage.plugin.FileTracer):
+ def source_filename(self):
+ return "something.foo"
+
+ def line_number_range(self, frame):
+ return 42.23
+ """)
+ self.run_bad_plugin("bad_plugin", our_error=False)
+
+ def test_line_number_range_returns_triple(self):
+ self.make_file("bad_plugin.py", """\
+ import coverage.plugin
+ class Plugin(coverage.plugin.CoveragePlugin):
+ def file_tracer(self, filename):
+ if filename.endswith("other.py"):
+ return BadFileTracer()
+
+ class BadFileTracer(coverage.plugin.FileTracer):
+ def source_filename(self):
+ return "something.foo"
+
+ def line_number_range(self, frame):
+ return (1, 2, 3)
+ """)
+ self.run_bad_plugin("bad_plugin", our_error=False)
+
+ def test_line_number_range_returns_pair_of_strings(self):
+ self.make_file("bad_plugin.py", """\
+ import coverage.plugin
+ class Plugin(coverage.plugin.CoveragePlugin):
+ def file_tracer(self, filename):
+ if filename.endswith("other.py"):
+ return BadFileTracer()
+
+ class BadFileTracer(coverage.plugin.FileTracer):
+ def source_filename(self):
+ return "something.foo"
+
+ def line_number_range(self, frame):
+ return ("5", "7")
+ """)
+ self.run_bad_plugin("bad_plugin", our_error=False)
diff --git a/tox.ini b/tox.ini
index ee91ebb..37ae451 100644
--- a/tox.ini
+++ b/tox.ini
@@ -5,6 +5,7 @@
[tox]
envlist = py26, py27, py33, py34, py35, pypy24, pypy3_24
+skip_missing_interpreters = True
[testenv]
commands =
@@ -35,10 +36,6 @@ setenv =
usedevelop = True
-[testenv:py35]
-# Just until tox learns with 35 means.
-basepython = python3.5
-
[testenv:pypy24]
basepython = pypy2.4