From a057806d4fabfdedc91830a907689802b3f118a4 Mon Sep 17 00:00:00 2001 From: Robert O'Shea Date: Thu, 4 Aug 2022 20:10:06 +0100 Subject: [PATCH] Changed how pytest reacts to long filenames and changed its test --- src/_pytest/config/__init__.py | 12 +++++------- testing/test_config.py | 27 ++++++++++++++------------- 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/src/_pytest/config/__init__.py b/src/_pytest/config/__init__.py index 5f560e00b..9d2975401 100644 --- a/src/_pytest/config/__init__.py +++ b/src/_pytest/config/__init__.py @@ -529,14 +529,12 @@ class PytestPluginManager(PluginManager): path = path[:i] anchor = absolutepath(current / path) try: - if anchor.exists(): # we found some file object - self._try_load_conftest(anchor, namespace.importmode, rootpath) - foundanchor = True - print(str(anchor)) + anchor_exists = anchor.exists() except OSError: - import pytest - - raise pytest.UsageError(f"filename is too long: {anchor}") + anchor_exists = False + if anchor_exists: + self._try_load_conftest(anchor, namespace.importmode, rootpath) + foundanchor = True if not foundanchor: self._try_load_conftest(current, namespace.importmode, rootpath) diff --git a/testing/test_config.py b/testing/test_config.py index e796915b7..9fa4ace0f 100644 --- a/testing/test_config.py +++ b/testing/test_config.py @@ -1780,24 +1780,25 @@ def test_help_and_version_after_argument_error(pytester: Pytester) -> None: assert result.ret == ExitCode.USAGE_ERROR -def test_filename_too_long(pytester: Pytester) -> None: +def test_filename_too_long(pytester: Pytester, monkeypatch: MonkeyPatch) -> None: + """And OSError may be raised if filename is too long. This test ensures + the issue of that error not being caught does not regress.""" + path_str = ( - "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" - + "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" - + "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" - + "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" - + "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" * 50 ) + + def FilePathTooLong(p): + if len(str(p)) >= 200: + raise OSError() + + monkeypatch.setattr(Path, "exists", FilePathTooLong, raising=True) + config = pytester.parseconfig() config.known_args_namespace.file_or_dir.append(path_str) - pytest.raises( - UsageError, - config.pluginmanager._set_initial_conftests, - config.known_args_namespace, - rootpath=config.rootpath, + config.pluginmanager._set_initial_conftests( + config.known_args_namespace, rootpath=config.rootpath ) - result = pytester.runpytest(path_str) - assert result.ret == ExitCode.USAGE_ERROR def test_help_formatter_uses_py_get_terminal_width(monkeypatch: MonkeyPatch) -> None: