-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix usage of tracer gem and add tests
The new tests are skipped when ruby below 3.1, as it was a default gem on it, and in a version we do not support. This also move definition of `use_tracer` to module Context instead of monkey patch.
- Loading branch information
1 parent
1b11fd9
commit b658e7c
Showing
8 changed files
with
120 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,76 +3,30 @@ | |
# irb/lib/tracer.rb - | ||
# by Keiju ISHITSUKA([email protected]) | ||
# | ||
|
||
# Loading the gem "tracer" will cause it to extend IRB commands with: | ||
# https://github.com/ruby/tracer/blob/v0.2.2/lib/tracer/irb.rb | ||
begin | ||
require "tracer" | ||
rescue LoadError | ||
$stderr.puts "Tracer extension of IRB is enabled but tracer gem wasn't found." | ||
module IRB | ||
class Context | ||
def use_tracer=(opt) | ||
# do nothing | ||
end | ||
end | ||
end | ||
return # This is about to disable loading below | ||
end | ||
|
||
module IRB | ||
|
||
# initialize tracing function | ||
def IRB.initialize_tracer | ||
Tracer.verbose = false | ||
Tracer.add_filter { | ||
|event, file, line, id, binding, *rests| | ||
/^#{Regexp.quote(@CONF[:IRB_LIB_PATH])}/ !~ file and | ||
File::basename(file) != "irb.rb" | ||
} | ||
end | ||
|
||
class Context | ||
# Whether Tracer is used when evaluating statements in this context. | ||
# | ||
# See +lib/tracer.rb+ for more information. | ||
attr_reader :use_tracer | ||
alias use_tracer? use_tracer | ||
|
||
# Sets whether or not to use the Tracer library when evaluating statements | ||
# in this context. | ||
# | ||
# See +lib/tracer.rb+ for more information. | ||
def use_tracer=(opt) | ||
if opt | ||
Tracer.set_get_line_procs(@irb_path) { | ||
|line_no, *rests| | ||
@io.line(line_no) | ||
} | ||
elsif !opt && @use_tracer | ||
Tracer.off | ||
end | ||
@use_tracer=opt | ||
end | ||
end | ||
|
||
class WorkSpace | ||
alias __evaluate__ evaluate | ||
# Evaluate the context of this workspace and use the Tracer library to | ||
# output the exact lines of code are being executed in chronological order. | ||
# | ||
# See +lib/tracer.rb+ for more information. | ||
def evaluate(context, statements, file = nil, line = nil) | ||
if context.use_tracer? && file != nil && line != nil | ||
Tracer.on | ||
begin | ||
# See https://github.com/ruby/tracer for more information. | ||
def evaluate(statements, file = __FILE__, line = __LINE__) | ||
if IRB.conf[:USE_TRACER] == true | ||
CallTracer.new(colorize: Color.colorable?).start do | ||
__evaluate__(statements, file, line) | ||
ensure | ||
Tracer.off | ||
end | ||
else | ||
__evaluate__(statements, file || __FILE__, line || __LINE__) | ||
__evaluate__(statements, file, line) | ||
end | ||
end | ||
end | ||
|
||
IRB.initialize_tracer | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
# frozen_string_literal: false | ||
require 'tempfile' | ||
require 'irb' | ||
require 'rubygems' | ||
|
||
require_relative "helper" | ||
|
||
module TestIRB | ||
class ContextWithTracerIntegrationTest < IntegrationTestCase | ||
def setup | ||
super | ||
|
||
@envs.merge!("NO_COLOR" => "true", "RUBY_DEBUG_HISTORY_FILE" => '') | ||
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_is_disabled_by_default | ||
write_rc <<~RUBY | ||
IRB.conf[:USE_TRACER] = false | ||
RUBY | ||
|
||
write_ruby example_ruby_file | ||
|
||
output = run_ruby_file do | ||
type "bar(Foo)" | ||
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 | ||
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 | ||
|
||
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") | ||
end | ||
end | ||
end |