Files
pytest2/testing/test_link_resolve.py
Pierre Sassoulas 4588653b24 Migrate from autoflake, black, isort, pyupgrade, flake8 and pydocstyle, to ruff
ruff is faster and handle everything we had prior.

isort configuration done based on the indication from
https://github.com/astral-sh/ruff/issues/4670, previousely based on
reorder-python-import (#11896)

flake8-docstrings was a wrapper around pydocstyle (now archived) that
explicitly asks to use ruff in https://github.com/PyCQA/pydocstyle/pull/658.

flake8-typing-import is useful mainly for project that support python 3.7
and the one useful check will be implemented in https://github.com/astral-sh/ruff/issues/2302

We need to keep blacken-doc because ruff does not handle detection
of python code inside .md and .rst. The direct link to the repo is
now used to avoid a redirection.

Manual fixes:
- Lines that became too long
- % formatting that was not done automatically
- type: ignore that were moved around
- noqa of hard to fix issues (UP031 generally)
- fmt: off and fmt: on that is not really identical
  between black and ruff
- autofix re-order in pre-commit from faster to slower

Co-authored-by: Ran Benita <ran@unusedvar.com>
2024-02-02 09:27:00 +01:00

83 lines
2.3 KiB
Python

# mypy: allow-untyped-defs
from contextlib import contextmanager
import os.path
from pathlib import Path
from string import ascii_lowercase
import subprocess
import sys
import textwrap
from _pytest.pytester import Pytester
@contextmanager
def subst_path_windows(filepath: Path):
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 = filepath.parent
basename = filepath.name
args = ["subst", drive, str(directory)]
subprocess.check_call(args)
assert os.path.exists(drive)
try:
filename = Path(drive, os.sep, basename)
yield filename
finally:
args = ["subst", "/D", drive]
subprocess.check_call(args)
@contextmanager
def subst_path_linux(filepath: Path):
directory = filepath.parent
basename = filepath.name
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
def test_link_resolve(pytester: Pytester) -> None:
"""See: https://github.com/pytest-dev/pytest/issues/5965."""
sub1 = pytester.mkpydir("sub1")
p = sub1.joinpath("test_foo.py")
p.write_text(
textwrap.dedent(
"""
import pytest
def test_foo():
raise AssertionError()
"""
),
encoding="utf-8",
)
subst = subst_path_linux
if sys.platform == "win32":
subst = subst_path_windows
with subst(p) as subst_p:
result = pytester.runpytest(str(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 "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.
expect = f"*{subst_p}*" if sys.platform == "win32" else "*sub2/test_foo.py*"
result.stdout.fnmatch_lines([expect])