Use _pytest.pathlib.safe_exists in get_dirs_from_args (#11407)
Related to #11394
This commit is contained in:
parent
28ccf476b9
commit
5936a79fdb
|
@ -59,6 +59,7 @@ from _pytest.pathlib import bestrelpath
|
||||||
from _pytest.pathlib import import_path
|
from _pytest.pathlib import import_path
|
||||||
from _pytest.pathlib import ImportMode
|
from _pytest.pathlib import ImportMode
|
||||||
from _pytest.pathlib import resolve_package_path
|
from _pytest.pathlib import resolve_package_path
|
||||||
|
from _pytest.pathlib import safe_exists
|
||||||
from _pytest.stash import Stash
|
from _pytest.stash import Stash
|
||||||
from _pytest.warning_types import PytestConfigWarning
|
from _pytest.warning_types import PytestConfigWarning
|
||||||
from _pytest.warning_types import warn_explicit_for
|
from _pytest.warning_types import warn_explicit_for
|
||||||
|
@ -562,12 +563,8 @@ class PytestPluginManager(PluginManager):
|
||||||
anchor = absolutepath(current / path)
|
anchor = absolutepath(current / path)
|
||||||
|
|
||||||
# Ensure we do not break if what appears to be an anchor
|
# Ensure we do not break if what appears to be an anchor
|
||||||
# is in fact a very long option (#10169).
|
# is in fact a very long option (#10169, #11394).
|
||||||
try:
|
if safe_exists(anchor):
|
||||||
anchor_exists = anchor.exists()
|
|
||||||
except OSError: # pragma: no cover
|
|
||||||
anchor_exists = False
|
|
||||||
if anchor_exists:
|
|
||||||
self._try_load_conftest(anchor, importmode, rootpath)
|
self._try_load_conftest(anchor, importmode, rootpath)
|
||||||
foundanchor = True
|
foundanchor = True
|
||||||
if not foundanchor:
|
if not foundanchor:
|
||||||
|
|
|
@ -15,6 +15,7 @@ from .exceptions import UsageError
|
||||||
from _pytest.outcomes import fail
|
from _pytest.outcomes import fail
|
||||||
from _pytest.pathlib import absolutepath
|
from _pytest.pathlib import absolutepath
|
||||||
from _pytest.pathlib import commonpath
|
from _pytest.pathlib import commonpath
|
||||||
|
from _pytest.pathlib import safe_exists
|
||||||
|
|
||||||
|
|
||||||
def _parse_ini_config(path: Path) -> iniconfig.IniConfig:
|
def _parse_ini_config(path: Path) -> iniconfig.IniConfig:
|
||||||
|
@ -147,14 +148,6 @@ def get_dirs_from_args(args: Iterable[str]) -> List[Path]:
|
||||||
return path
|
return path
|
||||||
return path.parent
|
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
|
# These look like paths but may not exist
|
||||||
possible_paths = (
|
possible_paths = (
|
||||||
absolutepath(get_file_part_from_node_id(arg))
|
absolutepath(get_file_part_from_node_id(arg))
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
import atexit
|
import atexit
|
||||||
import contextlib
|
import contextlib
|
||||||
import errno
|
|
||||||
import fnmatch
|
import fnmatch
|
||||||
import importlib.util
|
import importlib.util
|
||||||
import itertools
|
import itertools
|
||||||
|
@ -780,7 +779,7 @@ def safe_exists(p: Path) -> bool:
|
||||||
"""Like Path.exists(), but account for input arguments that might be too long (#11394)."""
|
"""Like Path.exists(), but account for input arguments that might be too long (#11394)."""
|
||||||
try:
|
try:
|
||||||
return p.exists()
|
return p.exists()
|
||||||
except OSError as e:
|
except (ValueError, OSError):
|
||||||
if e.errno == errno.ENAMETOOLONG:
|
# ValueError: stat: path too long for Windows
|
||||||
return False
|
# OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect
|
||||||
raise
|
return False
|
||||||
|
|
|
@ -688,7 +688,6 @@ def test_safe_exists(tmp_path: Path) -> None:
|
||||||
Path,
|
Path,
|
||||||
"exists",
|
"exists",
|
||||||
autospec=True,
|
autospec=True,
|
||||||
side_effect=OSError(errno.EIO, "another kind of error"),
|
side_effect=ValueError("name too long"),
|
||||||
):
|
):
|
||||||
with pytest.raises(OSError):
|
assert safe_exists(p) is False
|
||||||
_ = safe_exists(p)
|
|
||||||
|
|
Loading…
Reference in New Issue