Skip to content

Commit afdf03b

Browse files
virendrapatil24pre-commit-ci[bot]graingert
authored
Fixed invalid regex handling in filterwarnings (pytest-dev#13124)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Thomas Grainger <[email protected]>
1 parent b4009b3 commit afdf03b

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

changelog/13119.bugfix.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Improved handling of invalid regex patterns for filter warnings by providing a clear error message.

src/_pytest/config/__init__.py

+7
Original file line numberDiff line numberDiff line change
@@ -1953,6 +1953,13 @@ def parse_warning_filter(
19531953
) from None
19541954
else:
19551955
lineno = 0
1956+
try:
1957+
re.compile(message)
1958+
re.compile(module)
1959+
except re.error as e:
1960+
raise UsageError(
1961+
error_template.format(error=f"Invalid regex {e.pattern!r}: {e}")
1962+
) from None
19561963
return action, message, category, module, lineno
19571964

19581965

testing/test_warnings.py

+23
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,29 @@ def test_hidden_by_system(self, pytester: Pytester, monkeypatch) -> None:
511511
result = pytester.runpytest_subprocess()
512512
assert WARNINGS_SUMMARY_HEADER not in result.stdout.str()
513513

514+
def test_invalid_regex_in_filterwarning(self, pytester: Pytester) -> None:
515+
self.create_file(pytester)
516+
pytester.makeini(
517+
"""
518+
[pytest]
519+
filterwarnings =
520+
ignore::DeprecationWarning:*
521+
"""
522+
)
523+
result = pytester.runpytest_subprocess()
524+
assert result.ret == pytest.ExitCode.USAGE_ERROR
525+
result.stderr.fnmatch_lines(
526+
[
527+
"ERROR: while parsing the following warning configuration:",
528+
"",
529+
" ignore::DeprecationWarning:[*]",
530+
"",
531+
"This error occurred:",
532+
"",
533+
"Invalid regex '[*]': nothing to repeat at position 0",
534+
]
535+
)
536+
514537

515538
@pytest.mark.skip("not relevant until pytest 9.0")
516539
@pytest.mark.parametrize("change_default", [None, "ini", "cmdline"])

0 commit comments

Comments
 (0)