Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f4413c6

Browse files
committed
fix: set sys.path correctly when running through a symlink. #2157
1 parent e883de8 commit f4413c6

3 files changed

Lines changed: 15 additions & 1 deletion

File tree

CHANGES.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,13 @@ upgrading your version of coverage.py.
2323
Unreleased
2424
----------
2525

26+
- Fix: running a Python file through a symlink now sets the sys.path correctly,
27+
matching regular Python behavior. Fixes `issue 2157`_.
28+
2629
- We are no longer testing eventlet support. Eventlet issues stern deprecation
2730
warnings that break our tests. Our support code is still there.
2831

32+
.. _issue 2157: https://github.com/coveragepy/coveragepy/issues/2157
2933

3034
.. start-releases
3135

coverage/execfile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ def prepare(self) -> None:
101101
# directory.
102102
path0 = self.arg0
103103
else:
104-
path0 = os.path.abspath(os.path.dirname(self.arg0))
104+
path0 = os.path.abspath(os.path.dirname(os.path.realpath(self.arg0)))
105105

106106
if os.path.isdir(sys.path[0]):
107107
# sys.path fakery. If we are being run as a command, then sys.path[0]

tests/test_process.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -969,6 +969,16 @@ def test_coverage_zip_is_like_python(self) -> None:
969969
actual = self.run_command(f"python {cov_main} run run_me.py")
970970
self.assert_tryexecfile_output(expected, actual)
971971

972+
@pytest.mark.skipif(env.WINDOWS, reason="Windows can't make symlinks")
973+
def test_coverage_in_symlink_is_like_python(self) -> None:
974+
# https://github.com/coveragepy/coveragepy/issues/2157
975+
self.make_file("real/run_me.py", TRY_EXECFILE_CODE)
976+
os.mkdir("link")
977+
os.symlink("../real/run_me.py", "link/run_me.py")
978+
expected = self.run_command("python link/run_me.py")
979+
actual = self.run_command("coverage run link/run_me.py")
980+
self.assert_tryexecfile_output(expected, actual)
981+
972982
@pytest.mark.skipif(env.PYVERSION < (3, 11), reason="PYTHONSAFEPATH is new in 3.11")
973983
@pytest.mark.skipif(
974984
env.WINDOWS,

0 commit comments

Comments
 (0)