Skip to content

Commit

Permalink
Fix Ruby 2.6 __LINE__ and __FILE__ deprecated usage in Binding#eval
Browse files Browse the repository at this point in the history
  • Loading branch information
gsamokovarov committed Mar 3, 2019
1 parent f12c373 commit f97d8a8
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 30 deletions.
1 change: 1 addition & 0 deletions lib/web_console.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module WebConsole
autoload :Template
autoload :Middleware
autoload :Context
autoload :SourceLocation

autoload_at "web_console/errors" do
autoload :Error
Expand Down
6 changes: 4 additions & 2 deletions lib/web_console/exception_mapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ def guess_binding_for_index(index)
line = line.to_i

@bindings.find do |binding|
binding.eval("__FILE__") == file && binding.eval("__LINE__") == line
source_location = SourceLocation.new(binding)
source_location.path == file && source_location.lineno == line
end
end

def guess_the_first_application_binding
@bindings.find do |binding|
binding.eval("__FILE__").to_s.start_with?(Rails.root.to_s)
source_location = SourceLocation.new(binding)
source_location.path.to_s.start_with?(Rails.root.to_s)
end
end
end
Expand Down
15 changes: 15 additions & 0 deletions lib/web_console/source_location.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

class SourceLocation
def initialize(binding)
@binding = binding
end

if RUBY_VERSION >= "2.6"
def path() @binding.source_location.first end
def lineno() @binding.source_location.last end
else
def path() @binding.eval("__FILE__") end
def lineno() @binding.eval("__LINE__") end
end
end
6 changes: 3 additions & 3 deletions test/web_console/exception_mapper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class ExcetionMapperTest < ActiveSupport::TestCase

mapper = ExceptionMapper.new(External.exception)

assert_equal __FILE__, mapper.first.eval("__FILE__")
assert_equal __FILE__, SourceLocation.new(mapper.first).path
end

test ".[] tries match the binding for trace index" do
Expand All @@ -19,8 +19,8 @@ class ExcetionMapperTest < ActiveSupport::TestCase
last_index = exception.backtrace.count - 1
file, line = exception.backtrace.last.split(":")

assert_equal file, mapper[last_index].eval("__FILE__")
assert_equal line.to_i, mapper[last_index].eval("__LINE__")
assert_equal file, SourceLocation.new(mapper[last_index]).path
assert_equal line.to_i, SourceLocation.new(mapper[last_index]).lineno
end

test ".[] fall backs to index if no trace can be found" do
Expand Down
12 changes: 6 additions & 6 deletions test/web_console/integration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,37 @@ class IntegrationTest < ActiveSupport::TestCase
test "Exception#bindings returns all the bindings of where the error originated" do
exc = FlatScenario.new.call

assert_equal 6, exc.bindings.first.eval("__LINE__")
assert_equal 6, SourceLocation.new(exc.bindings.first).lineno
end

test "Exception#bindings returns all the bindings for a custom error" do
exc = CustomErrorScenario.new.call

assert_equal 8, exc.bindings.first.eval("__LINE__")
assert_equal 8, SourceLocation.new(exc.bindings.first).lineno
end

test "Exception#bindings returns all the bindings for a bad custom error" do
exc = BadCustomErrorScenario.new.call

assert_equal 13, exc.bindings.first.eval("__LINE__")
assert_equal 13, SourceLocation.new(exc.bindings.first).lineno
end

test "Exception#bindings goes down the stack" do
exc = BasicNestedScenario.new.call

assert_equal 14, exc.bindings.first.eval("__LINE__")
assert_equal 14, SourceLocation.new(exc.bindings.first).lineno
end

test "Exception#bindings inside of an eval" do
exc = EvalNestedScenario.new.call

assert_equal 14, exc.bindings.first.eval("__LINE__")
assert_equal 14, SourceLocation.new(exc.bindings.first).lineno
end

test "re-raising doesn't lose Exception#bindings information" do
exc = ReraisedScenario.new.call

assert_equal 6, exc.bindings.first.eval("__LINE__")
assert_equal 6, SourceLocation.new(exc.bindings.first).lineno
end

test "Exception#bindings is empty when exception is still not raised" do
Expand Down
8 changes: 4 additions & 4 deletions test/web_console/middleware_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def headers
Session.stubs(:from).returns(session)

get "/", params: nil
put "/repl_sessions/#{session.id}", xhr: true, params: { input: "__LINE__" }
put "/repl_sessions/#{session.id}", xhr: true, params: { input: "line" }

assert_equal("=> #{line}\n", JSON.parse(response.body)["output"])
end
Expand All @@ -181,10 +181,10 @@ def headers
test "can be changed mount point" do
Middleware.mount_point = "/customized/path"

session, line = Session.new([binding]), __LINE__
put "/customized/path/repl_sessions/#{session.id}", params: { input: "__LINE__" }, xhr: true
session, value = Session.new([binding]), __LINE__
put "/customized/path/repl_sessions/#{session.id}", params: { input: "value" }, xhr: true

assert_equal("=> #{line}\n", JSON.parse(response.body)["output"])
assert_equal("=> #{value}\n", JSON.parse(response.body)["output"])
end

test "can return context information by passing a context param" do
Expand Down
31 changes: 16 additions & 15 deletions test/web_console/session_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@

module WebConsole
class SessionTest < ActiveSupport::TestCase
class LineAwareError < StandardError
def self.raise
::Kernel.raise new(__LINE__)
class ValueAwareError < StandardError
def self.raise(value)
::Kernel.raise self, value
rescue => exc
exc
end

attr_reader :line
attr_reader :value

def initialize(line)
@line = line
def initialize(value)
@value = value
end
end

Expand Down Expand Up @@ -52,38 +52,39 @@ def eval(string)
end

test "#from can create session from a single binding" do
saved_line, saved_binding = __LINE__, binding
value, saved_binding = __LINE__, binding
Thread.current[:__web_console_binding] = saved_binding

session = Session.from(__web_console_binding: saved_binding)

assert_equal "=> #{saved_line}\n", session.eval("__LINE__")
assert_equal "=> #{value}\n", session.eval("value")
end

test "#from can create session from an exception" do
exc = LineAwareError.raise
value = __LINE__
exc = ValueAwareError.raise(value)

session = Session.from(__web_console_exception: exc)

assert_equal "=> #{exc.line}\n", session.eval("__LINE__")
assert_equal "=> #{exc.value}\n", session.eval("value")
end

test "#from can switch to bindings" do
exc, saved_line = LineAwareError.raise, __LINE__
value = __LINE__
exc = ValueAwareError.raise(value)

session = Session.from(__web_console_exception: exc)
session.switch_binding_to(1)

assert_equal "=> #{saved_line}\n", session.eval("__LINE__")
assert_equal "=> #{value}\n", session.eval("value")
end

test "#from prioritizes exceptions over bindings" do
exc, saved_line = LineAwareError.raise, __LINE__
exc = ValueAwareError.raise(42)

session = Session.from(__web_console_exception: exc, __web_console_binding: binding)
session.switch_binding_to(1)

assert_equal "=> #{saved_line}\n", session.eval("__LINE__")
assert_equal "=> WebConsole::SessionTest::ValueAwareError\n", session.eval("self")
end
end
end

0 comments on commit f97d8a8

Please sign in to comment.