Skip to content

Commit f97d8a8

Browse files
committedMar 3, 2019
Fix Ruby 2.6 __LINE__ and __FILE__ deprecated usage in Binding#eval
1 parent f12c373 commit f97d8a8

7 files changed

+49
-30
lines changed
 

‎lib/web_console.rb

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ module WebConsole
1717
autoload :Template
1818
autoload :Middleware
1919
autoload :Context
20+
autoload :SourceLocation
2021

2122
autoload_at "web_console/errors" do
2223
autoload :Error

‎lib/web_console/exception_mapper.rb

+4-2
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,15 @@ def guess_binding_for_index(index)
2222
line = line.to_i
2323

2424
@bindings.find do |binding|
25-
binding.eval("__FILE__") == file && binding.eval("__LINE__") == line
25+
source_location = SourceLocation.new(binding)
26+
source_location.path == file && source_location.lineno == line
2627
end
2728
end
2829

2930
def guess_the_first_application_binding
3031
@bindings.find do |binding|
31-
binding.eval("__FILE__").to_s.start_with?(Rails.root.to_s)
32+
source_location = SourceLocation.new(binding)
33+
source_location.path.to_s.start_with?(Rails.root.to_s)
3234
end
3335
end
3436
end

‎lib/web_console/source_location.rb

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# frozen_string_literal: true
2+
3+
class SourceLocation
4+
def initialize(binding)
5+
@binding = binding
6+
end
7+
8+
if RUBY_VERSION >= "2.6"
9+
def path() @binding.source_location.first end
10+
def lineno() @binding.source_location.last end
11+
else
12+
def path() @binding.eval("__FILE__") end
13+
def lineno() @binding.eval("__LINE__") end
14+
end
15+
end

‎test/web_console/exception_mapper_test.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class ExcetionMapperTest < ActiveSupport::TestCase
99

1010
mapper = ExceptionMapper.new(External.exception)
1111

12-
assert_equal __FILE__, mapper.first.eval("__FILE__")
12+
assert_equal __FILE__, SourceLocation.new(mapper.first).path
1313
end
1414

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

22-
assert_equal file, mapper[last_index].eval("__FILE__")
23-
assert_equal line.to_i, mapper[last_index].eval("__LINE__")
22+
assert_equal file, SourceLocation.new(mapper[last_index]).path
23+
assert_equal line.to_i, SourceLocation.new(mapper[last_index]).lineno
2424
end
2525

2626
test ".[] fall backs to index if no trace can be found" do

‎test/web_console/integration_test.rb

+6-6
Original file line numberDiff line numberDiff line change
@@ -7,37 +7,37 @@ class IntegrationTest < ActiveSupport::TestCase
77
test "Exception#bindings returns all the bindings of where the error originated" do
88
exc = FlatScenario.new.call
99

10-
assert_equal 6, exc.bindings.first.eval("__LINE__")
10+
assert_equal 6, SourceLocation.new(exc.bindings.first).lineno
1111
end
1212

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

16-
assert_equal 8, exc.bindings.first.eval("__LINE__")
16+
assert_equal 8, SourceLocation.new(exc.bindings.first).lineno
1717
end
1818

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

22-
assert_equal 13, exc.bindings.first.eval("__LINE__")
22+
assert_equal 13, SourceLocation.new(exc.bindings.first).lineno
2323
end
2424

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

28-
assert_equal 14, exc.bindings.first.eval("__LINE__")
28+
assert_equal 14, SourceLocation.new(exc.bindings.first).lineno
2929
end
3030

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

34-
assert_equal 14, exc.bindings.first.eval("__LINE__")
34+
assert_equal 14, SourceLocation.new(exc.bindings.first).lineno
3535
end
3636

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

40-
assert_equal 6, exc.bindings.first.eval("__LINE__")
40+
assert_equal 6, SourceLocation.new(exc.bindings.first).lineno
4141
end
4242

4343
test "Exception#bindings is empty when exception is still not raised" do

‎test/web_console/middleware_test.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ def headers
162162
Session.stubs(:from).returns(session)
163163

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

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

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

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

190190
test "can return context information by passing a context param" do

‎test/web_console/session_test.rb

+16-15
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@
44

55
module WebConsole
66
class SessionTest < ActiveSupport::TestCase
7-
class LineAwareError < StandardError
8-
def self.raise
9-
::Kernel.raise new(__LINE__)
7+
class ValueAwareError < StandardError
8+
def self.raise(value)
9+
::Kernel.raise self, value
1010
rescue => exc
1111
exc
1212
end
1313

14-
attr_reader :line
14+
attr_reader :value
1515

16-
def initialize(line)
17-
@line = line
16+
def initialize(value)
17+
@value = value
1818
end
1919
end
2020

@@ -52,38 +52,39 @@ def eval(string)
5252
end
5353

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

5858
session = Session.from(__web_console_binding: saved_binding)
5959

60-
assert_equal "=> #{saved_line}\n", session.eval("__LINE__")
60+
assert_equal "=> #{value}\n", session.eval("value")
6161
end
6262

6363
test "#from can create session from an exception" do
64-
exc = LineAwareError.raise
64+
value = __LINE__
65+
exc = ValueAwareError.raise(value)
6566

6667
session = Session.from(__web_console_exception: exc)
6768

68-
assert_equal "=> #{exc.line}\n", session.eval("__LINE__")
69+
assert_equal "=> #{exc.value}\n", session.eval("value")
6970
end
7071

7172
test "#from can switch to bindings" do
72-
exc, saved_line = LineAwareError.raise, __LINE__
73+
value = __LINE__
74+
exc = ValueAwareError.raise(value)
7375

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

77-
assert_equal "=> #{saved_line}\n", session.eval("__LINE__")
79+
assert_equal "=> #{value}\n", session.eval("value")
7880
end
7981

8082
test "#from prioritizes exceptions over bindings" do
81-
exc, saved_line = LineAwareError.raise, __LINE__
83+
exc = ValueAwareError.raise(42)
8284

8385
session = Session.from(__web_console_exception: exc, __web_console_binding: binding)
84-
session.switch_binding_to(1)
8586

86-
assert_equal "=> #{saved_line}\n", session.eval("__LINE__")
87+
assert_equal "=> WebConsole::SessionTest::ValueAwareError\n", session.eval("self")
8788
end
8889
end
8990
end

0 commit comments

Comments
 (0)
Please sign in to comment.