forked from getsentry/sentry-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_general.py
More file actions
113 lines (81 loc) · 2.86 KB
/
test_general.py
File metadata and controls
113 lines (81 loc) · 2.86 KB
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
# coding: utf-8
import sys
import os
import pytest
from hypothesis import given
import hypothesis.strategies as st
from sentry_sdk.utils import (
safe_repr,
exceptions_from_error_tuple,
format_and_strip,
strip_string,
filename_for_module,
)
from sentry_sdk._compat import text_type
any_string = st.one_of(st.binary(), st.text())
@given(x=any_string)
def test_safe_repr_never_broken_for_strings(x):
r = safe_repr(x)
assert isinstance(r, text_type)
assert u"broken repr" not in r
def test_safe_repr_regressions():
assert u"лошадь" in safe_repr(u"лошадь")
def test_abs_path():
"""Check if abs_path is actually an absolute path. This can happen either
with eval/exec like here, or when the file in the frame is relative to
__main__"""
code = compile("1/0", "test.py", "exec")
try:
exec(code, {})
except Exception:
exceptions = exceptions_from_error_tuple(sys.exc_info())
exception, = exceptions
frame1, frame2 = frames = exception["stacktrace"]["frames"]
for frame in frames:
assert os.path.abspath(frame["abs_path"]) == frame["abs_path"]
assert frame1["filename"] == "tests/utils/test_general.py"
assert frame2["filename"] == "test.py"
def test_non_string_variables():
"""There is some extremely terrible code in the wild that
inserts non-strings as variable names into `locals()`."""
try:
locals()[42] = True
1 / 0
except ZeroDivisionError:
exceptions = exceptions_from_error_tuple(sys.exc_info())
exception, = exceptions
assert exception["type"] == "ZeroDivisionError"
frame, = exception["stacktrace"]["frames"]
assert frame["vars"]["42"] == "True"
def test_format_and_strip():
max_length = None
def x(template, params):
return format_and_strip(
template,
params,
strip_string=lambda x: strip_string(x, max_length=max_length),
)
max_length = 3
assert x("", []) == ""
assert x("f", []) == "f"
pytest.raises(ValueError, lambda: x("%s", []))
# Don't raise errors on leftover params, some django extensions send too
# many SQL parameters.
assert x("", [123]) == ""
assert x("foo%s", ["bar"]) == "foobar"
rv = x("foo%s", ["baer"])
assert rv.value == "foo..."
assert rv.metadata == {"len": 7, "rem": [["!limit", "x", 3, 6]]}
rv = x("foo%sbar%s", ["baer", "boor"])
assert rv.value == "foo...bar..."
assert rv.metadata == {
"len": 14,
"rem": [["!limit", "x", 3, 6], ["!limit", "x", 9, 12]],
}
def test_filename():
x = filename_for_module
assert x("bogus", "bogus") == "bogus"
assert x("os", os.__file__) == "os.py"
assert x("pytest", pytest.__file__) == "pytest.py"
import sentry_sdk.utils
assert x("sentry_sdk.utils", sentry_sdk.utils.__file__) == "sentry_sdk/utils.py"