[ruby/irb] Polish tracer integration and tests

(https://github.com/ruby/irb/pull/864)

* 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

https://github.com/ruby/irb/commit/a97a4129a7
This commit is contained in:
Stan Lo 2024-02-07 14:59:06 +00:00 коммит произвёл git
Родитель 164c18af7b
Коммит 5f4245e74b
4 изменённых файлов: 42 добавлений и 44 удалений

Просмотреть файл

@ -61,7 +61,7 @@ module IRB
@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]
@ -162,8 +162,8 @@ module IRB
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

Просмотреть файл

@ -13,6 +13,13 @@ rescue LoadError
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

Просмотреть файл

@ -62,9 +62,6 @@ module IRB # :nodoc:
# @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")

Просмотреть файл

@ -10,7 +10,9 @@ module TestIRB
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
@ -29,54 +31,30 @@ module TestIRB
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
@ -85,13 +63,29 @@ module TestIRB
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