2010-01-26 07:57:11 -05:00
|
|
|
require "isolation/abstract_unit"
|
|
|
|
|
|
|
|
module ApplicationTests
|
2012-01-05 20:30:17 -05:00
|
|
|
class InitializersTest < ActiveSupport::TestCase
|
2010-01-26 07:57:11 -05:00
|
|
|
include ActiveSupport::Testing::Isolation
|
|
|
|
|
|
|
|
def setup
|
|
|
|
build_app
|
|
|
|
boot_rails
|
|
|
|
FileUtils.rm_rf "#{app_path}/config/environments"
|
|
|
|
end
|
|
|
|
|
2011-06-06 08:54:05 -04:00
|
|
|
def teardown
|
|
|
|
teardown_app
|
|
|
|
end
|
|
|
|
|
2010-01-26 07:57:11 -05:00
|
|
|
test "load initializers" do
|
|
|
|
app_file "config/initializers/foo.rb", "$foo = true"
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
assert $foo
|
|
|
|
end
|
|
|
|
|
2010-05-15 18:34:54 -04:00
|
|
|
test "hooks block works correctly without cache classes (before_eager_load is not called)" do
|
2010-01-26 07:57:11 -05:00
|
|
|
add_to_config <<-RUBY
|
2010-05-15 18:34:54 -04:00
|
|
|
$initialization_callbacks = []
|
2010-01-26 07:57:11 -05:00
|
|
|
config.root = "#{app_path}"
|
2010-05-15 18:34:54 -04:00
|
|
|
config.cache_classes = false
|
|
|
|
config.before_configuration { $initialization_callbacks << 1 }
|
|
|
|
config.before_initialize { $initialization_callbacks << 2 }
|
|
|
|
config.before_eager_load { Boom }
|
|
|
|
config.after_initialize { $initialization_callbacks << 3 }
|
2010-01-26 07:57:11 -05:00
|
|
|
RUBY
|
2010-05-15 18:34:54 -04:00
|
|
|
|
2010-01-26 07:57:11 -05:00
|
|
|
require "#{app_path}/config/environment"
|
2010-05-15 18:34:54 -04:00
|
|
|
assert_equal [1,2,3], $initialization_callbacks
|
|
|
|
end
|
2010-01-26 07:57:11 -05:00
|
|
|
|
2010-05-15 18:34:54 -04:00
|
|
|
test "hooks block works correctly with cache classes" do
|
|
|
|
add_to_config <<-RUBY
|
|
|
|
$initialization_callbacks = []
|
|
|
|
config.root = "#{app_path}"
|
|
|
|
config.cache_classes = true
|
|
|
|
config.before_configuration { $initialization_callbacks << 1 }
|
|
|
|
config.before_initialize { $initialization_callbacks << 2 }
|
|
|
|
config.before_eager_load { $initialization_callbacks << 3 }
|
|
|
|
config.after_initialize { $initialization_callbacks << 4 }
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
assert_equal [1,2,3,4], $initialization_callbacks
|
2010-01-26 07:57:11 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
test "after_initialize runs after frameworks have been initialized" do
|
|
|
|
$activerecord_configurations = nil
|
|
|
|
add_to_config <<-RUBY
|
|
|
|
config.after_initialize { $activerecord_configurations = ActiveRecord::Base.configurations }
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
assert $activerecord_configurations
|
|
|
|
assert $activerecord_configurations['development']
|
|
|
|
end
|
2010-02-18 12:39:39 -05:00
|
|
|
|
2010-02-19 02:02:28 -05:00
|
|
|
test "after_initialize happens after to_prepare in development" do
|
2010-02-18 12:39:39 -05:00
|
|
|
$order = []
|
|
|
|
add_to_config <<-RUBY
|
2010-02-19 02:02:28 -05:00
|
|
|
config.cache_classes = false
|
|
|
|
config.after_initialize { $order << :after_initialize }
|
|
|
|
config.to_prepare { $order << :to_prepare }
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
require "#{app_path}/config/environment"
|
2010-05-15 22:32:44 -04:00
|
|
|
assert_equal [:to_prepare, :after_initialize], $order
|
2010-02-19 02:02:28 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
test "after_initialize happens after to_prepare in production" do
|
|
|
|
$order = []
|
|
|
|
add_to_config <<-RUBY
|
|
|
|
config.cache_classes = true
|
2010-02-18 12:39:39 -05:00
|
|
|
config.after_initialize { $order << :after_initialize }
|
|
|
|
config.to_prepare { $order << :to_prepare }
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
require "#{app_path}/config/application"
|
|
|
|
Rails.env.replace "production"
|
|
|
|
require "#{app_path}/config/environment"
|
2010-05-15 22:32:44 -04:00
|
|
|
assert_equal [:to_prepare, :after_initialize], $order
|
2010-02-18 12:39:39 -05:00
|
|
|
end
|
2010-01-26 07:57:11 -05:00
|
|
|
end
|
|
|
|
end
|