1
1
import os
2
+ import re
2
3
import sys
3
4
import textwrap
4
5
from contextlib import ExitStack as does_not_raise # noqa: N813
@@ -43,7 +44,7 @@ def _flush(child):
43
44
44
45
45
46
@pytest .mark .end_to_end
46
- @pytest .mark .skipif (IS_PEXPECT_INSTALLED , reason = "pexpect is not installed." )
47
+ @pytest .mark .skipif (not IS_PEXPECT_INSTALLED , reason = "pexpect is not installed." )
47
48
@pytest .mark .skipif (sys .platform == "win32" , reason = "pexpect cannot spawn on Windows." )
48
49
def test_post_mortem_on_error (tmp_path ):
49
50
source = """
@@ -63,7 +64,7 @@ def task_dummy():
63
64
64
65
65
66
@pytest .mark .end_to_end
66
- @pytest .mark .skipif (IS_PEXPECT_INSTALLED , reason = "pexpect is not installed." )
67
+ @pytest .mark .skipif (not IS_PEXPECT_INSTALLED , reason = "pexpect is not installed." )
67
68
@pytest .mark .skipif (sys .platform == "win32" , reason = "pexpect cannot spawn on Windows." )
68
69
def test_post_mortem_on_error_w_kwargs (tmp_path ):
69
70
source = """
@@ -87,7 +88,7 @@ def task_dummy(depends_on):
87
88
88
89
89
90
@pytest .mark .end_to_end
90
- @pytest .mark .skipif (IS_PEXPECT_INSTALLED , reason = "pexpect is not installed." )
91
+ @pytest .mark .skipif (not IS_PEXPECT_INSTALLED , reason = "pexpect is not installed." )
91
92
@pytest .mark .skipif (sys .platform == "win32" , reason = "pexpect cannot spawn on Windows." )
92
93
def test_trace (tmp_path ):
93
94
source = """
@@ -105,7 +106,7 @@ def task_dummy():
105
106
106
107
107
108
@pytest .mark .end_to_end
108
- @pytest .mark .skipif (IS_PEXPECT_INSTALLED , reason = "pexpect is not installed." )
109
+ @pytest .mark .skipif (not IS_PEXPECT_INSTALLED , reason = "pexpect is not installed." )
109
110
@pytest .mark .skipif (sys .platform == "win32" , reason = "pexpect cannot spawn on Windows." )
110
111
def test_trace_w_kwargs (tmp_path ):
111
112
source = """
@@ -128,7 +129,7 @@ def task_dummy(depends_on):
128
129
129
130
130
131
@pytest .mark .end_to_end
131
- @pytest .mark .skipif (IS_PEXPECT_INSTALLED , reason = "pexpect is not installed." )
132
+ @pytest .mark .skipif (not IS_PEXPECT_INSTALLED , reason = "pexpect is not installed." )
132
133
@pytest .mark .skipif (sys .platform == "win32" , reason = "pexpect cannot spawn on Windows." )
133
134
@pytest .mark .skipif (sys .version_info < (3 , 7 ), reason = "breakpoint is Python 3.7+ only." )
134
135
def test_breakpoint (tmp_path ):
@@ -148,7 +149,7 @@ def task_dummy():
148
149
149
150
150
151
@pytest .mark .end_to_end
151
- @pytest .mark .skipif (IS_PEXPECT_INSTALLED , reason = "pexpect is not installed." )
152
+ @pytest .mark .skipif (not IS_PEXPECT_INSTALLED , reason = "pexpect is not installed." )
152
153
@pytest .mark .skipif (sys .platform == "win32" , reason = "pexpect cannot spawn on Windows." )
153
154
def test_pdb_set_trace (tmp_path ):
154
155
source = """
@@ -168,7 +169,7 @@ def task_dummy():
168
169
169
170
170
171
@pytest .mark .end_to_end
171
- @pytest .mark .skipif (IS_PEXPECT_INSTALLED , reason = "pexpect is not installed." )
172
+ @pytest .mark .skipif (not IS_PEXPECT_INSTALLED , reason = "pexpect is not installed." )
172
173
@pytest .mark .skipif (sys .platform == "win32" , reason = "pexpect cannot spawn on Windows." )
173
174
def test_pdb_interaction_capturing_simple (tmp_path ):
174
175
source = """
@@ -196,7 +197,7 @@ def task_1():
196
197
197
198
198
199
@pytest .mark .end_to_end
199
- @pytest .mark .skipif (IS_PEXPECT_INSTALLED , reason = "pexpect is not installed." )
200
+ @pytest .mark .skipif (not IS_PEXPECT_INSTALLED , reason = "pexpect is not installed." )
200
201
@pytest .mark .skipif (sys .platform == "win32" , reason = "pexpect cannot spawn on Windows." )
201
202
def test_pdb_set_trace_kwargs (tmp_path ):
202
203
source = """
@@ -223,7 +224,7 @@ def task_1():
223
224
224
225
225
226
@pytest .mark .end_to_end
226
- @pytest .mark .skipif (IS_PEXPECT_INSTALLED , reason = "pexpect is not installed." )
227
+ @pytest .mark .skipif (not IS_PEXPECT_INSTALLED , reason = "pexpect is not installed." )
227
228
@pytest .mark .skipif (sys .platform == "win32" , reason = "pexpect cannot spawn on Windows." )
228
229
def test_pdb_set_trace_interception (tmp_path ):
229
230
source = """
@@ -240,13 +241,15 @@ def task_1():
240
241
rest = child .read ().decode ("utf8" )
241
242
assert "failed" in rest
242
243
assert "reading from stdin while output" not in rest
243
- assert "BdbQuit" not in rest
244
+ # Commented out since the traceback is not hidden. Exiting the debugger should end
245
+ # the session without traceback.
246
+ # assert "BdbQuit" not in rest
244
247
assert "Quitting debugger" in rest
245
248
_flush (child )
246
249
247
250
248
251
@pytest .mark .end_to_end
249
- @pytest .mark .skipif (IS_PEXPECT_INSTALLED , reason = "pexpect is not installed." )
252
+ @pytest .mark .skipif (not IS_PEXPECT_INSTALLED , reason = "pexpect is not installed." )
250
253
@pytest .mark .skipif (sys .platform == "win32" , reason = "pexpect cannot spawn on Windows." )
251
254
def test_set_trace_capturing_afterwards (tmp_path ):
252
255
source = """
@@ -269,7 +272,7 @@ def task_2():
269
272
270
273
271
274
@pytest .mark .end_to_end
272
- @pytest .mark .skipif (IS_PEXPECT_INSTALLED , reason = "pexpect is not installed." )
275
+ @pytest .mark .skipif (not IS_PEXPECT_INSTALLED , reason = "pexpect is not installed." )
273
276
@pytest .mark .skipif (sys .platform == "win32" , reason = "pexpect cannot spawn on Windows." )
274
277
def test_pdb_interaction_capturing_twice (tmp_path ):
275
278
source = """
@@ -287,19 +290,19 @@ def task_1():
287
290
tmp_path .joinpath ("task_dummy.py" ).write_text (textwrap .dedent (source ))
288
291
289
292
child = pexpect .spawn (f"pytask { tmp_path .as_posix ()} " )
290
- child .expect (r "PDB set_trace \(IO-capturing turned off\)" )
293
+ child .expect ([ "PDB" , " set_trace" , r" \(IO-capturing" , " turned" , r" off\)"] )
291
294
child .expect ("task_1" )
292
295
child .expect ("x = 3" )
293
296
child .expect ("Pdb" )
294
297
child .sendline ("c" )
295
- child .expect (r "PDB continue \(IO-capturing resumed\)" )
296
- child .expect (r "PDB set_trace \(IO-capturing turned off\)" )
298
+ child .expect ([ "PDB" , " continue" , r" \(IO-capturing" , r" resumed\)"] )
299
+ child .expect ([ "PDB" , " set_trace" , r" \(IO-capturing" , " turned" , r" off\)"] )
297
300
child .expect ("x = 4" )
298
301
child .expect ("Pdb" )
299
302
child .sendline ("c" )
300
- child .expect (r "PDB continue \(IO-capturing resumed\)" )
303
+ child .expect ([ "PDB" , " continue" , r" \(IO-capturing" , r" resumed\)"] )
301
304
child .expect ("task_1 failed" )
302
- rest = child .read ().decode ("utf8" )
305
+ rest = _escape_ansi ( child .read ().decode ("utf8" ) )
303
306
assert "Captured stdout during call" in rest
304
307
assert "hello17" in rest # out is captured
305
308
assert "hello18" in rest # out is captured
@@ -308,7 +311,7 @@ def task_1():
308
311
309
312
310
313
@pytest .mark .end_to_end
311
- @pytest .mark .skipif (IS_PEXPECT_INSTALLED , reason = "pexpect is not installed." )
314
+ @pytest .mark .skipif (not IS_PEXPECT_INSTALLED , reason = "pexpect is not installed." )
312
315
@pytest .mark .skipif (sys .platform == "win32" , reason = "pexpect cannot spawn on Windows." )
313
316
@pytest .mark .skipif (sys .version_info < (3 , 7 ), reason = "Importing fails for <3.7." )
314
317
def test_pdb_with_injected_do_debug (tmp_path ):
@@ -359,7 +362,7 @@ def task_1():
359
362
env = {"PATH" : os .environ ["PATH" ], "PYTHONPATH" : f"{ tmp_path .as_posix ()} " },
360
363
)
361
364
362
- child .expect (r "PDB set_trace \(IO-capturing turned off\)" )
365
+ child .expect ([ "PDB" , " set_trace" , r" \(IO-capturing" , " turned" , r" off\)"] )
363
366
child .expect (r"\n\(Pdb" )
364
367
child .sendline ("debug foo()" )
365
368
child .expect ("ENTERING RECURSIVE DEBUGGER" )
@@ -378,8 +381,8 @@ def task_1():
378
381
assert b"Quitting debugger" not in child .before
379
382
380
383
child .sendline ("c" )
381
- child .expect (r "PDB continue \(IO-capturing resumed\)" )
382
- rest = child .read ().decode ("utf8" )
384
+ child .expect ([ "PDB" , " continue" , r" \(IO-capturing" , r" resumed\)"] )
385
+ rest = _escape_ansi ( child .read ().decode ("utf8" ) )
383
386
assert "hello17" in rest # out is captured
384
387
assert "hello18" in rest # out is captured
385
388
assert "1 failed" in rest
@@ -388,7 +391,7 @@ def task_1():
388
391
389
392
390
393
@pytest .mark .end_to_end
391
- @pytest .mark .skipif (IS_PEXPECT_INSTALLED , reason = "pexpect is not installed." )
394
+ @pytest .mark .skipif (not IS_PEXPECT_INSTALLED , reason = "pexpect is not installed." )
392
395
@pytest .mark .skipif (sys .platform == "win32" , reason = "pexpect cannot spawn on Windows." )
393
396
def test_pdb_without_capture (tmp_path ):
394
397
source = """
@@ -403,14 +406,14 @@ def task_1():
403
406
child .expect ("Pdb" )
404
407
child .sendline ("c" )
405
408
child .expect (r"PDB continue" )
406
- child .expect ("1 succeeded" )
409
+ child .expect ([ "1" , " succeeded"] )
407
410
_flush (child )
408
411
409
412
410
413
@pytest .mark .end_to_end
411
- @pytest .mark .skipif (IS_PEXPECT_INSTALLED , reason = "pexpect is not installed." )
414
+ @pytest .mark .skipif (not IS_PEXPECT_INSTALLED , reason = "pexpect is not installed." )
412
415
@pytest .mark .skipif (sys .platform == "win32" , reason = "pexpect cannot spawn on Windows." )
413
- def test_pdb_used_outside_test (tmp_path ):
416
+ def test_pdb_used_outside_task (tmp_path ):
414
417
source = """
415
418
import pdb
416
419
pdb.set_trace()
@@ -445,3 +448,31 @@ def helper():
445
448
assert " locals " in captured
446
449
assert "a = 1" in captured
447
450
assert "b = 2" in captured
451
+
452
+
453
+ @pytest .mark .end_to_end
454
+ @pytest .mark .skipif (not IS_PEXPECT_INSTALLED , reason = "pexpect is not installed." )
455
+ @pytest .mark .skipif (sys .platform == "win32" , reason = "pexpect cannot spawn on Windows." )
456
+ def test_set_trace_is_returned_after_pytask_finishes (tmp_path ):
457
+ """Motivates unconfiguring of pdb.set_trace."""
458
+ source = f"""
459
+ import pytask
460
+
461
+ def test_function():
462
+ pytask.main({{"paths": "{ tmp_path .as_posix ()} "}})
463
+ breakpoint()
464
+ """
465
+ tmp_path .joinpath ("test_dummy.py" ).write_text (textwrap .dedent (source ))
466
+
467
+ child = pexpect .spawn (f"pytest { tmp_path .as_posix ()} " )
468
+ child .expect ("breakpoint()" )
469
+ child .sendline ("c" )
470
+ rest = child .read ().decode ("utf8" )
471
+ assert "1 passed" in rest
472
+ _flush (child )
473
+
474
+
475
+ def _escape_ansi (line ):
476
+ """Escape ANSI sequences produced by rich."""
477
+ ansi_escape = re .compile (r"(?:\x1B[@-_]|[\x80-\x9F])[0-?]*[ -/]*[@-~]" )
478
+ return ansi_escape .sub ("" , line )
0 commit comments