summaryrefslogtreecommitdiff
path: root/spec/pry_spec.rb
blob: 8fa24e115d815d98c97a548c5d9d0005735ce6a9 (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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
# frozen_string_literal: true

describe Pry do
  before do
    @str_output = StringIO.new
  end

  describe ".configure" do
    it "yields a block with Pry.config as its argument" do
      Pry.config.foo = nil
      Pry.configure do |config|
        config.foo = "bar"
      end
      expect(Pry.config.foo).to eq("bar")
    end
  end

  describe "Exotic object support" do
    # regression test for exotic object support
    it "Should not error when return value is a BasicObject instance" do
      ReplTester.start do
        expect(input('BasicObject.new')).to match(/^=> #<BasicObject:/)
      end
    end
  end

  describe 'DISABLE_PRY' do
    before do
      allow(Pry::Env).to receive(:[])
      allow(Pry::Env).to receive(:[]).with('DISABLE_PRY').and_return(true)
    end

    it 'should not binding.pry' do
      expect(binding.pry).to eq nil # rubocop:disable Lint/Debugger
    end

    it 'should not Pry.start' do
      expect(Pry.start).to eq nil
    end
  end

  describe 'FAIL_PRY' do
    before do
      allow(Pry::Env).to receive(:[])
      allow(Pry::Env).to receive(:[]).with('FAIL_PRY').and_return(true)
    end

    it 'should raise an error for binding.pry' do
      expect { binding.pry }.to raise_error(RuntimeError) # rubocop:disable Lint/Debugger
    end

    it 'should raise an error for Pry.start' do
      expect { Pry.start }.to raise_error(RuntimeError)
    end
  end

  describe "Pry.critical_section" do
    it "should prevent Pry being called" do
      output = StringIO.new
      Pry.config.output = output
      Pry.critical_section do
        Pry.start
      end
      expect(output.string).to match(/Pry started inside Pry/)
    end
  end

  describe "Pry.binding_for" do
    # regression test for burg's bug (see git history)
    it "Should not error when object doesn't have a valid == method" do
      o = Object.new
      def o.==(_other)
        raise
      end

      expect { Pry.binding_for(o) }.to_not raise_error
    end

    it "should not leak local variables" do
      [Object.new, Array, 3].each do |obj|
        expect(Pry.binding_for(obj).eval("local_variables")).to be_empty
      end
    end

    it "should work on frozen objects" do
      a = "hello".freeze
      expect(Pry.binding_for(a).eval("self")).to equal(a)
    end
  end

  describe "#last_exception=" do
    before do
      @pry = Pry.new binding: binding
      @e = mock_exception "foo.rb:1"
    end

    it "returns an instance of Pry::LastException" do
      @pry.last_exception = @e
      expect(@pry.last_exception.wrapped_exception).to eq @e
    end

    it "returns a frozen exception" do
      @pry.last_exception = @e.freeze
      expect(@pry.last_exception).to be_frozen
    end

    it "returns an object who mirrors itself as the wrapped exception" do
      @pry.last_exception = @e.freeze
      expect(@pry.last_exception).to be_an_instance_of StandardError
    end
  end

  describe "open a Pry session on an object" do
    describe "rep" do
      before do
        class Hello
        end
      end

      after do
        Object.send(:remove_const, :Hello)
      end

      # bug fix for https://github.com/pry/pry/issues/93
      it 'should not leak pry constants into Object namespace' do
        expect { pry_eval(Object.new, "Command") }.to raise_error NameError
      end

      it 'should be able to operate inside the BasicObject class' do
        pry_eval(BasicObject, ":foo", "Pad.obj = _")
        expect(Pad.obj).to eq :foo
      end

      it 'should set an ivar on an object' do
        o = Object.new
        pry_eval(o, "@x = 10")
        expect(o.instance_variable_get(:@x)).to eq 10
      end

      it 'should display error if Pry instance runs out of input' do
        redirect_pry_io(StringIO.new, @str_output) do
          Pry.start
        end
        expect(@str_output.string).to match(/Error: Pry ran out of things to read/)
      end

      it 'should make self evaluate to the receiver of the rep session' do
        o = :john
        expect(pry_eval(o, "self")).to eq o
      end

      it 'should define a nested class under Hello and not on top-level or Pry' do
        mock_pry(Pry.binding_for(Hello), "class Nested", "end")
        expect(Hello.const_defined?(:Nested)).to eq true
      end

      it(
        'should suppress output if input ends in a ";" and is an Exception ' \
        'object (single line)'
      ) do
        expect(mock_pry("Exception.new;")).to eq ""
      end

      it 'should suppress output if input ends in a ";" (single line)' do
        expect(mock_pry("x = 5;")).to eq ""
      end

      it 'should be able to evaluate exceptions normally' do
        was_called = false
        mock_pry("RuntimeError.new", exception_handler: proc { was_called = true })
        expect(was_called).to eq false
      end

      it 'should notice when exceptions are raised' do
        was_called = false
        mock_pry("raise RuntimeError", exception_handler: proc { was_called = true })
        expect(was_called).to eq true
      end

      it 'should not try to catch intended exceptions' do
        expect { mock_pry("raise SystemExit") }.to raise_error SystemExit
        # SIGTERM
        expect { mock_pry("raise SignalException.new(15)") }
          .to raise_error SignalException
      end

      describe "inside signal handler" do
        before do
          unless Signal.list.has_key?('USR1')
            skip "Host OS #{RbConfig::CONFIG['host_os']} doesn't support signal USR1"
          end

          if Pry::Helpers::Platform.jruby?
            skip "jruby allows mutex usage in signal handlers"
          end

          if Gem::Version.new(RUBY_VERSION) < Gem::Version.new("2.0.0")
            skip "pre-2.0 mri allows mutex usage in signal handlers"
          end

          trap("USR1") { @str_output = mock_pry }
        end

        after do
          trap("USR1") do
            # do nothing
          end
        end

        it "should return with error message" do
          Process.kill("USR1", Process.pid)
          expect(@str_output).to match(/Unable to obtain mutex lock/)
        end
      end

      describe "multi-line input" do
        it "works" do
          expect(mock_pry('x = ', '1 + 4')).to match(/5/)
        end

        it 'should suppress output if input ends in a ";" (multi-line)' do
          expect(mock_pry('def self.blah', ':test', 'end;')).to eq ''
        end

        describe "newline stripping from an empty string" do
          it "with double quotes" do
            expect(mock_pry('"', '"')).to match(/"\\n"/)
            expect(mock_pry('"', "\n", "\n", "\n", '"')).to match(/"\\n\\n\\n\\n"/)
          end

          it "with single quotes" do
            expect(mock_pry("'", "'")).to match(/"\\n"/)
            expect(mock_pry("'", "\n", "\n", "\n", "'")).to match(/"\\n\\n\\n\\n"/)
          end

          it "with fancy delimiters" do
            expect(mock_pry('%(', ')')).to match(/"\\n"/)
            expect(mock_pry('%|', "\n", "\n", '|')).to match(/"\\n\\n\\n"/)
            expect(mock_pry('%q[', "\n", "\n", ']')).to match(/"\\n\\n\\n"/)
          end
        end

        describe "newline stripping from an empty regexp" do
          it "with regular regexp delimiters" do
            expect(mock_pry('/', '/')).to match(%r{/\n/})
          end

          it "with fancy delimiters" do
            expect(mock_pry('%r{', "\n", "\n", '}')).to match(%r{/\n\n\n/})
            expect(mock_pry('%r<', "\n", '>')).to match(%r{/\n\n/})
          end
        end

        describe "newline from an empty heredoc" do
          it "works" do
            expect(mock_pry('<<HERE', 'HERE')).to match(/""/)
            expect(mock_pry("<<'HERE'", "\n", 'HERE')).to match(/"\\n"/)
            expect(mock_pry("<<-'HERE'", "\n", "\n", 'HERE')).to match(/"\\n\\n"/)
          end
        end
      end
    end

    describe "repl" do
      describe "basic functionality" do
        it 'should set an ivar on an object and exit the repl' do
          input_strings = ["@x = 10", "exit-all"]
          input = InputTester.new(*input_strings)

          o = Object.new

          Pry.start(o, input: input, output: StringIO.new)

          expect(o.instance_variable_get(:@x)).to eq 10
        end
      end

      describe "complete_expression?" do
        it "should not mutate the input!" do
          clean = "puts <<-FOO\nhi\nFOO\n"
          a = clean.dup
          Pry::Code.complete_expression?(a)
          expect(a).to eq clean
        end
      end

      describe "history arrays" do
        it 'sets _ to the last result' do
          t = pry_tester
          t.eval ":foo"
          expect(t.eval("_")).to eq :foo
          t.eval "42"
          expect(t.eval("_")).to eq 42
        end

        it 'sets out to an array with the result' do
          t = pry_tester
          t.eval ":foo"
          t.eval "42"
          res = t.eval "_out_"

          expect(res).to be_a_kind_of(Pry::Ring)
          expect(res[1..2]).to eq [:foo, 42]
        end

        it 'sets _in_ to an array with the entered lines' do
          t = pry_tester
          t.eval ":foo"
          t.eval "42"
          res = t.eval "_in_"

          expect(res).to be_a_kind_of(Pry::Ring)
          expect(res[1..2]).to eq [":foo\n", "42\n"]
        end

        it 'uses 100 as the size of _in_ and _out_' do
          expect(pry_tester.eval("[_in_.max_size, _out_.max_size]")).to eq [100, 100]
        end

        it 'can change the size of the history arrays' do
          expect(pry_tester(memory_size: 1000).eval("[_out_, _in_].map(&:max_size)"))
            .to eq [1000, 1000]
        end

        it 'store exceptions' do
          mock_pry("foo!", "Pad.in = _in_[-1]; Pad.out = _out_[-1]")

          expect(Pad.in).to eq "foo!\n"
          expect(Pad.out).to be_a_kind_of NoMethodError
        end
      end

      describe "last_result" do
        it "should be set to the most recent value" do
          expect(pry_eval("2", "_ + 82")).to eq 84
        end

        # This test needs mock_pry because the command retvals work by
        # replacing the eval_string, so _ won't be modified without Pry doing
        # a REPL loop.
        it "should be set to the result of a command with :keep_retval" do
          Pry::Commands.block_command '++', '', keep_retval: true do |a|
            a.to_i + 1
          end

          # rubocop:disable Lint/InterpolationCheck
          expect(mock_pry('++ 86', '++ #{_}')).to match(/88/)
          # rubocop:enable Lint/InterpolationCheck
        end

        it "should be preserved over an empty line" do
          expect(pry_eval("2 + 2", " ", "\t",  " ", "_ + 92")).to eq 96
        end

        it "should be preserved when evalling a  command without :keep_retval" do
          expect(pry_eval("2 + 2", "ls -l", "_ + 96")).to eq 100
        end
      end

      describe "nesting" do
        after do
          Pry.reset_defaults
          Pry.config.color = false
        end

        it 'should nest properly' do
          Pry.config.input = InputTester.new(
            "cd 1", "cd 2", "cd 3",
            "\"nest:\#\{(pry_instance.binding_stack.size - 1)\}\"", "exit-all"
          )

          Pry.config.output = @str_output

          o = Object.new
          o.pry

          expect(@str_output.string).to match(/nest:3/)
        end
      end

      describe "defining methods" do
        it(
          'defines a method on the singleton class of an object when performing ' \
          '"def meth;end" inside the object'
        ) do
          [Object.new, {}, []].each do |val|
            pry_eval(val, 'def hello; end')
            expect(val.methods(false).map(&:to_sym).include?(:hello)).to eq true
          end
        end

        it(
          'defines an instance method on the module when performing ' \
          '"def meth;end" inside the module'
        ) do
          hello = Module.new
          pry_eval(hello, "def hello; end")
          expect(hello.instance_methods(false).map(&:to_sym).include?(:hello))
            .to be_truthy
        end

        it(
          'defines an instance method on the class when performing ' \
          '"def meth;end" inside the class'
        ) do
          hello = Class.new
          pry_eval(hello, "def hello; end")
          expect(hello.instance_methods(false).map(&:to_sym).include?(:hello))
            .to be_truthy
        end

        it(
          'defines a method on the class of an object when performing ' \
          '"def meth;end" inside an immediate value or Numeric'
        ) do
          # JRuby behaves different than CRuby here (seems it always has to some
          # extent, see 'unless' below). It didn't seem trivial to work
          # around. Skip for now.
          skip "JRuby incompatibility" if Pry::Helpers::Platform.jruby?
          [
            :test, 0, true, false, nil, (0.0 unless Pry::Helpers::Platform.jruby?)
          ].each do |val|
            pry_eval(val, "def hello; end")
            expect(val.class.instance_methods(false).map(&:to_sym).include?(:hello))
              .to be_truthy
          end
        end
      end

      describe "Object#pry" do
        after do
          Pry.reset_defaults
          Pry.config.color = false
        end

        it "should start a pry session on the receiver (first form)" do
          Pry.config.input = InputTester.new("self", "exit-all")

          str_output = StringIO.new
          Pry.config.output = str_output

          20.pry

          expect(str_output.string).to match(/20/)
        end

        it "should start a pry session on the receiver (second form)" do
          Pry.config.input = InputTester.new("self", "exit-all")

          str_output = StringIO.new
          Pry.config.output = str_output

          pry 20

          expect(str_output.string).to match(/20/)
        end

        it "should raise if more than two arguments are passed to Object#pry" do
          expect { pry(20, :quiet, input: Readline) }.to raise_error ArgumentError
        end
      end

      describe "Pry.binding_for" do
        it 'should return TOPLEVEL_BINDING if parameter self is main' do
          main = -> { TOPLEVEL_BINDING.eval('self') }
          expect(Pry.binding_for(main.call).is_a?(Binding)).to eq true
          expect(Pry.binding_for(main.call)).to eq TOPLEVEL_BINDING
          expect(Pry.binding_for(main.call)).to eq Pry.binding_for(main.call)
        end
      end
    end
  end

  describe 'setting custom options' do
    it 'does not raise for unrecognized options' do
      expect { Pry.new(custom_option: 'custom value') }.to_not raise_error
    end

    it 'correctly handles the :quiet option (#1261)' do
      instance = Pry.new(quiet: true)
      expect(instance.quiet?).to eq true
    end
  end

  describe "a fresh instance" do
    it "should use `caller` as its backtrace" do
      location  = "#{__FILE__}:#{__LINE__ + 1}"[1..-1] # omit leading .
      backtrace = Pry.new.backtrace

      expect(backtrace).not_to equal nil
      expect(backtrace.any? { |l| l.include?(location) }).to equal true
    end
  end
end