Skip to content

Commit 8a8eed6

Browse files
[8.0.x] Fix collection of short paths on Windows (#12024)
Co-authored-by: Bruno Oliveira <[email protected]>
1 parent 74346f0 commit 8a8eed6

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

Diff for: changelog/11895.bugfix.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix collection on Windows where initial paths contain the short version of a path (for example ``c:\PROGRA~1\tests``).

Diff for: src/_pytest/main.py

+4
Original file line numberDiff line numberDiff line change
@@ -902,6 +902,10 @@ def collect(self) -> Iterator[Union[nodes.Item, nodes.Collector]]:
902902
# Path part e.g. `/a/b/` in `/a/b/test_file.py::TestIt::test_it`.
903903
if isinstance(matchparts[0], Path):
904904
is_match = node.path == matchparts[0]
905+
if sys.platform == "win32" and not is_match:
906+
# In case the file paths do not match, fallback to samefile() to
907+
# account for short-paths on Windows (#11895).
908+
is_match = os.path.samefile(node.path, matchparts[0])
905909
# Name part e.g. `TestIt` in `/a/b/test_file.py::TestIt::test_it`.
906910
else:
907911
# TODO: Remove parametrized workaround once collection structure contains

Diff for: testing/test_collection.py

+28
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
import pprint
44
import shutil
55
import sys
6+
import tempfile
67
import textwrap
78
from typing import List
89

10+
from _pytest.assertion.util import running_on_ci
911
from _pytest.config import ExitCode
1012
from _pytest.fixtures import FixtureRequest
1113
from _pytest.main import _in_venv
@@ -1758,3 +1760,29 @@ def test_foo(): assert True
17581760

17591761
assert result.ret == ExitCode.OK
17601762
assert result.parseoutcomes() == {"passed": 1}
1763+
1764+
1765+
@pytest.mark.skipif(not sys.platform.startswith("win"), reason="Windows only")
1766+
def test_collect_short_file_windows(pytester: Pytester) -> None:
1767+
"""Reproducer for #11895: short paths not colleced on Windows."""
1768+
short_path = tempfile.mkdtemp()
1769+
if "~" not in short_path: # pragma: no cover
1770+
if running_on_ci():
1771+
# On CI, we are expecting that under the current GitHub actions configuration,
1772+
# tempfile.mkdtemp() is producing short paths, so we want to fail to prevent
1773+
# this from silently changing without us noticing.
1774+
pytest.fail(
1775+
f"tempfile.mkdtemp() failed to produce a short path on CI: {short_path}"
1776+
)
1777+
else:
1778+
# We want to skip failing this test locally in this situation because
1779+
# depending on the local configuration tempfile.mkdtemp() might not produce a short path:
1780+
# For example, user might have configured %TEMP% exactly to avoid generating short paths.
1781+
pytest.skip(
1782+
f"tempfile.mkdtemp() failed to produce a short path: {short_path}, skipping"
1783+
)
1784+
1785+
test_file = Path(short_path).joinpath("test_collect_short_file_windows.py")
1786+
test_file.write_text("def test(): pass", encoding="UTF-8")
1787+
result = pytester.runpytest(short_path)
1788+
assert result.parseoutcomes() == {"passed": 1}

0 commit comments

Comments
 (0)