-
Notifications
You must be signed in to change notification settings - Fork 111
/
Copy pathtest_utils.py
130 lines (116 loc) · 4.39 KB
/
test_utils.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import re
import pytest
import requests
import requests_mock
from inline_snapshot import snapshot
import logfire
from logfire._internal.utils import UnexpectedResponse, handle_internal_errors
from tests.import_used_for_tests.internal_error_handling import internal_logfire_code_example, user_code_example
def test_raise_for_status() -> None:
with requests_mock.Mocker() as m:
m.get('https://test.com', text='this is the body', status_code=503)
r = requests.get('https://test.com')
with pytest.raises(UnexpectedResponse) as exc_info:
UnexpectedResponse.raise_for_status(r)
s = str(exc_info.value)
assert s.startswith('Unexpected response 503')
assert 'body: this is the body' in s
def test_reraise_internal_exception():
with pytest.raises(ZeroDivisionError):
with handle_internal_errors:
str(1 / 0)
def test_internal_exception_tb(caplog: pytest.LogCaptureFixture):
# Pretend that `internal_logfire_code_example` is a module within logfire,
# so all frames from it should be included.
logfire.add_non_user_code_prefix(internal_logfire_code_example.__file__)
user_code_example.user1()
tracebacks = [
re.sub(
# Remove lines with ~ and ^ pointers (and whitespace) only
r'\n[ ~^]+\n',
'\n',
r.exc_text.replace( # type: ignore
user_code_example.__file__,
'user_code_example.py',
).replace(
internal_logfire_code_example.__file__,
'internal_logfire_code_example.py',
),
)
for r in caplog.records
]
# Important notes about these tracebacks:
# - They should look very similar to each other, regardless of how log_internal_error was called.
# - They should include all frames from internal_logfire_code_example.py.
# - They should include exactly 3 frames from user_code_example.py.
# - They should look seamless, with each frame pointing to the next one.
# - There should be no sign of logfire's internal error handling code.
# - The two files should be isolated and stable so that the exact traceback contents can be asserted.
assert tracebacks == snapshot(
[
"""\
Traceback (most recent call last):
File "user_code_example.py", line 22, in user4
user5()
File "user_code_example.py", line 26, in user5
user6()
File "user_code_example.py", line 30, in user6
outer2(using_decorator)
File "internal_logfire_code_example.py", line 36, in outer2
outer1(func)
File "internal_logfire_code_example.py", line 32, in outer1
func()
File "internal_logfire_code_example.py", line 16, in using_decorator
inner2()
File "internal_logfire_code_example.py", line 11, in inner2
inner1()
File "internal_logfire_code_example.py", line 7, in inner1
raise ValueError('inner1')
ValueError: inner1\
""",
"""\
Traceback (most recent call last):
File "user_code_example.py", line 22, in user4
user5()
File "user_code_example.py", line 26, in user5
user6()
File "user_code_example.py", line 31, in user6
outer2(using_context_manager)
File "internal_logfire_code_example.py", line 36, in outer2
outer1(func)
File "internal_logfire_code_example.py", line 32, in outer1
func()
File "internal_logfire_code_example.py", line 21, in using_context_manager
inner2()
File "internal_logfire_code_example.py", line 11, in inner2
inner1()
File "internal_logfire_code_example.py", line 7, in inner1
raise ValueError('inner1')
ValueError: inner1\
""",
"""\
Traceback (most recent call last):
File "user_code_example.py", line 22, in user4
user5()
File "user_code_example.py", line 26, in user5
user6()
File "user_code_example.py", line 32, in user6
outer2(using_try_except)
File "internal_logfire_code_example.py", line 36, in outer2
outer1(func)
File "internal_logfire_code_example.py", line 32, in outer1
func()
File "internal_logfire_code_example.py", line 26, in using_try_except
inner2()
File "internal_logfire_code_example.py", line 11, in inner2
inner1()
File "internal_logfire_code_example.py", line 7, in inner1
raise ValueError('inner1')
ValueError: inner1\
""",
]
)
def test_internal_error_base_exception():
with pytest.raises(BaseException):
with handle_internal_errors:
raise BaseException('base exception')