mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Started making progress on implementing a new initializer.
Class.any_instance.expects(:require).raises(LoadError) ... w0t
This commit is contained in:
parent
99b8248f6e
commit
a3309e4d70
4 changed files with 1818 additions and 1168 deletions
File diff suppressed because it is too large
Load diff
1137
railties/lib/initializer_old.rb
Normal file
1137
railties/lib/initializer_old.rb
Normal file
File diff suppressed because it is too large
Load diff
|
@ -38,14 +38,15 @@ class Initializer_eager_loading_Test < Test::Unit::TestCase
|
|||
@config.cache_classes = true
|
||||
@config.load_paths = [File.expand_path(File.dirname(__FILE__) + "/fixtures/eager")]
|
||||
@config.eager_load_paths = [File.expand_path(File.dirname(__FILE__) + "/fixtures/eager")]
|
||||
@initializer = Rails::Initializer.new(@config)
|
||||
@initializer.set_load_path
|
||||
@initializer.set_autoload_paths
|
||||
@initializer = Rails::Initializer.default
|
||||
@initializer.config = @config
|
||||
@initializer.run(:set_load_path)
|
||||
@initializer.run(:set_autoload_paths)
|
||||
end
|
||||
|
||||
def test_eager_loading_loads_parent_classes_before_children
|
||||
assert_nothing_raised do
|
||||
@initializer.load_application_classes
|
||||
@initializer.run(:load_application_classes)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -62,7 +63,7 @@ class Initializer_after_initialize_with_blocks_environment_Test < Test::Unit::Te
|
|||
assert_nil $test_after_initialize_block1
|
||||
assert_nil $test_after_initialize_block2
|
||||
|
||||
Rails::Initializer.any_instance.expects(:gems_dependencies_loaded).returns(true)
|
||||
config.expects(:gems_dependencies_loaded).returns(true)
|
||||
Rails::Initializer.run(:after_initialize, config)
|
||||
end
|
||||
|
||||
|
@ -92,7 +93,7 @@ class Initializer_after_initialize_with_no_block_environment_Test < Test::Unit::
|
|||
end
|
||||
assert_nil $test_after_initialize_block1
|
||||
|
||||
Rails::Initializer.any_instance.expects(:gems_dependencies_loaded).returns(true)
|
||||
config.expects(:gems_dependencies_loaded).returns(true)
|
||||
Rails::Initializer.run(:after_initialize, config)
|
||||
end
|
||||
|
||||
|
@ -114,68 +115,65 @@ class ConfigurationFrameworkPathsTests < Test::Unit::TestCase
|
|||
def setup
|
||||
@config = Rails::Configuration.new
|
||||
@config.frameworks.clear
|
||||
@initializer = Rails::Initializer.default
|
||||
@initializer.config = @config
|
||||
|
||||
File.stubs(:directory?).returns(true)
|
||||
@config.stubs(:framework_root_path).returns('')
|
||||
Rails::Initializer.run(:set_root_path, @config)
|
||||
end
|
||||
|
||||
def test_minimal
|
||||
expected = %w(
|
||||
/railties
|
||||
/railties/lib
|
||||
/activesupport/lib
|
||||
)
|
||||
assert_equal expected, @config.framework_paths
|
||||
expected = %w(railties railties/lib activesupport/lib)
|
||||
assert_equal expected.map {|e| "#{@config.framework_root_path}/#{e}"}, @config.framework_paths
|
||||
end
|
||||
|
||||
def test_actioncontroller_or_actionview_add_actionpack
|
||||
@config.frameworks << :action_controller
|
||||
assert_framework_path '/actionpack/lib'
|
||||
assert_framework_path "actionpack/lib"
|
||||
|
||||
@config.frameworks = [:action_view]
|
||||
assert_framework_path '/actionpack/lib'
|
||||
assert_framework_path 'actionpack/lib'
|
||||
end
|
||||
|
||||
def test_paths_for_ar_ares_and_mailer
|
||||
[:active_record, :action_mailer, :active_resource, :action_web_service].each do |framework|
|
||||
@config.frameworks = [framework]
|
||||
assert_framework_path "/#{framework.to_s.gsub('_', '')}/lib"
|
||||
assert_framework_path "#{framework.to_s.gsub('_', '')}/lib"
|
||||
end
|
||||
end
|
||||
|
||||
def test_unknown_framework_raises_error
|
||||
@config.frameworks << :action_foo
|
||||
initializer = Rails::Initializer.new @config
|
||||
initializer.expects(:require).raises(LoadError)
|
||||
|
||||
Class.any_instance.expects(:require).raises(LoadError)
|
||||
|
||||
assert_raise RuntimeError do
|
||||
initializer.send :require_frameworks
|
||||
@initializer.run(:require_frameworks)
|
||||
end
|
||||
end
|
||||
|
||||
def test_action_mailer_load_paths_set_only_if_action_mailer_in_use
|
||||
@config.frameworks = [:action_controller]
|
||||
initializer = Rails::Initializer.new @config
|
||||
initializer.send :require_frameworks
|
||||
@initializer.config = @config
|
||||
@initializer.run :require_frameworks
|
||||
|
||||
assert_nothing_raised NameError do
|
||||
initializer.send :load_view_paths
|
||||
@initializer.run :load_view_paths
|
||||
end
|
||||
end
|
||||
|
||||
def test_action_controller_load_paths_set_only_if_action_controller_in_use
|
||||
@config.frameworks = []
|
||||
initializer = Rails::Initializer.new @config
|
||||
initializer.send :require_frameworks
|
||||
@initializer.run :require_frameworks
|
||||
|
||||
assert_nothing_raised NameError do
|
||||
initializer.send :load_view_paths
|
||||
@initializer.run :load_view_paths
|
||||
end
|
||||
end
|
||||
|
||||
protected
|
||||
def assert_framework_path(path)
|
||||
assert @config.framework_paths.include?(path),
|
||||
assert @config.framework_paths.include?("#{@config.framework_root_path}/#{path}"),
|
||||
"<#{path.inspect}> not found among <#{@config.framework_paths.inspect}>"
|
||||
end
|
||||
end
|
||||
|
@ -187,14 +185,15 @@ class InitializerPluginLoadingTests < Test::Unit::TestCase
|
|||
@configuration = Rails::Configuration.new
|
||||
@configuration.frameworks -= [:action_mailer]
|
||||
@configuration.plugin_paths << plugin_fixture_root_path
|
||||
@initializer = Rails::Initializer.new(@configuration)
|
||||
@initializer = Rails::Initializer.default
|
||||
@initializer.config = @configuration
|
||||
@valid_plugin_path = plugin_fixture_path('default/stubby')
|
||||
@empty_plugin_path = plugin_fixture_path('default/empty')
|
||||
end
|
||||
|
||||
def test_no_plugins_are_loaded_if_the_configuration_has_an_empty_plugin_list
|
||||
only_load_the_following_plugins! []
|
||||
@initializer.load_plugins
|
||||
@initializer.run :load_plugins
|
||||
assert_equal [], @initializer.loaded_plugins
|
||||
end
|
||||
|
||||
|
|
|
@ -1,113 +1,165 @@
|
|||
require 'abstract_unit'
|
||||
require 'active_support/ruby/shim'
|
||||
|
||||
module Rails
|
||||
class Initializer
|
||||
class Error < StandardError ; end
|
||||
class Runner
|
||||
def initialize
|
||||
@names = {}
|
||||
@initializers = []
|
||||
end
|
||||
|
||||
def add(name, options = {}, &block)
|
||||
# If :before or :after is specified, set the index to the right spot
|
||||
if other = options[:before] || options[:after]
|
||||
raise Error, "The #{other.inspect} initializer does not exist" unless @names[other]
|
||||
index = @initializers.index(@names[other])
|
||||
index += 1 if options[:after]
|
||||
end
|
||||
|
||||
Class.new(Initializer, &block).new.tap do |initializer|
|
||||
@initializers.insert(index || -1, initializer)
|
||||
@names[name] = initializer
|
||||
end
|
||||
end
|
||||
|
||||
def run
|
||||
@initializers.each { |init| init.run }
|
||||
end
|
||||
end
|
||||
|
||||
def self.run(&blk)
|
||||
define_method(:run, &blk)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
require 'initializer'
|
||||
|
||||
class InitializerRunnerTest < ActiveSupport::TestCase
|
||||
|
||||
|
||||
def setup
|
||||
@runner = Rails::Initializer::Runner.new
|
||||
end
|
||||
|
||||
|
||||
test "A new runner can be created" do
|
||||
assert @runner
|
||||
end
|
||||
|
||||
test "You can create initializers" do
|
||||
init = @runner.add :foo do
|
||||
|
||||
end
|
||||
|
||||
assert_kind_of Rails::Initializer, init
|
||||
end
|
||||
|
||||
|
||||
test "The initializers actually get run when the runner is run" do
|
||||
state = nil
|
||||
|
||||
|
||||
@runner.add :foo do
|
||||
run { state = true }
|
||||
end
|
||||
|
||||
|
||||
@runner.run
|
||||
assert state
|
||||
end
|
||||
|
||||
|
||||
test "By default, initializers get run in the order that they are added" do
|
||||
state = []
|
||||
|
||||
|
||||
@runner.add :first do
|
||||
run { state << :first }
|
||||
end
|
||||
|
||||
|
||||
@runner.add :second do
|
||||
run { state << :second }
|
||||
end
|
||||
|
||||
|
||||
@runner.run
|
||||
assert_equal [:first, :second], state
|
||||
end
|
||||
|
||||
|
||||
test "Raises an exception if :before or :after are specified, but don't exist" do
|
||||
assert_raise(Rails::Initializer::Error) do
|
||||
@runner.add(:fail, :before => :whale) { 1 }
|
||||
end
|
||||
|
||||
|
||||
assert_raise(Rails::Initializer::Error) do
|
||||
@runner.add(:fail, :after => :whale) { 1 }
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
test "When adding an initializer, specifying :after allows you to move an initializer after another" do
|
||||
state = []
|
||||
|
||||
|
||||
@runner.add :first do
|
||||
run { state << :first }
|
||||
end
|
||||
|
||||
|
||||
@runner.add :second do
|
||||
run { state << :second }
|
||||
end
|
||||
|
||||
|
||||
@runner.add :third, :after => :first do
|
||||
run { state << :third }
|
||||
end
|
||||
|
||||
|
||||
@runner.run
|
||||
assert_equal [:first, :third, :second], state
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
test "An initializer can be deleted" do
|
||||
state = []
|
||||
|
||||
@runner.add :first do
|
||||
run { state << :first }
|
||||
end
|
||||
|
||||
@runner.add :second do
|
||||
run { state << :second }
|
||||
end
|
||||
|
||||
@runner.delete(:second)
|
||||
|
||||
@runner.run
|
||||
assert_equal [:first], state
|
||||
end
|
||||
|
||||
test "A runner can be initialized with an existing runner, which it copies" do
|
||||
state = []
|
||||
|
||||
@runner.add :first do
|
||||
run { state << :first }
|
||||
end
|
||||
|
||||
@runner.add :second do
|
||||
run { state << :second }
|
||||
end
|
||||
|
||||
Rails::Initializer::Runner.new(@runner).run
|
||||
assert_equal [:first, :second], state
|
||||
end
|
||||
|
||||
test "A child runner can be still be modified without modifying the parent" do
|
||||
state = []
|
||||
|
||||
@runner.add :first do
|
||||
run { state << :first }
|
||||
end
|
||||
|
||||
@runner.add :second do
|
||||
run { state << :second }
|
||||
end
|
||||
|
||||
new_runner = Rails::Initializer::Runner.new(@runner)
|
||||
new_runner.add :trois do
|
||||
run { state << :trois }
|
||||
end
|
||||
new_runner.delete(:second)
|
||||
|
||||
new_runner.run
|
||||
assert_equal [:first, :trois], state
|
||||
state.clear
|
||||
@runner.run
|
||||
assert_equal [:first, :second], state
|
||||
end
|
||||
|
||||
test "A child runner that is modified does not modify any other children of the same parent" do
|
||||
state = []
|
||||
|
||||
@runner.add :first do
|
||||
run { state << :first }
|
||||
end
|
||||
|
||||
@runner.add :second do
|
||||
run { state << :second }
|
||||
end
|
||||
|
||||
child_one = Rails::Initializer::Runner.new(@runner)
|
||||
child_two = Rails::Initializer::Runner.new(@runner)
|
||||
|
||||
child_one.delete(:second)
|
||||
child_two.run
|
||||
|
||||
assert_equal [:first, :second], state
|
||||
end
|
||||
|
||||
test "It does not run the initializer block immediately" do
|
||||
state = []
|
||||
@runner.add :first do
|
||||
state << :first
|
||||
end
|
||||
|
||||
assert_equal [], state
|
||||
end
|
||||
|
||||
test "It runs the block when the runner is run" do
|
||||
state = []
|
||||
@runner.add :first do
|
||||
state << :first
|
||||
end
|
||||
|
||||
@runner.run
|
||||
assert_equal [:first], state
|
||||
end
|
||||
|
||||
end
|
Loading…
Reference in a new issue