summaryrefslogtreecommitdiff
path: root/test/irb/test_tracer.rb
blob: 540f8be1314c5d7b27f054a136ab1accad1529bc (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
# frozen_string_literal: false
require 'tempfile'
require 'irb'

require_relative "helper"

module TestIRB
  class ContextWithTracerIntegrationTest < IntegrationTestCase
    def setup
      super

      omit "Tracer gem is not available when running on TruffleRuby" if RUBY_ENGINE == "truffleruby"

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

    def example_ruby_file
      <<~'RUBY'
        class Foo
          def self.foo
            100
          end
        end

        def bar(obj)
          obj.foo
        end

        binding.irb
      RUBY
    end

    def test_use_tracer_enabled_when_gem_is_unavailable
      write_rc <<~RUBY
        # Simulate the absence of the tracer gem
        ::Kernel.send(:alias_method, :irb_original_require, :require)

        ::Kernel.define_method(:require) do |name|
          raise LoadError, "cannot load such file -- tracer (test)" if name.match?("tracer")
          ::Kernel.send(:irb_original_require, name)
        end

        IRB.conf[:USE_TRACER] = true
      RUBY

      write_ruby example_ruby_file

      output = run_ruby_file do
        type "bar(Foo)"
        type "exit"
      end

      assert_include(output, "Tracer extension of IRB is enabled but tracer gem wasn't found.")
    end

    def test_use_tracer_enabled_when_gem_is_available
      write_rc <<~RUBY
        IRB.conf[:USE_TRACER] = true
      RUBY

      write_ruby example_ruby_file

      output = run_ruby_file do
        type "bar(Foo)"
        type "exit"
      end

      assert_include(output, "Object#bar at")
      assert_include(output, "Foo.foo at")
      assert_include(output, "Foo.foo #=> 100")
      assert_include(output, "Object#bar #=> 100")

      # Test that the tracer output does not include IRB's own files
      assert_not_include(output, "irb/workspace.rb")
    end

    def test_use_tracer_is_disabled_by_default
      write_ruby example_ruby_file

      output = run_ruby_file do
        type "bar(Foo)"
        type "exit"
      end

      assert_not_include(output, "#depth:")
      assert_not_include(output, "Foo.foo")
    end

  end
end