Ned Batchelder avatar Ned Batchelder committed 5ae8bd3

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

Comments (0)

Files changed (3)


 import imp, os, sys
 from coverage.backward import exec_function
+from coverage.misc import NoSource
     sys.path[0] = os.path.dirname(filename)
-        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__)
         # Restore the old __main__


         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")
+        self.assert_matches(out, "No file to run: .*")
+        self.assert_("Traceback" not in out)
 class RecursionTest(CoverageTest):
     """Check what happens when recursive code gets near limits."""


 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('', [''])
         self.assertEqual(self.stdout(), "Hello, world!\n"*3)
+    def test_no_such_file(self):
+        self.assertRaises(NoSource, run_python_file, "", [])
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
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.