Skip to content

Commit a97a412

Browse files
authored
Polish tracer integration and tests (#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
1 parent 08834fb commit a97a412

File tree

6 files changed

+44
-48
lines changed

6 files changed

+44
-48
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,17 @@ jobs:
2525
run: bundle exec rubocop
2626
irb:
2727
needs: ruby-versions
28-
name: rake test ${{ matrix.ruby }} ${{ matrix.with_latest_reline && '(latest reline)' || '' }} ${{ matrix.with_tracer && '(with tracer)' || '' }}
28+
name: rake test ${{ matrix.ruby }} ${{ matrix.with_latest_reline && '(latest reline)' || '' }}
2929
strategy:
3030
matrix:
3131
ruby: ${{ fromJson(needs.ruby-versions.outputs.versions) }}
3232
with_latest_reline: [true, false]
33-
with_tracer: [true, false]
3433
exclude:
3534
- ruby: truffleruby
3635
fail-fast: false
3736
runs-on: ubuntu-latest
3837
env:
3938
WITH_LATEST_RELINE: ${{matrix.with_latest_reline}}
40-
WITH_TRACER: ${{matrix.with_tracer}}
4139
timeout-minutes: 30
4240
steps:
4341
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11

Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ gem "test-unit-ruby-core"
1919

2020
gem "rubocop"
2121

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

2525
if RUBY_VERSION >= "3.0.0" && !is_truffleruby

lib/irb/context.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def initialize(irb, workspace = nil, input_method = nil)
6161
@io = nil
6262

6363
self.inspect_mode = IRB.conf[:INSPECT_MODE]
64-
self.use_tracer = IRB.conf[:USE_TRACER] if IRB.conf[:USE_TRACER]
64+
self.use_tracer = IRB.conf[:USE_TRACER]
6565
self.use_loader = IRB.conf[:USE_LOADER] if IRB.conf[:USE_LOADER]
6666
self.eval_history = IRB.conf[:EVAL_HISTORY] if IRB.conf[:EVAL_HISTORY]
6767

@@ -162,8 +162,8 @@ def initialize(irb, workspace = nil, input_method = nil)
162162
private_constant :KEYWORD_ALIASES
163163

164164
def use_tracer=(val)
165-
require_relative "ext/tracer"
166-
@use_tracer = val
165+
require_relative "ext/tracer" if val
166+
IRB.conf[:USE_TRACER] = val
167167
end
168168

169169
private def build_completor

lib/irb/ext/tracer.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@
1313
end
1414

1515
module IRB
16+
class CallTracer < ::CallTracer
17+
IRB_DIR = File.expand_path('../..', __dir__)
18+
19+
def skip?(tp)
20+
super || tp.path.match?(IRB_DIR) || tp.path.match?('<internal:prelude>')
21+
end
22+
end
1623
class WorkSpace
1724
alias __evaluate__ evaluate
1825
# Evaluate the context of this workspace and use the Tracer library to

lib/irb/init.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,6 @@ def IRB.setup(ap_path, argv: ::ARGV)
6262

6363
# @CONF default setting
6464
def IRB.init_config(ap_path)
65-
# class instance variables
66-
@TRACER_INITIALIZED = false
67-
6865
# default configurations
6966
unless ap_path and @CONF[:AP_NAME]
7067
ap_path = File.join(File.dirname(File.dirname(__FILE__)), "irb.rb")

test/irb/test_tracer.rb

Lines changed: 32 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ class ContextWithTracerIntegrationTest < IntegrationTestCase
1010
def setup
1111
super
1212

13-
@envs.merge!("NO_COLOR" => "true", "RUBY_DEBUG_HISTORY_FILE" => '')
13+
omit "Tracer gem is not available when running on TruffleRuby" if RUBY_ENGINE == "truffleruby"
14+
15+
@envs.merge!("NO_COLOR" => "true")
1416
end
1517

1618
def example_ruby_file
@@ -29,54 +31,30 @@ def bar(obj)
2931
RUBY
3032
end
3133

32-
def test_use_tracer_is_disabled_by_default
34+
def test_use_tracer_enabled_when_gem_is_unavailable
3335
write_rc <<~RUBY
34-
IRB.conf[:USE_TRACER] = false
36+
# Simulate the absence of the tracer gem
37+
::Kernel.send(:alias_method, :irb_original_require, :require)
38+
39+
::Kernel.define_method(:require) do |name|
40+
raise LoadError, "cannot load such file -- tracer (test)" if name.match?("tracer")
41+
::Kernel.send(:irb_original_require, name)
42+
end
43+
44+
IRB.conf[:USE_TRACER] = true
3545
RUBY
3646

3747
write_ruby example_ruby_file
3848

3949
output = run_ruby_file do
4050
type "bar(Foo)"
41-
type "exit!"
51+
type "exit"
4252
end
4353

44-
assert_nil IRB.conf[:USER_TRACER]
45-
assert_not_include(output, "#depth:")
46-
assert_not_include(output, "Foo.foo")
47-
end
48-
49-
def test_use_tracer_enabled_when_gem_is_unavailable
50-
begin
51-
gem 'tracer'
52-
omit "Skipping because 'tracer' gem is available."
53-
rescue Gem::LoadError
54-
write_rc <<~RUBY
55-
IRB.conf[:USE_TRACER] = true
56-
RUBY
57-
58-
write_ruby example_ruby_file
59-
60-
output = run_ruby_file do
61-
type "bar(Foo)"
62-
type "exit!"
63-
end
64-
65-
assert_include(output, "Tracer extension of IRB is enabled but tracer gem wasn't found.")
66-
end
54+
assert_include(output, "Tracer extension of IRB is enabled but tracer gem wasn't found.")
6755
end
6856

6957
def test_use_tracer_enabled_when_gem_is_available
70-
if Gem::Version.new(RUBY_VERSION) < Gem::Version.new('3.1.0')
71-
omit "Ruby version before 3.1.0 does not support Tracer integration. Skipping this test."
72-
end
73-
74-
begin
75-
gem 'tracer'
76-
rescue Gem::LoadError
77-
omit "Skipping because 'tracer' gem is not available. Enable with WITH_TRACER=true."
78-
end
79-
8058
write_rc <<~RUBY
8159
IRB.conf[:USE_TRACER] = true
8260
RUBY
@@ -85,13 +63,29 @@ def test_use_tracer_enabled_when_gem_is_available
8563

8664
output = run_ruby_file do
8765
type "bar(Foo)"
88-
type "exit!"
66+
type "exit"
8967
end
9068

9169
assert_include(output, "Object#bar at")
9270
assert_include(output, "Foo.foo at")
9371
assert_include(output, "Foo.foo #=> 100")
9472
assert_include(output, "Object#bar #=> 100")
73+
74+
# Test that the tracer output does not include IRB's own files
75+
assert_not_include(output, "irb/workspace.rb")
76+
end
77+
78+
def test_use_tracer_is_disabled_by_default
79+
write_ruby example_ruby_file
80+
81+
output = run_ruby_file do
82+
type "bar(Foo)"
83+
type "exit"
84+
end
85+
86+
assert_not_include(output, "#depth:")
87+
assert_not_include(output, "Foo.foo")
9588
end
89+
9690
end
9791
end

0 commit comments

Comments
 (0)