From f529ed6df994140f038138505416f6035e1ebc22 Mon Sep 17 00:00:00 2001 From: Daniel Szoke Date: Wed, 10 Jul 2024 17:49:12 +0200 Subject: [PATCH] test: Remove `Hub` usage in `conftest` --- tests/conftest.py | 15 +++++++++------ tests/test_new_scopes_compat.py | 8 ++++++++ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 8a4af3e98c..048f8bc140 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -186,10 +186,9 @@ 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 @@ -197,8 +196,12 @@ def inner(*a, **kw): # 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): @@ -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): @@ -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): @@ -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 diff --git a/tests/test_new_scopes_compat.py b/tests/test_new_scopes_compat.py index 21e2ac27d3..77f5401b2d 100644 --- a/tests/test_new_scopes_compat.py +++ b/tests/test_new_scopes_compat.py @@ -1,3 +1,5 @@ +import pytest + import sentry_sdk from sentry_sdk.hub import Hub @@ -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.