1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
|
# frozen_string_literal: true
require "set"
require "isolation/abstract_unit"
class ZeitwerkIntegrationTest < ActiveSupport::TestCase
include ActiveSupport::Testing::Isolation
def setup
build_app
end
def boot(env = "development")
app(env)
end
def teardown
teardown_app
end
def deps
ActiveSupport::Dependencies
end
test "The integration is minimally looking good" do
boot
assert_predicate Rails.autoloaders, :zeitwerk_enabled?
assert_instance_of Zeitwerk::Loader, Rails.autoloaders.main
assert_instance_of Zeitwerk::Loader, Rails.autoloaders.once
assert_equal [Rails.autoloaders.main, Rails.autoloaders.once], Rails.autoloaders.to_a
end
test "autoloaders inflect with Active Support" do
app_file "config/initializers/inflections.rb", <<-RUBY
ActiveSupport::Inflector.inflections(:en) do |inflect|
inflect.acronym 'RESTful'
end
RUBY
app_file "app/controllers/restful_controller.rb", <<-RUBY
class RESTfulController < ApplicationController
end
RUBY
boot
basename = "restful_controller"
abspath = "#{Rails.root}/app/controllers/#{basename}.rb"
camelized = "RESTfulController"
Rails.autoloaders.each do |autoloader|
assert_equal camelized, autoloader.inflector.camelize(basename, abspath)
end
assert RESTfulController
end
test "root directories manually set by the user are honored (once)" do
app_file "extras1/x.rb", "ZeitwerkIntegrationTestExtras::X = true"
app_file "extras2/y.rb", "ZeitwerkIntegrationTestExtras::Y = true"
add_to_env_config "development", <<~'RUBY'
config.autoload_once_paths << "#{Rails.root}/extras1"
config.autoload_once_paths << Rails.root.join("extras2")
module ZeitwerkIntegrationTestExtras; end
autoloader = Rails.autoloaders.once
autoloader.push_dir("#{Rails.root}/extras1", namespace: ZeitwerkIntegrationTestExtras)
autoloader.push_dir("#{Rails.root}/extras2", namespace: ZeitwerkIntegrationTestExtras)
RUBY
boot
assert ZeitwerkIntegrationTestExtras::X
assert ZeitwerkIntegrationTestExtras::Y
end
test "root directories manually set by the user are honored (main)" do
app_file "app/services/x.rb", "ZeitwerkIntegrationTestServices::X = true"
app_file "extras/x.rb", "ZeitwerkIntegrationTestExtras::X = true"
app_file "config/initializers/namespaces.rb", <<~'RUBY'
module ZeitwerkIntegrationTestServices; end
module ZeitwerkIntegrationTestExtras; end
ActiveSupport::Dependencies.autoload_paths << Rails.root.join("extras")
Rails.autoloaders.main.tap do |main|
main.push_dir("#{Rails.root}/app/services", namespace: ZeitwerkIntegrationTestServices)
main.push_dir("#{Rails.root}/extras", namespace: ZeitwerkIntegrationTestExtras)
end
RUBY
boot
assert ZeitwerkIntegrationTestServices::X
assert ZeitwerkIntegrationTestExtras::X
end
test "the once autoloader can autoload from initializers" do
app_file "extras0/x.rb", "X = 0"
app_file "extras1/y.rb", "Y = 0"
# We should be able to configure autoload_once_paths in
# config/application.rb and in config/environments/*.rb.
add_to_config 'config.autoload_once_paths << "#{Rails.root}/extras0"'
add_to_env_config "development", 'config.autoload_once_paths << "#{Rails.root}/extras1"'
# Collections should br frozen after bootstrap, and you are ready to
# autoload with the once autoloader. In particular, from initializers.
$config_autoload_once_paths_is_frozen = false
$global_autoload_once_paths_is_frozen = false
add_to_config <<~RUBY
initializer :test_autoload_once_paths_is_frozen, after: :bootstrap_hook do
$config_autoload_once_paths_is_frozen = config.autoload_once_paths.frozen?
$global_autoload_once_paths_is_frozen = ActiveSupport::Dependencies.autoload_once_paths.frozen?
X
end
RUBY
app_file "config/initializers/autoload_Y.rb", "Y.succ"
# Preconditions.
assert_not Object.const_defined?(:X)
assert_not Object.const_defined?(:Y)
boot
assert Object.const_defined?(:X)
assert Object.const_defined?(:Y)
assert $config_autoload_once_paths_is_frozen
assert $global_autoload_once_paths_is_frozen
end
test "the once autoloader can eager load" do
app_file "app/serializers/money_serializer.rb", "MoneySerializer = :dummy_value"
add_to_config 'config.autoload_once_paths << "#{Rails.root}/app/serializers"'
add_to_config 'config.eager_load_paths << "#{Rails.root}/app/serializers"'
assert_not Object.const_defined?(:MoneySerializer)
boot("production")
assert Object.const_defined?(:MoneySerializer)
end
test "eager loading loads the application code" do
$zeitwerk_integration_test_user = false
$zeitwerk_integration_test_post = false
app_file "app/models/user.rb", "class User; end; $zeitwerk_integration_test_user = true"
app_file "app/models/post.rb", "class Post; end; $zeitwerk_integration_test_post = true"
boot("production")
assert $zeitwerk_integration_test_user
assert $zeitwerk_integration_test_post
end
test "eager loading loads the application code if invoked manually too (regression test)" do
$zeitwerk_integration_test_user = false
$zeitwerk_integration_test_post = false
app_file "app/models/user.rb", "class User; end; $zeitwerk_integration_test_user = true"
app_file "app/models/post.rb", "class Post; end; $zeitwerk_integration_test_post = true"
boot
# Preconditions.
assert_not $zeitwerk_integration_test_user
assert_not $zeitwerk_integration_test_post
Rails.application.eager_load!
# Postconditions.
assert $zeitwerk_integration_test_user
assert $zeitwerk_integration_test_post
end
test "reloading is enabled if config.enable_reloading is true" do
add_to_env_config "development", "config.enable_reloading = true"
boot
assert_predicate Rails.autoloaders.main, :reloading_enabled?
assert_not Rails.autoloaders.once.reloading_enabled?
end
test "reloading is disabled if config.enable_reloading is false" do
add_to_env_config "development", "config.enable_reloading = false"
boot
assert_not Rails.autoloaders.main.reloading_enabled?
assert_not Rails.autoloaders.once.reloading_enabled?
end
test "eager loading loads code in engines" do
$test_blog_engine_eager_loaded = false
engine("blog") do |bukkit|
bukkit.write("lib/blog.rb", "class BlogEngine < Rails::Engine; end")
bukkit.write("app/models/post.rb", "Post = $test_blog_engine_eager_loaded = true")
end
boot("production")
assert $test_blog_engine_eager_loaded
end
test "eager loading loads anything managed by Zeitwerk" do
$zeitwerk_integration_test_user = false
app_file "app/models/user.rb", "class User; end; $zeitwerk_integration_test_user = true"
$zeitwerk_integration_test_extras = false
app_dir "extras"
app_file "extras/webhook_hacks.rb", "WebhookHacks = 1; $zeitwerk_integration_test_extras = true"
require "zeitwerk"
autoloader = Zeitwerk::Loader.new
autoloader.push_dir("#{app_path}/extras")
autoloader.setup
boot("production")
assert $zeitwerk_integration_test_user
assert $zeitwerk_integration_test_extras
end
test "autoload directories not present in eager load paths are not eager loaded" do
$zeitwerk_integration_test_user = false
app_file "app/models/user.rb", "class User; end; $zeitwerk_integration_test_user = true"
$zeitwerk_integration_test_extras = false
app_dir "extras"
app_file "extras/websocket_hacks.rb", "WebsocketHacks = 1; $zeitwerk_integration_test_extras = true"
add_to_config "config.autoload_once_paths << '#{app_path}/extras'"
boot("production")
assert $zeitwerk_integration_test_user
assert_not $zeitwerk_integration_test_extras
assert WebsocketHacks
assert $zeitwerk_integration_test_extras
end
test "autoload_paths not in autoload_once_paths are set as root dirs of main, and in the same order" do
boot
existing_autoload_paths = \
deps.autoload_paths.select { |dir| File.directory?(dir) } -
deps.autoload_once_paths
assert_equal existing_autoload_paths, Rails.autoloaders.main.dirs
end
test "autoload_once_paths go to the once autoloader, and in the same order" do
extras = %w(e1 e2 e3)
extras.each do |extra|
app_dir extra
add_to_config %(config.autoload_once_paths << "\#{Rails.root}/#{extra}")
end
boot
extras = extras.map { |extra| "#{app_path}/#{extra}" }
extras.each do |extra|
assert_not_includes Rails.autoloaders.main.dirs, extra
end
e1_index = Rails.autoloaders.once.dirs.index(extras.first)
assert e1_index
assert_equal extras, Rails.autoloaders.once.dirs.slice(e1_index, extras.length)
end
test "clear reloads the main autoloader, and does not reload the once one" do
boot
$zeitwerk_integration_reload_test = []
main_autoloader = Rails.autoloaders.main
def main_autoloader.reload
$zeitwerk_integration_reload_test << :main_autoloader
super
end
once_autoloader = Rails.autoloaders.once
def once_autoloader.reload
$zeitwerk_integration_reload_test << :once_autoloader
super
end
ActiveSupport::Dependencies.clear
assert_equal %i(main_autoloader), $zeitwerk_integration_reload_test
end
test "reloading eager loads again, if enabled" do
add_to_env_config "development", "config.eager_load = true"
$zeitwerk_integration_test_eager_load_count = 0
app_file "app/models/user.rb", "class User; end; $zeitwerk_integration_test_eager_load_count += 1"
boot
assert_equal 1, $zeitwerk_integration_test_eager_load_count
Rails.application.reloader.reload!
assert_equal 2, $zeitwerk_integration_test_eager_load_count
end
test "reloading clears autoloaded tracked classes" do
eval <<~RUBY
class Parent
extend ActiveSupport::DescendantsTracker
end
RUBY
app_file "app/models/child.rb", <<~RUBY
class Child < #{self.class.name}::Parent
end
RUBY
app_file "app/models/grandchild.rb", <<~RUBY
class Grandchild < Child
end
RUBY
boot
assert Grandchild
# Preconditions, we add some redundancy about descendants tracking.
assert_equal Set[Child, Grandchild], ActiveSupport::Dependencies._autoloaded_tracked_classes
assert_equal [Child, Grandchild], Parent.descendants
Rails.application.reloader.reload!
assert_empty ActiveSupport::Dependencies._autoloaded_tracked_classes
assert_equal [], Parent.descendants
end
test "autoloaders.logger=" do
boot
logger = ->(_msg) { }
Rails.autoloaders.logger = logger
Rails.autoloaders.each do |autoloader|
assert_same logger, autoloader.logger
end
Rails.autoloaders.logger = Rails.logger
Rails.autoloaders.each do |autoloader|
assert_same Rails.logger, autoloader.logger
end
Rails.autoloaders.logger = nil
Rails.autoloaders.each do |autoloader|
assert_nil autoloader.logger
end
end
test "autoloaders.log!" do
app_file "extras/utils.rb", "module Utils; end"
add_to_config %(config.autoload_once_paths << "\#{Rails.root}/extras")
add_to_config "Rails.autoloaders.log!"
out, _err = capture_io { boot }
assert_match %r/^Zeitwerk@rails.main: autoload set for ApplicationRecord/, out
assert_match %r/^Zeitwerk@rails.once: autoload set for Utils/, out
end
end
|