Fixed handling of invalid regex pattern
This commit is contained in:
parent
237152552e
commit
4354bae714
|
@ -1,31 +1,32 @@
|
||||||
# mypy: allow-untyped-defs
|
# mypy: allow-untyped-defs
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from collections.abc import Collection
|
|
||||||
from collections.abc import Sized
|
|
||||||
from decimal import Decimal
|
|
||||||
import math
|
import math
|
||||||
from numbers import Complex
|
|
||||||
import pprint
|
import pprint
|
||||||
|
import re
|
||||||
|
from collections.abc import Collection, Sized
|
||||||
|
from decimal import Decimal
|
||||||
|
from numbers import Complex
|
||||||
from types import TracebackType
|
from types import TracebackType
|
||||||
from typing import Any
|
from typing import (
|
||||||
from typing import Callable
|
TYPE_CHECKING,
|
||||||
from typing import cast
|
Any,
|
||||||
from typing import ContextManager
|
Callable,
|
||||||
from typing import final
|
ContextManager,
|
||||||
from typing import Mapping
|
Mapping,
|
||||||
from typing import overload
|
Pattern,
|
||||||
from typing import Pattern
|
Sequence,
|
||||||
from typing import Sequence
|
Tuple,
|
||||||
from typing import Tuple
|
Type,
|
||||||
from typing import Type
|
TypeVar,
|
||||||
from typing import TYPE_CHECKING
|
cast,
|
||||||
from typing import TypeVar
|
final,
|
||||||
|
overload,
|
||||||
|
)
|
||||||
|
|
||||||
import _pytest._code
|
import _pytest._code
|
||||||
from _pytest.outcomes import fail
|
from _pytest.outcomes import fail
|
||||||
|
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from numpy import ndarray
|
from numpy import ndarray
|
||||||
|
|
||||||
|
@ -1006,6 +1007,9 @@ class RaisesContext(ContextManager[_pytest._code.ExceptionInfo[E]]):
|
||||||
# Cast to narrow the exception type now that it's verified.
|
# Cast to narrow the exception type now that it's verified.
|
||||||
exc_info = cast(Tuple[Type[E], E, TracebackType], (exc_type, exc_val, exc_tb))
|
exc_info = cast(Tuple[Type[E], E, TracebackType], (exc_type, exc_val, exc_tb))
|
||||||
self.excinfo.fill_unfilled(exc_info)
|
self.excinfo.fill_unfilled(exc_info)
|
||||||
if self.match_expr is not None:
|
try:
|
||||||
self.excinfo.match(self.match_expr)
|
if self.match_expr:
|
||||||
|
self.excinfo.match(self.match_expr)
|
||||||
|
except re.error as e:
|
||||||
|
fail(f"Invalid regex pattern provided to 'match': {e}")
|
||||||
return True
|
return True
|
||||||
|
|
|
@ -6,6 +6,7 @@ import sys
|
||||||
|
|
||||||
from _pytest.outcomes import Failed
|
from _pytest.outcomes import Failed
|
||||||
from _pytest.pytester import Pytester
|
from _pytest.pytester import Pytester
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
|
|
||||||
|
@ -132,6 +133,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