diff --git a/test/debug/break_test.rb b/test/debug/break_test.rb index a161730ef..0ffb98611 100644 --- a/test/debug/break_test.rb +++ b/test/debug/break_test.rb @@ -205,15 +205,15 @@ def test_debugger_doesnt_stop_when_other_instance_calls_the_inherited_method end class PathOptionTest < TestCase - def additional_file + include ExtraFileHelper + + def extra_file <<~RUBY Foo.new.bar RUBY end - ADDITIONAL_FILE_BASENAME = __FILE__.hash.abs.to_s(16) - - def program(additional_file_path) + def program(extra_file_path) <<~RUBY 1| class Foo 2| def bar; end @@ -221,26 +221,16 @@ def program(additional_file_path) 4| 5| Foo.new.bar 6| - 7| load "#{additional_file_path}" + 7| load "#{extra_file_path}" RUBY end - def with_tempfile - t = Tempfile.create([ADDITIONAL_FILE_BASENAME, '.rb']).tap do |f| - f.write(additional_file) - f.close - end - yield t - ensure - File.unlink t if t - end - def test_break_only_stops_when_path_matches - with_tempfile do |additional_file| - debug_code(program(additional_file.path)) do - type "break Foo#bar path: #{additional_file.path}" + with_extra_tempfile do |extra_file| + debug_code(program(extra_file.path)) do + type "break Foo#bar path: #{extra_file.path}" type 'c' - assert_line_text(/#{ADDITIONAL_FILE_BASENAME}/) + assert_line_text(/#{extra_file.path}/) type 'c' assert_finish end @@ -322,37 +312,27 @@ def test_break_C_method_with_singleton_method end class PathOptionTest < TestCase - def additional_file + include ExtraFileHelper + + def extra_file <<~RUBY 1.abs RUBY end - ADDITIONAL_FILE_BASENAME = __FILE__.hash.abs.to_s(16) - - def program(additional_file_path) + def program(extra_file_path) <<~RUBY - 1| load "#{additional_file_path}" + 1| load "#{extra_file_path}" 2| 1.abs RUBY end - def with_tempfile - t = Tempfile.create([ADDITIONAL_FILE_BASENAME, '.rb']).tap do |f| - f.write(additional_file) - f.close - end - yield t - ensure - File.unlink t if t - end - def test_break_only_stops_when_path_matches - with_tempfile do |additional_file| - debug_code(program(additional_file.path)) do - type "break Integer#abs path: #{additional_file.path}" + with_extra_tempfile do |extra_file| + debug_code(program(extra_file.path)) do + type "break Integer#abs path: #{extra_file.path}" type 'c' - assert_line_text(/#{ADDITIONAL_FILE_BASENAME}/) + assert_line_text(/#{extra_file.path}/) type 'c' assert_finish end @@ -643,40 +623,32 @@ def test_conditional_breakpoint_shows_error end class PathOptionTest < TestCase - def additional_file + include ExtraFileHelper + + def extra_file <<~RUBY - a = 1 - b = 200 + a = 100 + b = 1 + _ = 0 RUBY end - ADDITIONAL_FILE_BASENAME = __FILE__.hash.abs.to_s(16) - - def program(additional_file_path) + def program(extra_file_path) <<~RUBY - 1| a = 1 - 2| b = 100 - 3| load "#{additional_file_path}" + 1| a = 200 + 2| b = 1 + 3| _ = 0 + 4| load "#{extra_file_path}" RUBY end - def with_tempfile - t = Tempfile.create([ADDITIONAL_FILE_BASENAME, '.rb']).tap do |f| - f.write(additional_file) - f.close - end - yield t - ensure - File.unlink t if t - end - def test_conditional_breakpoint_only_stops_when_path_matches - with_tempfile do |additional_file| - debug_code(program(additional_file.path)) do - type "break if: a == 1 path: #{additional_file.path}" + with_extra_tempfile do |extra_file| + debug_code(program(extra_file.path)) do + type "break if: b == 1 path: #{extra_file.path}" type 'c' - assert_line_text(/#{additional_file.path}/) - assert_line_text(/b = 200/) + type 'a + b' + assert_line_text(/101/) type 'c' assert_finish end diff --git a/test/debug/catch_test.rb b/test/debug/catch_test.rb index 69c13b693..8ef28445b 100644 --- a/test/debug/catch_test.rb +++ b/test/debug/catch_test.rb @@ -150,7 +150,9 @@ def test_catch_with_namespace_stops_at_exception end class PathOptionTest < TestCase - def additional_file + include ExtraFileHelper + + def extra_file <<~RUBY def bar raise "bar" @@ -159,11 +161,9 @@ def bar RUBY end - ADDITIONAL_FILE_BASENAME = __FILE__.hash.abs.to_s(16) - - def program(additional_file_path) + def program(extra_file_path) <<~RUBY - 1| load "#{additional_file_path}" + 1| load "#{extra_file_path}" 2| 3| def foo 4| raise "foo" @@ -175,20 +175,10 @@ def program(additional_file_path) RUBY end - def with_tempfile - t = Tempfile.create([ADDITIONAL_FILE_BASENAME, '.rb']).tap do |f| - f.write(additional_file) - f.close - end - yield t - ensure - File.unlink t if t - end - def test_catch_only_stops_when_path_matches - with_tempfile do |additional_file| - debug_code(program(additional_file.path)) do - type "catch RuntimeError path: #{additional_file.path}" + with_extra_tempfile do |extra_file| + debug_code(program(extra_file.path)) do + type "catch RuntimeError path: #{extra_file.path}" type 'c' assert_line_text(/bar/) type 'c' diff --git a/test/support/extra_file_helper.rb b/test/support/extra_file_helper.rb new file mode 100644 index 000000000..bd599e351 --- /dev/null +++ b/test/support/extra_file_helper.rb @@ -0,0 +1,15 @@ +require 'securerandom' + +module DEBUGGER__ + module ExtraFileHelper + def with_extra_tempfile + t = Tempfile.create([SecureRandom.hex(5), '.rb']).tap do |f| + f.write(extra_file) + f.close + end + yield t + ensure + File.unlink t if t + end + end +end diff --git a/test/support/test_case.rb b/test/support/test_case.rb index a8740eb95..b0c7b8b70 100644 --- a/test/support/test_case.rb +++ b/test/support/test_case.rb @@ -5,6 +5,7 @@ require_relative 'utils' require_relative 'assertions' +require_relative 'extra_file_helper' module DEBUGGER__ class TestCase < Test::Unit::TestCase