Ned Batchelder avatar Ned Batchelder committed 5ae8bd3

Report errors finding source files to execute more nicely. Fixes issue #23.

Comments (0)

Files changed (3)

coverage/execfile.py

 import imp, os, sys
 
 from coverage.backward import exec_function
+from coverage.misc import NoSource
 
 
 try:
     sys.path[0] = os.path.dirname(filename)
 
     try:
-        source = open(filename, 'rU').read()
+        try:
+            source = open(filename, 'rU').read()
+        except IOError:
+            raise NoSource("No file to run: %r" % filename)
         exec_function(source, filename, main_mod.__dict__)
     finally:
         # Restore the old __main__

test/test_coverage.py

         self.assert_matches(out, "No source for code: '.*fleeting'")
         self.assert_("Traceback" not in out)
 
+    def test_running_missing_file(self):
+        out = self.run_command("coverage run xyzzy.py")
+        self.assert_matches(out, "No file to run: .*xyzzy.py")
+        self.assert_("Traceback" not in out)
+
+
 class RecursionTest(CoverageTest):
     """Check what happens when recursive code gets near limits."""
 

test/test_execfile.py

 import os, sys
 
 from coverage.execfile import run_python_file
+from coverage.misc import NoSource
 
 sys.path.insert(0, os.path.split(__file__)[0]) # Force relative import for Py3k
 from coveragetest import CoverageTest
             run_python_file('nl.py', ['nl.py'])
         self.assertEqual(self.stdout(), "Hello, world!\n"*3)
 
+    def test_no_such_file(self):
+        self.assertRaises(NoSource, run_python_file, "xyzzy.py", [])
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.