diff --git a/changelog/13119.bugfix.rst b/changelog/13119.bugfix.rst new file mode 100644 index 00000000000..b7e56af9bb8 --- /dev/null +++ b/changelog/13119.bugfix.rst @@ -0,0 +1 @@ +Improved handling of invalid regex patterns for filter warnings by providing a clear error message. diff --git a/src/_pytest/config/__init__.py b/src/_pytest/config/__init__.py index 3db5e3da983..9e5b192b335 100644 --- a/src/_pytest/config/__init__.py +++ b/src/_pytest/config/__init__.py @@ -1953,6 +1953,13 @@ def parse_warning_filter( ) from None else: lineno = 0 + try: + re.compile(message) + re.compile(module) + except re.error as e: + raise UsageError( + error_template.format(error=f"Invalid regex {e.pattern!r}: {e}") + ) from None return action, message, category, module, lineno diff --git a/testing/test_warnings.py b/testing/test_warnings.py index d4d0e0b7f93..c302e7c6e3c 100644 --- a/testing/test_warnings.py +++ b/testing/test_warnings.py @@ -511,6 +511,29 @@ def test_hidden_by_system(self, pytester: Pytester, monkeypatch) -> None: result = pytester.runpytest_subprocess() assert WARNINGS_SUMMARY_HEADER not in result.stdout.str() + def test_invalid_regex_in_filterwarning(self, pytester: Pytester) -> None: + self.create_file(pytester) + pytester.makeini( + """ + [pytest] + filterwarnings = + ignore::DeprecationWarning:* + """ + ) + result = pytester.runpytest_subprocess() + assert result.ret == pytest.ExitCode.USAGE_ERROR + result.stderr.fnmatch_lines( + [ + "ERROR: while parsing the following warning configuration:", + "", + " ignore::DeprecationWarning:[*]", + "", + "This error occurred:", + "", + "Invalid regex '[*]': nothing to repeat at position 0", + ] + ) + @pytest.mark.skip("not relevant until pytest 9.0") @pytest.mark.parametrize("change_default", [None, "ini", "cmdline"])