Skip to content

Commit 1024598

Browse files
committed
Fix collection of short paths on Windows
Passing a short path in the command line was causing the matchparts check to fail, because ``Path(short_path) != Path(long_path)``. Using ``os.path.samefile`` as fallback ensures the comparsion works on Windows when comparing short/long paths. Fix #11895
1 parent 0149556 commit 1024598

File tree

3 files changed

+6
-1
lines changed

3 files changed

+6
-1
lines changed

changelog/11895.bugfix.rst

Lines changed: 1 addition & 0 deletions
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``).

src/_pytest/main.py

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

testing/test_collection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1766,7 +1766,7 @@ def test_collect_short_file_windows(pytester: Pytester) -> None:
17661766
f"tempfile.mkdtemp() failed to produce a short path: {short_path}, skipping"
17671767
)
17681768

1769-
test_file = Path(short_path).joinpath("test_foo.py")
1769+
test_file = Path(short_path).joinpath("test_collect_short_file_windows.py")
17701770
test_file.write_text("def test(): pass")
17711771
result = pytester.runpytest(short_path)
17721772
assert result.parseoutcomes() == {"passed": 1}

0 commit comments

Comments
 (0)