From bfc9065d58508fb19dd1a4170406604dd3b3234a Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Sun, 28 Jul 2019 13:49:26 +0200 Subject: Improves compatibility of require_dependency in zeitwerk mode [Closes #36774] Applications are not supposed to use require_dependency in their own code if running in zeitwerk mode, and require_dependency was initially aliased to require with that use case in mind. However, there are situations in which you cannot control the mode and need to be compatible with both. There, you might need require_dependency in case you are being executed in classic mode. Think about engines that want to support both modes in their parent applications, for example. Furthermore, Rails itself loads helpers using require_dependency. Therefore, we need better compatibility. --- .../test/application/zeitwerk_integration_test.rb | 29 ++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'railties') diff --git a/railties/test/application/zeitwerk_integration_test.rb b/railties/test/application/zeitwerk_integration_test.rb index ff8c06b479..d03fa3d0c6 100644 --- a/railties/test/application/zeitwerk_integration_test.rb +++ b/railties/test/application/zeitwerk_integration_test.rb @@ -150,6 +150,35 @@ class ZeitwerkIntegrationTest < ActiveSupport::TestCase assert_empty deps.autoloaded_constants end + [true, false].each do |add_aps_to_lp| + test "require_dependency looks autoload paths up (#{add_aps_to_lp})" do + add_to_config "config.add_autoload_paths_to_load_path = #{add_aps_to_lp}" + app_file "app/models/user.rb", "class User; end" + boot + + assert require_dependency("user") + end + + test "require_dependency handles absolute paths correctly (#{add_aps_to_lp})" do + add_to_config "config.add_autoload_paths_to_load_path = #{add_aps_to_lp}" + app_file "app/models/user.rb", "class User; end" + boot + + assert require_dependency("#{app_path}/app/models/user.rb") + end + + test "require_dependency supports arguments that repond to to_path (#{add_aps_to_lp})" do + add_to_config "config.add_autoload_paths_to_load_path = #{add_aps_to_lp}" + app_file "app/models/user.rb", "class User; end" + boot + + user = Object.new + def user.to_path; "user"; end + + assert require_dependency(user) + end + end + test "eager loading loads the application code" do $zeitwerk_integration_test_user = false $zeitwerk_integration_test_post = false -- cgit v1.2.3