1 require "#{File.dirname(__FILE__)}/abstract_unit"
4 class ConfigurationMock < Rails::Configuration
5 attr_reader :environment_path
7 def initialize(envpath)
9 @environment_path = envpath
13 class Initializer_load_environment_Test < Test::Unit::TestCase
15 def test_load_environment_with_constant
16 config = ConfigurationMock.new("#{File.dirname(__FILE__)}/fixtures/environment_with_constant.rb")
17 assert_nil $initialize_test_set_from_env
18 Rails::Initializer.run(:load_environment, config)
19 assert_equal "success", $initialize_test_set_from_env
21 $initialize_test_set_from_env = nil
26 class Initializer_after_initialize_with_blocks_environment_Test < Test::Unit::TestCase
28 config = ConfigurationMock.new("")
29 config.after_initialize do
30 $test_after_initialize_block1 = "success"
32 config.after_initialize do
33 $test_after_initialize_block2 = "congratulations"
35 assert_nil $test_after_initialize_block1
36 assert_nil $test_after_initialize_block2
38 Rails::Initializer.run(:after_initialize, config)
42 $test_after_initialize_block1 = nil
43 $test_after_initialize_block2 = nil
46 def test_should_have_called_the_first_after_initialize_block
47 assert_equal "success", $test_after_initialize_block1
50 def test_should_have_called_the_second_after_initialize_block
51 assert_equal "congratulations", $test_after_initialize_block2
55 class Initializer_after_initialize_with_no_block_environment_Test < Test::Unit::TestCase
58 config = ConfigurationMock.new("")
59 config.after_initialize do
60 $test_after_initialize_block1 = "success"
62 config.after_initialize # don't pass a block, this is what we're testing!
63 config.after_initialize do
64 $test_after_initialize_block2 = "congratulations"
66 assert_nil $test_after_initialize_block1
68 Rails::Initializer.run(:after_initialize, config)
72 $test_after_initialize_block1 = nil
73 $test_after_initialize_block2 = nil
76 def test_should_have_called_the_first_after_initialize_block
77 assert_equal "success", $test_after_initialize_block1, "should still get set"
80 def test_should_have_called_the_second_after_initialize_block
81 assert_equal "congratulations", $test_after_initialize_block2
86 uses_mocha 'framework paths' do
87 class ConfigurationFrameworkPathsTests < Test::Unit::TestCase
89 @config = Rails::Configuration.new
90 @config.frameworks.clear
92 File.stubs(:directory?).returns(true)
93 @config.stubs(:framework_root_path).returns('')
102 assert_equal expected, @config.framework_paths
105 def test_actioncontroller_or_actionview_add_actionpack
106 @config.frameworks << :action_controller
107 assert_framework_path '/actionpack/lib'
109 @config.frameworks = [:action_view]
110 assert_framework_path '/actionpack/lib'
113 def test_paths_for_ar_ares_and_mailer
114 [:active_record, :action_mailer, :active_resource, :action_web_service].each do |framework|
115 @config.frameworks = [framework]
116 assert_framework_path "/#{framework.to_s.gsub('_', '')}/lib"
120 def test_unknown_framework_raises_error
121 @config.frameworks << :action_foo
122 initializer = Rails::Initializer.new @config
123 initializer.expects(:require).raises(LoadError)
125 assert_raise RuntimeError do
126 initializer.send :require_frameworks
132 def assert_framework_path(path)
133 assert @config.framework_paths.include?(path),
134 "<#{path.inspect}> not found among <#{@config.framework_paths.inspect}>"
139 uses_mocha "Initializer plugin loading tests" do
140 require File.dirname(__FILE__) + '/plugin_test_helper'
142 class InitializerPluginLoadingTests < Test::Unit::TestCase
144 @configuration = Rails::Configuration.new
145 @configuration.plugin_paths << plugin_fixture_root_path
146 @initializer = Rails::Initializer.new(@configuration)
147 @valid_plugin_path = plugin_fixture_path('default/stubby')
148 @empty_plugin_path = plugin_fixture_path('default/empty')
151 def test_no_plugins_are_loaded_if_the_configuration_has_an_empty_plugin_list
152 only_load_the_following_plugins! []
153 @initializer.load_plugins
154 assert_equal [], @initializer.loaded_plugins
157 def test_only_the_specified_plugins_are_located_in_the_order_listed
158 plugin_names = [:plugin_with_no_lib_dir, :acts_as_chunky_bacon]
159 only_load_the_following_plugins! plugin_names
161 assert_plugins plugin_names, @initializer.loaded_plugins
164 def test_all_plugins_are_loaded_when_registered_plugin_list_is_untouched
165 failure_tip = "It's likely someone has added a new plugin fixture without updating this list"
167 assert_plugins [:a, :acts_as_chunky_bacon, :plugin_with_no_lib_dir, :stubby], @initializer.loaded_plugins, failure_tip
170 def test_all_plugins_loaded_when_all_is_used
171 plugin_names = [:stubby, :acts_as_chunky_bacon, :all]
172 only_load_the_following_plugins! plugin_names
174 failure_tip = "It's likely someone has added a new plugin fixture without updating this list"
175 assert_plugins [:stubby, :acts_as_chunky_bacon, :a, :plugin_with_no_lib_dir], @initializer.loaded_plugins, failure_tip
178 def test_all_plugins_loaded_after_all
179 plugin_names = [:stubby, :all, :acts_as_chunky_bacon]
180 only_load_the_following_plugins! plugin_names
182 failure_tip = "It's likely someone has added a new plugin fixture without updating this list"
183 assert_plugins [:stubby, :a, :plugin_with_no_lib_dir, :acts_as_chunky_bacon], @initializer.loaded_plugins, failure_tip
186 def test_plugin_names_may_be_strings
187 plugin_names = ['stubby', 'acts_as_chunky_bacon', :a, :plugin_with_no_lib_dir]
188 only_load_the_following_plugins! plugin_names
190 failure_tip = "It's likely someone has added a new plugin fixture without updating this list"
191 assert_plugins plugin_names, @initializer.loaded_plugins, failure_tip
194 def test_registering_a_plugin_name_that_does_not_exist_raises_a_load_error
195 only_load_the_following_plugins! [:stubby, :acts_as_a_non_existant_plugin]
196 assert_raises(LoadError) do
201 def test_should_ensure_all_loaded_plugins_load_paths_are_added_to_the_load_path
202 only_load_the_following_plugins! [:stubby, :acts_as_chunky_bacon]
204 @initializer.add_plugin_load_paths
206 assert $LOAD_PATH.include?(File.join(plugin_fixture_path('default/stubby'), 'lib'))
207 assert $LOAD_PATH.include?(File.join(plugin_fixture_path('default/acts/acts_as_chunky_bacon'), 'lib'))
213 @initializer.add_plugin_load_paths
214 @initializer.load_plugins