diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2011-02-07 08:37:14 -0500 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2011-02-07 08:37:14 -0500 |
commit | 2419ccf5218042b6caf9e972873d4815490c2690 (patch) | |
tree | caa84ed53ee0f10f31efc23057eabfb0c1bb0d48 | |
parent | 1a347c5401d6fe8b6f43307339fca40a1ef49bc6 (diff) | |
download | python-coveragepy-git-2419ccf5218042b6caf9e972873d4815490c2690.tar.gz |
Don't warn about not collecting data if we never ran any code in the first place.
-rw-r--r-- | coverage/cmdline.py | 17 | ||||
-rw-r--r-- | coverage/control.py | 21 | ||||
-rw-r--r-- | test/test_cmdline.py | 26 | ||||
-rw-r--r-- | test/test_process.py | 10 |
4 files changed, 50 insertions, 24 deletions
diff --git a/coverage/cmdline.py b/coverage/cmdline.py index cdcf3178..b739afab 100644 --- a/coverage/cmdline.py +++ b/coverage/cmdline.py @@ -4,7 +4,7 @@ import optparse, re, sys, traceback from coverage.backward import sorted # pylint: disable=W0622 from coverage.execfile import run_python_file, run_python_module -from coverage.misc import CoverageException, ExceptionDuringRun +from coverage.misc import CoverageException, ExceptionDuringRun, NoSource class Opts(object): @@ -511,13 +511,18 @@ class CoverageScript(object): if 'execute' in options.actions: # Run the script. self.coverage.start() + never_run = False try: - if options.module: - self.run_python_module(args[0], args) - else: - self.run_python_file(args[0], args) + try: + if options.module: + self.run_python_module(args[0], args) + else: + self.run_python_file(args[0], args) + except NoSource: + never_run = True + raise finally: - self.coverage.stop() + self.coverage.stop(never_run) self.coverage.save() if 'combine' in options.actions: diff --git a/coverage/control.py b/coverage/control.py index 514f23d9..6fc1b98d 100644 --- a/coverage/control.py +++ b/coverage/control.py @@ -172,6 +172,10 @@ class coverage(object): # Only _harvest_data once per measurement cycle. self._harvested = False + + # When stop() is called, we can tell it that in fact no product code + # was run, to make the warnings more reasonable. + self._never_run = False # Set the reporting precision. Numbers.set_precision(self.config.precision) @@ -361,8 +365,14 @@ class coverage(object): self._harvested = False self.collector.start() - def stop(self): - """Stop measuring code coverage.""" + def stop(self, never_run=False): + """Stop measuring code coverage. + + Set `never_run` to True to indicate that no product code was run, so + we don't warn unnecessarily. + + """ + self._never_run = never_run self.collector.stop() self._harvest_data() @@ -444,9 +454,10 @@ class coverage(object): 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.") + if not self._never_run: + 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: diff --git a/test/test_cmdline.py b/test/test_cmdline.py index d4cc763d..f0f9d22f 100644 --- a/test/test_cmdline.py +++ b/test/test_cmdline.py @@ -111,7 +111,7 @@ class ClassicCmdLineTest(CmdLineTest): .load() .start() .run_python_file('foo.py', ['foo.py']) - .stop() + .stop(False) .save() """) # -e -x calls coverage.erase first. @@ -120,7 +120,7 @@ class ClassicCmdLineTest(CmdLineTest): .erase() .start() .run_python_file('foo.py', ['foo.py']) - .stop() + .stop(False) .save() """) # --timid sets a flag, and program arguments get passed through. @@ -129,7 +129,7 @@ class ClassicCmdLineTest(CmdLineTest): .load() .start() .run_python_file('foo.py', ['foo.py', 'abc', '123']) - .stop() + .stop(False) .save() """) # -L sets a flag, and flags for the program don't confuse us. @@ -138,7 +138,7 @@ class ClassicCmdLineTest(CmdLineTest): .load() .start() .run_python_file('foo.py', ['foo.py', '-a', '-b']) - .stop() + .stop(False) .save() """) @@ -470,7 +470,7 @@ class NewCmdLineTest(CmdLineTest): .erase() .start() .run_python_file('foo.py', ['foo.py']) - .stop() + .stop(False) .save() """) self.cmd_executes("run --rcfile=myrc.rc foo.py", """\ @@ -478,7 +478,7 @@ class NewCmdLineTest(CmdLineTest): .erase() .start() .run_python_file('foo.py', ['foo.py']) - .stop() + .stop(False) .save() """) self.cmd_executes("run --include=pre1,pre2 foo.py", """\ @@ -486,7 +486,7 @@ class NewCmdLineTest(CmdLineTest): .erase() .start() .run_python_file('foo.py', ['foo.py']) - .stop() + .stop(False) .save() """) self.cmd_executes("run --omit=opre1,opre2 foo.py", """\ @@ -494,7 +494,7 @@ class NewCmdLineTest(CmdLineTest): .erase() .start() .run_python_file('foo.py', ['foo.py']) - .stop() + .stop(False) .save() """) self.cmd_executes("run --include=pre1,pre2 --omit=opre1,opre2 foo.py", @@ -506,7 +506,7 @@ class NewCmdLineTest(CmdLineTest): .erase() .start() .run_python_file('foo.py', ['foo.py']) - .stop() + .stop(False) .save() """) self.cmd_executes("run --source=quux,hi.there,/home/bar foo.py", @@ -518,7 +518,7 @@ class NewCmdLineTest(CmdLineTest): .erase() .start() .run_python_file('foo.py', ['foo.py']) - .stop() + .stop(False) .save() """) @@ -528,7 +528,7 @@ class NewCmdLineTest(CmdLineTest): .erase() .start() .run_python_module('mymodule', ['mymodule']) - .stop() + .stop(False) .save() """) self.cmd_executes("run -m mymodule -qq arg1 arg2", """\ @@ -536,7 +536,7 @@ class NewCmdLineTest(CmdLineTest): .erase() .start() .run_python_module('mymodule', ['mymodule', '-qq', 'arg1', 'arg2']) - .stop() + .stop(False) .save() """) self.cmd_executes("run --branch -m mymodule", """\ @@ -544,7 +544,7 @@ class NewCmdLineTest(CmdLineTest): .erase() .start() .run_python_module('mymodule', ['mymodule']) - .stop() + .stop(False) .save() """) self.cmd_executes_same("run -m mymodule", "run --module mymodule") diff --git a/test/test_process.py b/test/test_process.py index acfb8896..8a47bfc7 100644 --- a/test/test_process.py +++ b/test/test_process.py @@ -275,3 +275,13 @@ class ProcessTest(CoverageTest): Coverage.py warning: Module quux was never imported. Coverage.py warning: No data was collected. """) in out) + + def test_warnings_if_never_run(self): + out = self.run_command("coverage run i_dont_exist.py") + self.assertTrue("No file to run: 'i_dont_exist.py'" in out) + self.assertTrue("warning" not in out) + + out = self.run_command("coverage run -m no_such_module") + self.assertTrue("No module named no_such_module" in out) + self.assertTrue("warning" not in out) + |