Improved handling of invalid regex pattern in pytest.raises (#12526)
This commit is contained in:
parent
e8aee21384
commit
49bb5c89a6
1
AUTHORS
1
AUTHORS
|
@ -430,6 +430,7 @@ Victor Rodriguez
|
||||||
Victor Uriarte
|
Victor Uriarte
|
||||||
Vidar T. Fauske
|
Vidar T. Fauske
|
||||||
Vijay Arora
|
Vijay Arora
|
||||||
|
Virendra Patil
|
||||||
Virgil Dupras
|
Virgil Dupras
|
||||||
Vitaly Lashmanov
|
Vitaly Lashmanov
|
||||||
Vivaan Verma
|
Vivaan Verma
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Improve handling of invalid regex patterns in :func:`pytest.raises(match=r'...') <pytest.raises>` by providing a clear error message.
|
|
@ -7,6 +7,7 @@ from decimal import Decimal
|
||||||
import math
|
import math
|
||||||
from numbers import Complex
|
from numbers import Complex
|
||||||
import pprint
|
import pprint
|
||||||
|
import re
|
||||||
from types import TracebackType
|
from types import TracebackType
|
||||||
from typing import Any
|
from typing import Any
|
||||||
from typing import Callable
|
from typing import Callable
|
||||||
|
@ -986,6 +987,14 @@ class RaisesContext(ContextManager[_pytest._code.ExceptionInfo[E]]):
|
||||||
self.message = message
|
self.message = message
|
||||||
self.match_expr = match_expr
|
self.match_expr = match_expr
|
||||||
self.excinfo: _pytest._code.ExceptionInfo[E] | None = None
|
self.excinfo: _pytest._code.ExceptionInfo[E] | None = None
|
||||||
|
if self.match_expr is not None:
|
||||||
|
re_error = None
|
||||||
|
try:
|
||||||
|
re.compile(self.match_expr)
|
||||||
|
except re.error as e:
|
||||||
|
re_error = e
|
||||||
|
if re_error is not None:
|
||||||
|
fail(f"Invalid regex pattern provided to 'match': {re_error}")
|
||||||
|
|
||||||
def __enter__(self) -> _pytest._code.ExceptionInfo[E]:
|
def __enter__(self) -> _pytest._code.ExceptionInfo[E]:
|
||||||
self.excinfo = _pytest._code.ExceptionInfo.for_later()
|
self.excinfo = _pytest._code.ExceptionInfo.for_later()
|
||||||
|
|
|
@ -132,6 +132,26 @@ class TestRaises:
|
||||||
result = pytester.runpytest()
|
result = pytester.runpytest()
|
||||||
result.stdout.fnmatch_lines(["*2 failed*"])
|
result.stdout.fnmatch_lines(["*2 failed*"])
|
||||||
|
|
||||||
|
def test_raises_with_invalid_regex(self, pytester: Pytester) -> None:
|
||||||
|
pytester.makepyfile(
|
||||||
|
"""
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
def test_invalid_regex():
|
||||||
|
with pytest.raises(ValueError, match="invalid regex character ["):
|
||||||
|
raise ValueError()
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
result = pytester.runpytest()
|
||||||
|
result.stdout.fnmatch_lines(
|
||||||
|
[
|
||||||
|
"*Invalid regex pattern provided to 'match': unterminated character set at position 24*",
|
||||||
|
]
|
||||||
|
)
|
||||||
|
result.stdout.no_fnmatch_line("*Traceback*")
|
||||||
|
result.stdout.no_fnmatch_line("*File*")
|
||||||
|
result.stdout.no_fnmatch_line("*line*")
|
||||||
|
|
||||||
def test_noclass(self) -> None:
|
def test_noclass(self) -> None:
|
||||||
with pytest.raises(TypeError):
|
with pytest.raises(TypeError):
|
||||||
pytest.raises("wrong", lambda: None) # type: ignore[call-overload]
|
pytest.raises("wrong", lambda: None) # type: ignore[call-overload]
|
||||||
|
|
Loading…
Reference in New Issue