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 #11394
  • Loading branch information
nicoddemus committed Sep 7, 2023
1 parent 884b911 commit 63b0c6f
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 22 deletions.
9 changes: 3 additions & 6 deletions src/_pytest/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
from _pytest.pathlib import import_path
from _pytest.pathlib import ImportMode
from _pytest.pathlib import resolve_package_path
from _pytest.pathlib import safe_exists
from _pytest.stash import Stash
from _pytest.warning_types import PytestConfigWarning
from _pytest.warning_types import warn_explicit_for
Expand Down Expand Up @@ -557,12 +558,8 @@ def _set_initial_conftests(
anchor = absolutepath(current / path)

# Ensure we do not break if what appears to be an anchor
# is in fact a very long option (#10169).
try:
anchor_exists = anchor.exists()
except OSError: # pragma: no cover
anchor_exists = False
if anchor_exists:
# is in fact a very long option (#10169, #11394).
if safe_exists(anchor):
self._try_load_conftest(anchor, importmode, rootpath)
foundanchor = True
if not foundanchor:
Expand Down
9 changes: 1 addition & 8 deletions src/_pytest/config/findpaths.py
Original file line number Diff line number Diff line change
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
9 changes: 4 additions & 5 deletions src/_pytest/pathlib.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import atexit
import contextlib
import errno
import fnmatch
import importlib.util
import itertools
Expand Down Expand Up @@ -798,7 +797,7 @@ 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 OSError as e:
if e.errno == errno.ENAMETOOLONG:
return False
raise
except (ValueError, OSError):

Check warning on line 800 in src/_pytest/pathlib.py

View check run for this annotation

Codecov / codecov/patch

src/_pytest/pathlib.py#L800

Added line #L800 was not covered by tests
# ValueError: stat: path too long for Windows
# OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect
return False

Check warning on line 803 in src/_pytest/pathlib.py

View check run for this annotation

Codecov / codecov/patch

src/_pytest/pathlib.py#L803

Added line #L803 was not covered by tests
5 changes: 2 additions & 3 deletions testing/test_pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,6 @@ def test_safe_exists(tmp_path: Path) -> None:
Path,
"exists",
autospec=True,
side_effect=OSError(errno.EIO, "another kind of error"),
side_effect=ValueError("name too long"),
):
with pytest.raises(OSError):
_ = safe_exists(p)
assert safe_exists(p) is False

0 comments on commit 63b0c6f

Please sign in to comment.