Skip to content

Commit 4f4e7a7

Browse files
authored
Merge pull request rails#53055 from kddnewton/switch-to-using-rubyvm-iseq
Use RubyVM::InstructionSequence for callable source
2 parents ca962ab + 7209bf6 commit 4f4e7a7

File tree

2 files changed

+20
-13
lines changed

2 files changed

+20
-13
lines changed

activesupport/lib/active_support/testing/assertions.rb

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -299,17 +299,24 @@ def _assert_nothing_raised_or_warn(assertion, &block)
299299
end
300300

301301
def _callable_to_source_string(callable)
302-
if defined?(RubyVM::AbstractSyntaxTree) && callable.is_a?(Proc)
303-
ast = begin
304-
RubyVM::AbstractSyntaxTree.of(callable, keep_script_lines: true)
305-
rescue SystemCallError
306-
# Failed to get the source somehow
307-
return callable
308-
end
309-
return callable unless ast
302+
if defined?(RubyVM::InstructionSequence) && callable.is_a?(Proc)
303+
iseq = RubyVM::InstructionSequence.of(callable)
304+
source =
305+
if iseq.script_lines
306+
iseq.script_lines.join("\n")
307+
elsif File.readable?(iseq.absolute_path)
308+
File.read(iseq.absolute_path)
309+
end
310+
311+
return callable unless source
312+
313+
location = iseq.to_a[4][:code_location]
314+
return callable unless location
310315

311-
source = ast.source
312-
source.strip!
316+
lines = source.lines[(location[0] - 1)..(location[2] - 1)]
317+
lines[-1] = lines[-1].byteslice(...location[3])
318+
lines[0] = lines[0].byteslice(location[1]...)
319+
source = lines.join.strip
313320

314321
# We ignore procs defined with do/end as they are likely multi-line anyway.
315322
if source.start_with?("{")

activesupport/test/test_case_test.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ def test_assert_difference_message_includes_change
171171
end
172172

173173
def test_assert_difference_message_with_lambda
174-
skip if !defined?(RubyVM::AbstractSyntaxTree)
174+
skip if !defined?(RubyVM::InstructionSequence)
175175

176176
error = assert_raises Minitest::Assertion do
177177
assert_difference(-> { @object.num }, 1, "Object Changed") do
@@ -247,7 +247,7 @@ def test_assert_changes_with_to_option_but_no_change_has_special_message
247247
end
248248

249249
def test_assert_changes_message_with_lambda
250-
skip if !defined?(RubyVM::AbstractSyntaxTree)
250+
skip if !defined?(RubyVM::InstructionSequence)
251251

252252
error = assert_raises Minitest::Assertion do
253253
assert_changes -> { @object.num }, to: 0 do
@@ -375,7 +375,7 @@ def test_assert_no_changes_with_message
375375
end
376376

377377
def test_assert_no_changes_message_with_lambda
378-
skip if !defined?(RubyVM::AbstractSyntaxTree)
378+
skip if !defined?(RubyVM::InstructionSequence)
379379

380380
error = assert_raises Minitest::Assertion do
381381
assert_no_changes -> { @object.num } do

0 commit comments

Comments
 (0)