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 10, 2024
1 parent 1c86489 commit f529ed6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 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
8 changes: 8 additions & 0 deletions tests/test_new_scopes_compat.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import pytest

import sentry_sdk
from sentry_sdk.hub import Hub

Expand All @@ -11,6 +13,12 @@
"""


@pytest.fixture(autouse=True)
def isolate_hub():
with sentry_sdk.Hub(None):
yield


def test_configure_scope_sdk1(sentry_init, capture_events):
"""
Mutate data in a `with configure_scope` block.
Expand Down

0 comments on commit f529ed6

Please sign in to comment.