diff options
-rw-r--r-- | coverage/execfile.py | 6 | ||||
-rw-r--r-- | test/test_process.py | 6 | ||||
-rw-r--r-- | test/try_execfile.py | 5 |
3 files changed, 14 insertions, 3 deletions
diff --git a/coverage/execfile.py b/coverage/execfile.py index 71227b71..1c0048ff 100644 --- a/coverage/execfile.py +++ b/coverage/execfile.py @@ -65,6 +65,7 @@ def run_python_module(modulename, args): openfile.close() # Finally, hand the file off to run_python_file for execution. + args[0] = pathname run_python_file(pathname, args, package=packagename) @@ -89,7 +90,10 @@ def run_python_file(filename, args, package=None): old_argv = sys.argv old_path0 = sys.path[0] sys.argv = args - sys.path[0] = os.path.abspath(os.path.dirname(filename)) + if package: + sys.path[0] = '' + else: + sys.path[0] = os.path.abspath(os.path.dirname(filename)) try: # Open the source file. diff --git a/test/test_process.py b/test/test_process.py index 4cf5524f..085ac130 100644 --- a/test/test_process.py +++ b/test/test_process.py @@ -279,6 +279,12 @@ class ProcessTest(CoverageTest): out2 = self.run_command("python run_me.py") self.assertMultiLineEqual(out, out2) + def test_coverage_run_dashm_is_like_python_dashm(self): + # These -m commands assume the coverage tree is on the path. + out = self.run_command("coverage run -m test.try_execfile") + out2 = self.run_command("python -m test.try_execfile") + self.assertMultiLineEqual(out, out2) + if hasattr(os, 'fork'): def test_fork(self): self.make_file("fork.py", """\ diff --git a/test/try_execfile.py b/test/try_execfile.py index d7ea4398..825fdb70 100644 --- a/test/try_execfile.py +++ b/test/try_execfile.py @@ -1,6 +1,6 @@ """Test file for run_python_file.""" -import pprint, sys +import os, pprint, sys DATA = "xyzzy" @@ -18,11 +18,12 @@ globals_to_check = { '__doc__': __doc__, '__builtins__.has_open': hasattr(__builtins__, 'open'), '__builtins__.dir': dir(__builtins__), + '__package__': __package__, 'DATA': DATA, 'FN_VAL': FN_VAL, '__main__.DATA': getattr(__main__, "DATA", "nothing"), 'argv': sys.argv, - 'path0': sys.path[0], + 'path': [os.path.normcase(p) for p in sys.path], } pprint.pprint(globals_to_check) |