Commits

Ned Batchelder  committed 03f351b

Don't warn about not collecting data if we never ran any code in the first place.

  • Participants
  • Parent commits dbe25dc

Comments (0)

Files changed (4)

File coverage/cmdline.py

 
 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):
         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:

File coverage/control.py

 
         # 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)
         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()
 
                 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:

File test/test_cmdline.py

             .load()
             .start()
             .run_python_file('foo.py', ['foo.py'])
-            .stop()
+            .stop(False)
             .save()
             """)
         # -e -x calls coverage.erase first.
             .erase()
             .start()
             .run_python_file('foo.py', ['foo.py'])
-            .stop()
+            .stop(False)
             .save()
             """)
         # --timid sets a flag, and program arguments get passed through.
             .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.
             .load()
             .start()
             .run_python_file('foo.py', ['foo.py', '-a', '-b'])
-            .stop()
+            .stop(False)
             .save()
             """)
 
             .erase()
             .start()
             .run_python_file('foo.py', ['foo.py'])
-            .stop()
+            .stop(False)
             .save()
             """)
         self.cmd_executes("run --rcfile=myrc.rc foo.py", """\
             .erase()
             .start()
             .run_python_file('foo.py', ['foo.py'])
-            .stop()
+            .stop(False)
             .save()
             """)
         self.cmd_executes("run --include=pre1,pre2 foo.py", """\
             .erase()
             .start()
             .run_python_file('foo.py', ['foo.py'])
-            .stop()
+            .stop(False)
             .save()
             """)
         self.cmd_executes("run --omit=opre1,opre2 foo.py", """\
             .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",
             .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",
             .erase()
             .start()
             .run_python_file('foo.py', ['foo.py'])
-            .stop()
+            .stop(False)
             .save()
             """)
 
             .erase()
             .start()
             .run_python_module('mymodule', ['mymodule'])
-            .stop()
+            .stop(False)
             .save()
             """)
         self.cmd_executes("run -m mymodule -qq arg1 arg2", """\
             .erase()
             .start()
             .run_python_module('mymodule', ['mymodule', '-qq', 'arg1', 'arg2'])
-            .stop()
+            .stop(False)
             .save()
             """)
         self.cmd_executes("run --branch -m mymodule", """\
             .erase()
             .start()
             .run_python_module('mymodule', ['mymodule'])
-            .stop()
+            .stop(False)
             .save()
             """)
         self.cmd_executes_same("run -m mymodule", "run --module mymodule")

File test/test_process.py

             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)
+