From 7ff3f767fed456bff438710751fdd8be7bd6e6b4 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Thu, 7 Sep 2023 13:15:11 -0300 Subject: [PATCH] Use _pytest.pathlib.safe_exists in get_dirs_from_args Related to #11394 --- src/_pytest/config/findpaths.py | 9 +-------- src/_pytest/pathlib.py | 4 ++++ testing/test_pathlib.py | 8 ++++++++ 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/_pytest/config/findpaths.py b/src/_pytest/config/findpaths.py index 234b9e12906..02674ffae3b 100644 --- a/src/_pytest/config/findpaths.py +++ b/src/_pytest/config/findpaths.py @@ -16,6 +16,7 @@ from _pytest.outcomes import fail from _pytest.pathlib import absolutepath from _pytest.pathlib import commonpath +from _pytest.pathlib import safe_exists if TYPE_CHECKING: from . import Config @@ -151,14 +152,6 @@ def get_dir_from_path(path: Path) -> Path: return path return path.parent - def safe_exists(path: Path) -> bool: - # This can throw on paths that contain characters unrepresentable at the OS level, - # or with invalid syntax on Windows (https://bugs.python.org/issue35306) - try: - return path.exists() - except OSError: - return False - # These look like paths but may not exist possible_paths = ( absolutepath(get_file_part_from_node_id(arg)) diff --git a/src/_pytest/pathlib.py b/src/_pytest/pathlib.py index b5c2d86452f..fb1ea40b67a 100644 --- a/src/_pytest/pathlib.py +++ b/src/_pytest/pathlib.py @@ -798,6 +798,10 @@ def safe_exists(p: Path) -> bool: """Like Path.exists(), but account for input arguments that might be too long (#11394).""" try: return p.exists() + except ValueError: + # Might be raised on Windows: + # ValueError: stat: path too long for Windows + return False except OSError as e: if e.errno == errno.ENAMETOOLONG: return False diff --git a/testing/test_pathlib.py b/testing/test_pathlib.py index 8a9659aabd9..a3482cd9c9c 100644 --- a/testing/test_pathlib.py +++ b/testing/test_pathlib.py @@ -684,6 +684,14 @@ def test_safe_exists(tmp_path: Path) -> None: ): assert safe_exists(p) is False + with unittest.mock.patch.object( + Path, + "exists", + autospec=True, + side_effect=ValueError("name too long"), + ): + assert safe_exists(p) is False + with unittest.mock.patch.object( Path, "exists",