Ned Batchelder avatar Ned Batchelder committed 811ed58

The current directory auto-inserted into sys.path is now absolute, to better mimic Python execution.

Comments (0)

Files changed (3)

 - Modules can now be run directly using ``coverage run -m modulename``, to
   mirror Python's ``-m`` flag.  Closes `issue 95`_, thanks, Brandon Rhodes.
 
+- ``coverage run`` didn't emulate Python accurately in one detail: the
+  current directory inserted into ``sys.path`` was relative rather than
+  absolute. This is now fixed.
+
 - A little bit of Jython support: `coverage run` can now measure Jython
   execution by adapting when $py.class files are traced. Thanks, Adi Roiban.
 

coverage/execfile.py

     old_argv = sys.argv
     old_path0 = sys.path[0]
     sys.argv = args
-    sys.path[0] = os.path.dirname(filename)
+    sys.path[0] = os.path.abspath(os.path.dirname(filename))
 
     try:
         # Open the source file.

test/test_process.py

 sys.path.insert(0, os.path.split(__file__)[0]) # Force relative import for Py3k
 from coveragetest import CoverageTest
 
+here = os.path.dirname(__file__)
 
 class ProcessTest(CoverageTest):
     """Tests of the per-process behavior of coverage.py."""
         self.assertEqual(status, status2)
         self.assertEqual(status, 0)
 
+    def test_coverage_run_is_like_python(self):
+        tryfile = os.path.join(here, "try_execfile.py")
+        self.make_file("run_me.py", open(tryfile).read())
+        out = self.run_command("coverage run run_me.py")
+        out2 = self.run_command("python run_me.py")
+        self.assertMultiLineEqual(out, out2)
 
     if hasattr(os, 'fork'):
         def test_fork(self):
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.