2020-06-08 21:56:40 +08:00
|
|
|
import os.path
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
import textwrap
|
|
|
|
from contextlib import contextmanager
|
2020-12-16 12:16:05 +08:00
|
|
|
from pathlib import Path
|
2020-06-08 21:56:40 +08:00
|
|
|
from string import ascii_lowercase
|
|
|
|
|
2020-12-16 12:16:05 +08:00
|
|
|
from _pytest.pytester import Pytester
|
2020-06-08 21:56:40 +08:00
|
|
|
|
|
|
|
|
|
|
|
@contextmanager
|
2020-12-16 12:16:05 +08:00
|
|
|
def subst_path_windows(filepath: Path):
|
2020-06-08 21:56:40 +08:00
|
|
|
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.")
|
|
|
|
|
2020-12-16 12:16:05 +08:00
|
|
|
directory = filepath.parent
|
|
|
|
basename = filepath.name
|
2020-06-08 21:56:40 +08:00
|
|
|
|
|
|
|
args = ["subst", drive, str(directory)]
|
|
|
|
subprocess.check_call(args)
|
|
|
|
assert os.path.exists(drive)
|
|
|
|
try:
|
2020-12-16 12:16:05 +08:00
|
|
|
filename = Path(drive, os.sep, basename)
|
2020-06-08 21:56:40 +08:00
|
|
|
yield filename
|
|
|
|
finally:
|
|
|
|
args = ["subst", "/D", drive]
|
|
|
|
subprocess.check_call(args)
|
|
|
|
|
|
|
|
|
|
|
|
@contextmanager
|
2020-12-16 12:16:05 +08:00
|
|
|
def subst_path_linux(filepath: Path):
|
|
|
|
directory = filepath.parent
|
|
|
|
basename = filepath.name
|
2020-06-08 21:56:40 +08:00
|
|
|
|
|
|
|
target = directory / ".." / "sub2"
|
|
|
|
os.symlink(str(directory), str(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
|
|
|
|
|
|
|
|
|
2020-12-16 12:16:05 +08:00
|
|
|
def test_link_resolve(pytester: Pytester) -> None:
|
2020-07-18 17:35:13 +08:00
|
|
|
"""See: https://github.com/pytest-dev/pytest/issues/5965."""
|
2020-12-16 12:16:05 +08:00
|
|
|
sub1 = pytester.mkpydir("sub1")
|
|
|
|
p = sub1.joinpath("test_foo.py")
|
|
|
|
p.write_text(
|
2020-06-08 21:56:40 +08:00
|
|
|
textwrap.dedent(
|
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
def test_foo():
|
|
|
|
raise AssertionError()
|
|
|
|
"""
|
2023-06-20 19:55:40 +08:00
|
|
|
),
|
|
|
|
encoding="utf-8",
|
2020-06-08 21:56:40 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
subst = subst_path_linux
|
|
|
|
if sys.platform == "win32":
|
|
|
|
subst = subst_path_windows
|
|
|
|
|
|
|
|
with subst(p) as subst_p:
|
2020-12-16 12:16:05 +08:00
|
|
|
result = pytester.runpytest(str(subst_p), "-v")
|
2020-06-08 21:56:40 +08:00
|
|
|
# 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 "sub1/test_foo.py" not in stdout
|
|
|
|
|
|
|
|
# i.e.: Expect drive on windows because we just have drive:filename, whereas
|
|
|
|
# we expect a relative path on Linux.
|
2020-10-03 04:16:22 +08:00
|
|
|
expect = f"*{subst_p}*" if sys.platform == "win32" else "*sub2/test_foo.py*"
|
2020-06-08 21:56:40 +08:00
|
|
|
result.stdout.fnmatch_lines([expect])
|