Commits

Ned Batchelder  committed 811ed58

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

  • Participants
  • Parent commits cdc34b7

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.
 

File 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.

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