11import madbg
2- from unittest .mock import Mock
32from pytest import raises
43
54from madbg .debugger import RemoteIPythonDebugger
65
7- from .utils import run_in_process , run_script_in_process , JOIN_TIMEOUT , run_client
6+ from .utils import run_in_process , run_script_in_process , run_client
87
98
10- def set_trace_script (port , times = 1 ):
9+ def set_trace_script (port , times = 1 , debugger_fails = False ):
10+ if debugger_fails :
11+ RemoteIPythonDebugger .__init__ = lambda * a , ** k : 1 / 0
1112 for _ in range (times ):
1213 madbg .set_trace (port = port )
1314
@@ -22,38 +23,27 @@ def set_trace_and_expect_var_to_change_script(port) -> bool:
2223
2324
2425def test_set_trace (port , start_debugger_with_ctty ):
25- debugger_future = run_script_in_process (set_trace_and_expect_var_to_change_script , start_debugger_with_ctty , port )
26- client_future = run_in_process (run_client , port , b'value_to_change += 1\n c\n ' )
27- assert debugger_future .result (JOIN_TIMEOUT )
28- client_output = client_future .result (JOIN_TIMEOUT )
29- assert b'Closing connection' in client_output
26+ with run_script_in_process (set_trace_and_expect_var_to_change_script , start_debugger_with_ctty , port ):
27+ assert b'Closing connection' in run_in_process (run_client , port , b'value_to_change += 1\n c\n ' ).finish ().get (0 )
3028
3129
3230def test_set_trace_and_connect_twice (port , start_debugger_with_ctty ):
33- debugger_future = run_script_in_process (set_trace_script , start_debugger_with_ctty , port , 2 )
34- assert b'Closing connection' in run_in_process (run_client , port , b'q\n ' ).result (JOIN_TIMEOUT )
35- assert b'Closing connection' in run_in_process (run_client , port , b'q\n ' ).result (JOIN_TIMEOUT )
36- debugger_future .result (JOIN_TIMEOUT )
31+ with run_script_in_process (set_trace_script , start_debugger_with_ctty , port , 2 ):
32+ assert b'Closing connection' in run_in_process (run_client , port , b'q\n ' ).finish ().get (0 )
33+ assert b'Closing connection' in run_in_process (run_client , port , b'q\n ' ).finish ().get (0 )
3734
3835
3936def test_set_trace_twice_and_continue (port , start_debugger_with_ctty ):
40- debugger_future = run_script_in_process (set_trace_script , start_debugger_with_ctty , port , 2 )
41- assert b'Closing connection' in run_in_process (run_client , port , b'c\n q\n ' ).result (JOIN_TIMEOUT )
42- debugger_future .result (JOIN_TIMEOUT )
37+ with run_script_in_process (set_trace_script , start_debugger_with_ctty , port , 2 ):
38+ assert b'Closing connection' in run_in_process (run_client , port , b'c\n q\n ' ).finish ().get (0 )
4339
4440
4541def test_set_trace_and_quit_debugger (port , start_debugger_with_ctty ):
46- debugger_future = run_script_in_process (set_trace_script , start_debugger_with_ctty , port )
47- client_future = run_in_process (run_client , port , b'q\n ' )
48- debugger_future .result (JOIN_TIMEOUT )
49- client_future .result (JOIN_TIMEOUT )
42+ with run_script_in_process (set_trace_script , start_debugger_with_ctty , port ):
43+ run_in_process (run_client , port , b'q\n ' ).finish ()
5044
5145
5246def test_set_trace_with_failing_debugger (port , start_debugger_with_ctty , monkeypatch ):
53- monkeypatch .setattr (RemoteIPythonDebugger , '__init__' , Mock (side_effect = lambda * a , ** k : 1 / 0 ))
54- debugger_future = run_script_in_process (set_trace_script , start_debugger_with_ctty , port )
55- client_future = run_in_process (run_client , port , b'bla\n ' )
5647 with raises (ZeroDivisionError ):
57- debugger_future .result (JOIN_TIMEOUT )
58- client_output = client_future .result (JOIN_TIMEOUT )
59- assert ZeroDivisionError .__name__ .encode () in client_output
48+ with run_script_in_process (set_trace_script , start_debugger_with_ctty , port , debugger_fails = True ) as script_result :
49+ assert ZeroDivisionError .__name__ .encode () in run_in_process (run_client , port , b'bla\n ' ).finish ().get (0 )
0 commit comments