aboutsummaryrefslogtreecommitdiffstats
path: root/railties/test/initializer_test.rb
blob: 219c65e637fb2cc66e41bada9559747c37b0d942 (plain) (blame)
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
require 'abstract_unit'
require 'rails/initializer'
require 'rails/generators'

require 'action_view'
require 'action_mailer'
require 'active_record'

# Mocks out the configuration
module Rails
  def self.configuration
    Rails::Configuration.new
  end

  module Generators
    def self.clear_aliases!
      @aliases = nil
    end

    def self.clear_options!
      @@options = nil
    end
  end
end


class ConfigurationMock < Rails::Configuration
  attr_reader :environment_path

  def initialize(envpath)
    super()
    @environment_path = envpath
  end
end

class Initializer_after_initialize_with_blocks_environment_Test < Test::Unit::TestCase
  def setup
    config = ConfigurationMock.new("")
    config.after_initialize do
      $test_after_initialize_block1 = "success"
    end
    config.after_initialize do
      $test_after_initialize_block2 = "congratulations"
    end
    assert_nil $test_after_initialize_block1
    assert_nil $test_after_initialize_block2

    config.expects(:gems_dependencies_loaded).returns(true)
    Rails::Initializer.run(:after_initialize, config)
  end

  def teardown
    $test_after_initialize_block1 = nil
    $test_after_initialize_block2 = nil
  end

  def test_should_have_called_the_first_after_initialize_block
    assert_equal "success", $test_after_initialize_block1
  end

  def test_should_have_called_the_second_after_initialize_block
    assert_equal "congratulations", $test_after_initialize_block2
  end
end

class Initializer_after_initialize_with_no_block_environment_Test < Test::Unit::TestCase
  def setup
    config = ConfigurationMock.new("")
    config.after_initialize do
      $test_after_initialize_block1 = "success"
    end
    config.after_initialize # don't pass a block, this is what we're testing!
    config.after_initialize do
      $test_after_initialize_block2 = "congratulations"
    end
    assert_nil $test_after_initialize_block1

    config.expects(:gems_dependencies_loaded).returns(true)
    Rails::Initializer.run(:after_initialize, config)
  end

  def teardown
    $test_after_initialize_block1 = nil
    $test_after_initialize_block2 = nil
  end

  def test_should_have_called_the_first_after_initialize_block
    assert_equal "success", $test_after_initialize_block1, "should still get set"
  end

  def test_should_have_called_the_second_after_initialize_block
    assert_equal "congratulations", $test_after_initialize_block2
  end
end

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)
    Rails::Initializer.run(:set_root_path, @config)
  end

  def test_minimal
    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"

    @config.frameworks = [:action_view]
    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"
    end
  end

  def test_action_mailer_load_paths_set_only_if_action_mailer_in_use
    @config.frameworks = [:action_controller]
    @initializer.config = @config
    @initializer.run :require_frameworks

    assert_nothing_raised NameError do
      @initializer.run :load_view_paths
    end
  end

  def test_action_controller_load_paths_set_only_if_action_controller_in_use
    @config.frameworks = []
    @initializer.run :require_frameworks

    assert_nothing_raised NameError do
      @initializer.run :load_view_paths
    end
  end

  protected
    def assert_framework_path(path)
      assert @config.framework_paths.include?("#{@config.framework_root_path}/#{path}"),
        "<#{path.inspect}> not found among <#{@config.framework_paths.inspect}>"
    end
end

require 'plugin_test_helper'

class InitializerPluginLoadingTests < Test::Unit::TestCase
  def setup
    @configuration     = Rails::Configuration.new
    @configuration.frameworks -= [:action_mailer]
    @configuration.plugin_paths << plugin_fixture_root_path
    @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.run :load_plugins
    assert_equal [], @configuration.loaded_plugins
  end

  def test_only_the_specified_plugins_are_located_in_the_order_listed
    plugin_names = [:plugin_with_no_lib_dir, :acts_as_chunky_bacon]
    only_load_the_following_plugins! plugin_names
    load_plugins!
    assert_plugins plugin_names, @configuration.loaded_plugins
  end

  private

    def load_plugins!
      @initializer.run(:add_plugin_load_paths)
      @initializer.run(:load_plugins)
    end
end

class InitializerGeneratorsTests < Test::Unit::TestCase

  def setup
    @configuration = Rails::Configuration.new
    @initializer   = Rails::Initializer.default
    @initializer.config = @configuration
  end

  def test_generators_default_values
    assert_equal(true, @configuration.generators.colorize_logging)
    assert_equal({}, @configuration.generators.aliases)
    assert_equal({}, @configuration.generators.options)
  end

  def test_generators_set_rails_options
    @configuration.generators.orm = :datamapper
    @configuration.generators.test_framework = :rspec
    expected = { :rails => { :orm => :datamapper, :test_framework => :rspec } }
    assert_equal expected, @configuration.generators.options
  end

  def test_generators_set_rails_aliases
    @configuration.generators.aliases = { :rails => { :test_framework => "-w" } }
    expected = { :rails => { :test_framework => "-w" } }
    assert_equal expected, @configuration.generators.aliases
  end

  def test_generators_aliases_and_options_on_initialization
    @configuration.generators.rails :aliases => { :test_framework => "-w" }
    @configuration.generators.orm :datamapper
    @configuration.generators.test_framework :rspec

    @initializer.run(:initialize_generators)

    assert_equal :rspec, Rails::Generators.options[:rails][:test_framework]
    assert_equal "-w", Rails::Generators.aliases[:rails][:test_framework]
  end

  def test_generators_no_color_on_initialization
    @configuration.generators.colorize_logging = false
    @initializer.run(:initialize_generators)
    assert_equal Thor::Base.shell, Thor::Shell::Basic
  end

  def test_generators_with_hashes_for_options_and_aliases
    @configuration.generators do |g|
      g.orm    :datamapper, :migration => false
      g.plugin :aliases => { :generator => "-g" },
               :generator => true
    end

    expected = {
      :rails => { :orm => :datamapper },
      :plugin => { :generator => true },
      :datamapper => { :migration => false }
    }

    assert_equal expected, @configuration.generators.options
    assert_equal({ :plugin => { :generator => "-g" } }, @configuration.generators.aliases)
  end

  def test_generators_with_hashes_are_deep_merged
    @configuration.generators do |g|
      g.orm    :datamapper, :migration => false
      g.plugin :aliases => { :generator => "-g" },
               :generator => true
    end
    @initializer.run(:initialize_generators)

    assert Rails::Generators.aliases.size >= 1
    assert Rails::Generators.options.size >= 1
  end

  protected

    def teardown
      Rails::Generators.clear_aliases!
      Rails::Generators.clear_options!
    end
end

class InitializerSetupI18nTests < Test::Unit::TestCase
  def test_no_config_locales_dir_present_should_return_empty_load_path
    File.stubs(:exist?).returns(false)
    assert_equal [], Rails::Configuration.new.i18n.load_path
  end

  def test_config_locales_dir_present_should_be_added_to_load_path
    File.stubs(:exist?).returns(true)
    Dir.stubs(:[]).returns([ "my/test/locale.yml" ])
    assert_equal [ "my/test/locale.yml" ], Rails::Configuration.new.i18n.load_path
  end

  def test_config_defaults_should_be_added_with_config_settings
    File.stubs(:exist?).returns(true)
    Dir.stubs(:[]).returns([ "my/test/locale.yml" ])

    config = Rails::Configuration.new
    config.i18n.load_path << "my/other/locale.yml"

    assert_equal [ "my/test/locale.yml", "my/other/locale.yml" ], config.i18n.load_path
  end

  def test_setting_another_default_locale
    config = Rails::Configuration.new
    config.i18n.default_locale = :de
    Rails::Initializer.run(:initialize_i18n, config)
    assert_equal :de, I18n.default_locale
  end
end

class InitializerDatabaseMiddlewareTest < Test::Unit::TestCase
  def setup
    @config = Rails::Configuration.new
    @config.frameworks = [:active_record, :action_controller, :action_view]
  end

  def test_initialize_database_middleware_doesnt_perform_anything_when_active_record_not_in_frameworks
    @config.frameworks.clear
    @config.expects(:middleware).never
    Rails::Initializer.run(:initialize_database_middleware, @config)
  end

  def test_database_middleware_initializes_when_session_store_is_active_record
    store = ActionController::Base.session_store
    ActionController::Base.session_store = ActiveRecord::SessionStore

    @config.middleware.expects(:insert_before).with(:"ActiveRecord::SessionStore", ActiveRecord::ConnectionAdapters::ConnectionManagement)
    @config.middleware.expects(:insert_before).with(:"ActiveRecord::SessionStore", ActiveRecord::QueryCache)

    Rails::Initializer.run(:initialize_database_middleware, @config)
  ensure
    ActionController::Base.session_store = store
  end

  def test_database_middleware_doesnt_initialize_when_session_store_is_not_active_record
    store = ActionController::Base.session_store
    ActionController::Base.session_store = ActionDispatch::Session::CookieStore

    # Define the class, so we don't have to actually make it load
    eval("class ActiveRecord::ConnectionAdapters::ConnectionManagement; end")

    @config.middleware.expects(:use).with(ActiveRecord::ConnectionAdapters::ConnectionManagement)
    @config.middleware.expects(:use).with(ActiveRecord::QueryCache)

    Rails::Initializer.run(:initialize_database_middleware, @config)
  ensure
    ActionController::Base.session_store = store
  end

  def test_ensure_database_middleware_doesnt_use_action_controller_on_initializing
    @config.frameworks -= [:action_controller]
    store = ActionController::Base.session_store
    ActionController::Base.session_store = ActiveRecord::SessionStore

    @config.middleware.expects(:use).with(ActiveRecord::ConnectionAdapters::ConnectionManagement)
    @config.middleware.expects(:use).with(ActiveRecord::QueryCache)

    Rails::Initializer.run(:initialize_database_middleware, @config)
  ensure
    ActionController::Base.session_store = store
    @config.frameworks += [:action_controller]
  end
end

class InitializerViewPathsTest  < Test::Unit::TestCase
  def setup
    @config = Rails::Configuration.new
    @config.frameworks = [:action_view, :action_controller, :action_mailer]

    ActionController::Base.stubs(:view_paths).returns(stub)
    ActionMailer::Base.stubs(:view_paths).returns(stub)
  end

  def test_load_view_paths_doesnt_perform_anything_when_action_view_not_in_frameworks
    @config.frameworks -= [:action_view]
    ActionController::Base.view_paths.expects(:load!).never
    ActionMailer::Base.view_paths.expects(:load!).never
    Rails::Initializer.run(:load_view_paths, @config)
  end
end

class RailsRootTest < Test::Unit::TestCase
  def test_rails_dot_root_equals_rails_root
    assert_equal RAILS_ROOT, Rails.root.to_s
  end

  def test_rails_dot_root_should_be_a_pathname
    assert_equal File.join(RAILS_ROOT, 'app', 'controllers'), Rails.root.join('app', 'controllers').to_s
  end
end