|
| 1 | +import os.path |
| 2 | +import subprocess |
| 3 | +import sys |
| 4 | +import textwrap |
| 5 | +from contextlib import contextmanager |
| 6 | +from string import ascii_lowercase |
| 7 | + |
| 8 | +import py.path |
| 9 | + |
| 10 | +from _pytest import pytester |
| 11 | + |
| 12 | + |
| 13 | +@contextmanager |
| 14 | +def subst_path_windows(filename): |
| 15 | + for c in ascii_lowercase[7:]: # Create a subst drive from H-Z. |
| 16 | + c += ":" |
| 17 | + if not os.path.exists(c): |
| 18 | + drive = c |
| 19 | + break |
| 20 | + else: |
| 21 | + raise AssertionError("Unable to find suitable drive letter for subst.") |
| 22 | + |
| 23 | + directory = filename.dirpath() |
| 24 | + basename = filename.basename |
| 25 | + |
| 26 | + args = ["subst", drive, str(directory)] |
| 27 | + subprocess.check_call(args) |
| 28 | + assert os.path.exists(drive) |
| 29 | + try: |
| 30 | + filename = py.path.local(drive) / basename |
| 31 | + yield filename |
| 32 | + finally: |
| 33 | + args = ["subst", "/D", drive] |
| 34 | + subprocess.check_call(args) |
| 35 | + |
| 36 | + |
| 37 | +@contextmanager |
| 38 | +def subst_path_linux(filename): |
| 39 | + directory = filename.dirpath() |
| 40 | + basename = filename.basename |
| 41 | + |
| 42 | + target = directory / ".." / "sub2" |
| 43 | + os.symlink(str(directory), str(target), target_is_directory=True) |
| 44 | + try: |
| 45 | + filename = target / basename |
| 46 | + yield filename |
| 47 | + finally: |
| 48 | + # We don't need to unlink (it's all in the tempdir). |
| 49 | + pass |
| 50 | + |
| 51 | + |
| 52 | +def test_link_resolve(testdir: pytester.Testdir) -> None: |
| 53 | + """ |
| 54 | + See: https://github.com/pytest-dev/pytest/issues/5965 |
| 55 | + """ |
| 56 | + sub1 = testdir.mkpydir("sub1") |
| 57 | + p = sub1.join("test_foo.py") |
| 58 | + p.write( |
| 59 | + textwrap.dedent( |
| 60 | + """ |
| 61 | + import pytest |
| 62 | + def test_foo(): |
| 63 | + raise AssertionError() |
| 64 | + """ |
| 65 | + ) |
| 66 | + ) |
| 67 | + |
| 68 | + subst = subst_path_linux |
| 69 | + if sys.platform == "win32": |
| 70 | + subst = subst_path_windows |
| 71 | + |
| 72 | + with subst(p) as subst_p: |
| 73 | + result = testdir.runpytest(str(subst_p), "-v") |
| 74 | + # i.e.: Make sure that the error is reported as a relative path, not as a |
| 75 | + # resolved path. |
| 76 | + # See: https://github.com/pytest-dev/pytest/issues/5965 |
| 77 | + stdout = result.stdout.str() |
| 78 | + assert "sub1/test_foo.py" not in stdout |
| 79 | + |
| 80 | + # i.e.: Expect drive on windows because we just have drive:filename, whereas |
| 81 | + # we expect a relative path on Linux. |
| 82 | + expect = ( |
| 83 | + "*{}*".format(subst_p) if sys.platform == "win32" else "*sub2/test_foo.py*" |
| 84 | + ) |
| 85 | + result.stdout.fnmatch_lines([expect]) |
0 commit comments