summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorSamuel Williams <samuel.williams@oriontransfer.co.nz>2026-02-12 18:21:59 -0500
committerGitHub <noreply@github.com>2026-02-13 12:21:59 +1300
commitf0ff0df584b75b2588952e516d16ff8f134f9832 (patch)
treea1e5b18bf1a1466a1d152dcd4f341bc2de227a3a /test
parentc61f52a012f0a390a869db4825143187ea468d21 (diff)
Introduce suport for `autoload_relative`. (#16148)
Diffstat (limited to 'test')
-rw-r--r--test/ruby/test_autoload.rb89
1 files changed, 89 insertions, 0 deletions
diff --git a/test/ruby/test_autoload.rb b/test/ruby/test_autoload.rb
index 82bf2d9d2c..de08be96e4 100644
--- a/test/ruby/test_autoload.rb
+++ b/test/ruby/test_autoload.rb
@@ -614,6 +614,95 @@ p Foo::Bar
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)