diff options
author | Xavier Noria <fxn@hashref.com> | 2019-04-09 11:06:44 +0200 |
---|---|---|
committer | Xavier Noria <fxn@hashref.com> | 2019-04-09 11:06:44 +0200 |
commit | 9b5401fcc9624be9bd60331d59169267ae2f7bac (patch) | |
tree | 1e28b8596386b03ecfc526ec9e5eb81d3678fc2e /railties/test | |
parent | 496e8ee9370e9f165fd1f5d2620296299a6a8649 (diff) | |
download | rails-9b5401fcc9624be9bd60331d59169267ae2f7bac.tar.gz rails-9b5401fcc9624be9bd60331d59169267ae2f7bac.tar.bz2 rails-9b5401fcc9624be9bd60331d59169267ae2f7bac.zip |
depend on Zeitwerk 2.1.0
Diffstat (limited to 'railties/test')
-rw-r--r-- | railties/test/application/zeitwerk_integration_test.rb | 70 |
1 files changed, 61 insertions, 9 deletions
diff --git a/railties/test/application/zeitwerk_integration_test.rb b/railties/test/application/zeitwerk_integration_test.rb index a9da060347..b461d82450 100644 --- a/railties/test/application/zeitwerk_integration_test.rb +++ b/railties/test/application/zeitwerk_integration_test.rb @@ -98,24 +98,35 @@ class ZeitwerkIntegrationTest < ActiveSupport::TestCase assert_nil deps.safe_constantize("Admin") end - test "autoloaded_constants returns autoloaded constant paths" do - app_file "app/models/admin/user.rb", "class Admin::User; end" + test "to_unload? says if a constant would be unloaded (main)" do + app_file "app/models/user.rb", "class User; end" app_file "app/models/post.rb", "class Post; end" boot - assert Admin::User - assert_equal ["Admin", "Admin::User"], deps.autoloaded_constants + assert Post + assert deps.to_unload?("Post") + assert_not deps.to_unload?("User") + end + + test "to_unload? says if a constant would be unloaded (once)" do + add_to_config 'config.autoload_once_paths << "#{Rails.root}/extras"' + app_file "extras/foo.rb", "class Foo; end" + app_file "extras/bar.rb", "class Bar; end" + boot + + assert Foo + assert_not deps.to_unload?("Foo") + assert_not deps.to_unload?("Bar") end - test "autoloaded? says if a constant has been autoloaded" do + test "to_unload? says if a constant would be unloaded (reloading disabled)" do app_file "app/models/user.rb", "class User; end" app_file "app/models/post.rb", "class Post; end" - boot + boot("production") assert Post - assert deps.autoloaded?("Post") - assert deps.autoloaded?(Post) - assert_not deps.autoloaded?("User") + assert_not deps.to_unload?("Post") + assert_not deps.to_unload?("User") end test "eager loading loads the application code" do @@ -315,4 +326,45 @@ class ZeitwerkIntegrationTest < ActiveSupport::TestCase assert_nil autoloader.logger end end + + # This is here because to guarantee classic mode works as always, Zeitwerk + # integration does not touch anything in classic. The descendants tracker is a + # very small one-liner exception. We leave its main test suite untouched, and + # add some minimal safety net here. + # + # When time passes, things are going to be reorganized (famous last words). + test "descendants tracker" do + class ::ZeitwerkDTIntegrationTestRoot + extend ActiveSupport::DescendantsTracker + end + class ::ZeitwerkDTIntegrationTestChild < ::ZeitwerkDTIntegrationTestRoot; end + class ::ZeitwerkDTIntegrationTestGrandchild < ::ZeitwerkDTIntegrationTestChild; end + + begin + app_file "app/models/user.rb", "class User < ZeitwerkDTIntegrationTestRoot; end" + app_file "app/models/post.rb", "class Post < ZeitwerkDTIntegrationTestRoot; end" + app_file "app/models/tutorial.rb", "class Tutorial < Post; end" + boot + + assert User + assert Tutorial + + direct_descendants = [ZeitwerkDTIntegrationTestChild, User, Post].to_set + assert_equal direct_descendants, ZeitwerkDTIntegrationTestRoot.direct_descendants.to_set + + descendants = direct_descendants.merge([ZeitwerkDTIntegrationTestGrandchild, Tutorial]) + assert_equal descendants, ZeitwerkDTIntegrationTestRoot.descendants.to_set + + ActiveSupport::DescendantsTracker.clear + + direct_descendants = [ZeitwerkDTIntegrationTestChild].to_set + assert_equal direct_descendants, ZeitwerkDTIntegrationTestRoot.direct_descendants.to_set + + descendants = direct_descendants.merge([ZeitwerkDTIntegrationTestGrandchild]) + assert_equal descendants, ZeitwerkDTIntegrationTestRoot.descendants.to_set + ensure + Object.send(:remove_const, :ZeitwerkDTIntegrationTestRoot) + Object.send(:remove_const, :ZeitwerkDTIntegrationTestChild) + end + end end |