def get_data(self): """Get the collected data and reset the collector. Also warn about various problems collecting data. Returns a :class:`coverage.CoverageData`, the collected coverage data. .. versionadded:: 4.0 """ self._init() if not self._measured: return self.collector.save_data(self.data) # If there are still entries in the source_pkgs list, then we never # encountered those packages. if self._warn_unimported_source: for pkg in self.source_pkgs: if pkg not in sys.modules: self._warn("Module %s was never imported." % pkg) elif not ( hasattr(sys.modules[pkg], '__file__') and os.path.exists(sys.modules[pkg].__file__) ): self._warn("Module %s has no Python source." % pkg) else: self._warn("Module %s was previously imported, but not measured." % pkg) # Find out if we got any data. if not self.data and self._warn_no_data: self._warn("No data was collected.") # Find files that were never executed at all. for src in self.source: for py_file in find_python_files(src): py_file = files.canonical_filename(py_file) if self.omit_match and self.omit_match.match(py_file): # Turns out this file was omitted, so don't pull it back # in as unexecuted. continue self.data.touch_file(py_file) # Add run information. self.data.add_run_info( brief_sys=" ".join([ platform.python_implementation(), platform.python_version(), platform.system(), ]) ) if self.config.note: self.data.add_run_info(note=self.config.note) self._measured = False return self.data
def _harvest_data(self): """Get the collected data and reset the collector. Also warn about various problems collecting data. """ if not self._measured: return self.data.add_line_data(self.collector.get_line_data()) self.data.add_arc_data(self.collector.get_arc_data()) self.collector.reset() if self._warn_unimported_source: for pkg in self.source_pkgs: self._warn('Module %s was never imported.' % pkg) summary = self.data.summary() if not summary and self._warn_no_data: self._warn('No data was collected.') for src in self.source: for py_file in find_python_files(src): py_file = self.file_locator.canonical_filename(py_file) if self.omit_match and self.omit_match.match(py_file): continue self.data.touch_file(py_file) self._measured = False
def _harvest_data(self): """Get the collected data and reset the collector. Also warn about various problems collecting data. """ if not self._harvested: self.data.add_line_data(self.collector.get_line_data()) self.data.add_arc_data(self.collector.get_arc_data()) self.collector.reset() # If there are still entries in the source_pkgs list, then we never # encountered those packages. for pkg in self.source_pkgs: self._warn("Module %s was never imported." % pkg) # Find out if we got any data. summary = self.data.summary() if not summary and self._warn_no_data: self._warn("No data was collected.") # Find files that were never executed at all. for src in self.source: for py_file in find_python_files(src): py_file = self.file_locator.canonical_filename(py_file) self.data.touch_file(py_file) self._harvested = True
def _harvest_data(self): """Get the collected data and reset the collector. Also warn about various problems collecting data. """ if not self._harvested: self.data.add_line_data(self.collector.get_line_data()) self.data.add_arc_data(self.collector.get_arc_data()) self.collector.reset() # If there are still entries in the source_pkgs list, then we never # encountered those packages. for pkg in self.source_pkgs: self._warn("Module %s was never imported." % pkg) # Find out if we got any data. summary = self.data.summary() if not summary: self._warn("No data was collected.") # Find files that were never executed at all. for src in self.source: for py_file in find_python_files(src): self.data.touch_file(py_file) self._harvested = True
def get_data(self): """Get the collected data and reset the collector. Also warn about various problems collecting data. Returns: :class:`CoverageData`: the collected coverage data. """ self._init() if not self._measured: return def abs_file_dict(d): """Return a dict like d, but with keys modified by `abs_file`.""" return dict((abs_file(k), v) for k,v in iitems(d)) # TODO: seems like this parallel structure is getting kinda old... self.data.add_lines(abs_file_dict(self.collector.get_line_data())) self.data.add_arcs(abs_file_dict(self.collector.get_arc_data())) self.data.add_plugins(abs_file_dict(self.collector.get_plugin_data())) self.collector.reset() # If there are still entries in the source_pkgs list, then we never # encountered those packages. if self._warn_unimported_source: for pkg in self.source_pkgs: if pkg not in sys.modules: self._warn("Module %s was never imported." % pkg) elif not ( hasattr(sys.modules[pkg], '__file__') and os.path.exists(sys.modules[pkg].__file__) ): self._warn("Module %s has no Python source." % pkg) else: self._warn( "Module %s was previously imported, " "but not measured." % pkg ) # Find out if we got any data. summary = self.data.summary() if not summary and self._warn_no_data: self._warn("No data was collected.") # Find files that were never executed at all. for src in self.source: for py_file in find_python_files(src): py_file = files.canonical_filename(py_file) if self.omit_match and self.omit_match.match(py_file): # Turns out this file was omitted, so don't pull it back # in as unexecuted. continue self.data.touch_file(py_file) self._measured = False return self.data
def _harvest_data(self): """Get the collected data and reset the collector. Also warn about various problems collecting data. """ self._init() if not self._measured: return def abs_file_dict(d): """Return a dict like d, but with keys modified by `abs_file`.""" return dict((abs_file(k), v) for k,v in iitems(d)) # TODO: seems like this parallel structure is getting kinda old... self.data.add_line_data(abs_file_dict(self.collector.get_line_data())) self.data.add_arc_data(abs_file_dict(self.collector.get_arc_data())) self.data.add_plugin_data(abs_file_dict(self.collector.get_plugin_data())) self.collector.reset() # If there are still entries in the source_pkgs list, then we never # encountered those packages. if self._warn_unimported_source: for pkg in self.source_pkgs: if pkg not in sys.modules: self._warn("Module %s was never imported." % pkg) elif not ( hasattr(sys.modules[pkg], '__file__') and os.path.exists(sys.modules[pkg].__file__) ): self._warn("Module %s has no Python source." % pkg) else: self._warn( "Module %s was previously imported, " "but not measured." % pkg ) # Find out if we got any data. summary = self.data.summary() if not summary and self._warn_no_data: self._warn("No data was collected.") # Find files that were never executed at all. for src in self.source: for py_file in find_python_files(src): py_file = files.canonical_filename(py_file) if self.omit_match and self.omit_match.match(py_file): # Turns out this file was omitted, so don't pull it back # in as unexecuted. continue self.data.touch_file(py_file) self._measured = False
def _harvest_data(self): """Get the collected data and reset the collector. Also warn about various problems collecting data. """ self._init() if not self._measured: return # TODO: seems like this parallel structure is getting kinda old... self.data.add_line_data(self.collector.get_line_data()) self.data.add_arc_data(self.collector.get_arc_data()) self.data.add_plugin_data(self.collector.get_plugin_data()) self.collector.reset() # If there are still entries in the source_pkgs list, then we never # encountered those packages. if self._warn_unimported_source: for pkg in self.source_pkgs: if pkg not in sys.modules: self._warn("Module %s was never imported." % pkg) elif not ( hasattr(sys.modules[pkg], '__file__') and os.path.exists(sys.modules[pkg].__file__) ): self._warn("Module %s has no Python source." % pkg) else: raise AssertionError( "Unexpected third case: name = %s, " "object = %r, " "__file__ = %s" % ( pkg, sys.modules[pkg], sys.modules[pkg].__file__ ) ) # Find out if we got any data. summary = self.data.summary() if not summary and self._warn_no_data: self._warn("No data was collected.") # Find files that were never executed at all. for src in self.source: for py_file in find_python_files(src): py_file = self.file_locator.canonical_filename(py_file) if self.omit_match and self.omit_match.match(py_file): # Turns out this file was omitted, so don't pull it back # in as unexecuted. continue self.data.touch_file(py_file) self._measured = False
def test_find_python_files(self): self.make_file("sub/a.py") self.make_file("sub/b.py") self.make_file("sub/x.c") # nope: not .py self.make_file("sub/ssub/__init__.py") self.make_file("sub/ssub/s.py") self.make_file("sub/ssub/~s.py") # nope: editor effluvia self.make_file("sub/lab/exp.py") # nope: no __init__.py py_files = set(find_python_files("sub")) self.assert_same_files(py_files, [ "sub/a.py", "sub/b.py", "sub/ssub/__init__.py", "sub/ssub/s.py", ])
def test_find_python_files(self): self.make_file("sub/a.py") self.make_file("sub/b.py") self.make_file("sub/x.c") # nope: not .py self.make_file("sub/ssub/__init__.py") self.make_file("sub/ssub/s.py") self.make_file("sub/ssub/~s.py") # nope: editor effluvia self.make_file("sub/lab/exp.py") # nope: no __init__.py self.make_file("sub/windows.pyw") py_files = set(find_python_files("sub")) self.assert_same_files(py_files, [ "sub/a.py", "sub/b.py", "sub/ssub/__init__.py", "sub/ssub/s.py", "sub/windows.pyw", ])
def _find_unexecuted_files(self, src_dir): """Find unexecuted files in `src_dir`. Search for files in `src_dir` that are probably importable, and add them as unexecuted files in `self.data`. """ for py_file in find_python_files(src_dir): py_file = files.canonical_filename(py_file) if self.omit_match and self.omit_match.match(py_file): # Turns out this file was omitted, so don't pull it back # in as unexecuted. continue self.data.touch_file(py_file)
def _find_unexecuted_files(self, src_dir): """Find unexecuted files in `src_dir`. Search for files in `src_dir` that are probably importable, and add them as unexecuted files in `self.data`. """ py_files = ((py_file, None) for py_file in find_python_files(src_dir)) plugin_files = self._find_plugin_files(src_dir) for file_path, plugin_name in itertools.chain(py_files, plugin_files): file_path = canonical_filename(file_path) if self.omit_match and self.omit_match.match(file_path): # Turns out this file was omitted, so don't pull it back # in as unexecuted. continue self.data.touch_file(file_path, plugin_name)
def _find_executable_files(self, src_dir): """Find executable files in `src_dir`. Search for files in `src_dir` that can be executed because they are probably importable. Don't include ones that have been omitted by the configuration. Yield the file path, and the plugin name that handles the file. """ py_files = ((py_file, None) for py_file in find_python_files(src_dir)) plugin_files = self._find_plugin_files(src_dir) for file_path, plugin_name in itertools.chain(py_files, plugin_files): file_path = canonical_filename(file_path) if self.omit_match and self.omit_match.match(file_path): # Turns out this file was omitted, so don't pull it back # in as unexecuted. continue yield file_path, plugin_name
def _harvest_data(self): if not self._measured: return self.data.add_line_data(self.collector.get_line_data()) self.data.add_arc_data(self.collector.get_arc_data()) self.collector.reset() if self._warn_unimported_source: for pkg in self.source_pkgs: self._warn('Module %s was never imported.' % pkg) summary = self.data.summary() if not summary and self._warn_no_data: self._warn('No data was collected.') for src in self.source: for py_file in find_python_files(src): py_file = self.file_locator.canonical_filename(py_file) if self.omit_match and self.omit_match.match(py_file): continue self.data.touch_file(py_file) self._measured = False
def _harvest_data(self): """Get the collected data and reset the collector. Also warn about various problems collecting data. """ if not self._measured: return # TODO: seems like this parallel structure is getting kinda old... self.data.add_line_data(self.collector.get_line_data()) self.data.add_arc_data(self.collector.get_arc_data()) self.data.add_plugin_data(self.collector.get_plugin_data()) self.collector.reset() # If there are still entries in the source_pkgs list, then we never # encountered those packages. if self._warn_unimported_source: for pkg in self.source_pkgs: self._warn("Module %s was never imported." % pkg) # Find out if we got any data. summary = self.data.summary() if not summary and self._warn_no_data: self._warn("No data was collected.") # Find files that were never executed at all. for src in self.source: for py_file in find_python_files(src): py_file = self.file_locator.canonical_filename(py_file) if self.omit_match and self.omit_match.match(py_file): # Turns out this file was omitted, so don't pull it back # in as unexecuted. continue self.data.touch_file(py_file) self._measured = False
def python_files(folder): _skiplist = skiplist() for fname in find_python_files(folder): f = valid_file(fname, _skiplist) if f: yield f