aboutsummaryrefslogtreecommitdiffstats
path: root/railties/test/application/initializer_test.rb
blob: 805ff8b6be71f24812b79a0bbbd2f597f80b745f (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
require "isolation/abstract_unit"

module ApplicationTests
  class InitializerTest < Test::Unit::TestCase
    include ActiveSupport::Testing::Isolation

    def setup
      build_app
      boot_rails
    end

    test "initializing an application initializes rails" do
      Rails::Initializer.run do |config|
        config.root = app_path
      end

      if RUBY_VERSION < '1.9'
        $KCODE = ''
        Rails.application.new
        assert_equal 'UTF8', $KCODE
      else
        Encoding.default_external = Encoding::US_ASCII
        Rails.application.new
        assert_equal Encoding::UTF_8, Encoding.default_external
      end
    end

    test "initializing an application adds the application paths to the load path" do
      Rails::Initializer.run do |config|
        config.root = app_path
      end

      Rails.application.new
      assert $:.include?("#{app_path}/app/models")
    end

    test "adding an unknown framework raises an error" do
      Rails::Initializer.run do |config|
        config.root = app_path
        config.frameworks << :action_foo
      end

      assert_raises RuntimeError do
        Rails.application.new
      end
    end

    test "eager loading loads parent classes before children" do
      app_file "lib/zoo.rb", <<-ZOO
        class Zoo ; include ReptileHouse ; end
      ZOO
      app_file "lib/zoo/reptile_house.rb", <<-ZOO
        module Zoo::ReptileHouse ; end
      ZOO

      Rails::Initializer.run do |config|
        config.root = app_path
        config.eager_load_paths = "#{app_path}/lib"
      end

      Rails.application.new

      assert Zoo
    end

    test "load environment with global" do
      app_file "config/environments/development.rb", "$initialize_test_set_from_env = 'success'"
      assert_nil $initialize_test_set_from_env
      Rails::Initializer.run { |config| config.root = app_path }
      Rails.application.new
      assert_equal "success", $initialize_test_set_from_env
    end

    test "action_controller load paths set only if action controller in use" do
      assert_nothing_raised NameError do
        Rails::Initializer.run do |config|
          config.root = app_path
          config.frameworks = []
        end
        Rails.application.new
      end
    end

    test "action_pack is added to the load path if action_controller is required" do
      Rails::Initializer.run do |config|
        config.root = app_path
        config.frameworks = [:action_controller]
      end
      Rails.application.new

      assert $:.include?("#{framework_path}/actionpack/lib")
    end

    test "action_pack is added to the load path if action_view is required" do
      Rails::Initializer.run do |config|
        config.root = app_path
        config.frameworks = [:action_view]
      end
      Rails.application.new

      assert $:.include?("#{framework_path}/actionpack/lib")
    end

    test "after_initialize block works correctly" do
      Rails::Initializer.run do |config|
        config.root = app_path
        config.after_initialize { $test_after_initialize_block1 = "success" }
        config.after_initialize { $test_after_initialize_block2 = "congratulations" }
      end
      Rails.application.new

      assert_equal "success", $test_after_initialize_block1
      assert_equal "congratulations", $test_after_initialize_block2
    end

    test "after_initialize block works correctly when no block is passed" do
      Rails::Initializer.run do |config|
        config.root = app_path
        config.after_initialize { $test_after_initialize_block1 = "success" }
        config.after_initialize # don't pass a block, this is what we're testing!
        config.after_initialize { $test_after_initialize_block2 = "congratulations" }
      end
      Rails.application.new

      assert_equal "success", $test_after_initialize_block1
      assert_equal "congratulations", $test_after_initialize_block2
    end

    # i18n
    test "setting another default locale" do
      Rails::Initializer.run do |config|
        config.root = app_path
        config.i18n.default_locale = :de
      end
      Rails.application.new

      assert_equal :de, I18n.default_locale
    end

    test "no config locales dir present should return empty load path" do
      FileUtils.rm_rf "#{app_path}/config/locales"
      Rails::Initializer.run do |c|
        c.root = app_path
        assert_equal [], c.i18n.load_path
      end
    end

    test "config locales dir present should be added to load path" do
      Rails::Initializer.run do |c|
        c.root = app_path
        assert_equal ["#{app_path}/config/locales/en.yml"], c.i18n.load_path
      end
    end

    test "config defaults should be added with config settings" do
      Rails::Initializer.run do |c|
        c.root = app_path
        c.i18n.load_path << "my/other/locale.yml"
      end

      assert_equal [
        "#{app_path}/config/locales/en.yml", "my/other/locale.yml"
      ], Rails.application.config.i18n.load_path
    end

    # DB middleware
    test "database middleware doesn't initialize when session store is not active_record" do
      Rails::Initializer.run do |config|
        config.root = app_path
        config.action_controller.session_store = :cookie_store
      end
      Rails.application.new

      assert !Rails.application.config.middleware.include?(ActiveRecord::SessionStore)
    end

    test "database middleware doesn't initialize when activerecord is not in frameworks" do
      Rails::Initializer.run do |c|
        c.root = app_path
        c.frameworks = []
      end
      assert_equal [], Rails.application.config.middleware
    end

    test "database middleware initializes when session store is active record" do
      Rails::Initializer.run do |c|
        c.root = app_path
        c.action_controller.session_store = :active_record_store
      end
      Rails.application.new

      expects = [ActiveRecord::ConnectionAdapters::ConnectionManagement, ActiveRecord::QueryCache, ActiveRecord::SessionStore]
      middleware = Rails.application.config.middleware.map { |m| m.klass }
      assert_equal expects, middleware & expects
    end

    test "ensure database middleware doesn't use action_controller on initializing" do
      Rails::Initializer.run do |c|
        c.root = app_path
        c.frameworks -= [:action_controller]
        c.action_controller.session_store = :active_record_store
      end
      Rails.application.new

      assert !Rails.application.config.middleware.include?(ActiveRecord::SessionStore)
    end

    # Pathview test
    test "load view paths doesn't perform anything when action_view not in frameworks" do
      Rails::Initializer.run do |c|
        c.root = app_path
        c.frameworks -= [:action_view]
      end
      Rails.application.new

      assert_equal nil, ActionMailer::Base.template_root
      assert_equal [], ActionController::Base.view_paths
    end

    test "Rails.root should be a Pathname" do
      Rails::Initializer.run do |c|
        c.root = app_path
      end
      Rails.application.new
      assert_instance_of Pathname, Rails.root
    end
  end
end