From c95c76bfb6ef49c6ca19328a336b7ed6d0c8d7e4 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Mon, 5 Feb 2024 17:49:40 -0300 Subject: [PATCH] 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 --- changelog/11895.bugfix.rst | 1 + src/_pytest/main.py | 4 ++++ 2 files changed, 5 insertions(+) create mode 100644 changelog/11895.bugfix.rst diff --git a/changelog/11895.bugfix.rst b/changelog/11895.bugfix.rst new file mode 100644 index 000000000..4211213c1 --- /dev/null +++ b/changelog/11895.bugfix.rst @@ -0,0 +1 @@ +Fix collection on Windows where initial paths contain the short version of a path (for example ``c:\PROGRA~1\tests``). diff --git a/src/_pytest/main.py b/src/_pytest/main.py index 5c70ad74e..edb1a69e2 100644 --- a/src/_pytest/main.py +++ b/src/_pytest/main.py @@ -901,6 +901,10 @@ class Session(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