diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2010-06-23 22:30:35 -0400 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2010-06-23 22:30:35 -0400 |
commit | 8ecf012d6bc4061cca7471b32f86ef5efa871ac0 (patch) | |
tree | d2915ecafaa9deca02b4822bae3b2538783671f9 | |
parent | d0755b95a5f3c1745a20644d4c0703bc639e518d (diff) | |
download | python-coveragepy-8ecf012d6bc4061cca7471b32f86ef5efa871ac0.tar.gz |
Move the warnings to get them in more cases, and test one of those cases.
-rw-r--r-- | coverage/control.py | 42 | ||||
-rw-r--r-- | test/backtest.py | 2 | ||||
-rw-r--r-- | test/test_process.py | 17 |
3 files changed, 44 insertions, 17 deletions
diff --git a/coverage/control.py b/coverage/control.py index 1268b3d..578a853 100644 --- a/coverage/control.py +++ b/coverage/control.py @@ -161,6 +161,9 @@ class coverage(object): self.pylib_match = self.cover_match = None self.include_match = self.omit_match = None + # Only _harvest_data once per measurement cycle. + self._harvested = False + def canonical_dir(self, f): """Return the canonical directory of the file `f`.""" return os.path.split(self.file_locator.canonical_filename(f))[0] @@ -238,7 +241,7 @@ class coverage(object): def _warn(self, msg): """Use `msg` as a warning.""" - sys.stderr.write("Warning: " + msg + "\n") + sys.stderr.write("Coverage.py warning: " + msg + "\n") def _abs_files(self, files): """Return a list of absolute file names for the names in `files`.""" @@ -317,24 +320,14 @@ class coverage(object): if self.omit: self.omit_match = FnmatchMatcher(self.omit) + self._harvested = False self.collector.start() def stop(self): """Stop measuring code coverage.""" self.collector.stop() - - # If there are still entries in the source_pkgs list, then we never - # encountered those packages. - for pkg in self.source_pkgs: - self._warn("Source module %s was never encountered." % pkg) - self._harvest_data() - # Find out if we got any data. - summary = self.data.summary() - if not summary: - self._warn("No data was collected.") - def erase(self): """Erase previously-collected coverage data. @@ -396,10 +389,27 @@ class coverage(object): self.data.combine_parallel_data() def _harvest_data(self): - """Get the collected data and reset the collector.""" - self.data.add_line_data(self.collector.get_line_data()) - self.data.add_arc_data(self.collector.get_arc_data()) - self.collector.reset() + """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("Source module %s was never encountered." % pkg) + + # Find out if we got any data. + summary = self.data.summary() + if not summary: + self._warn("No data was collected.") + + self._harvested = True # Backward compatibility with version 1. def analysis(self, morf): diff --git a/test/backtest.py b/test/backtest.py index 05a1e14..e8d8366 100644 --- a/test/backtest.py +++ b/test/backtest.py @@ -18,6 +18,7 @@ except ImportError: """ _, stdouterr = os.popen4(cmd) return status, stdouterr.read() + else: def run_command(cmd, status=0): """Run a command in a subprocess. @@ -25,7 +26,6 @@ else: Returns the exit status code and the combined stdout and stderr. """ - proc = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT diff --git a/test/test_process.py b/test/test_process.py index d59d072..684a5de 100644 --- a/test/test_process.py +++ b/test/test_process.py @@ -243,3 +243,20 @@ class ProcessTest(CoverageTest): data = coverage.CoverageData() data.read_file(".coverage") self.assertEqual(data.summary()['fork.py'], 9) + + def test_warnings(self): + self.make_file("hello.py", """\ + import sys, os + print("Hello") + """) + out = self.run_command("coverage run --source=sys,xyzzy hello.py") + + # This output is not in the same order it appears in real command line + # output, but this is how it appears in the combined stdout/stderr that + # run_command gives us. + self.assertMultiLineEqual(out, textwrap.dedent("""\ + Hello + Coverage.py warning: Module sys has no python source. + Coverage.py warning: Source module xyzzy was never encountered. + Coverage.py warning: No data was collected. + """)) |