Skip to content

Commit

Permalink
test: Remove Hub usage in conftest
Browse files Browse the repository at this point in the history
  • Loading branch information
szokeasaurusrex committed Jul 11, 2024
1 parent c359c82 commit cfcd5b1
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 8 deletions.
15 changes: 9 additions & 6 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,19 +186,22 @@ def reset_integrations():
@pytest.fixture
def sentry_init(request):
def inner(*a, **kw):
hub = sentry_sdk.Hub.current
kw.setdefault("transport", TestTransport())
client = sentry_sdk.Client(*a, **kw)
hub.bind_client(client)
sentry_sdk.Scope.get_global_scope().set_client(client)

if request.node.get_closest_marker("forked"):
# Do not run isolation if the test is already running in
# ultimate isolation (seems to be required for celery tests that
# fork)
yield inner
else:
with sentry_sdk.Hub(None):
old_client = sentry_sdk.Scope.get_global_scope().client
try:
sentry_sdk.Scope.get_current_scope().set_client(None)
yield inner
finally:
sentry_sdk.Scope.get_global_scope().set_client(old_client)


class TestTransport(Transport):
Expand All @@ -214,7 +217,7 @@ def capture_envelope(self, _: Envelope) -> None:
def capture_events(monkeypatch):
def inner():
events = []
test_client = sentry_sdk.Hub.current.client
test_client = sentry_sdk.get_client()
old_capture_envelope = test_client.transport.capture_envelope

def append_event(envelope):
Expand All @@ -234,7 +237,7 @@ def append_event(envelope):
def capture_envelopes(monkeypatch):
def inner():
envelopes = []
test_client = sentry_sdk.Hub.current.client
test_client = sentry_sdk.get_client()
old_capture_envelope = test_client.transport.capture_envelope

def append_envelope(envelope):
Expand Down Expand Up @@ -274,7 +277,7 @@ def inner():
events_r = os.fdopen(events_r, "rb", 0)
events_w = os.fdopen(events_w, "wb", 0)

test_client = sentry_sdk.Hub.current.client
test_client = sentry_sdk.get_client()

old_capture_envelope = test_client.transport.capture_envelope

Expand Down
7 changes: 7 additions & 0 deletions tests/new_scopes_compat/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"""
Separate module for tests that check backwards compatibility of the Hub API with 1.x.
These tests should be removed once we remove the Hub API, likely in the next major.
All tests in this module are run with hub isolation, provided by `isolate_hub` autouse
fixture, defined in `conftest.py`.
"""
8 changes: 8 additions & 0 deletions tests/new_scopes_compat/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import pytest
import sentry_sdk


@pytest.fixture(autouse=True)
def isolate_hub():
with sentry_sdk.Hub(None):
yield
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ def create_expected_error_event(trx, span):
"stacktrace": {
"frames": [
{
"filename": "tests/test_new_scopes_compat_event.py",
"filename": "tests/new_scopes_compat/test_new_scopes_compat_event.py",
"abs_path": mock.ANY,
"function": "_faulty_function",
"module": "tests.test_new_scopes_compat_event",
"module": "tests.new_scopes_compat.test_new_scopes_compat_event",
"lineno": mock.ANY,
"pre_context": [
" return create_expected_transaction_event",
Expand Down

0 comments on commit cfcd5b1

Please sign in to comment.