-
-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy path__init__.py
69 lines (59 loc) · 1.92 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
"""Testing utilities for CmdStanPy."""
import contextlib
import logging
import platform
import re
from importlib import reload
from typing import Tuple, Type
from unittest import mock
import pytest
mark_windows_only = pytest.mark.skipif(
platform.system() != 'Windows', reason='only runs on windows'
)
mark_not_windows = pytest.mark.skipif(
platform.system() == 'Windows', reason='does not run on windows'
)
# pylint: disable=invalid-name
@contextlib.contextmanager
def raises_nested(expected_exception: Type[Exception], match: str) -> None:
"""A version of assertRaisesRegex that checks the full traceback.
Useful for when an exception is raised from another and you wish to
inspect the inner exception.
"""
with pytest.raises(expected_exception) as ctx:
yield
exception: Exception = ctx.value
lines = []
while exception:
lines.append(str(exception))
exception = exception.__cause__
text = "\n".join(lines)
assert re.search(match, text), f"pattern `{match}` does not match `{text}`"
@contextlib.contextmanager
def without_import(library, module):
with mock.patch.dict('sys.modules', {library: None}):
reload(module)
yield
reload(module)
def check_present(
caplog: pytest.LogCaptureFixture,
*conditions: Tuple,
clear: bool = True,
) -> None:
"""
Check that all desired records exist.
"""
for condition in conditions:
logger, level, message = condition
if isinstance(level, str):
level = getattr(logging, level)
found = any(
logger == logger_ and level == level_ and message.match(message_)
if isinstance(message, re.Pattern)
else message == message_
for logger_, level_, message_ in caplog.record_tuples
)
if not found:
raise ValueError(f"logs did not contain the record {condition}")
if clear:
caplog.clear()