@@ -98,24 +98,35 @@ class RESTfulController < ApplicationController
98
98
assert_nil deps . safe_constantize ( "Admin" )
99
99
end
100
100
101
- test "autoloaded_constants returns autoloaded constant paths " do
102
- app_file "app/models/admin/ user.rb" , "class Admin:: User; end"
101
+ test "to_unload? says if a constant would be unloaded (main) " do
102
+ app_file "app/models/user.rb" , "class User; end"
103
103
app_file "app/models/post.rb" , "class Post; end"
104
104
boot
105
105
106
- assert Admin ::User
107
- assert_equal [ "Admin" , "Admin::User" ] , deps . autoloaded_constants
106
+ assert Post
107
+ assert deps . to_unload? ( "Post" )
108
+ assert_not deps . to_unload? ( "User" )
109
+ end
110
+
111
+ test "to_unload? says if a constant would be unloaded (once)" do
112
+ add_to_config 'config.autoload_once_paths << "#{Rails.root}/extras"'
113
+ app_file "extras/foo.rb" , "class Foo; end"
114
+ app_file "extras/bar.rb" , "class Bar; end"
115
+ boot
116
+
117
+ assert Foo
118
+ assert_not deps . to_unload? ( "Foo" )
119
+ assert_not deps . to_unload? ( "Bar" )
108
120
end
109
121
110
- test "autoloaded ? says if a constant has been autoloaded " do
122
+ test "to_unload ? says if a constant would be unloaded (reloading disabled) " do
111
123
app_file "app/models/user.rb" , "class User; end"
112
124
app_file "app/models/post.rb" , "class Post; end"
113
- boot
125
+ boot ( "production" )
114
126
115
127
assert Post
116
- assert deps . autoloaded? ( "Post" )
117
- assert deps . autoloaded? ( Post )
118
- assert_not deps . autoloaded? ( "User" )
128
+ assert_not deps . to_unload? ( "Post" )
129
+ assert_not deps . to_unload? ( "User" )
119
130
end
120
131
121
132
test "eager loading loads the application code" do
@@ -315,4 +326,45 @@ def once_autoloader.reload
315
326
assert_nil autoloader . logger
316
327
end
317
328
end
329
+
330
+ # This is here because to guarantee classic mode works as always, Zeitwerk
331
+ # integration does not touch anything in classic. The descendants tracker is a
332
+ # very small one-liner exception. We leave its main test suite untouched, and
333
+ # add some minimal safety net here.
334
+ #
335
+ # When time passes, things are going to be reorganized (famous last words).
336
+ test "descendants tracker" do
337
+ class ::ZeitwerkDTIntegrationTestRoot
338
+ extend ActiveSupport ::DescendantsTracker
339
+ end
340
+ class ::ZeitwerkDTIntegrationTestChild < ::ZeitwerkDTIntegrationTestRoot ; end
341
+ class ::ZeitwerkDTIntegrationTestGrandchild < ::ZeitwerkDTIntegrationTestChild ; end
342
+
343
+ begin
344
+ app_file "app/models/user.rb" , "class User < ZeitwerkDTIntegrationTestRoot; end"
345
+ app_file "app/models/post.rb" , "class Post < ZeitwerkDTIntegrationTestRoot; end"
346
+ app_file "app/models/tutorial.rb" , "class Tutorial < Post; end"
347
+ boot
348
+
349
+ assert User
350
+ assert Tutorial
351
+
352
+ direct_descendants = [ ZeitwerkDTIntegrationTestChild , User , Post ] . to_set
353
+ assert_equal direct_descendants , ZeitwerkDTIntegrationTestRoot . direct_descendants . to_set
354
+
355
+ descendants = direct_descendants . merge ( [ ZeitwerkDTIntegrationTestGrandchild , Tutorial ] )
356
+ assert_equal descendants , ZeitwerkDTIntegrationTestRoot . descendants . to_set
357
+
358
+ ActiveSupport ::DescendantsTracker . clear
359
+
360
+ direct_descendants = [ ZeitwerkDTIntegrationTestChild ] . to_set
361
+ assert_equal direct_descendants , ZeitwerkDTIntegrationTestRoot . direct_descendants . to_set
362
+
363
+ descendants = direct_descendants . merge ( [ ZeitwerkDTIntegrationTestGrandchild ] )
364
+ assert_equal descendants , ZeitwerkDTIntegrationTestRoot . descendants . to_set
365
+ ensure
366
+ Object . send ( :remove_const , :ZeitwerkDTIntegrationTestRoot )
367
+ Object . send ( :remove_const , :ZeitwerkDTIntegrationTestChild )
368
+ end
369
+ end
318
370
end
0 commit comments