Skip to content

Commit

Permalink
Fix collection of short paths on Windows
Browse files Browse the repository at this point in the history
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 pytest-dev#11895
  • Loading branch information
nicoddemus committed Feb 17, 2024
1 parent dc3130b commit c95c76b
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog/11895.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix collection on Windows where initial paths contain the short version of a path (for example ``c:\PROGRA~1\tests``).
4 changes: 4 additions & 0 deletions src/_pytest/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -901,6 +901,10 @@ def collect(self) -> Iterator[Union[nodes.Item, nodes.Collector]]:
# Path part e.g. `/a/b/` in `/a/b/test_file.py::TestIt::test_it`.
if isinstance(matchparts[0], Path):
is_match = node.path == matchparts[0]
if sys.platform == "win32" and not is_match:
# In case the file paths do not match, fallback to samefile() to
# account for short-paths on Windows (#11895).
is_match = os.path.samefile(node.path, matchparts[0])
# Name part e.g. `TestIt` in `/a/b/test_file.py::TestIt::test_it`.
else:
# TODO: Remove parametrized workaround once collection structure contains
Expand Down

0 comments on commit c95c76b

Please sign in to comment.