diff options
author | Ned Batchelder <nedbat@gmail.com> | 2015-04-24 21:06:26 -0400 |
---|---|---|
committer | Ned Batchelder <nedbat@gmail.com> | 2015-04-24 21:06:26 -0400 |
commit | 4cac18d36cd53c85f43ab79816b71cd2d4a3e6db (patch) | |
tree | 5741c738325d92c1a417b2c943f9b3f98f3417ba | |
parent | 3d88444cd3659fb93f17cbf4288c23bc82f33ead (diff) | |
parent | c3a98ff12933710d751ec28b984edb7936d457d3 (diff) | |
download | python-coveragepy-4cac18d36cd53c85f43ab79816b71cd2d4a3e6db.tar.gz |
Merged in lep/coverage.py (pull request #48)
Fix #363: crash when annotating non-ascii characters in python 2.
57 files changed, 1235 insertions, 608 deletions
diff --git a/AUTHORS.txt b/AUTHORS.txt index fcd128e..b0ea69f 100644 --- a/AUTHORS.txt +++ b/AUTHORS.txt @@ -16,7 +16,9 @@ Catherine Proulx Chris Adams Chris Rose Christian Heimes +Christine Lytwynec Christoph Zwerschke +Conrad Ho Danek Duvall Danny Allen David Christian @@ -33,6 +35,7 @@ Guillaume Chazarain Imri Goldberg JT Olds Jessamyn Smith +Jon Chappell Joseph Tate Julian Berman Krystian Kichewko @@ -42,6 +45,7 @@ Marcus Cobden Mark van der Wal Martin Fuzzey Matthew Desmarais +Mickie Betz Noel O'Boyle Pablo Carballo Patrick Mezard diff --git a/CHANGES.txt b/CHANGES.txt index f96db6e..1712208 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -3,6 +3,42 @@ Change history for Coverage.py ------------------------------ +Latest +------ + +- The ``coverage combine`` command now accepts any number of directories as + arguments, and will combine all the data files from those directories. This + means you don't have to copy the files to one directory before combining. + Thanks, Christine Lytwynec. Finishes `issue 354`_. + +- Branch coverage couldn't properly handle certain extremely long files. This + is now fixed (`issue 359`_). + +- Branch coverage didn't understand yield statements properly. Mickie Betz + persisted in pursuing this despite Ned's pessimism. Fixes `issue 308`_ and + `issue 324`_. + +- 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>`_. + +- HTML reports now include a timestamp in the footer, closing `issue 299`_. + Thanks, Conrad Ho. + +- HTML reports now begrudgingly use double-quotes rather than single quotes, + because there are "software engineers" out there writing tools that read HTML + and somehow have no idea that single quotes exist. Fixes `issue 361`_. + Thanks, Jon Chappell. + +.. _issue 299: https://bitbucket.org/ned/coveragepy/issue/299/inserted-created-on-yyyy-mm-dd-hh-mm-in +.. _issue 308: https://bitbucket.org/ned/coveragepy/issue/308/yield-lambda-branch-coverage +.. _issue 324: https://bitbucket.org/ned/coveragepy/issue/324/yield-in-loop-confuses-branch-coverage +.. _issue 354: https://bitbucket.org/ned/coveragepy/issue/354/coverage-combine-should-take-a-list-of +.. _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 +.. _issue 361: https://bitbucket.org/ned/coveragepy/issue/361/use-double-quotes-in-html-output-to + + Version 4.0a5 --- 16 February 2015 ---------------------------------- @@ -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 d4bbeb3..6e68d4a 100644 --- a/coverage/annotate.py +++ b/coverage/annotate.py @@ -42,10 +42,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) @@ -53,18 +53,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/cmdline.py b/coverage/cmdline.py index 2be3294..66a76fa 100644 --- a/coverage/cmdline.py +++ b/coverage/cmdline.py @@ -249,10 +249,13 @@ CMDS = { ), 'combine': CmdOptionParser("combine", GLOBAL_ARGS, - usage = " ", + usage = "<dir1> <dir2> ... <dirN>", description = "Combine data from multiple coverage files collected " "with 'run -p'. The combined results are written to a single " - "file representing the union of the data." + "file representing the union of the data. The positional " + "arguments are directories from which the data files should be " + "combined. By default, only data files in the current directory " + "are combined." ), 'debug': CmdOptionParser("debug", GLOBAL_ARGS, @@ -430,7 +433,8 @@ class CoverageScript(object): self.do_run(options, args) if options.action == "combine": - self.coverage.combine() + data_dirs = argv if argv else None + self.coverage.combine(data_dirs) self.coverage.save() # Remaining actions are reporting, with some common options. 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..2c8d384 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 @@ -715,13 +717,17 @@ class Coverage(object): self._harvest_data() self.data.write(suffix=data_suffix) - def combine(self): + def combine(self, data_dirs=None): """Combine together a number of similarly-named coverage data files. All coverage data files whose name starts with `data_file` (from the coverage() constructor) will be read, and combined together into the current measurements. + `data_dirs` is a list of directories from which data files should be + combined. If no list is passed, then the data files from the current + directory will be combined. + """ self._init() aliases = None @@ -731,7 +737,7 @@ class Coverage(object): result = paths[0] for pattern in paths[1:]: aliases.add(pattern, result) - self.data.combine_parallel_data(aliases=aliases) + self.data.combine_parallel_data(aliases=aliases, data_dirs=data_dirs) def _harvest_data(self): """Get the collected data and reset the collector. @@ -835,12 +841,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 +855,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 +901,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 +1001,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 +1023,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/data.py b/coverage/data.py index 2c5d351..8a699b5 100644 --- a/coverage/data.py +++ b/coverage/data.py @@ -1,5 +1,6 @@ """Coverage data for Coverage.""" +import glob import os from coverage.backward import iitems, pickle @@ -190,7 +191,7 @@ class CoverageData(object): pass return lines, arcs, plugins - def combine_parallel_data(self, aliases=None): + def combine_parallel_data(self, aliases=None, data_dirs=None): """Combine a number of data files together. Treat `self.filename` as a file prefix, and combine the data from all @@ -199,23 +200,30 @@ class CoverageData(object): If `aliases` is provided, it's a `PathAliases` object that is used to re-map paths to match the local machine's. + If `data_dirs` is provided, then it combines the data files from each + directory into a single file. + """ aliases = aliases or PathAliases() data_dir, local = os.path.split(self.filename) - localdot = local + '.' - for f in os.listdir(data_dir or '.'): - if f.startswith(localdot): - full_path = os.path.join(data_dir, f) - new_lines, new_arcs, new_plugins = self._read_file(full_path) - for filename, file_data in iitems(new_lines): - filename = aliases.map(filename) - self.lines.setdefault(filename, {}).update(file_data) - for filename, file_data in iitems(new_arcs): - filename = aliases.map(filename) - self.arcs.setdefault(filename, {}).update(file_data) - self.plugins.update(new_plugins) - if f != local: - os.remove(full_path) + localdot = local + '.*' + + data_dirs = data_dirs or [data_dir] + files_to_combine = [] + for d in data_dirs: + pattern = os.path.join(os.path.abspath(d), localdot) + files_to_combine.extend(glob.glob(pattern)) + + for f in files_to_combine: + new_lines, new_arcs, new_plugins = self._read_file(f) + for filename, file_data in iitems(new_lines): + filename = aliases.map(filename) + self.lines.setdefault(filename, {}).update(file_data) + for filename, file_data in iitems(new_arcs): + filename = aliases.map(filename) + self.arcs.setdefault(filename, {}).update(file_data) + self.plugins.update(new_plugins) + os.remove(f) def add_line_data(self, line_data): """Add executed line data. diff --git a/coverage/html.py b/coverage/html.py index 2a9e0d1..0b2cc25 100644 --- a/coverage/html.py +++ b/coverage/html.py @@ -2,6 +2,7 @@ from __future__ import unicode_literals +import datetime import json import os import re @@ -92,6 +93,7 @@ class HtmlReporter(Reporter): self.status = HtmlStatus() self.extra_css = None self.totals = Numbers() + self.time_stamp = datetime.datetime.now().strftime('%Y-%m-%d %H:%M') def report(self, morfs): """Generate an HTML report for `morfs`. @@ -150,20 +152,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 +188,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 = "" @@ -221,7 +223,7 @@ class HtmlReporter(Reporter): else: tok_html = escape(tok_text) or ' ' html.append( - "<span class='%s'>%s</span>" % (tok_type, tok_html) + '<span class="%s">%s</span>' % (tok_type, tok_html) ) lines.append({ @@ -236,7 +238,8 @@ 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, + 'time_stamp': self.time_stamp, } html = spaceless(self.source_tmpl.render(template_values)) @@ -248,7 +251,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) @@ -266,6 +269,7 @@ class HtmlReporter(Reporter): 'extra_css': self.extra_css, 'files': self.files, 'totals': self.totals, + 'time_stamp': self.time_stamp, }) self.write_html( diff --git a/coverage/htmlfiles/index.html b/coverage/htmlfiles/index.html index 90802c8..1afc57c 100644 --- a/coverage/htmlfiles/index.html +++ b/coverage/htmlfiles/index.html @@ -1,30 +1,30 @@ <!DOCTYPE html> <html> <head> - <meta http-equiv='Content-Type' content='text/html; charset=utf-8'> + <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>{{ title|escape }}</title> - <link rel='stylesheet' href='style.css' type='text/css'> + <link rel="stylesheet" href="style.css" type="text/css"> {% if extra_css %} - <link rel='stylesheet' href='{{ extra_css }}' type='text/css'> + <link rel="stylesheet" href="{{ extra_css }}" type="text/css"> {% endif %} - <script type='text/javascript' src='jquery.min.js'></script> - <script type='text/javascript' src='jquery.debounce.min.js'></script> - <script type='text/javascript' src='jquery.tablesorter.min.js'></script> - <script type='text/javascript' src='jquery.hotkeys.js'></script> - <script type='text/javascript' src='coverage_html.js'></script> - <script type='text/javascript'> + <script type="text/javascript" src="jquery.min.js"></script> + <script type="text/javascript" src="jquery.debounce.min.js"></script> + <script type="text/javascript" src="jquery.tablesorter.min.js"></script> + <script type="text/javascript" src="jquery.hotkeys.js"></script> + <script type="text/javascript" src="coverage_html.js"></script> + <script type="text/javascript"> jQuery(document).ready(coverage.index_ready); </script> </head> -<body class='indexfile'> +<body class="indexfile"> -<div id='header'> - <div class='content'> +<div id="header"> + <div class="content"> <h1>{{ title|escape }}: - <span class='pc_cov'>{{totals.pc_covered_str}}%</span> + <span class="pc_cov">{{totals.pc_covered_str}}%</span> </h1> - <img id='keyboard_icon' src='keybd_closed.png' alt='Show keyboard shortcuts' /> + <img id="keyboard_icon" src="keybd_closed.png" alt="Show keyboard shortcuts" /> <form id="filter_container"> <input id="filter" type="text" value="" placeholder="filter..." /> @@ -32,44 +32,44 @@ </div> </div> -<div class='help_panel'> - <img id='panel_icon' src='keybd_open.png' alt='Hide keyboard shortcuts' /> - <p class='legend'>Hot-keys on this page</p> +<div class="help_panel"> + <img id="panel_icon" src="keybd_open.png" alt="Hide keyboard shortcuts" /> + <p class="legend">Hot-keys on this page</p> <div> - <p class='keyhelp'> - <span class='key'>n</span> - <span class='key'>s</span> - <span class='key'>m</span> - <span class='key'>x</span> + <p class="keyhelp"> + <span class="key">n</span> + <span class="key">s</span> + <span class="key">m</span> + <span class="key">x</span> {% if arcs %} - <span class='key'>b</span> - <span class='key'>p</span> + <span class="key">b</span> + <span class="key">p</span> {% endif %} - <span class='key'>c</span> change column sorting + <span class="key">c</span> change column sorting </p> </div> </div> -<div id='index'> - <table class='index'> +<div id="index"> + <table class="index"> <thead> - {# The title='' attr doesn't work in Safari. #} - <tr class='tablehead' title='Click to sort'> - <th class='name left headerSortDown shortkey_n'>Module</th> - <th class='shortkey_s'>statements</th> - <th class='shortkey_m'>missing</th> - <th class='shortkey_x'>excluded</th> + {# The title="" attr doesn"t work in Safari. #} + <tr class="tablehead" title="Click to sort"> + <th class="name left headerSortDown shortkey_n">Module</th> + <th class="shortkey_s">statements</th> + <th class="shortkey_m">missing</th> + <th class="shortkey_x">excluded</th> {% if arcs %} - <th class='shortkey_b'>branches</th> - <th class='shortkey_p'>partial</th> + <th class="shortkey_b">branches</th> + <th class="shortkey_p">partial</th> {% endif %} - <th class='right shortkey_c'>coverage</th> + <th class="right shortkey_c">coverage</th> </tr> </thead> {# HTML syntax requires thead, tfoot, tbody #} <tfoot> - <tr class='total'> - <td class='name left'>Total</td> + <tr class="total"> + <td class="name left">Total</td> <td>{{totals.n_statements}}</td> <td>{{totals.n_missing}}</td> <td>{{totals.n_excluded}}</td> @@ -77,13 +77,13 @@ <td>{{totals.n_branches}}</td> <td>{{totals.n_partial_branches}}</td> {% endif %} - <td class='right' data-ratio='{{totals.ratio_covered|pair}}'>{{totals.pc_covered_str}}%</td> + <td class="right" data-ratio="{{totals.ratio_covered|pair}}">{{totals.pc_covered_str}}%</td> </tr> </tfoot> <tbody> {% for file in files %} - <tr class='file'> - <td class='name left'><a href='{{file.html_filename}}'>{{file.name}}</a></td> + <tr class="file"> + <td class="name left"><a href="{{file.html_filename}}">{{file.name}}</a></td> <td>{{file.nums.n_statements}}</td> <td>{{file.nums.n_missing}}</td> <td>{{file.nums.n_excluded}}</td> @@ -91,7 +91,7 @@ <td>{{file.nums.n_branches}}</td> <td>{{file.nums.n_partial_branches}}</td> {% endif %} - <td class='right' data-ratio='{{file.nums.ratio_covered|pair}}'>{{file.nums.pc_covered_str}}%</td> + <td class="right" data-ratio="{{file.nums.ratio_covered|pair}}">{{file.nums.pc_covered_str}}%</td> </tr> {% endfor %} </tbody> @@ -102,10 +102,11 @@ </p> </div> -<div id='footer'> - <div class='content'> +<div id="footer"> + <div class="content"> <p> - <a class='nav' href='{{__url__}}'>coverage.py v{{__version__}}</a> + <a class="nav" href="{{__url__}}">coverage.py v{{__version__}}</a>, + created at {{ time_stamp }} </p> </div> </div> diff --git a/coverage/htmlfiles/pyfile.html b/coverage/htmlfiles/pyfile.html index 38dfb47..d78ba53 100644 --- a/coverage/htmlfiles/pyfile.html +++ b/coverage/htmlfiles/pyfile.html @@ -1,90 +1,91 @@ <!DOCTYPE html> <html> <head> - <meta http-equiv='Content-Type' content='text/html; charset=utf-8'> + <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> {# 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> - <link rel='stylesheet' href='style.css' type='text/css'> + <meta http-equiv="X-UA-Compatible" content="IE=emulateIE7" /> + <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'> + <link rel="stylesheet" href="{{ extra_css }}" type="text/css"> {% endif %} - <script type='text/javascript' src='jquery.min.js'></script> - <script type='text/javascript' src='jquery.hotkeys.js'></script> - <script type='text/javascript' src='jquery.isonscreen.js'></script> - <script type='text/javascript' src='coverage_html.js'></script> - <script type='text/javascript'> + <script type="text/javascript" src="jquery.min.js"></script> + <script type="text/javascript" src="jquery.hotkeys.js"></script> + <script type="text/javascript" src="jquery.isonscreen.js"></script> + <script type="text/javascript" src="coverage_html.js"></script> + <script type="text/javascript"> jQuery(document).ready(coverage.pyfile_ready); </script> </head> -<body class='pyfile'> +<body class="pyfile"> -<div id='header'> - <div class='content'> - <h1>Coverage for <b>{{cu.name|escape}}</b> : - <span class='pc_cov'>{{nums.pc_covered_str}}%</span> +<div id="header"> + <div class="content"> + <h1>Coverage for <b>{{fr.name|escape}}</b> : + <span class="pc_cov">{{nums.pc_covered_str}}%</span> </h1> - <img id='keyboard_icon' src='keybd_closed.png' alt='Show keyboard shortcuts' /> + <img id="keyboard_icon" src="keybd_closed.png" alt="Show keyboard shortcuts" /> - <h2 class='stats'> + <h2 class="stats"> {{nums.n_statements}} statements - <span class='{{c_run}} shortkey_r button_toggle_run'>{{nums.n_executed}} run</span> - <span class='{{c_mis}} shortkey_m button_toggle_mis'>{{nums.n_missing}} missing</span> - <span class='{{c_exc}} shortkey_x button_toggle_exc'>{{nums.n_excluded}} excluded</span> + <span class="{{c_run}} shortkey_r button_toggle_run">{{nums.n_executed}} run</span> + <span class="{{c_mis}} shortkey_m button_toggle_mis">{{nums.n_missing}} missing</span> + <span class="{{c_exc}} shortkey_x button_toggle_exc">{{nums.n_excluded}} excluded</span> {% if arcs %} - <span class='{{c_par}} shortkey_p button_toggle_par'>{{nums.n_partial_branches}} partial</span> + <span class="{{c_par}} shortkey_p button_toggle_par">{{nums.n_partial_branches}} partial</span> {% endif %} </h2> </div> </div> -<div class='help_panel'> - <img id='panel_icon' src='keybd_open.png' alt='Hide keyboard shortcuts' /> - <p class='legend'>Hot-keys on this page</p> +<div class="help_panel"> + <img id="panel_icon" src="keybd_open.png" alt="Hide keyboard shortcuts" /> + <p class="legend">Hot-keys on this page</p> <div> - <p class='keyhelp'> - <span class='key'>r</span> - <span class='key'>m</span> - <span class='key'>x</span> - <span class='key'>p</span> toggle line displays + <p class="keyhelp"> + <span class="key">r</span> + <span class="key">m</span> + <span class="key">x</span> + <span class="key">p</span> toggle line displays </p> - <p class='keyhelp'> - <span class='key'>j</span> - <span class='key'>k</span> next/prev highlighted chunk + <p class="keyhelp"> + <span class="key">j</span> + <span class="key">k</span> next/prev highlighted chunk </p> - <p class='keyhelp'> - <span class='key'>0</span> (zero) top of page + <p class="keyhelp"> + <span class="key">0</span> (zero) top of page </p> - <p class='keyhelp'> - <span class='key'>1</span> (one) first highlighted chunk + <p class="keyhelp"> + <span class="key">1</span> (one) first highlighted chunk </p> </div> </div> -<div id='source'> +<div id="source"> <table> <tr> - <td class='linenos'> + <td class="linenos"> {% for line in lines %} - <p id='n{{line.number}}' class='{{line.class}}'><a href='#n{{line.number}}'>{{line.number}}</a></p> + <p id="n{{line.number}}" class="{{line.class}}"><a href="#n{{line.number}}">{{line.number}}</a></p> {% endfor %} </td> - <td class='text'> + <td class="text"> {% for line in lines %} - <p id='t{{line.number}}' class='{{line.class}}'>{% if line.annotate %}<span class='annotate' title='{{line.annotate_title}}'>{{line.annotate}}</span>{% endif %}{{line.html}}<span class='strut'> </span></p> + <p id="t{{line.number}}" class="{{line.class}}">{% if line.annotate %}<span class="annotate" title="{{line.annotate_title}}">{{line.annotate}}</span>{% endif %}{{line.html}}<span class="strut"> </span></p> {% endfor %} </td> </tr> </table> </div> -<div id='footer'> - <div class='content'> +<div id="footer"> + <div class="content"> <p> - <a class='nav' href='index.html'>« index</a> <a class='nav' href='{{__url__}}'>coverage.py v{{__version__}}</a> + <a class="nav" href="index.html">« index</a> <a class="nav" href="{{__url__}}">coverage.py v{{__version__}}</a>, + created at {{ time_stamp }} </p> </div> </div> diff --git a/coverage/parser.py b/coverage/parser.py index f488367..fc751eb 100644 --- a/coverage/parser.py +++ b/coverage/parser.py @@ -460,7 +460,7 @@ class ByteParser(object): # Walk the byte codes building chunks. for bc in bytecodes: - # Maybe have to start a new chunk + # Maybe have to start a new chunk. start_new_chunk = False first_chunk = False if bc.offset in bytes_lines_map: @@ -481,9 +481,13 @@ class ByteParser(object): if chunk: chunk.exits.add(bc.offset) chunk = Chunk(bc.offset, chunk_lineno, first_chunk) + if not chunks: + # The very first chunk of a code object is always an + # entrance. + chunk.entrance = True chunks.append(chunk) - # Look at the opcode + # Look at the opcode. if bc.jump_to >= 0 and bc.op not in OPS_NO_JUMP: if ignore_branch: # Someone earlier wanted us to ignore this branch. @@ -570,15 +574,15 @@ class ByteParser(object): """ chunks = self._split_into_chunks() - # A map from byte offsets to chunks jumped into. + # A map from byte offsets to the chunk starting at that offset. byte_chunks = dict((c.byte, c) for c in chunks) - # There's always an entrance at the first chunk. - yield (-1, byte_chunks[0].line) - # Traverse from the first chunk in each line, and yield arcs where # the trace function will be invoked. for chunk in chunks: + if chunk.entrance: + yield (-1, chunk.line) + if not chunk.first: continue @@ -586,7 +590,7 @@ class ByteParser(object): chunks_to_consider = [chunk] while chunks_to_consider: # Get the chunk we're considering, and make sure we don't - # consider it again + # consider it again. this_chunk = chunks_to_consider.pop() chunks_considered.add(this_chunk) @@ -649,6 +653,8 @@ class Chunk(object): .. _basic block: http://en.wikipedia.org/wiki/Basic_block + `byte` is the offset to the bytecode starting this chunk. + `line` is the source line number containing this chunk. `first` is true if this is the first chunk in the source line. @@ -656,19 +662,24 @@ class Chunk(object): An exit < 0 means the chunk can leave the code (return). The exit is the negative of the starting line number of the code block. + The `entrance` attribute is a boolean indicating whether the code object + can be entered at this chunk. + """ def __init__(self, byte, line, first): self.byte = byte self.line = line self.first = first self.length = 0 + self.entrance = False self.exits = set() def __repr__(self): - if self.first: - bang = "!" - else: - bang = "" - return "<%d+%d @%d%s %r>" % ( - self.byte, self.length, self.line, bang, list(self.exits) + return "<%d+%d @%d%s%s %r>" % ( + self.byte, + self.length, + self.line, + "!" if self.first else "", + "v" if self.entrance else "", + list(self.exits), ) 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/pytracer.py b/coverage/pytracer.py index 0eafbef..3f03aaf 100644 --- a/coverage/pytracer.py +++ b/coverage/pytracer.py @@ -1,7 +1,15 @@ """Raw data collector for Coverage.""" +import dis import sys +from coverage import env + +# We need the YIELD_VALUE opcode below, in a comparison-friendly form. +YIELD_VALUE = dis.opmap['YIELD_VALUE'] +if env.PY2: + YIELD_VALUE = chr(YIELD_VALUE) + class PyTracer(object): """Python implementation of the raw data tracer.""" @@ -79,9 +87,11 @@ class PyTracer(object): if tracename not in self.data: self.data[tracename] = {} self.cur_file_dict = self.data[tracename] - # Set the last_line to -1 because the next arc will be entering a - # code block, indicated by (-1, n). - self.last_line = -1 + # The call event is really a "start frame" event, and happens for + # function calls and re-entering generators. The f_lasti field is + # -1 for calls, and a real offset for generators. Use -1 as the + # line number for calls, and the real line number for generators. + self.last_line = -1 if (frame.f_lasti < 0) else frame.f_lineno elif event == 'line': # Record an executed line. if self.cur_file_dict is not None: @@ -93,8 +103,12 @@ class PyTracer(object): self.last_line = lineno elif event == 'return': if self.arcs and self.cur_file_dict: - first = frame.f_code.co_firstlineno - self.cur_file_dict[(self.last_line, -first)] = None + # Record an arc leaving the function, but beware that a + # "return" event might just mean yielding from a generator. + bytecode = frame.f_code.co_code[frame.f_lasti] + if bytecode != YIELD_VALUE: + first = frame.f_code.co_firstlineno + self.cur_file_dict[(self.last_line, -first)] = None # Leaving this function, pop the filename stack. self.cur_file_dict, self.last_line = self.data_stack.pop() elif event == 'exception': 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..7b621c1 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() @@ -101,10 +101,13 @@ class Analysis(object): # Exclude arcs here which connect a line to itself. They can occur # in executed data in some cases. This is where they can cause # trouble, and here is where it's the least burden to remove them. + # Also, generators can somehow cause arcs from "enter" to "exit", so + # make sure we have at least one positive value. unpredicted = ( e for e in executed if e not in possible and e[0] != e[1] + and (e[0] > 0 or e[1] > 0) ) return sorted(unpredicted) 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/templite.py b/coverage/templite.py index c102a8f..9f882cf 100644 --- a/coverage/templite.py +++ b/coverage/templite.py @@ -201,7 +201,7 @@ class Templite(object): for var_name in self.all_vars - self.loop_vars: vars_code.add_line("c_%s = context[%r]" % (var_name, var_name)) - code.add_line("return ''.join(result)") + code.add_line('return "".join(result)') code.dedent() self._render_function = code.get_globals()['render_function'] diff --git a/coverage/test_helpers.py b/coverage/test_helpers.py index 55a67a0..3f058b1 100644 --- a/coverage/test_helpers.py +++ b/coverage/test_helpers.py @@ -147,8 +147,15 @@ class TempDirMixin(SysPathAwareMixin, ModuleAwareMixin, TestCase): """ # Our own setting: most of these tests run in their own temp directory. + # Set this to False in your subclass if you don't want a temp directory + # created. run_in_temp_dir = True + # Set this if you aren't creating any files with make_file, but still want + # the temp directory. This will stop the test behavior checker from + # complaining. + no_files_in_temp_dir = False + def setUp(self): super(TempDirMixin, self).setUp() @@ -165,8 +172,8 @@ class TempDirMixin(SysPathAwareMixin, ModuleAwareMixin, TestCase): class_behavior = self.class_behavior() class_behavior.tests += 1 - class_behavior.test_method_made_any_files = False class_behavior.temp_dir = self.run_in_temp_dir + class_behavior.no_files_ok = self.no_files_in_temp_dir self.addCleanup(self.check_behavior) @@ -239,6 +246,7 @@ class TempDirMixin(SysPathAwareMixin, ModuleAwareMixin, TestCase): self.tests = 0 self.skipped = 0 self.temp_dir = True + self.no_files_ok = False self.tests_making_files = 0 self.test_method_made_any_files = False @@ -252,7 +260,8 @@ class TempDirMixin(SysPathAwareMixin, ModuleAwareMixin, TestCase): if behavior.tests <= behavior.skipped: bad = "" elif behavior.temp_dir and behavior.tests_making_files == 0: - bad = "Inefficient" + if not behavior.no_files_ok: + bad = "Inefficient" elif not behavior.temp_dir and behavior.tests_making_files > 0: bad = "Unsafe" else: diff --git a/coverage/tracer.c b/coverage/tracer.c index 43ecd18..fe40fc6 100644 --- a/coverage/tracer.c +++ b/coverage/tracer.c @@ -3,6 +3,7 @@ #include "Python.h" #include "structmember.h" #include "frameobject.h" +#include "opcode.h" /* Compile-time debugging helpers */ #undef WHAT_LOG /* Define to log the WHAT params in the trace function. */ @@ -21,7 +22,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 +34,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 +48,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 +192,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 +290,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 +357,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 +377,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 +420,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 +448,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 +465,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 +479,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 +509,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 +530,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 +549,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 +576,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 +614,133 @@ CTracer_handle_call(CTracer *self, PyFrameObject *frame) SHOWLOG(self->pdata_stack->depth, frame->f_lineno, filename, "skipped"); } - self->cur_entry.last_line = -1; + self->cur_entry.disposition = disposition; + + /* A call event is really a "start frame" event, and can happen for + * re-entering a generator also. f_lasti is -1 for a true call, and a + * real byte offset for a generator re-entry. + */ + self->cur_entry.last_line = (frame->f_lasti < 0) ? -1 : frame->f_lineno; +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 +755,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,14 +809,18 @@ 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) { if (self->tracing_arcs && self->cur_entry.file_data) { - int first = frame->f_code->co_firstlineno; - if (CTracer_record_pair(self, self->cur_entry.last_line, -first) < 0) { - goto error; + /* Need to distinguish between RETURN_VALUE and YIELD_VALUE. */ + int bytecode = MyBytes_AS_STRING(frame->f_code->co_code)[frame->f_lasti]; + if (bytecode != YIELD_VALUE) { + int first = frame->f_code->co_firstlineno; + if (CTracer_record_pair(self, self->cur_entry.last_line, -first) < 0) { + goto error; + } } } @@ -742,45 +875,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 +986,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 +1033,7 @@ CTracer_stop(CTracer *self, PyObject *args_unused) self->started = 0; } - return Py_BuildValue(""); + Py_RETURN_NONE; } static PyObject * @@ -921,7 +1054,7 @@ CTracer_get_stats(CTracer *self) "errors", self->stats.errors ); #else - return Py_BuildValue(""); + Py_RETURN_NONE; #endif /* COLLECT_STATS */ } @@ -1045,7 +1178,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 @@ -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/parser.py b/lab/parser.py index 1783468..662183a 100644 --- a/lab/parser.py +++ b/lab/parser.py @@ -9,8 +9,8 @@ from optparse import OptionParser import disgen from coverage.misc import CoverageException -from coverage.files import get_python_source from coverage.parser import ByteParser, PythonParser +from coverage.python import get_python_source opcode_counts = collections.Counter() @@ -82,7 +82,7 @@ class ParserMain(object): self.disassemble(bp, histogram=options.histogram) arcs = bp._all_arcs() - if options.chunks and not options.dis: + if options.chunks:# and not options.dis: chunks = bp._all_chunks() if options.recursive: print("%6d: %s" % (len(chunks), filename)) @@ -116,7 +116,7 @@ class ParserMain(object): m2 = 'C' if lineno in cp.excluded: m3 = 'x' - a = arc_chars.get(lineno, '').ljust(arc_width) + a = arc_chars[lineno].ljust(arc_width) print("%4d %s%s%s%s%s %s" % (lineno, m0, m1, m2, m3, a, ltext) ) @@ -162,12 +162,12 @@ class ParserMain(object): dictionary mapping line numbers to ascii strings to draw for that line. """ - arc_chars = {} + arc_chars = collections.defaultdict(str) for lfrom, lto in sorted(arcs): if lfrom < 0: - arc_chars[lto] = arc_chars.get(lto, '') + 'v' + arc_chars[lto] += 'v' elif lto < 0: - arc_chars[lfrom] = arc_chars.get(lfrom, '') + '^' + arc_chars[lfrom] += '^' else: if lfrom == lto - 1: # Don't show obvious arcs. @@ -176,7 +176,7 @@ class ParserMain(object): l1, l2 = lfrom, lto else: l1, l2 = lto, lfrom - w = max([len(arc_chars.get(l, '')) for l in range(l1, l2+1)]) + w = max(len(arc_chars[l]) for l in range(l1, l2+1)) for l in range(l1, l2+1): if l == lfrom: ch = '<' @@ -184,11 +184,11 @@ class ParserMain(object): ch = '>' else: ch = '|' - arc_chars[l] = arc_chars.get(l, '').ljust(w) + ch + arc_chars[l] = arc_chars[l].ljust(w) + ch arc_width = 0 if arc_chars: - arc_width = max([len(a) for a in arc_chars.values()]) + arc_width = max(len(a) for a in arc_chars.values()) else: arc_width = 0 diff --git a/lab/run_trace.py b/lab/run_trace.py new file mode 100644 index 0000000..3822a80 --- /dev/null +++ b/lab/run_trace.py @@ -0,0 +1,32 @@ +"""Run a simple trace function on a file of Python code.""" + +import os, sys + +nest = 0 + +def trace(frame, event, arg): + global nest + + if nest is None: + # This can happen when Python is shutting down. + return None + + print "%s%s %s %d @%d" % ( + " " * nest, + event, + os.path.basename(frame.f_code.co_filename), + frame.f_lineno, + frame.f_lasti, + ) + + if event == 'call': + nest += 1 + if event == 'return': + nest -= 1 + + return trace + +the_program = sys.argv[1] + +sys.settrace(trace) +execfile(the_program) diff --git a/lab/sample.py b/lab/sample.py deleted file mode 100644 index bb62848..0000000 --- a/lab/sample.py +++ /dev/null @@ -1,5 +0,0 @@ -a, b = 1, 0 -if a or b or fn(): - # Hey - a = 3 -d = 4 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/lab/trace_sample.py b/lab/trace_sample.py deleted file mode 100644 index 3f81919..0000000 --- a/lab/trace_sample.py +++ /dev/null @@ -1,57 +0,0 @@ -import os, sys - -global nest -nest = 0 - -def trace(frame, event, arg): - #if event == 'line': - global nest - - print "%s%s %s %d" % ( - " " * nest, - event, - os.path.basename(frame.f_code.co_filename), - frame.f_lineno, - ) - - if event == 'call': - nest += 1 - if event == 'return': - nest -= 1 - - return trace - -def trace2(frame, event, arg): - #if event == 'line': - global nest - - print "2: %s%s %s %d" % ( - " " * nest, - event, - os.path.basename(frame.f_code.co_filename), - frame.f_lineno, - ) - - if event == 'call': - nest += 1 - if event == 'return': - nest -= 1 - - return trace2 - -sys.settrace(trace) - -def bar(): - print "nar" - -a = 26 -def foo(n): - a = 28 - sys.settrace(sys.gettrace()) - bar() - a = 30 - return 2*n - -print foo(a) -#import sample -#import littleclass 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/farm/html/run_a.py b/tests/farm/html/run_a.py index 7963d2e..f510e11 100644 --- a/tests/farm/html/run_a.py +++ b/tests/farm/html/run_a.py @@ -13,14 +13,14 @@ runfunc(html_it, rundir="src") # and check that certain key strings are in the output. compare("gold_a", "html_a", size_within=10, file_pattern="*.html") contains("html_a/a_py.html", - "<span class='key'>if</span> <span class='num'>1</span> <span class='op'><</span> <span class='num'>2</span>", - " <span class='nam'>a</span> <span class='op'>=</span> <span class='num'>3</span>", - "<span class='pc_cov'>67%</span>" + '<span class="key">if</span> <span class="num">1</span> <span class="op"><</span> <span class="num">2</span>', + ' <span class="nam">a</span> <span class="op">=</span> <span class="num">3</span>', + '<span class="pc_cov">67%</span>' ) contains("html_a/index.html", - "<a href='a_py.html'>a.py</a>", - "<span class='pc_cov'>67%</span>", - "<td class='right' data-ratio='2 3'>67%</td>", + '<a href="a_py.html">a.py</a>', + '<span class="pc_cov">67%</span>', + '<td class="right" data-ratio="2 3">67%</td>', ) clean("html_a") diff --git a/tests/farm/html/run_b_branch.py b/tests/farm/html/run_b_branch.py index c92252c..388b5c5 100644 --- a/tests/farm/html/run_b_branch.py +++ b/tests/farm/html/run_b_branch.py @@ -13,17 +13,17 @@ runfunc(html_it, rundir="src") # and check that certain key strings are in the output. compare("gold_b_branch", "html_b_branch", size_within=10, file_pattern="*.html") contains("html_b_branch/b_py.html", - "<span class='key'>if</span> <span class='nam'>x</span> <span class='op'><</span> <span class='num'>2</span>", - " <span class='nam'>a</span> <span class='op'>=</span> <span class='num'>3</span>", - "<span class='pc_cov'>70%</span>", - "<span class='annotate' title='no jump to this line number'>8</span>", - "<span class='annotate' title='no jump to this line number'>exit</span>", - "<span class='annotate' title='no jumps to these line numbers'>23 25</span>", + '<span class="key">if</span> <span class="nam">x</span> <span class="op"><</span> <span class="num">2</span>', + ' <span class="nam">a</span> <span class="op">=</span> <span class="num">3</span>', + '<span class="pc_cov">70%</span>', + '<span class="annotate" title="no jump to this line number">8</span>', + '<span class="annotate" title="no jump to this line number">exit</span>', + '<span class="annotate" title="no jumps to these line numbers">23 25</span>', ) contains("html_b_branch/index.html", - "<a href='b_py.html'>b.py</a>", - "<span class='pc_cov'>70%</span>", - "<td class='right' data-ratio='16 23'>70%</td>", + '<a href="b_py.html">b.py</a>', + '<span class="pc_cov">70%</span>', + '<td class="right" data-ratio="16 23">70%</td>', ) clean("html_b_branch") diff --git a/tests/farm/html/run_bom.py b/tests/farm/html/run_bom.py index 9694975..a6e6d8b 100644 --- a/tests/farm/html/run_bom.py +++ b/tests/farm/html/run_bom.py @@ -15,7 +15,7 @@ runfunc(html_it, rundir="src") # and check that certain key strings are in the output. compare("gold_bom", "html_bom", size_within=10, file_pattern="*.html") contains("html_bom/bom_py.html", - "<span class='str'>"3×4 = 12, ÷2 = 6±0"</span>", + '<span class="str">"3×4 = 12, ÷2 = 6±0"</span>', ) clean("html_bom") diff --git a/tests/farm/html/run_isolatin1.py b/tests/farm/html/run_isolatin1.py index bf3746d..b03cd25 100644 --- a/tests/farm/html/run_isolatin1.py +++ b/tests/farm/html/run_isolatin1.py @@ -15,7 +15,7 @@ runfunc(html_it, rundir="src") # and check that certain key strings are in the output. compare("gold_isolatin1", "html_isolatin1", size_within=10, file_pattern="*.html") contains("html_isolatin1/isolatin1_py.html", - "<span class='str'>"3×4 = 12, ÷2 = 6±0"</span>", + '<span class="str">"3×4 = 12, ÷2 = 6±0"</span>', ) clean("html_isolatin1") diff --git a/tests/farm/html/run_other.py b/tests/farm/html/run_other.py index 05efa0f..7c7f344 100644 --- a/tests/farm/html/run_other.py +++ b/tests/farm/html/run_other.py @@ -19,8 +19,8 @@ for p in glob.glob("html_other/*_other_py.html"): # and check that certain key strings are in the output. compare("gold_other", "html_other", size_within=10, file_pattern="*.html") contains("html_other/index.html", - "<a href='here_py.html'>here.py</a>", - "other_py.html'>", "other.py</a>", + '<a href="here_py.html">here.py</a>', + 'other_py.html">', 'other.py</a>', ) clean("html_other") diff --git a/tests/farm/html/run_partial.py b/tests/farm/html/run_partial.py index 3879079..7330bb2 100644 --- a/tests/farm/html/run_partial.py +++ b/tests/farm/html/run_partial.py @@ -15,17 +15,17 @@ runfunc(html_it, rundir="src") # and check that certain key strings are in the output. compare("gold_partial", "html_partial", size_within=10, file_pattern="*.html") contains("html_partial/partial_py.html", - "<p id='t5' class='stm run hide_run'>", - "<p id='t8' class='stm run hide_run'>", - "<p id='t11' class='stm run hide_run'>", + '<p id="t5" class="stm run hide_run">', + '<p id="t8" class="stm run hide_run">', + '<p id="t11" class="stm run hide_run">', # The "if 0" and "if 1" statements are optimized away. - "<p id='t14' class='pln'>", + '<p id="t14" class="pln">', ) contains("html_partial/index.html", - "<a href='partial_py.html'>partial.py</a>", + '<a href="partial_py.html">partial.py</a>', ) contains("html_partial/index.html", - "<span class='pc_cov'>100%</span>" + '<span class="pc_cov">100%</span>' ) clean("html_partial") diff --git a/tests/farm/html/run_styled.py b/tests/farm/html/run_styled.py index a18096a..ebfbf3b 100644 --- a/tests/farm/html/run_styled.py +++ b/tests/farm/html/run_styled.py @@ -14,15 +14,15 @@ runfunc(html_it, rundir="src") compare("gold_styled", "html_styled", size_within=10, file_pattern="*.html") compare("gold_styled", "html_styled", size_within=10, file_pattern="*.css") contains("html_styled/a_py.html", - "<link rel='stylesheet' href='extra.css' type='text/css'>", - "<span class='key'>if</span> <span class='num'>1</span> <span class='op'><</span> <span class='num'>2</span>", - " <span class='nam'>a</span> <span class='op'>=</span> <span class='num'>3</span>", - "<span class='pc_cov'>67%</span>" + '<link rel="stylesheet" href="extra.css" type="text/css">', + '<span class="key">if</span> <span class="num">1</span> <span class="op"><</span> <span class="num">2</span>', + ' <span class="nam">a</span> <span class="op">=</span> <span class="num">3</span>', + '<span class="pc_cov">67%</span>' ) contains("html_styled/index.html", - "<link rel='stylesheet' href='extra.css' type='text/css'>", - "<a href='a_py.html'>a.py</a>", - "<span class='pc_cov'>67%</span>" + '<link rel="stylesheet" href="extra.css" type="text/css">', + '<a href="a_py.html">a.py</a>', + '<span class="pc_cov">67%</span>' ) clean("html_styled") diff --git a/tests/farm/html/run_tabbed.py b/tests/farm/html/run_tabbed.py index 679db2e..3e8a900 100644 --- a/tests/farm/html/run_tabbed.py +++ b/tests/farm/html/run_tabbed.py @@ -13,11 +13,11 @@ runfunc(html_it, rundir="src") contains("src/tabbed.py", "\tif x:\t\t\t\t\t# look nice") contains("html_tabbed/tabbed_py.html", - "> <span class='key'>if</span> " - "<span class='nam'>x</span><span class='op'>:</span>" - " " - " " - "<span class='com'># look nice</span>" + '> <span class="key">if</span> ' + '<span class="nam">x</span><span class="op">:</span>' + ' ' + ' ' + '<span class="com"># look nice</span>' ) doesnt_contain("html_tabbed/tabbed_py.html", "\t") diff --git a/tests/farm/html/run_unicode.py b/tests/farm/html/run_unicode.py index ba34f63..455d016 100644 --- a/tests/farm/html/run_unicode.py +++ b/tests/farm/html/run_unicode.py @@ -13,12 +13,12 @@ runfunc(html_it, rundir="src") # and check that certain key strings are in the output. compare("gold_unicode", "html_unicode", size_within=10, file_pattern="*.html") contains("html_unicode/unicode_py.html", - "<span class='str'>"ʎd˙ǝbɐɹǝʌoɔ"</span>", + '<span class="str">"ʎd˙ǝbɐɹǝʌoɔ"</span>', ) contains_any("html_unicode/unicode_py.html", - "<span class='str'>"db40,dd00: x��"</span>", - "<span class='str'>"db40,dd00: x󠄀"</span>", + '<span class="str">"db40,dd00: x��"</span>', + '<span class="str">"db40,dd00: x󠄀"</span>', ) clean("html_unicode") 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..a4462ea 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): @@ -558,6 +560,114 @@ class ExceptionArcTest(CoverageTest): arcz_missing="67 7B", arcz_unpredicted="68") +class YieldTest(CoverageTest): + """Arc tests for generators.""" + + def test_yield_in_loop(self): + self.check_coverage("""\ + def gen(inp): + for n in inp: + yield n + + list(gen([1,2,3])) + """, + arcz=".1 .2 23 2. 32 15 5.", + arcz_missing="", + arcz_unpredicted="") + + def test_padded_yield_in_loop(self): + self.check_coverage("""\ + def gen(inp): + i = 2 + for n in inp: + i = 4 + yield n + i = 6 + i = 7 + + list(gen([1,2,3])) + """, + arcz=".1 19 9. .2 23 34 45 56 63 37 7.", + arcz_missing="", + arcz_unpredicted="") + + def test_bug_308(self): + self.check_coverage("""\ + def run(): + for i in range(10): + yield lambda: i + + for f in run(): + print(f()) + """, + arcz=".1 15 56 65 5. .2 23 32 2. .3 3-3", + arcz_missing="", + arcz_unpredicted="") + + self.check_coverage("""\ + def run(): + yield lambda: 100 + for i in range(10): + yield lambda: i + + for f in run(): + print(f()) + """, + arcz=".1 16 67 76 6. .2 23 34 43 3. 2-2 .4 4-4", + arcz_missing="", + arcz_unpredicted="") + + self.check_coverage("""\ + def run(): + yield lambda: 100 # no branch miss + + for f in run(): + print(f()) + """, + arcz=".1 14 45 54 4. .2 2. 2-2", + arcz_missing="", + arcz_unpredicted="") + + def test_bug_324(self): + # This code is tricky: the list() call pulls all the values from gen(), + # but each of them is a generator itself that is never iterated. As a + # result, the generator expression on line 3 is never entered or run. + self.check_coverage("""\ + def gen(inp): + for n in inp: + yield (i * 2 for i in range(n)) + + list(gen([1,2,3])) + """, + arcz= + ".1 15 5. " # The module level + ".2 23 32 2. " # The gen() function + ".3 3-3", # The generator expression + arcz_missing=".3 3-3", + arcz_unpredicted="") + + def test_coroutines(self): + self.check_coverage("""\ + def double_inputs(): + while [1]: # avoid compiler differences + x = yield + x *= 2 + yield x + + gen = double_inputs() + next(gen) + print(gen.send(10)) + next(gen) + print(gen.send(6)) + """, + arcz= + ".1 17 78 89 9A AB B. " + ".2 23 34 45 52 2.", + arcz_missing="2.", + arcz_unpredicted="") + self.assertEqual(self.stdout(), "20\n12\n") + + class MiscArcTest(CoverageTest): """Miscellaneous arc-measuring tests.""" @@ -575,6 +685,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 +737,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_cmdline.py b/tests/test_cmdline.py index 775e003..b616ed5 100644 --- a/tests/test_cmdline.py +++ b/tests/test_cmdline.py @@ -213,11 +213,18 @@ class CmdLineTest(BaseCmdLineTest): """) def test_combine(self): - # coverage combine + # coverage combine with args + self.cmd_executes("combine datadir1", """\ + .coverage() + .load() + .combine(["datadir1"]) + .save() + """) + # coverage combine without args self.cmd_executes("combine", """\ .coverage() .load() - .combine() + .combine(None) .save() """) 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_coverage.py b/tests/test_coverage.py index 35c7c25..3de381f 100644 --- a/tests/test_coverage.py +++ b/tests/test_coverage.py @@ -1672,7 +1672,7 @@ class ReportingTest(CoverageTest): # We don't make any temp files, but we need an empty directory to run the # tests in. - run_in_temp_dir = True + no_files_in_temp_dir = True def test_no_data_to_report_on_annotate(self): # Reporting with no data produces a nice message and no output dir. @@ -1680,10 +1680,6 @@ class ReportingTest(CoverageTest): self.command_line("annotate -d ann") self.assert_doesnt_exist("ann") - # CoverageTest will yell at us for using a temp directory with no files - # made. Instead of adding a way to shut it up, just make a file. - self.make_file("touch.txt", "") - def test_no_data_to_report_on_html(self): # Reporting with no data produces a nice message and no output dir. with self.assertRaisesRegex(CoverageException, "No data to report."): diff --git a/tests/test_data.py b/tests/test_data.py index 0549a3c..776f7b5 100644 --- a/tests/test_data.py +++ b/tests/test_data.py @@ -1,5 +1,7 @@ """Tests for coverage.data""" +import os + from coverage.backward import pickle from coverage.data import CoverageData from coverage.files import PathAliases @@ -36,10 +38,8 @@ X_PY_ARCS_3 = [(1, 2), (2, 3)] Y_PY_ARCS_3 = [(17, 23)] -class DataTest(CoverageTest): - """Test cases for coverage.data.""" - - run_in_temp_dir = False +class DataTestHelpers(CoverageTest): + """Test helpers for data tests.""" def assert_summary(self, covdata, summary, fullpath=False): """Check that the summary of `covdata` is `summary`.""" @@ -49,6 +49,12 @@ class DataTest(CoverageTest): """Check that `covdata`'s measured files are `measured`.""" self.assertCountEqual(covdata.measured_files(), measured) + +class DataTest(DataTestHelpers, CoverageTest): + """Test cases for coverage.data.""" + + run_in_temp_dir = False + def test_reading_empty(self): covdata = CoverageData() covdata.read() @@ -154,3 +160,29 @@ class DataTest(CoverageTest): covdata3, {'./a.py': 4, './sub/b.py': 2}, fullpath=True ) self.assert_measured_files(covdata3, ['./a.py', './sub/b.py']) + + +class DataTestInTempDir(DataTestHelpers, CoverageTest): + """Test cases for coverage.data.""" + + no_files_in_temp_dir = True + + def test_combining_from_different_directories(self): + covdata1 = CoverageData() + covdata1.add_line_data(DATA_1) + os.makedirs('cov1') + covdata1.write_file('cov1/.coverage.1') + + covdata2 = CoverageData() + covdata2.add_line_data(DATA_2) + os.makedirs('cov2') + covdata2.write_file('cov2/.coverage.2') + + covdata3 = CoverageData() + covdata3.combine_parallel_data(data_dirs=[ + 'cov1/', + 'cov2/', + ]) + + self.assert_summary(covdata3, SUMMARY_1_2) + self.assert_measured_files(covdata3, MEASURED_FILES_1_2) 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..6f0b294 100644 --- a/tests/test_html.py +++ b/tests/test_html.py @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- """Tests that HTML generation is awesome.""" +import datetime import os.path import re @@ -53,6 +54,23 @@ class HtmlTestHelpers(CoverageTest): with open(filename) as f: return f.read() + def get_html_index_content(self, scrub_time_stamp=True): + """Return the content of index.html. + + If `scrub_time_stamp` is true, then replace the timestamp with a + placeholder so that clocks don't matter. + + """ + with open("htmlcov/index.html") as f: + index = f.read() + if scrub_time_stamp: + index = re.sub( + r"created at \d{4}-\d{2}-\d{2} \d{2}:\d{2}", + r"created at YYYY-MM-DD HH:MM", + index, + ) + return index + class HtmlDeltaTest(HtmlTestHelpers, CoverageTest): """Tests of the HTML delta speed-ups.""" @@ -86,8 +104,7 @@ class HtmlDeltaTest(HtmlTestHelpers, CoverageTest): # In this case, helper1 changes because its source is different. self.create_initial_files() self.run_coverage() - with open("htmlcov/index.html") as f: - index1 = f.read() + index1 = self.get_html_index_content() self.remove_html_files() # Now change a file and do it again @@ -104,8 +121,7 @@ class HtmlDeltaTest(HtmlTestHelpers, CoverageTest): self.assert_exists("htmlcov/helper1_py.html") self.assert_doesnt_exist("htmlcov/main_file_py.html") self.assert_doesnt_exist("htmlcov/helper2_py.html") - with open("htmlcov/index.html") as f: - index2 = f.read() + index2 = self.get_html_index_content() self.assertMultiLineEqual(index1, index2) def test_html_delta_from_coverage_change(self): @@ -136,8 +152,7 @@ class HtmlDeltaTest(HtmlTestHelpers, CoverageTest): # changed. self.create_initial_files() self.run_coverage(covargs=dict(omit=[])) - with open("htmlcov/index.html") as f: - index1 = f.read() + index1 = self.get_html_index_content() self.remove_html_files() self.run_coverage(covargs=dict(omit=['xyzzy*'])) @@ -147,8 +162,7 @@ class HtmlDeltaTest(HtmlTestHelpers, CoverageTest): self.assert_exists("htmlcov/helper1_py.html") self.assert_exists("htmlcov/main_file_py.html") self.assert_exists("htmlcov/helper2_py.html") - with open("htmlcov/index.html") as f: - index2 = f.read() + index2 = self.get_html_index_content() self.assertMultiLineEqual(index1, index2) def test_html_delta_from_coverage_version_change(self): @@ -157,8 +171,7 @@ class HtmlDeltaTest(HtmlTestHelpers, CoverageTest): # changed. self.create_initial_files() self.run_coverage() - with open("htmlcov/index.html") as f: - index1 = f.read() + index1 = self.get_html_index_content() self.remove_html_files() # "Upgrade" coverage.py! @@ -171,8 +184,7 @@ class HtmlDeltaTest(HtmlTestHelpers, CoverageTest): self.assert_exists("htmlcov/helper1_py.html") self.assert_exists("htmlcov/main_file_py.html") self.assert_exists("htmlcov/helper2_py.html") - with open("htmlcov/index.html") as f: - index2 = f.read() + index2 = self.get_html_index_content() fixed_index2 = index2.replace("XYZZY", self.real_coverage_version) self.assertMultiLineEqual(index1, fixed_index2) @@ -183,8 +195,7 @@ class HtmlTitleTest(HtmlTestHelpers, CoverageTest): def test_default_title(self): self.create_initial_files() self.run_coverage() - with open("htmlcov/index.html") as f: - index = f.read() + index = self.get_html_index_content() self.assertIn("<title>Coverage report</title>", index) self.assertIn("<h1>Coverage report:", index) @@ -192,8 +203,7 @@ class HtmlTitleTest(HtmlTestHelpers, CoverageTest): self.create_initial_files() self.make_file(".coveragerc", "[html]\ntitle = Metrics & stuff!\n") self.run_coverage() - with open("htmlcov/index.html") as f: - index = f.read() + index = self.get_html_index_content() self.assertIn("<title>Metrics & stuff!</title>", index) self.assertIn("<h1>Metrics & stuff!:", index) @@ -203,8 +213,7 @@ class HtmlTitleTest(HtmlTestHelpers, CoverageTest): "[html]\ntitle = «ταБЬℓσ» numbers" ) self.run_coverage() - with open("htmlcov/index.html") as f: - index = f.read() + index = self.get_html_index_content() self.assertIn( "<title>«ταБЬℓσ»" " numbers", index @@ -218,8 +227,7 @@ class HtmlTitleTest(HtmlTestHelpers, CoverageTest): self.create_initial_files() self.make_file(".coveragerc", "[html]\ntitle = Good title\n") self.run_coverage(htmlargs=dict(title="«ταБЬℓσ» & stüff!")) - with open("htmlcov/index.html") as f: - index = f.read() + index = self.get_html_index_content() self.assertIn( "<title>«ταБЬℓσ»" " & stüff!</title>", index @@ -322,8 +330,18 @@ class HtmlWithUnparsableFilesTest(HtmlTestHelpers, CoverageTest): expected = "# Isn't this great?Ë!" 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): +class HtmlTest(HtmlTestHelpers, CoverageTest): """Moar HTML tests.""" def test_missing_source_file_incorrect_message(self): @@ -353,6 +371,32 @@ class HtmlTest(CoverageTest): self.assert_exists("htmlcov/afile.html") self.assert_exists("htmlcov/afile_py.html") + def test_has_date_stamp_in_files(self): + self.create_initial_files() + self.run_coverage() + + with open("htmlcov/index.html") as f: + self.assert_correct_timestamp(f.read()) + with open("htmlcov/main_file_py.html") as f: + self.assert_correct_timestamp(f.read()) + + def assert_correct_timestamp(self, html): + """Extract the timestamp from `html`, and assert it is recent.""" + timestamp_pat = r"created at (\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2})" + m = re.search(timestamp_pat, html) + self.assertTrue(m, "Didn't find a timestamp!") + timestamp = datetime.datetime(*map(int, m.groups())) + age = datetime.datetime.now() - timestamp + # Python2.6 doesn't have total_seconds :( + self.assertEqual(age.days, 0) + # The timestamp only records the minute, so the delta could be from + # 12:00 to 12:01:59, or two minutes. + self.assertLessEqual( + abs(age.seconds), + 120, + "Timestamp is wrong: {0}".format(timestamp) + ) + class HtmlStaticFileTest(CoverageTest): """Tests of the static file copying for the HTML report.""" 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_parser.py b/tests/test_parser.py index 244d4c7..81916a9 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -34,6 +34,22 @@ class PythonParserTest(CoverageTest): 2:1, 3:1, 4:2, 5:1, 7:1, 9:1, 10:1 }) + def test_generator_exit_counts(self): + # https://bitbucket.org/ned/coveragepy/issue/324/yield-in-loop-confuses-branch-coverage + parser = self.parse_source("""\ + def gen(input): + for n in inp: + yield (i * 2 for i in range(n)) + + list(gen([1,2,3])) + """) + self.assertEqual(parser.exit_counts(), { + 1:1, # def -> list + 2:2, # for -> yield; for -> exit + 3:2, # yield -> for; genexp exit + 5:1, # list -> exit + }) + def test_try_except(self): parser = self.parse_source("""\ try: 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) @@ -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 |