diff options
Diffstat (limited to 'test/ruby/test_autoload.rb')
| -rw-r--r-- | test/ruby/test_autoload.rb | 119 |
1 files changed, 116 insertions, 3 deletions
diff --git a/test/ruby/test_autoload.rb b/test/ruby/test_autoload.rb index ca3e3d5f7f..de08be96e4 100644 --- a/test/ruby/test_autoload.rb +++ b/test/ruby/test_autoload.rb @@ -224,11 +224,18 @@ p Foo::Bar Kernel.module_eval do alias old_require require end + Ruby::Box.module_eval do + alias old_require require + end called_with = [] Kernel.send :define_method, :require do |path| called_with << path old_require path end + Ruby::Box.send :define_method, :require do |path| + called_with << path + old_require path + end yield called_with ensure Kernel.module_eval do @@ -236,6 +243,11 @@ p Foo::Bar alias require old_require undef old_require end + Ruby::Box.module_eval do + undef require + alias require old_require + undef old_require + end end def test_require_implemented_in_ruby_is_called @@ -249,7 +261,8 @@ p Foo::Bar ensure remove_autoload_constant end - assert_equal [file.path], called_with + # .dup to prevent breaking called_with by autoloading pp, etc + assert_equal [file.path], called_with.dup } end end @@ -267,7 +280,8 @@ p Foo::Bar ensure remove_autoload_constant end - assert_equal [a.path, b.path], called_with + # .dup to prevent breaking called_with by autoloading pp, etc + assert_equal [a.path, b.path], called_with.dup end end end @@ -560,7 +574,7 @@ p Foo::Bar autoload_path = File.join(tmpdir, "autoload_parallel_race.rb") File.write(autoload_path, 'module Foo; end; module Bar; end') - assert_separately([], <<-RUBY, timeout: 100) + assert_ruby_status([], <<-RUBY, timeout: 100) autoload_path = #{File.realpath(autoload_path).inspect} # This should work with no errors or failures. @@ -599,4 +613,103 @@ p Foo::Bar RUBY end end + + def test_autoload_relative_toplevel + Dir.mktmpdir('autoload_relative') do |tmpdir| + main_file = File.join(tmpdir, 'main.rb') + module_file = File.join(tmpdir, 'test_module.rb') + + File.write(module_file, <<-RUBY) + module AutoloadRelativeTest + VERSION = '1.0' + end + RUBY + + File.write(main_file, <<-RUBY) + autoload_relative :AutoloadRelativeTest, 'test_module.rb' + puts AutoloadRelativeTest::VERSION + RUBY + + assert_in_out_err([main_file], '', ['1.0'], []) + end + end + + def test_autoload_relative_module_level + Dir.mktmpdir('autoload_relative') do |tmpdir| + main_file = File.join(tmpdir, 'main_mod.rb') + module_file = File.join(tmpdir, 'nested_module.rb') + + File.write(module_file, <<-RUBY) + module Container + module NestedModule + MSG = 'loaded' + end + end + RUBY + + File.write(main_file, <<-RUBY) + module Container + autoload_relative :NestedModule, 'nested_module.rb' + end + puts Container::NestedModule::MSG + RUBY + + assert_in_out_err([main_file], '', ['loaded'], []) + end + end + + def test_autoload_relative_query + Dir.mktmpdir('autoload_relative') do |tmpdir| + main_file = File.join(tmpdir, 'query_test.rb') + module_file = File.join(tmpdir, 'query_module.rb') + + File.write(module_file, 'module QueryModule; end') + + File.write(main_file, <<-RUBY) + autoload_relative :QueryModule, 'query_module.rb' + path = autoload?(:QueryModule) + # Use realpath for comparison to handle symlinks (e.g., /var -> /private/var on macOS) + real_tmpdir = File.realpath('#{tmpdir}') + puts path.start_with?(real_tmpdir) && path.end_with?('query_module.rb') + RUBY + + assert_in_out_err([main_file], '', ['true'], []) + end + end + + def test_autoload_relative_nested_directory + Dir.mktmpdir('autoload_relative') do |tmpdir| + nested_dir = File.join(tmpdir, 'nested') + Dir.mkdir(nested_dir) + + main_file = File.join(tmpdir, 'nested_test.rb') + module_file = File.join(nested_dir, 'deep_module.rb') + + File.write(module_file, 'module DeepModule; VALUE = 42; end') + + File.write(main_file, <<-RUBY) + autoload_relative :DeepModule, 'nested/deep_module.rb' + puts DeepModule::VALUE + RUBY + + assert_in_out_err([main_file], '', ['42'], []) + end + end + + def test_autoload_relative_no_basepath + # Test that autoload_relative raises an error when called from eval without file context + assert_raise(LoadError) do + eval('autoload_relative :TestConst, "test.rb"') + end + end + + private + + def assert_separately(*args, **kwargs) + super(*args, timeout: 60, **kwargs) + end + + def assert_ruby_status(*args, **kwargs) + super(*args, timeout: 60, **kwargs) + end end |
