summaryrefslogtreecommitdiff
path: root/test/irb/test_debugger_integration.rb
blob: eca40c57027e021213a13ca1a11d17974e59cb92 (plain)
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
# frozen_string_literal: true

require "tempfile"
require "tmpdir"

require_relative "helper"

module TestIRB
  class DebuggerIntegrationTest < IntegrationTestCase
    def setup
      super

      if RUBY_ENGINE == 'truffleruby'
        omit "This test runs with ruby/debug, which doesn't work with truffleruby"
      end

      @envs.merge!("NO_COLOR" => "true", "RUBY_DEBUG_HISTORY_FILE" => '')
    end

    def test_backtrace
      write_ruby <<~'RUBY'
        def foo
          binding.irb
        end
        foo
      RUBY

      output = run_ruby_file do
        type "backtrace"
        type "exit!"
      end

      assert_match(/irb\(main\):001> backtrace/, output)
      assert_match(/Object#foo at #{@ruby_file.to_path}/, output)
    end

    def test_debug
      write_ruby <<~'ruby'
        binding.irb
        puts "hello"
      ruby

      output = run_ruby_file do
        type "debug"
        type "next"
        type "continue"
      end

      assert_match(/irb\(main\):001> debug/, output)
      assert_match(/irb:rdbg\(main\):002> next/, output)
      assert_match(/=>   2\| puts "hello"/, output)
    end

    def test_debug_command_only_runs_once
      write_ruby <<~'ruby'
        binding.irb
      ruby

      output = run_ruby_file do
        type "debug"
        type "debug"
        type "continue"
      end

      assert_match(/irb\(main\):001> debug/, output)
      assert_match(/irb:rdbg\(main\):002> debug/, output)
      assert_match(/IRB is already running with a debug session/, output)
    end

    def test_next
      write_ruby <<~'ruby'
        binding.irb
        puts "hello"
      ruby

      output = run_ruby_file do
        type "next"
        type "continue"
      end

      assert_match(/irb\(main\):001> next/, output)
      assert_match(/=>   2\| puts "hello"/, output)
    end

    def test_break
      write_ruby <<~'RUBY'
        binding.irb
        puts "Hello"
      RUBY

      output = run_ruby_file do
        type "break 2"
        type "continue"
        type "continue"
      end

      assert_match(/irb\(main\):001> break/, output)
      assert_match(/=>   2\| puts "Hello"/, output)
    end

    def test_delete
      write_ruby <<~'RUBY'
        binding.irb
        puts "Hello"
        binding.irb
        puts "World"
      RUBY

      output = run_ruby_file do
        type "break 4"
        type "continue"
        type "delete 0"
        type "continue"
      end

      assert_match(/irb:rdbg\(main\):003> delete/, output)
      assert_match(/deleted: #0  BP - Line/, output)
    end

    def test_step
      write_ruby <<~'RUBY'
        def foo
          puts "Hello"
        end
        binding.irb
        foo
      RUBY

      output = run_ruby_file do
        type "step"
        type "step"
        type "continue"
      end

      assert_match(/irb\(main\):001> step/, output)
      assert_match(/=>   5\| foo/, output)
      assert_match(/=>   2\|   puts "Hello"/, output)
    end

    def test_long_stepping
      write_ruby <<~'RUBY'
        class Foo
          def foo(num)
            bar(num + 10)
          end

          def bar(num)
            num
          end
        end

        binding.irb
        Foo.new.foo(100)
      RUBY

      output = run_ruby_file do
        type "step"
        type "step"
        type "step"
        type "step"
        type "num"
        type "continue"
      end

      assert_match(/irb\(main\):001> step/, output)
      assert_match(/irb:rdbg\(main\):002> step/, output)
      assert_match(/irb:rdbg\(#<Foo:.*>\):003> step/, output)
      assert_match(/irb:rdbg\(#<Foo:.*>\):004> step/, output)
      assert_match(/irb:rdbg\(#<Foo:.*>\):005> num/, output)
      assert_match(/=> 110/, output)
    end

    def test_continue
      write_ruby <<~'RUBY'
        binding.irb
        puts "Hello"
        binding.irb
        puts "World"
      RUBY

      output = run_ruby_file do
        type "continue"
        type "continue"
      end

      assert_match(/irb\(main\):001> continue/, output)
      assert_match(/=> 3: binding.irb/, output)
      assert_match(/irb:rdbg\(main\):002> continue/, output)
    end

    def test_finish
      write_ruby <<~'RUBY'
        def foo
          binding.irb
          puts "Hello"
        end
        foo
      RUBY

      output = run_ruby_file do
        type "finish"
        type "continue"
      end

      assert_match(/irb\(main\):001> finish/, output)
      assert_match(/=>   4\| end/, output)
    end

    def test_info
      write_ruby <<~'RUBY'
        def foo
          a = "He" + "llo"
          binding.irb
        end
        foo
      RUBY

      output = run_ruby_file do
        type "info"
        type "continue"
      end

      assert_match(/irb\(main\):001> info/, output)
      assert_match(/%self = main/, output)
      assert_match(/a = "Hello"/, output)
    end

    def test_catch
      write_ruby <<~'RUBY'
        binding.irb
        1 / 0
      RUBY

      output = run_ruby_file do
        type "catch ZeroDivisionError"
        type "continue"
        type "continue"
      end

      assert_match(/irb\(main\):001> catch/, output)
      assert_match(/Stop by #0  BP - Catch  "ZeroDivisionError"/, output)
    end

    def test_exit
      write_ruby <<~'RUBY'
        binding.irb
        puts "he" + "llo"
      RUBY

      output = run_ruby_file do
        type "debug"
        type "exit"
      end

      assert_match(/irb:rdbg\(main\):002>/, output)
      assert_match(/hello/, output)
    end

    def test_force_exit
      write_ruby <<~'RUBY'
        binding.irb
        puts "he" + "llo"
      RUBY

      output = run_ruby_file do
        type "debug"
        type "exit!"
      end

      assert_match(/irb:rdbg\(main\):002>/, output)
      assert_not_match(/hello/, output)
    end

    def test_quit
      write_ruby <<~'RUBY'
        binding.irb
        puts "he" + "llo"
      RUBY

      output = run_ruby_file do
        type "debug"
        type "quit!"
      end

      assert_match(/irb:rdbg\(main\):002>/, output)
      assert_not_match(/hello/, output)
    end

    def test_prompt_line_number_continues
      write_ruby <<~'ruby'
        binding.irb
        puts "Hello"
        puts "World"
      ruby

      output = run_ruby_file do
        type "123"
        type "456"
        type "next"
        type "info"
        type "next"
        type "continue"
      end

      assert_match(/irb\(main\):003> next/, output)
      assert_match(/irb:rdbg\(main\):004> info/, output)
      assert_match(/irb:rdbg\(main\):005> next/, output)
    end

    def test_prompt_irb_name_is_kept
      write_rc <<~RUBY
        IRB.conf[:IRB_NAME] = "foo"
      RUBY

      write_ruby <<~'ruby'
        binding.irb
        puts "Hello"
      ruby

      output = run_ruby_file do
        type "next"
        type "continue"
      end

      assert_match(/foo\(main\):001> next/, output)
      assert_match(/foo:rdbg\(main\):002> continue/, output)
    end

    def test_irb_commands_are_available_after_moving_around_with_the_debugger
      write_ruby <<~'ruby'
        class Foo
          def bar
            puts "bar"
          end
        end

        binding.irb
        Foo.new.bar
      ruby

      output = run_ruby_file do
        # Due to the way IRB defines its commands, moving into the Foo instance from main is necessary for proper testing.
        type "next"
        type "step"
        type "irb_info"
        type "continue"
      end

      assert_include(output, "InputMethod: RelineInputMethod")
    end

    def test_help_command_is_delegated_to_the_debugger
      write_ruby <<~'ruby'
        binding.irb
      ruby

      output = run_ruby_file do
        type "debug"
        type "help"
        type "continue"
      end

      assert_include(output, "### Frame control")
    end

    def test_help_display_different_content_when_debugger_is_enabled
      write_ruby <<~'ruby'
        binding.irb
      ruby

      output = run_ruby_file do
        type "debug"
        type "help"
        type "continue"
      end

      # IRB's commands should still be listed
      assert_match(/help\s+List all available commands/, output)
      # debug gem's commands should be appended at the end
      assert_match(/Debugging \(from debug\.gem\)\s+### Control flow/, output)
    end

    def test_input_is_evaluated_in_the_context_of_the_current_thread
      write_ruby <<~'ruby'
        current_thread = Thread.current
        binding.irb
      ruby

      output = run_ruby_file do
        type "debug"
        type '"Threads match: #{current_thread == Thread.current}"'
        type "continue"
      end

      assert_match(/irb\(main\):001> debug/, output)
      assert_match(/Threads match: true/, output)
    end

    def test_irb_switches_debugger_interface_if_debug_was_already_activated
      write_ruby <<~'ruby'
        require 'debug'
        class Foo
          def bar
            puts "bar"
          end
        end

        binding.irb
        Foo.new.bar
      ruby

      output = run_ruby_file do
        # Due to the way IRB defines its commands, moving into the Foo instance from main is necessary for proper testing.
        type "next"
        type "step"
        type 'irb_info'
        type "continue"
      end

      assert_match(/irb\(main\):001> next/, output)
      assert_include(output, "InputMethod: RelineInputMethod")
    end

    def test_debugger_cant_be_activated_while_multi_irb_is_active
      write_ruby <<~'ruby'
        binding.irb
        a = 1
      ruby

      output = run_ruby_file do
        type "jobs"
        type "next"
        type "exit"
      end

      assert_match(/irb\(main\):001> jobs/, output)
      assert_include(output, "Can't start the debugger when IRB is running in a multi-IRB session.")
    end

    def test_multi_irb_commands_are_not_available_after_activating_the_debugger
      write_ruby <<~'ruby'
        binding.irb
        a = 1
      ruby

      output = run_ruby_file do
        type "next"
        type "jobs"
        type "continue"
      end

      assert_match(/irb\(main\):001> next/, output)
      assert_include(output, "Multi-IRB commands are not available when the debugger is enabled.")
    end

    def test_irb_passes_empty_input_to_debugger_to_repeat_the_last_command
      write_ruby <<~'ruby'
        binding.irb
        puts "foo"
        puts "bar"
        puts "baz"
      ruby

      output = run_ruby_file do
        type "next"
        type ""
        # Test that empty input doesn't repeat expressions
        type "123"
        type ""
        type "next"
        type ""
        type ""
      end

      assert_include(output, "=>   2\| puts \"foo\"")
      assert_include(output, "=>   3\| puts \"bar\"")
      assert_include(output, "=>   4\| puts \"baz\"")
    end
  end
end