@@ -191,6 +191,12 @@ def close(self):
191191 raise self .close_error
192192
193193
194+ def _use_client (monkeypatch , client ):
195+ monkeypatch .setattr (
196+ SandboxClient , "from_active_cluster" , classmethod (lambda _cls , ** _kw : client )
197+ )
198+
199+
194200@pytest .mark .parametrize (
195201 ("fail_at" , "kwargs" ),
196202 [
@@ -203,9 +209,7 @@ def close(self):
203209)
204210def test_context_entry_failure_closes_client (monkeypatch , fail_at , kwargs ):
205211 client = _EntryFailureClient (fail_at )
206- monkeypatch .setattr (
207- SandboxClient , "from_active_cluster" , classmethod (lambda _cls , ** _kw : client )
208- )
212+ _use_client (monkeypatch , client )
209213 managed = Sandbox (workspace = "default" , delete_on_exit = False , ** kwargs )
210214
211215 with pytest .raises (RuntimeError ) as observed , managed :
@@ -219,9 +223,7 @@ def test_context_entry_failure_closes_client(monkeypatch, fail_at, kwargs):
219223
220224def test_context_entry_failure_keeps_original_error_when_close_fails (monkeypatch ):
221225 client = _EntryFailureClient ("wait_ready" , close_error = OSError ("close failed" ))
222- monkeypatch .setattr (
223- SandboxClient , "from_active_cluster" , classmethod (lambda _cls , ** _kw : client )
224- )
226+ _use_client (monkeypatch , client )
225227 managed = Sandbox (workspace = "default" )
226228
227229 with pytest .raises (RuntimeError ) as observed , managed :
@@ -235,9 +237,7 @@ def test_context_entry_failure_keeps_original_error_when_close_fails(monkeypatch
235237
236238def test_context_closes_client_once_on_success (monkeypatch ):
237239 client = _EntryFailureClient (None )
238- monkeypatch .setattr (
239- SandboxClient , "from_active_cluster" , classmethod (lambda _cls , ** _kw : client )
240- )
240+ _use_client (monkeypatch , client )
241241
242242 with Sandbox (workspace = "default" , delete_on_exit = False ):
243243 assert client .closed == 0
0 commit comments