8000 test: Treat warnings as errors (#416) · adamchainz/sentry-python@de96351 · GitHub
[go: up one dir, main page]

Skip to content

Commit de96351

Browse files
authored
test: Treat warnings as errors (getsentry#416)
Fix getsentry#415
1 parent 4c3f8f7 commit de96351

File tree

7 files changed

+87
-17
lines changed

7 files changed

+87
-17
lines changed

sentry_sdk/integrations/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def setup_integrations(integrations, with_defaults=True):
8282
type(integration).setup_once()
8383
except NotImplementedError:
8484
if getattr(integration, "install", None) is not None:
85-
logger.warn(
85+
logger.warning(
8686
"Integration %s: The install method is "
8787
"deprecated. Use `setup_once`.",
8888
identifier,

sentry_sdk/integrations/flask.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ def cookies(self):
131131

132132
def raw_data(self):
133133
# type: () -> bytes
134-
return self.request.data
134+
return self.request.get_data()
135135

136136
def form(self):
137137
# type: () -> ImmutableMultiDict

sentry_sdk/tracing.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
import re
22
import uuid
33
import contextlib
4-
import collections
54

65
from datetime import datetime
76

87
from sentry_sdk.utils import capture_internal_exceptions, concat_strings
8+
from sentry_sdk._compat import PY2
99

10+
if PY2:
11+
from collections import Mapping
12+
else:
13+
from collections.abc import Mapping
1014

1115
if False:
16+
import typing
17+
1218
from typing import Optional
1319
from typing import Any
1420
from typing import Dict
15-
from typing import Mapping
1621
from typing import List
1722

1823
_traceparent_header_format_re = re.compile(
@@ -24,10 +29,10 @@
2429
)
2530

2631

27-
class EnvironHeaders(collections.Mapping): # type: ignore
32+
class EnvironHeaders(Mapping): # type: ignore
2833
def __init__(
2934
self,
30-
environ, # type: Mapping[str, str]
35+
environ, # type: typing.Mapping[str, str]
3136
prefix="HTTP_", # type: str
3237
):
3338
# type: (...) -> None

tests/__init__.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import sys
2+
3+
import pytest
4+
5+
# This is used in _capture_internal_warnings. We need to run this at import
6+
# time because that's where many deprecation warnings might get thrown.
7+
#
8+
# This lives in tests/__init__.py because apparently even tests/conftest.py
9+
# gets loaded too late.
10+
assert "sentry_sdk" not in< 629A /span> sys.modules
11+
12+
_warning_recorder_mgr = pytest.warns(None)
13+
_warning_recorder = _warning_recorder_mgr.__enter__()

tests/conftest.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
from sentry_sdk._compat import reraise, string_types, iteritems
99
from sentry_sdk.transport import Transport
1010

11+
from tests import _warning_recorder, _warning_recorder_mgr
12+
1113
SEMAPHORE = "./semaphore"
1214

1315
if not os.path.isfile(SEMAPHORE):
@@ -48,6 +50,56 @@ def _():
4850
return errors
4951

5052

53+
@pytest.fixture(autouse=True, scope="session")
54+
def _capture_internal_warnings():
55+
yield
56+
57+
_warning_recorder_mgr.__exit__(None, None, None)
58+
recorder = _warning_recorder
59+
60+
for warning in recorder:
61+
try:
62+
if isinstance(warning.message, ResourceWarning):
63+
continue
64+
except NameError:
65+
pass
66+
67+
# pytest-django
68+
if "getfuncargvalue" in str(warning.message):
69+
continue
70+
71+
# Happens when re-initializing the SDK
72+
if "but it was only enabled on init()" in str(warning.message):
73+
continue
74+
75+
# sanic's usage of aiohttp for test client
76+
if "verify_ssl is deprecated, use ssl=False instead" in str(warning.message):
77+
continue
78+
79+
if "getargspec" in str(warning.message) and warning.filename.endswith(
80+
("pyramid/config/util.py", "pyramid/config/views.py")
81+
):
82+
continue
83+
84+
if "isAlive() is deprecated" in str(
85+
warning.message
86+
) and warning.filename.endswith("celery/utils/timer2.py"):
87+
continue
88+
89+
if "collections.abc" in str(warning.message) and warning.filename.endswith(
90+
("celery/canvas.py", "werkzeug/datastructures.py", "tornado/httputil.py")
91+
):
92+
continue
93+
94+
# Django 1.7 emits a (seemingly) false-positive warning for our test
95+
# app and suggests to use a middleware that does not exist in later
96+
# Django versions.
97+
if "SessionAuthenticationMiddleware" in str(warning.message):
98+
continue
99+
100+
raise AssertionError(warning)
101+
102+
51103
@pytest.fixture
52104
def monkeypatch_test_transport(monkeypatch, assert_semaphore_acceptance):
53105
def check_event(event):

tests/integrations/flask/test_flask.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,8 @@ def test_flask_large_json_request(sentry_init, capture_events, app):
195195

196196
@app.route("/", methods=["POST"])
197197
def index():
198-
assert request.json == data
199-
assert request.data == json.dumps(data).encode("ascii")
198+
assert request.get_json() == data
199+
assert request.get_data() == json.dumps(data).encode("ascii")
200200
assert not request.form
201201
capture_message("hi")
202202
return "ok"
@@ -220,8 +220,8 @@ def test_flask_empty_json_request(sentry_init, capture_events, app, data):
220220

221221
@app.route("/", methods=["POST"])
222222
def index():
223-
assert request.json == data
224-
assert request.data == json.dumps(data).encode("ascii")
223+
assert request.get_json() == data
224+
assert request.get_data() == json.dumps(data).encode("ascii")
225225
assert not request.form
226226
capture_message("hi")
227227
return "ok"
@@ -244,8 +244,8 @@ def test_flask_medium_formdata_request(sentry_init, capture_events, app):
244244
@app.route("/", methods=["POST"])
245245
def index():
246246
assert request.form["foo"] == data["foo"]
247-
assert not request.data
248-
assert not request.json
247+
assert not request.get_data()
248+
assert not request.get_json()
249249
capture_message("hi")
250250
return "ok"
251251

@@ -272,10 +272,10 @@ def test_flask_too_large_raw_request(sentry_init, input_char, capture_events, ap
272272
def index():
273273
assert not request.form
274274
if isinstance(data, bytes):
275-
assert request.data == data
275+
assert request.get_data() == data
276276
else:
277-
assert request.data == data.encode("ascii")
278-
assert not request.json
277+
assert request.get_data() == data.encode("ascii")
278+
assert not request.get_json()
279279
capture_message("hi")
280280
return "ok"
281281

@@ -301,7 +301,7 @@ def test_flask_files_and_form(sentry_init, capture_events, app):
301301
def index():
302302
assert list(request.form) == ["foo"]
303303
assert list(request.files) == ["file"]
304-
assert not request.json
304+
assert not request.get_json()
305305
capture_message("hi")
306306
return "ok"
307307

tests/integrations/logging/test_logging.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def test_logging_level(sentry_init, capture_events):
9191
del events[:]
9292

9393
logger.setLevel(logging.ERROR)
94-
logger.warn("hi")
94+
logger.warning("hi")
9595
assert not events
9696

9797

0 commit comments

Comments
 (0)
0