Skip to content

Commit

Permalink
Use _pytest.pathlib.safe_exists in get_dirs_from_args
Browse files Browse the repository at this point in the history
Related to pytest-dev#11394
  • Loading branch information
nicoddemus committed Sep 7, 2023
1 parent 884b911 commit 7ff3f76
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
9 changes: 1 addition & 8 deletions src/_pytest/config/findpaths.py
Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand Down
4 changes: 4 additions & 0 deletions src/_pytest/pathlib.py
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions testing/test_pathlib.py
Expand Up @@ -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",
Expand Down

0 comments on commit 7ff3f76

Please sign in to comment.