Skip to content

Commit

Permalink
Polish tracer integration and tests (#864)
Browse files Browse the repository at this point in the history
* Remove useless ivar

* Simplify tracer test setup

* Treat tracer like a normal development dependency

* Only require ext/tracer when value is truthy

* Make tracer integration skip IRB traces
  • Loading branch information
st0012 authored Feb 7, 2024
1 parent 08834fb commit a97a412
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 48 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,17 @@ jobs:
run: bundle exec rubocop
irb:
needs: ruby-versions
name: rake test ${{ matrix.ruby }} ${{ matrix.with_latest_reline && '(latest reline)' || '' }} ${{ matrix.with_tracer && '(with tracer)' || '' }}
name: rake test ${{ matrix.ruby }} ${{ matrix.with_latest_reline && '(latest reline)' || '' }}
strategy:
matrix:
ruby: ${{ fromJson(needs.ruby-versions.outputs.versions) }}
with_latest_reline: [true, false]
with_tracer: [true, false]
exclude:
- ruby: truffleruby
fail-fast: false
runs-on: ubuntu-latest
env:
WITH_LATEST_RELINE: ${{matrix.with_latest_reline}}
WITH_TRACER: ${{matrix.with_tracer}}
timeout-minutes: 30
steps:
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11
Expand Down
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ gem "test-unit-ruby-core"

gem "rubocop"

gem "tracer" if ENV["WITH_TRACER"] == "true"
gem "tracer" if !is_truffleruby
gem "debug", github: "ruby/debug", platforms: [:mri, :mswin]

if RUBY_VERSION >= "3.0.0" && !is_truffleruby
Expand Down
6 changes: 3 additions & 3 deletions lib/irb/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def initialize(irb, workspace = nil, input_method = nil)
@io = nil

self.inspect_mode = IRB.conf[:INSPECT_MODE]
self.use_tracer = IRB.conf[:USE_TRACER] if IRB.conf[:USE_TRACER]
self.use_tracer = IRB.conf[:USE_TRACER]
self.use_loader = IRB.conf[:USE_LOADER] if IRB.conf[:USE_LOADER]
self.eval_history = IRB.conf[:EVAL_HISTORY] if IRB.conf[:EVAL_HISTORY]

Expand Down Expand Up @@ -162,8 +162,8 @@ def initialize(irb, workspace = nil, input_method = nil)
private_constant :KEYWORD_ALIASES

def use_tracer=(val)
require_relative "ext/tracer"
@use_tracer = val
require_relative "ext/tracer" if val
IRB.conf[:USE_TRACER] = val
end

private def build_completor
Expand Down
7 changes: 7 additions & 0 deletions lib/irb/ext/tracer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@
end

module IRB
class CallTracer < ::CallTracer
IRB_DIR = File.expand_path('../..', __dir__)

def skip?(tp)
super || tp.path.match?(IRB_DIR) || tp.path.match?('<internal:prelude>')
end
end
class WorkSpace
alias __evaluate__ evaluate
# Evaluate the context of this workspace and use the Tracer library to
Expand Down
3 changes: 0 additions & 3 deletions lib/irb/init.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,6 @@ def IRB.setup(ap_path, argv: ::ARGV)

# @CONF default setting
def IRB.init_config(ap_path)
# class instance variables
@TRACER_INITIALIZED = false

# default configurations
unless ap_path and @CONF[:AP_NAME]
ap_path = File.join(File.dirname(File.dirname(__FILE__)), "irb.rb")
Expand Down
70 changes: 32 additions & 38 deletions test/irb/test_tracer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ class ContextWithTracerIntegrationTest < IntegrationTestCase
def setup
super

@envs.merge!("NO_COLOR" => "true", "RUBY_DEBUG_HISTORY_FILE" => '')
omit "Tracer gem is not available when running on TruffleRuby" if RUBY_ENGINE == "truffleruby"

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

def example_ruby_file
Expand All @@ -29,54 +31,30 @@ def bar(obj)
RUBY
end

def test_use_tracer_is_disabled_by_default
def test_use_tracer_enabled_when_gem_is_unavailable
write_rc <<~RUBY
IRB.conf[:USE_TRACER] = false
# 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!"
type "exit"
end

assert_nil IRB.conf[:USER_TRACER]
assert_not_include(output, "#depth:")
assert_not_include(output, "Foo.foo")
end

def test_use_tracer_enabled_when_gem_is_unavailable
begin
gem 'tracer'
omit "Skipping because 'tracer' gem is available."
rescue Gem::LoadError
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, "Tracer extension of IRB is enabled but tracer gem wasn't found.")
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
if Gem::Version.new(RUBY_VERSION) < Gem::Version.new('3.1.0')
omit "Ruby version before 3.1.0 does not support Tracer integration. Skipping this test."
end

begin
gem 'tracer'
rescue Gem::LoadError
omit "Skipping because 'tracer' gem is not available. Enable with WITH_TRACER=true."
end

write_rc <<~RUBY
IRB.conf[:USE_TRACER] = true
RUBY
Expand All @@ -85,13 +63,29 @@ def test_use_tracer_enabled_when_gem_is_available

output = run_ruby_file do
type "bar(Foo)"
type "exit!"
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

0 comments on commit a97a412

Please sign in to comment.