|
3 | 3 | import sys |
4 | 4 | import subprocess |
5 | 5 | from textwrap import dedent |
6 | | -from sentry_sdk import Client |
| 6 | +from sentry_sdk import init, Hub, Client, configure_scope |
| 7 | +from sentry_sdk.hub import HubMeta |
7 | 8 | from sentry_sdk.transport import Transport |
8 | 9 | from sentry_sdk.utils import Event, Dsn |
9 | 10 |
|
@@ -77,3 +78,46 @@ def send_event(pool, event, auth): |
77 | 78 | end = time.time() |
78 | 79 | assert int(end - start) == num_messages / 10 |
79 | 80 | assert output.count(b"HI") == num_messages |
| 81 | + |
| 82 | + |
| 83 | +def test_configure_scope_available(request, monkeypatch): |
| 84 | + # Test that scope is configured if client is configured |
| 85 | + init() |
| 86 | + request.addfinalizer(lambda: Hub.current.bind_client(None)) |
| 87 | + |
| 88 | + with configure_scope() as scope: |
| 89 | + assert scope is Hub.current._stack[-1][1] |
| 90 | + scope.set_tag("foo", "bar") |
| 91 | + |
| 92 | + calls = [] |
| 93 | + |
| 94 | + def callback(scope): |
| 95 | + calls.append(scope) |
| 96 | + scope.set_tag("foo", "bar") |
| 97 | + |
| 98 | + assert configure_scope(callback) is None |
| 99 | + assert len(calls) == 1 |
| 100 | + assert calls[0] is Hub.current._stack[-1][1] |
| 101 | + |
| 102 | + |
| 103 | +@pytest.mark.parametrize("no_sdk", (True, False)) |
| 104 | +def test_configure_scope_unavailable(no_sdk, monkeypatch): |
| 105 | + if no_sdk: |
| 106 | + # Emulate sentry_minimal without SDK installation: callbacks are not called |
| 107 | + monkeypatch.setattr(HubMeta, "current", None) |
| 108 | + assert not Hub.current |
| 109 | + else: |
| 110 | + # Still, no client configured |
| 111 | + assert Hub.current |
| 112 | + |
| 113 | + calls = [] |
| 114 | + |
| 115 | + def callback(scope): |
| 116 | + calls.append(scope) |
| 117 | + scope.set_tag("foo", "bar") |
| 118 | + |
| 119 | + with configure_scope() as scope: |
| 120 | + scope.set_tag("foo", "bar") |
| 121 | + |
| 122 | + assert configure_scope(callback) is None |
| 123 | + assert not calls |
0 commit comments