-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix issue where working dir becomes wrong on symlinks. Fixes #5965
This addresses subst drive on Windows and symlinks on Linux.
- Loading branch information
Showing
5 changed files
with
83 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
No longer resolve symlinks so that the current directory remains correct with drives mapped with subst on Windows or symlinks on Linux. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
from contextlib import contextmanager | ||
import os.path | ||
import subprocess | ||
from string import ascii_lowercase | ||
import sys | ||
import textwrap | ||
|
||
import py.path | ||
from _pytest import pytester | ||
|
||
|
||
@contextmanager | ||
def subst_path_windows(filename): | ||
for c in ascii_lowercase[7:]: # Create a subst drive from H-Z. | ||
c += ":" | ||
if not os.path.exists(c): | ||
drive = c | ||
break | ||
else: | ||
raise AssertionError("Unable to find suitable drive letter for subst.") | ||
|
||
directory = os.path.dirname(filename) | ||
basename = os.path.basename(filename) | ||
|
||
args = ["subst", drive, directory] | ||
subprocess.check_call(args) | ||
assert os.path.exists(drive) | ||
try: | ||
filename = py.path.local(drive) / basename | ||
yield filename | ||
finally: | ||
args = ["subst", "/D", drive] | ||
subprocess.check_call(args) | ||
|
||
|
||
@contextmanager | ||
def subst_path_linux(filename): | ||
directory = os.path.dirname(filename) | ||
basename = os.path.basename(filename) | ||
|
||
target = py.path.local(directory) / ".." / "sub2" | ||
os.symlink(directory, target, target_is_directory=True) | ||
try: | ||
filename = target / basename | ||
yield filename | ||
finally: | ||
# We don't need to unlink (it's all in the tempdir). | ||
pass | ||
|
||
|
||
def test_link_resolve(testdir: pytester.Testdir) -> None: | ||
""" | ||
See: https://github.com/pytest-dev/pytest/issues/5965 | ||
""" | ||
sub1 = testdir.mkpydir("sub1") | ||
p = sub1.join("test_foo.py") | ||
p.write( | ||
textwrap.dedent( | ||
""" | ||
import pytest | ||
def test_foo(): | ||
raise AssertionError() | ||
""" | ||
) | ||
) | ||
|
||
subst = subst_path_linux | ||
if sys.platform == "win32": | ||
subst = subst_path_windows | ||
|
||
with subst(p) as subst_p: | ||
result = testdir.runpytest(subst_p, "-v") | ||
# i.e.: Make sure that the error is reported as a relative path, not as a | ||
# resolved path. | ||
# See: https://github.com/pytest-dev/pytest/issues/5965 | ||
stdout = result.stdout.str() | ||
assert str(p) not in stdout | ||
assert str(subst_p) in stdout |