From 43d31ddd53c47f63d8c8e41c04d4fa0bbae7b6d3 Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Tue, 15 Jul 2025 15:48:20 +0300 Subject: [PATCH] main: use a better windows short-path comparison method The previous check does not account for multiple levels of symlinks: given a -> b -> c, a would match b. --- src/_pytest/main.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/_pytest/main.py b/src/_pytest/main.py index 77d8b52ca46..1ac95b388ab 100644 --- a/src/_pytest/main.py +++ b/src/_pytest/main.py @@ -935,13 +935,11 @@ def collect(self) -> Iterator[nodes.Item | nodes.Collector]: 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). - same_file = os.path.samefile(node.path, matchparts[0]) - # We don't want to match links to the current node, - # otherwise we would match the same file more than once (#12039). - is_match = same_file and ( - os.path.islink(node.path) - == os.path.islink(matchparts[0]) + # account for short-paths on Windows (#11895). But use a version + # which doesn't resolve symlinks, otherwise we might match the + # same file more than once (#12039). + is_match = os.path.samestat( + node.path.lstat(), matchparts[0].lstat() ) # Name part e.g. `TestIt` in `/a/b/test_file.py::TestIt::test_it`.