-
-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathtest_debug_console.py
More file actions
1235 lines (953 loc) · 49.2 KB
/
Copy pathtest_debug_console.py
File metadata and controls
1235 lines (953 loc) · 49.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Tests for the CLI debugger — the `ConsoleInterpreter` *is* the debug front-end.
Integration tests drive a real in-process suite through the *interpreter's own*
logger (enabled via `forward_events`), with the `DebugController` registered as
an observer and the interpreter's `read_line` swapped for a scripted reader —
the same wiring `repl robot` uses, minus the runner. Output is captured through
a stub `Application`. Pure command-resolution tests need no run.
"""
import io
import tempfile
from pathlib import Path
from typing import IO, Any, AnyStr, Callable, List, Optional, Tuple, Union, cast
import pytest
from robot.api import TestSuite as _RobotSuite
from robot.api import get_model
from robot.output import LOGGER
from robotcode.plugin import Application
from robotcode.repl._debug.controller import DebugController
from robotcode.repl._debug.types import ResumeAction, StackFrame, StopEvent, StopReason
from robotcode.repl.console_interpreter import ConsoleInterpreter
from robotcode.robot.utils import RF_VERSION
# A writable temp path (not a hardcoded "/tmp/…", which is not writable on Windows).
_SOURCE = str(Path(tempfile.gettempdir()) / "dbg_console_suite.robot")
# 11 = `Log before`, 12 = `Outer`
STEP_SUITE = """\
*** Keywords ***
Inner
Log inner
Outer
Inner
Log outer
*** Test Cases ***
T
Log before
Outer
"""
VAR_SUITE = """\
*** Variables ***
${SUITE_VAR} sv
*** Keywords ***
Add
[Arguments] ${a} ${b}
${sum}= Evaluate ${a} + ${b}
Log ${sum}
*** Test Cases ***
T
${r}= Add 2 3
"""
BREAKPOINT_SUITE = """\
*** Settings ***
Library robotcode.repl.Repl
*** Test Cases ***
T
Log before
Breakpoint
Log after
"""
class _CaptureApp(Application):
"""Real `Application` whose echo channel captures into a list."""
def __init__(self) -> None:
super().__init__()
self.messages: List[str] = []
def echo(
self,
message: Union[str, Callable[[], Any], None],
file: Optional[IO[AnyStr]] = None,
nl: bool = True,
err: bool = False,
) -> None:
self.messages.append(message() if callable(message) else str(message))
class _Reader:
"""Scripted line reader — pops queued lines, then signals EOF.
Installed as the interpreter's `read_line`, so it accepts (and ignores)
the `completer`/`prefill`/… keyword arguments the real one takes.
"""
def __init__(self, lines: List[str]) -> None:
self._lines = list(lines)
def __call__(self, prompt: str, **kwargs: Any) -> str:
if not self._lines:
raise EOFError
return self._lines.pop(0)
def _run_debug(
suite_text: str,
reader_lines: List[str],
*,
stop_on_entry: bool = False,
line_breakpoints: Optional[List[int]] = None,
keyword_breakpoints: Optional[List[str]] = None,
exception_filters: Optional[List[str]] = None,
prepare: Optional[Callable[[ConsoleInterpreter], None]] = None,
) -> List[str]:
"""Run `suite_text` with the console debugger attached; return echoed lines.
`prepare`, if given, is called with the interpreter before the run — used to
capture `show_doc` or otherwise tweak the front-end.
"""
app = _CaptureApp()
interpreter = ConsoleInterpreter(app=app)
interpreter.source = Path(_SOURCE)
controller = DebugController()
controller.set_frontend(interpreter)
interpreter.set_controller(controller)
interpreter.register_observer(controller)
# Swap the prompt reader for a scripted one (the interpreter *is* the
# front-end, so its own `read_line` drives the debug prompt).
interpreter.read_line = _Reader(reader_lines) # type: ignore[method-assign]
if prepare is not None:
prepare(interpreter)
if stop_on_entry:
controller.set_stop_on_entry(True)
if line_breakpoints:
controller.set_line_breakpoints(_SOURCE, line_breakpoints)
if keyword_breakpoints:
controller.set_keyword_breakpoints(keyword_breakpoints)
if exception_filters:
controller.set_exception_breakpoints(exception_filters)
try:
with interpreter.forward_events(echo_messages=False):
with io.StringIO(suite_text) as src:
model = get_model(src)
model.source = _SOURCE
suite = _RobotSuite.from_model(model)
suite.run(output=None, log=None, report=None, console="none", stdout=io.StringIO(), stderr=io.StringIO())
finally:
interpreter.unregister_observer(controller)
LOGGER.unregister_logger(interpreter._logger)
return app.messages
def _stop_lines(messages: List[str]) -> List[str]:
return [m for m in messages if m.startswith("* ")]
# ---------------------------------------------------------------------------
# Integration — stop rendering, navigation, inspection, evaluation
# ---------------------------------------------------------------------------
def test_stop_on_entry_renders_then_continues() -> None:
messages = _run_debug(STEP_SUITE, [".continue"], stop_on_entry=True)
stops = _stop_lines(messages)
assert len(stops) == 1
assert stops[0].startswith("* entry")
assert "Log" in stops[0]
def test_where_renders_innermost_first() -> None:
messages = _run_debug(STEP_SUITE, [".where", ".continue"], keyword_breakpoints=["Inner"])
text = "\n".join(messages)
assert "#0 Inner" in text
assert "#1 Outer" in text
def test_up_then_vars_shows_enclosing_keyword_locals() -> None:
# Stop at the inner `Log`; its own frame has no locals, so move up to the
# `Add` frame, whose scope carries the arguments + assignment.
messages = _run_debug(VAR_SUITE, [".up", ".vars", ".continue"], keyword_breakpoints=["Log"])
text = "\n".join(messages)
assert "Local:" in text
assert "${a} = '2'" in text # args arrive as strings
assert "${sum} = 5" in text # Evaluate `2 + 3` → int
# the suite variable lands in its own scope, not Local
assert "Suite:" in text
assert "${SUITE_VAR} = 'sv'" in text
def test_evaluate_runs_keyword_in_paused_context() -> None:
messages = _run_debug(STEP_SUITE, ["Set Variable 42", ".continue"], stop_on_entry=True)
assert "=> '42'" in messages
def test_evaluated_variable_is_visible_afterwards() -> None:
messages = _run_debug(
VAR_SUITE,
[".up", "Set Test Variable ${injected} hi", ".vars", ".continue"],
keyword_breakpoints=["Log"],
)
text = "\n".join(messages)
assert "${injected} = 'hi'" in text
def test_step_over_advances_to_next_keyword() -> None:
messages = _run_debug(STEP_SUITE, [".next", ".continue"], stop_on_entry=True)
stops = _stop_lines(messages)
assert len(stops) == 2
assert stops[0].startswith("* entry")
assert "Log" in stops[0]
assert stops[1].startswith("* step")
assert "Outer" in stops[1]
def test_eof_at_prompt_continues() -> None:
# An empty script → reader raises EOFError immediately → treated as continue.
messages = _run_debug(STEP_SUITE, [], stop_on_entry=True)
assert len(_stop_lines(messages)) == 1 # stopped once, then ran to the end
# ---------------------------------------------------------------------------
# 3b — embedded Breakpoint keyword, .print/.set, runtime breakpoint management
# ---------------------------------------------------------------------------
def test_embedded_breakpoint_keyword_stops() -> None:
# No triggers configured — the `Breakpoint` keyword alone pauses the run.
messages = _run_debug(BREAKPOINT_SUITE, [".continue"])
stops = _stop_lines(messages)
assert len(stops) == 1
assert stops[0].startswith("* breakpoint")
assert "Breakpoint" in stops[0]
def test_print_evaluates_variable_and_expression() -> None:
messages = _run_debug(
VAR_SUITE, [".up", ".print ${a}", ".print ${a} + ${b}", ".continue"], keyword_breakpoints=["Log"]
)
text = "\n".join(messages)
assert "${a} = '2'" in text # bare reference → raw value (string)
assert "${a} + ${b} = 5" in text # expression → evaluated (int)
def test_set_changes_variable() -> None:
messages = _run_debug(
VAR_SUITE, [".up", ".set ${a} world", ".print ${a}", ".continue"], keyword_breakpoints=["Log"]
)
# .set stores the literal (substituted) string — like `Set Variable`
assert "${a} = 'world'" in messages
def test_break_added_at_runtime_triggers() -> None:
messages = _run_debug(STEP_SUITE, [".break Outer", ".continue", ".continue"], stop_on_entry=True)
stops = _stop_lines(messages)
assert len(stops) == 2
assert stops[0].startswith("* entry")
assert stops[1].startswith("* breakpoint")
assert "Outer" in stops[1]
def test_breakpoints_lists_active() -> None:
messages = _run_debug(STEP_SUITE, [".break Nonexistent", ".breakpoints", ".continue"], stop_on_entry=True)
text = "\n".join(messages)
assert "#1 keyword Nonexistent" in text # numbered listing
def test_catch_sets_and_shows_exception_filter() -> None:
messages = _run_debug(STEP_SUITE, [".catch uncaught", ".catch", ".continue"], stop_on_entry=True)
# Both `.catch uncaught` (set) and bare `.catch` (show) echo a `catching:`
# line with the armed filter — so exactly two such lines appear.
catching = [m for m in messages if m.startswith("catching:") and "uncaught_failed_keyword" in m]
assert len(catching) == 2
# ---------------------------------------------------------------------------
# 3c — detach (let the run finish) and quit (abort)
# ---------------------------------------------------------------------------
def test_detach_disables_further_stops() -> None:
# Stop at entry, detach → the later `Outer` keyword breakpoint never fires.
messages = _run_debug(STEP_SUITE, [".detach"], stop_on_entry=True, keyword_breakpoints=["Outer"])
assert len(_stop_lines(messages)) == 1
def test_abort_aborts_the_run() -> None:
# `.abort` exits via SystemExit (the only thing Robot propagates from a
# logger callback); the run does not finish.
with pytest.raises(SystemExit):
_run_debug(STEP_SUITE, [".abort"], stop_on_entry=True)
# ---------------------------------------------------------------------------
# Command resolution (pure — no run needed)
# ---------------------------------------------------------------------------
def _bare_interpreter() -> ConsoleInterpreter:
# Only command-resolution is exercised; no run, no app needed. Unregister the
# logger the interpreter registers on construction so these run-less tests
# don't leak a global LOGGER logger that would fire during later tests.
interp = ConsoleInterpreter(app=None)
LOGGER.unregister_logger(interp._logger)
return interp
def test_alias_and_prefix_resolution() -> None:
interp = _bare_interpreter()
assert interp._resolve_dot_command("c") == ("_continue", None) # short alias wins
assert interp._resolve_dot_command("continue") == ("_continue", None) # exact long
assert interp._resolve_dot_command("cont")[0] == "_continue" # unambiguous prefix
assert interp._resolve_dot_command("s") == ("_step", None) # short alias
assert interp._resolve_dot_command("frame")[0] == "_frame"
assert interp._resolve_dot_command("p") == ("_print", None)
assert interp._resolve_dot_command("b") == ("_break", None)
assert interp._resolve_dot_command("bp") == ("_breakpoints", None)
assert interp._resolve_dot_command("abort") == ("_abort", None)
# Session commands resolve through the same table.
assert interp._resolve_dot_command("kw") == ("_kw", None)
def test_ambiguous_prefix_reports_matches() -> None:
interp = _bare_interpreter()
attr, error = interp._resolve_dot_command("br") # break / breakpoints
assert attr is None
assert error is not None
assert ".break" in error
assert ".breakpoints" in error
def test_cross_group_prefix_is_ambiguous() -> None:
# Resolving over the unified table means a prefix can now straddle the
# Session and Debugger groups: `.do` matches `.doc` (session) and `.down`
# (debugger), so it must report ambiguity rather than guess.
interp = _bare_interpreter()
attr, error = interp._resolve_dot_command("do")
assert attr is None
assert error is not None
assert ".doc" in error
assert ".down" in error
def test_unknown_command_reports_error() -> None:
interp = _bare_interpreter()
attr, error = interp._resolve_dot_command("nope")
assert attr is None
assert error is not None
assert "Unknown" in error
def _fake_stop() -> StopEvent:
frame = StackFrame(name="Log", type="KEYWORD", source=_SOURCE, line=1, depth=0)
return StopEvent(reason=StopReason.ENTRY, frame=frame, stack=[frame])
def test_wait_at_stop_reads_through_interpreter_read_line() -> None:
# The interpreter *is* the front-end: `wait_at_stop` reads via its own
# `read_line`, threading the debug completer built in `set_controller`.
app = _CaptureApp()
interpreter = ConsoleInterpreter(app=app)
controller = DebugController()
controller.set_frontend(interpreter)
interpreter.set_controller(controller)
seen: List[Tuple[str, Any]] = []
def _record(prompt: str, *, completer: Any = None, **kwargs: Any) -> str:
seen.append((prompt, completer))
return ".continue"
interpreter.read_line = _record # type: ignore[method-assign]
try:
action = interpreter.wait_at_stop(_fake_stop())
finally:
LOGGER.unregister_logger(interpreter._logger)
assert action is ResumeAction.CONTINUE
assert seen == [("(rdb) ", interpreter._debug_completer)]
assert interpreter._stop is None # stop state cleared on resume
assert interpreter._pending_action is None
def test_wait_at_stop_clears_state_when_abort_unwinds() -> None:
# `.abort` raises SystemExit straight through the prompt loop; the stop state
# must STILL be cleared (try/finally) so the interactive shell prompt isn't
# left wedged in a phantom debug state after the run unwinds.
app = _CaptureApp()
interpreter = ConsoleInterpreter(app=app)
controller = DebugController()
controller.set_frontend(interpreter)
interpreter.set_controller(controller)
interpreter.read_line = _Reader([".abort"]) # type: ignore[method-assign]
try:
with pytest.raises(SystemExit):
interpreter.wait_at_stop(_fake_stop())
finally:
LOGGER.unregister_logger(interpreter._logger)
assert interpreter._stop is None
assert interpreter._pending_action is None
def test_help_lists_session_and_debugger_commands() -> None:
app = _CaptureApp()
interpreter = ConsoleInterpreter(app=app)
shown: List[Tuple[str, str]] = []
interpreter.show_doc = lambda title, markdown, *, scroll_to=None: shown.append((title, markdown)) # type: ignore[method-assign]
try:
interpreter._dispatch_dot_command(".help")
finally:
LOGGER.unregister_logger(interpreter._logger)
assert len(shown) == 1
title, body = shown[0]
assert title == "Dot-commands"
assert "### Session" in body
assert "### Debugger" in body
assert body.index("### Session") < body.index("### Debugger") # group order
assert ".continue" in body # a debugger command
assert ".kw" in body # session command
assert ".vars" in body # session command
# ---------------------------------------------------------------------------
# Review follow-ups: subscripted .print, frame navigation, .break path:line,
# .catch multi/off.
# ---------------------------------------------------------------------------
LIST_SUITE = """\
*** Keywords ***
Make
${items}= Evaluate [10, 20, 30]
Log done
*** Test Cases ***
T
Make
"""
def test_print_subscripted_variable() -> None:
messages = _run_debug(
LIST_SUITE, [".up", ".print ${items}", ".print ${items}[1]", ".continue"], keyword_breakpoints=["Log"]
)
text = "\n".join(messages)
assert "${items} = [10, 20, 30]" in text
assert "${items}[1] = 20" in text # item access, type preserved
def test_frame_up_down_and_select_by_number() -> None:
messages = _run_debug(VAR_SUITE, [".up", ".down", ".frame 1", ".continue"], keyword_breakpoints=["Log"])
text = "\n".join(messages)
assert "#1 Add" in text # .up / .frame 1 select the enclosing keyword (suite-local, no prefix)
assert "#0 BuiltIn.Log" in text # .down returns to the innermost frame, shown with its full name
def test_frame_invalid_number_shows_usage() -> None:
messages = _run_debug(VAR_SUITE, [".frame nope", ".continue"], keyword_breakpoints=["Log"])
assert any("usage: .frame" in m for m in messages)
def test_break_with_path_line_format_triggers() -> None:
messages = _run_debug(STEP_SUITE, [f".break {_SOURCE}:12", ".continue", ".continue"], stop_on_entry=True)
stops = _stop_lines(messages)
assert len(stops) == 2
assert stops[0].startswith("* entry")
# The location is shown relative to the cwd (a `…/`-prefixed path here, since
# the test source lives in a temp dir); assert the filename:line suffix, which
# is stable regardless of where the tests run from.
assert "dbg_console_suite.robot:12)" in stops[1] # the line breakpoint fired
def test_catch_multiple_filters_then_off() -> None:
messages = _run_debug(STEP_SUITE, [".catch uncaught test", ".catch off", ".catch", ".continue"], stop_on_entry=True)
text = "\n".join(messages)
assert "failed_test" in text # both set at once
assert "uncaught_failed_keyword" in text
# `.catch off` and the bare `.catch` after it both report the empty set
assert text.count("catching: (none)") == 2
# ---------------------------------------------------------------------------
# Unified prompt: session commands work at a stop (incl. the doc viewer), and
# debugger navigation commands report when there's nothing to act on.
# ---------------------------------------------------------------------------
def test_session_command_opens_doc_viewer_at_stop() -> None:
# `.kw` (a session command) works at a debug stop and routes through the
# same `show_doc` path the shell prompt uses — so `repl robot` gets the
# doc viewer at a stop, not a plain echo.
shown: List[Tuple[str, str]] = []
def _capture_show_doc(interp: ConsoleInterpreter) -> None:
interp.show_doc = lambda title, markdown, *, scroll_to=None: shown.append((title, markdown)) # type: ignore[method-assign]
_run_debug(STEP_SUITE, [".kw Log", ".continue"], stop_on_entry=True, prepare=_capture_show_doc)
# title is exactly "Log" for the keyword page; the search-fallback would be
# "Keywords matching 'Log'", so equality pins the doc-viewer path.
assert any(title == "Log" for title, _ in shown)
def test_debugger_command_without_stop_reports_not_at_breakpoint() -> None:
# The debugger commands exist at the shell prompt too (one unified set), but
# the navigation/resume ones need a stop to act on.
app = _CaptureApp()
interpreter = ConsoleInterpreter(app=app)
controller = DebugController()
controller.set_frontend(interpreter)
interpreter.set_controller(controller)
try:
interpreter._dispatch_dot_command(".continue")
interpreter._dispatch_dot_command(".where")
interpreter._dispatch_dot_command(".break Login") # breakpoint cmds work without a stop
finally:
LOGGER.unregister_logger(interpreter._logger)
text = "\n".join(app.messages)
assert text.count("not at a breakpoint") == 2 # .continue and .where
assert "breakpoint 1 at keyword 'Login'" in text
assert "Login" in controller.keyword_breakpoints
def test_exit_at_stop_guides_instead_of_leaving() -> None:
# `.exit`/`.quit` leave the REPL at the shell prompt; at a debug stop that's
# ambiguous, so it points at the resume/abort commands rather than raising.
messages = _run_debug(STEP_SUITE, [".exit", ".continue"], stop_on_entry=True)
text = "\n".join(messages)
assert "at a debug stop" in text
assert ".abort" in text
assert not any("EOFError" in m for m in messages)
@pytest.mark.skipif(RF_VERSION < (7, 0), reason="result.name is only deprecated on Robot Framework >= 7")
def test_repl_marker_check_does_not_read_deprecated_result_name() -> None:
# The marker check runs on every body-item start, including control
# structures (RETURN, FOR, …) whose `result.name` RF>=7 deprecates. It must
# decide via `full_name` only and never touch the deprecated `.name`, or a
# UserWarning leaks into the debug output.
class _ControlResult:
full_name = None # control structures expose no full_name
@property
def name(self) -> str:
raise AssertionError("must not read the deprecated result.name on RF>=7")
interpreter = ConsoleInterpreter(app=None)
try:
assert interpreter._is_repl_marker(cast(Any, _ControlResult())) is False
finally:
LOGGER.unregister_logger(interpreter._logger)
# ---------------------------------------------------------------------------
# Phase 5 — pdb alignment
# ---------------------------------------------------------------------------
# Cluster 1 — alias cleanup (pdb-pure: one long name + one short letter)
def test_return_replaces_stepout_aliases() -> None:
interp = _bare_interpreter()
assert interp._resolve_dot_command("return") == ("_return", None)
assert interp._resolve_dot_command("r") == ("_return", None)
# the old gdb-style step-out names are gone
for gone in ("stepout", "finish", "o"):
attr, error = interp._resolve_dot_command(gone)
assert attr is None
assert error is not None
def test_where_drops_backtrace_aliases() -> None:
interp = _bare_interpreter()
assert interp._resolve_dot_command("where") == ("_where", None)
assert interp._resolve_dot_command("w") == ("_where", None)
for gone in ("bt", "backtrace"):
attr, error = interp._resolve_dot_command(gone)
assert attr is None
assert error is not None
RETURN_SUITE = """\
*** Keywords ***
Inner
Log inner
Wrapper
Inner
Log after
*** Test Cases ***
T
Wrapper
Log last
"""
def test_return_steps_out_to_outer_frame() -> None:
# Stop deep in `Inner`; `.return` runs until `Inner` *and* its caller
# `Wrapper` have returned, landing on the next sibling of `Wrapper` (`Log
# last`) — it skips `Inner`'s body and the rest of `Wrapper`'s body.
messages = _run_debug(RETURN_SUITE, [".return", ".continue"], keyword_breakpoints=["Inner"])
stops = _stop_lines(messages)
assert len(stops) == 2
assert stops[0].startswith("* breakpoint")
assert "Inner" in stops[0]
assert stops[1].startswith("* step")
assert "Log" in stops[1]
# Cluster 4 — .until (stop at a later line in the current frame, or on return)
def test_until_aliases_resolve_by_prefix() -> None:
interp = _bare_interpreter()
assert interp._resolve_dot_command("until") == ("_until", None)
assert interp._resolve_dot_command("unt")[0] == "_until" # pdb short form, via prefix
def test_until_advances_to_later_line_in_frame() -> None:
# From the entry stop at `Log before` (line 11), `.until` runs to the next
# line in the same frame — `Outer` (line 12).
messages = _run_debug(STEP_SUITE, [".until", ".continue"], stop_on_entry=True)
stops = _stop_lines(messages)
assert len(stops) == 2
assert stops[0].startswith("* entry")
assert stops[1].startswith("* step")
assert "Outer" in stops[1]
# Cluster 2 — .whatis / .pprint / .display
def test_whatis_shows_type_not_value() -> None:
messages = _run_debug(
VAR_SUITE, [".up", ".whatis ${a}", ".whatis ${a} + ${b}", ".continue"], keyword_breakpoints=["Log"]
)
text = "\n".join(messages)
assert "${a}: str" in text # bare reference → raw value is a string
assert "${a} + ${b}: int" in text # expression → evaluated to int
def test_pprint_formats_value_without_name_prefix() -> None:
messages = _run_debug(LIST_SUITE, [".up", ".pprint ${items}", ".continue"], keyword_breakpoints=["Log"])
# `.print` would echo `${items} = [...]`; `.pprint` emits the bare pformat.
assert "[10, 20, 30]" in messages
DISPLAY_SUITE = """\
*** Variables ***
${V} hello
*** Test Cases ***
T
Log one
Log two
"""
def test_display_persists_across_subsequent_stops() -> None:
messages = _run_debug(DISPLAY_SUITE, [".display ${V}", ".next", ".continue"], stop_on_entry=True)
text = "\n".join(messages)
assert "displaying ${V}" in text # registration echo
# shown at the *next* stop (the entry stop happened before `.display`)
assert sum("${V} = 'hello'" in m for m in messages) == 1
def test_undisplay_removes_expression() -> None:
messages = _run_debug(DISPLAY_SUITE, [".display ${V}", ".undisplay ${V}", ".next", ".continue"], stop_on_entry=True)
text = "\n".join(messages)
assert "no longer displaying ${V}" in text
assert not any("${V} = 'hello'" in m for m in messages) # never shown after undisplay
# Cluster 3 — .list / .source (shared ±5 source-window renderer)
def test_list_renders_source_window_at_stop() -> None:
Path(_SOURCE).write_text(STEP_SUITE, encoding="utf-8")
messages = _run_debug(STEP_SUITE, [".list", ".continue"], stop_on_entry=True)
marked = [m for m in messages if m.lstrip().startswith("->")]
assert len(marked) == 1 # exactly the current line is marked
assert "Log" in marked[0]
assert "before" in marked[0]
assert any("Outer" in m for m in messages) # the ±5 window includes line 12
def test_source_renders_named_keyword_window() -> None:
# `.source Log` resolves the library keyword and renders its definition
# window from the BuiltIn Python source (same renderer as `.list`).
messages = _run_debug(STEP_SUITE, [".source Log", ".continue"], stop_on_entry=True)
assert any(m.startswith("Log (") and ".py:" in m for m in messages) # header with location
assert any(m.lstrip().startswith("->") for m in messages) # the definition line is marked
def _source_window_count(messages: List[str]) -> int:
# Count the numbered source lines (`-> 12 …` / ` 13 …`) the inline
# `.source`/`.list` window emits.
return sum(1 for m in messages if (s := m.lstrip()) and (s.startswith("->") or s[:1].isdigit()))
def test_source_count_arg_limits_inline_window() -> None:
# Plain backend: a trailing count sets how many lines are shown from the
# definition (default 10).
default = _run_debug(STEP_SUITE, [".source Log", ".continue"], stop_on_entry=True)
assert _source_window_count(default) == 10
limited = _run_debug(STEP_SUITE, [".source Log 3", ".continue"], stop_on_entry=True)
assert _source_window_count(limited) == 3
def test_source_opens_scrollable_viewer_when_available() -> None:
# prompt_toolkit backend: `.source` loads the WHOLE file into the scrollable
# doc viewer, marks the definition line, and opens scrolled to it.
shown: List[Tuple[str, str, Optional[str]]] = []
def _enable_viewer(interp: ConsoleInterpreter) -> None:
interp.has_scrollable_viewer = True
interp.show_doc = lambda title, markdown, *, scroll_to=None: shown.append( # type: ignore[method-assign]
(title, markdown, scroll_to)
)
messages = _run_debug(STEP_SUITE, [".source Log", ".continue"], stop_on_entry=True, prepare=_enable_viewer)
assert shown, "the viewer was not opened"
title, md, scroll_to = shown[0]
assert title.startswith("Log (")
assert ".py:" in title
assert "```" in md # source rendered in a code fence
assert " 1 " in md # the WHOLE file is loaded (line 1 present), not just a window
assert "->" in md # the definition line is marked
assert scroll_to is not None # opens scrolled to the marked line
assert scroll_to.startswith("->")
# nothing printed inline through the window in viewer mode
assert _source_window_count(messages) == 0
def test_list_opens_scrollable_viewer_when_available() -> None:
# prompt_toolkit backend: `.list` loads the WHOLE file into the scrollable
# viewer and opens scrolled to the current stop line (marked).
Path(_SOURCE).write_text(STEP_SUITE, encoding="utf-8")
shown: List[Tuple[str, str, Optional[str]]] = []
def _enable_viewer(interp: ConsoleInterpreter) -> None:
interp.has_scrollable_viewer = True
interp.show_doc = lambda title, markdown, *, scroll_to=None: shown.append( # type: ignore[method-assign]
(title, markdown, scroll_to)
)
messages = _run_debug(STEP_SUITE, [".list", ".continue"], stop_on_entry=True, prepare=_enable_viewer)
assert shown, "the viewer was not opened"
_title, md, scroll_to = shown[0]
assert "```" in md
assert " 1 " in md # whole file loaded, not a ±5 window
assert "->" in md # the current line is marked
assert scroll_to is not None
assert scroll_to.startswith("->")
assert _source_window_count(messages) == 0 # nothing inline in viewer mode
def test_source_unknown_keyword_reports() -> None:
messages = _run_debug(STEP_SUITE, [".source Nonexistent Keyword", ".continue"], stop_on_entry=True)
assert any("not found" in m for m in messages)
# Cluster 5 — conditional / temporary / ignore / delete / disable breakpoints
COND_SUITE = """\
*** Test Cases ***
T
FOR ${i} IN RANGE 3
Log ${i}
END
"""
def test_conditional_breakpoint_stops_only_when_true() -> None:
def add_cond(interp: ConsoleInterpreter) -> None:
interp._controller.add_keyword_breakpoint("Log", condition="${i} == 1") # type: ignore[union-attr]
messages = _run_debug(COND_SUITE, [".continue"], prepare=add_cond)
stops = _stop_lines(messages)
# `Log` runs three times (i=0,1,2); the condition is true only for i==1.
assert sum(s.startswith("* breakpoint") for s in stops) == 1
def test_condition_error_still_stops() -> None:
# A failing condition (undefined variable) stops anyway, so the breakage is
# noticed rather than silently swallowed (pdb semantics).
def add_bad(interp: ConsoleInterpreter) -> None:
interp._controller.add_keyword_breakpoint("Outer", condition="${nonexistent} > 0") # type: ignore[union-attr]
messages = _run_debug(STEP_SUITE, [".continue"], prepare=add_bad)
stops = _stop_lines(messages)
assert any(s.startswith("* breakpoint") and "Outer" in s for s in stops)
def test_tbreak_is_removed_after_first_hit() -> None:
# `Log` is hit three times in STEP_SUITE; a temporary breakpoint stops once.
messages = _run_debug(STEP_SUITE, [".tbreak Log", ".continue", ".continue"], stop_on_entry=True)
stops = _stop_lines(messages)
assert sum(s.startswith("* breakpoint") for s in stops) == 1
def test_ignore_skips_next_n_hits() -> None:
# The entry stop is already on the first `Log` (`Log before`); after
# resuming, `Log` fires twice more (`Log inner`, `Log outer`). Ignoring the
# next one skips `Log inner` and stops on `Log outer`.
messages = _run_debug(STEP_SUITE, [".break Log", ".ignore 1 1", ".continue", ".continue"], stop_on_entry=True)
stops = _stop_lines(messages)
assert sum(s.startswith("* breakpoint") for s in stops) == 1
def test_delete_removes_breakpoint() -> None:
messages = _run_debug(STEP_SUITE, [".break Outer", ".delete 1", ".continue"], stop_on_entry=True)
stops = _stop_lines(messages)
assert sum(s.startswith("* breakpoint") for s in stops) == 0 # deleted → never fires
def test_disable_then_enable_breakpoint() -> None:
disabled = _run_debug(STEP_SUITE, [".break Outer", ".disable 1", ".continue"], stop_on_entry=True)
assert sum(s.startswith("* breakpoint") for s in _stop_lines(disabled)) == 0
enabled = _run_debug(
STEP_SUITE, [".break Outer", ".disable 1", ".enable 1", ".continue", ".continue"], stop_on_entry=True
)
assert sum(s.startswith("* breakpoint") for s in _stop_lines(enabled)) == 1
def test_breakpoints_listing_shows_numbers_and_attributes() -> None:
messages = _run_debug(
STEP_SUITE,
[".break Outer, ${x} > 0", ".tbreak Inner", ".ignore 1 3", ".disable 2", ".breakpoints", ".continue"],
stop_on_entry=True,
)
text = "\n".join(messages)
assert "#1 keyword Outer" in text
assert "if ${x} > 0" in text
assert "ignore 3" in text
assert "#2 keyword Inner" in text
assert "temp" in text
assert "disabled" in text
def test_logpoint_logs_and_continues() -> None:
# A logpoint (log_message set) logs at the hit and keeps running — never
# stops. This is the shared core DAP `logMessage` reuses.
def add_logpoint(interp: ConsoleInterpreter) -> None:
interp._controller.add_keyword_breakpoint("Outer", log_message="hit outer") # type: ignore[union-attr]
messages = _run_debug(STEP_SUITE, [], prepare=add_logpoint)
stops = _stop_lines(messages)
assert sum(s.startswith("* breakpoint") for s in stops) == 0
assert "hit outer" in messages
# Cluster 6 — .commands (sub-prompt collection, replayed at each hit)
def test_commands_requires_breakpoint_number() -> None:
messages = _run_debug(STEP_SUITE, [".commands", ".continue"], stop_on_entry=True)
assert any("usage: .commands" in m for m in messages)
def test_commands_replay_silent_and_resume() -> None:
# Attach `silent` + `.where` + `.continue` to the Outer breakpoint. At the
# hit: the banner is suppressed (silent), `.where` replays, and the trailing
# `.continue` resumes without consuming an interactive prompt line.
messages = _run_debug(
STEP_SUITE,
[".break Outer", ".commands 1", "silent", ".where", ".continue", "end", ".continue"],
stop_on_entry=True,
)
text = "\n".join(messages)
stops = _stop_lines(messages)
assert "breakpoint 1: 3 command(s)" in text # collection echo (silent counts)
assert sum(s.startswith("* breakpoint") for s in stops) == 0 # Outer hit was silent
assert "#0 Outer" in text # the replayed `.where` ran at the hit
# ---------------------------------------------------------------------------
# Phase 5 — coverage follow-ups (audit gaps): error paths, bare forms, feature
# interactions, prefix-collision resolution, help listing.
# ---------------------------------------------------------------------------
# Cluster 2 — inspection error/edge paths
def test_whatis_and_pprint_surface_evaluate_errors() -> None:
# An unresolvable variable surfaces a `! <err>` line (same path as `.print`).
messages = _run_debug(
STEP_SUITE, [".whatis ${nonexistent}", ".pprint ${nonexistent}", ".continue"], stop_on_entry=True
)
assert sum(1 for m in messages if m.startswith("! ")) >= 2
def test_bare_display_shows_values_when_stopped() -> None:
messages = _run_debug(DISPLAY_SUITE, [".display ${V}", ".display", ".continue"], stop_on_entry=True)
# bare `.display` while stopped re-evaluates the registered exprs
assert any(m == "${V} = 'hello'" for m in messages)
def test_bare_display_lists_expressions_when_not_stopped() -> None:
app = _CaptureApp()
interp = ConsoleInterpreter(app=app)
interp._display_exprs.append("${X}")
try:
interp._dispatch_dot_command(".display") # not at a stop -> list the raw exprs
finally:
LOGGER.unregister_logger(interp._logger)
assert "${X}" in app.messages
def test_display_error_does_not_abort_stop_render() -> None:
messages = _run_debug(DISPLAY_SUITE, [".display ${nonexistent}", ".next", ".continue"], stop_on_entry=True)
# the failing display is reported inline...
assert any(m.startswith("${nonexistent} = ! ") for m in messages)
# ...and the stop banner still renders (entry + the `.next` step stop)
assert sum(1 for m in messages if m.startswith("* ")) >= 2
def test_display_evaluates_against_selected_frame() -> None:
# ${a} is a local of the enclosing `Add` keyword, not the inner `Log` frame;
# bare `.display` after `.up` evaluates against the selected (Add) frame.
messages = _run_debug(VAR_SUITE, [".up", ".display ${a}", ".display", ".continue"], keyword_breakpoints=["Log"])
assert any(m == "${a} = '2'" for m in messages)
# Cluster 3 — .source variants
def test_source_suite_local_keyword_not_resolved() -> None:
# Known limitation: `.source` resolves keywords from imported libraries and
# resources (see the BuiltIn `Log` and resource tests), but NOT keywords
# defined in the running suite file itself — `lookup_keyword_owner` does not
# enumerate the executing suite's own keywords, so it reports "not found".
Path(_SOURCE).write_text(STEP_SUITE, encoding="utf-8")
messages = _run_debug(STEP_SUITE, [".source Outer", ".continue"], stop_on_entry=True)
assert any("keyword 'Outer' not found" in m for m in messages)
def test_source_resource_keyword(tmp_path: Path) -> None:
res = tmp_path / "kw.resource"
res.write_text("*** Keywords ***\nCustom Step\n Log hi\n", encoding="utf-8")
# Forward slashes: a raw Windows path (backslashes) in .robot source would be
# mangled by Robot's escape handling, so the resource import would fail.
suite_text = f"*** Settings ***\nResource {res.as_posix()}\n\n*** Test Cases ***\nT\n Custom Step\n"
messages = _run_debug(suite_text, [".source Custom Step", ".continue"], stop_on_entry=True)
assert any(m.startswith("Custom Step (") and "kw.resource:" in m for m in messages)
assert any(m.lstrip().startswith("->") for m in messages)
def test_source_without_line_reports_no_source(monkeypatch: "pytest.MonkeyPatch") -> None:
import types
from robotcode.repl import console_interpreter as ci
def _no_line_doc(*a: object, **k: object) -> object:
return types.SimpleNamespace(source="/x.py", line_no=None)
monkeypatch.setattr(ci, "_diagnostics_keyword_doc", _no_line_doc)
messages = _run_debug(STEP_SUITE, [".source Log", ".continue"], stop_on_entry=True)
assert any("(no source available for" in m for m in messages)
# Cluster 5 — bare forms, reference errors, condition set/clear, tbreak+condition
def test_bare_delete_removes_all_breakpoints() -> None:
messages = _run_debug(STEP_SUITE, [".break Outer", ".break Inner", ".delete", ".continue"], stop_on_entry=True)
assert any("removed 2 breakpoint(s)" in m for m in messages)
assert sum(s.startswith("* breakpoint") for s in _stop_lines(messages)) == 0
def test_bare_disable_disables_all_breakpoints() -> None:
messages = _run_debug(STEP_SUITE, [".break Outer", ".break Inner", ".disable", ".continue"], stop_on_entry=True)
assert any("all breakpoints disabled" in m for m in messages)
assert sum(s.startswith("* breakpoint") for s in _stop_lines(messages)) == 0
def test_breakpoint_reference_errors() -> None:
messages = _run_debug(STEP_SUITE, [".delete abc", ".delete 99", ".continue"], stop_on_entry=True)
text = "\n".join(messages)
assert "not a breakpoint number: abc" in text
assert "no breakpoint 99" in text
def test_condition_command_sets_then_clears() -> None:
# Setting a condition gates the breakpoint to the truthy iteration only.
gated = _run_debug(
COND_SUITE, [".break Log", ".condition 1 ${i} == 1", ".continue", ".continue"], stop_on_entry=True
)
assert "breakpoint 1: condition ${i} == 1" in "\n".join(gated)
assert sum(s.startswith("* breakpoint") for s in _stop_lines(gated)) == 1
# Clearing it echoes the cleared message.
cleared = _run_debug(
COND_SUITE,
[".break Log", ".condition 1 ${i} == 1", ".condition 1", ".continue", ".continue", ".continue", ".continue"],
stop_on_entry=True,
)
assert "breakpoint 1: condition cleared" in "\n".join(cleared)