aboutsummaryrefslogtreecommitdiffstats
path: root/railties/test/application/loading_test.rb
blob: 9ab400acfcbd944ed891b9ce28b0fd669c0bfd9e (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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
# frozen_string_literal: true

require "isolation/abstract_unit"

class LoadingTest < ActiveSupport::TestCase
  include ActiveSupport::Testing::Isolation

  def setup
    build_app
  end

  def teardown
    teardown_app
  end

  def app
    @app ||= Rails.application
  end

  test "constants in app are autoloaded" do
    app_file "app/models/post.rb", <<-MODEL
      class Post < ActiveRecord::Base
        validates_acceptance_of :title, accept: "omg"
      end
    MODEL

    require "#{rails_root}/config/environment"
    setup_ar!

    p = Post.create(title: "omg")
    assert_equal 1, Post.count
    assert_equal "omg", p.title
    p = Post.first
    assert_equal "omg", p.title
  end

  test "constants without a matching file raise NameError" do
    app_file "app/models/post.rb", <<-RUBY
      class Post
        NON_EXISTING_CONSTANT
      end
    RUBY

    boot_app

    e = assert_raise(NameError) { User }
    assert_equal "uninitialized constant #{self.class}::User", e.message

    e = assert_raise(NameError) { Post }
    assert_equal "uninitialized constant Post::NON_EXISTING_CONSTANT", e.message
  end

  test "concerns in app are autoloaded" do
    app_file "app/controllers/concerns/trackable.rb", <<-CONCERN
      module Trackable
      end
    CONCERN

    app_file "app/mailers/concerns/email_loggable.rb", <<-CONCERN
      module EmailLoggable
      end
    CONCERN

    app_file "app/models/concerns/orderable.rb", <<-CONCERN
      module Orderable
      end
    CONCERN

    app_file "app/validators/concerns/matchable.rb", <<-CONCERN
      module Matchable
      end
    CONCERN

    require "#{rails_root}/config/environment"

    assert_nothing_raised { Trackable }
    assert_nothing_raised { EmailLoggable }
    assert_nothing_raised { Orderable }
    assert_nothing_raised { Matchable }
  end

  test "models without table do not panic on scope definitions when loaded" do
    app_file "app/models/user.rb", <<-MODEL
      class User < ActiveRecord::Base
        default_scope { where(published: true) }
      end
    MODEL

    require "#{rails_root}/config/environment"
    setup_ar!

    User
  end

  test "load config/environments/environment before Bootstrap initializers" do
    app_file "config/environments/development.rb", <<-RUBY
      Rails.application.configure do
        config.development_environment_loaded = true
      end
    RUBY

    add_to_config <<-RUBY
      config.before_initialize do
        config.loaded = config.development_environment_loaded
      end
    RUBY

    require "#{app_path}/config/environment"
    assert ::Rails.application.config.loaded
  end

  test "descendants loaded after framework initialization are cleaned on each request without cache classes" do
    add_to_config <<-RUBY
      config.cache_classes = false
      config.reload_classes_only_on_change = false
    RUBY

    app_file "app/models/post.rb", <<-MODEL
      class Post < ApplicationRecord
      end
    MODEL

    app_file "config/routes.rb", <<-RUBY
      Rails.application.routes.draw do
        get '/load',   to: lambda { |env| [200, {}, Post.all] }
        get '/unload', to: lambda { |env| [200, {}, []] }
      end
    RUBY

    require "rack/test"
    extend Rack::Test::Methods

    require "#{rails_root}/config/environment"
    setup_ar!

    initial = [ActiveStorage::Blob, ActiveStorage::Attachment, ActiveRecord::SchemaMigration, ActiveRecord::InternalMetadata, ApplicationRecord, "primary::SchemaMigration"].collect(&:to_s).sort
    assert_equal initial, ActiveRecord::Base.descendants.collect(&:to_s).sort
    get "/load"
    assert_equal [Post].collect(&:to_s).sort, ActiveRecord::Base.descendants.collect(&:to_s).sort - initial
    get "/unload"
    assert_equal ["ActiveRecord::InternalMetadata", "ActiveRecord::SchemaMigration", "primary::SchemaMigration"], ActiveRecord::Base.descendants.collect(&:to_s).sort.uniq
  end

  test "initialize cant be called twice" do
    require "#{app_path}/config/environment"
    assert_raise(RuntimeError) { Rails.application.initialize! }
  end

  test "reload constants on development" do
    add_to_config <<-RUBY
      config.cache_classes = false
    RUBY

    app_file "config/routes.rb", <<-RUBY
      Rails.application.routes.draw do
        get '/c', to: lambda { |env| [200, {"Content-Type" => "text/plain"}, [User.counter.to_s]] }
      end
    RUBY

    app_file "app/models/user.rb", <<-MODEL
      class User
        def self.counter; 1; end
      end
    MODEL

    require "rack/test"
    extend Rack::Test::Methods

    require "#{rails_root}/config/environment"

    get "/c"
    assert_equal "1", last_response.body

    app_file "app/models/user.rb", <<-MODEL
      class User
        def self.counter; 2; end
      end
    MODEL

    get "/c"
    assert_equal "2", last_response.body
  end

  test "does not reload constants on development if custom file watcher always returns false" do
    add_to_config <<-RUBY
      config.cache_classes = false
      config.file_watcher = Class.new do
        def initialize(*); end
        def updated?; false; end
        def execute; end
        def execute_if_updated; false; end
      end
    RUBY

    app_file "config/routes.rb", <<-RUBY
      Rails.application.routes.draw do
        get '/c', to: lambda { |env| [200, {"Content-Type" => "text/plain"}, [User.counter.to_s]] }
      end
    RUBY

    app_file "app/models/user.rb", <<-MODEL
      class User
        def self.counter; 1; end
      end
    MODEL

    require "rack/test"
    extend Rack::Test::Methods

    require "#{rails_root}/config/environment"

    get "/c"
    assert_equal "1", last_response.body

    app_file "app/models/user.rb", <<-MODEL
      class User
        def self.counter; 2; end
      end
    MODEL

    get "/c"
    assert_equal "1", last_response.body
  end

  test "added files (like db/schema.rb) also trigger reloading" do
    add_to_config <<-RUBY
      config.cache_classes = false
    RUBY

    app_file "config/routes.rb", <<-RUBY
      $counter ||= 0
      Rails.application.routes.draw do
        get '/c', to: lambda { |env| User.name; [200, {"Content-Type" => "text/plain"}, [$counter.to_s]] }
      end
    RUBY

    app_file "app/models/user.rb", <<-MODEL
      class User
        $counter += 1
      end
    MODEL

    require "rack/test"
    extend Rack::Test::Methods

    require "#{rails_root}/config/environment"

    get "/c"
    assert_equal "1", last_response.body

    app_file "db/schema.rb", ""

    get "/c"
    assert_equal "2", last_response.body
  end

  test "dependencies reloading is followed by routes reloading" do
    add_to_config <<-RUBY
      config.cache_classes = false
    RUBY

    app_file "config/routes.rb", <<-RUBY
      $counter ||= 1
      $counter  *= 2
      Rails.application.routes.draw do
        get '/c', to: lambda { |env| User.name; [200, {"Content-Type" => "text/plain"}, [$counter.to_s]] }
      end
    RUBY

    app_file "app/models/user.rb", <<-MODEL
      class User
        $counter += 1
      end
    MODEL

    require "rack/test"
    extend Rack::Test::Methods

    require "#{rails_root}/config/environment"

    get "/c"
    assert_equal "3", last_response.body

    app_file "db/schema.rb", ""

    get "/c"
    assert_equal "7", last_response.body
  end

  test "columns migrations also trigger reloading" do
    add_to_config <<-RUBY
      config.cache_classes = false
    RUBY

    app_file "config/routes.rb", <<-RUBY
      Rails.application.routes.draw do
        get '/title', to: lambda { |env| [200, {"Content-Type" => "text/plain"}, [Post.new.title]] }
        get '/body',  to: lambda { |env| [200, {"Content-Type" => "text/plain"}, [Post.new.body]] }
      end
    RUBY

    app_file "app/models/post.rb", <<-MODEL
      class Post < ActiveRecord::Base
      end
    MODEL

    require "rack/test"
    extend Rack::Test::Methods

    app_file "db/migrate/1_create_posts.rb", <<-MIGRATION
      class CreatePosts < ActiveRecord::Migration::Current
        def change
          create_table :posts do |t|
            t.string :title, default: "TITLE"
          end
        end
      end
    MIGRATION

    rails("db:migrate")
    require "#{rails_root}/config/environment"

    get "/title"
    assert_equal "TITLE", last_response.body

    app_file "db/migrate/2_add_body_to_posts.rb", <<-MIGRATION
      class AddBodyToPosts < ActiveRecord::Migration::Current
        def change
          add_column :posts, :body, :text, default: "BODY"
        end
      end
    MIGRATION

    rails("db:migrate")

    get "/body"
    assert_equal "BODY", last_response.body
  end

  test "AC load hooks can be used with metal" do
    app_file "app/controllers/omg_controller.rb", <<-RUBY
      begin
        class OmgController < ActionController::Metal
          ActiveSupport.run_load_hooks(:action_controller, self)
          def show
            self.response_body = ["OK"]
          end
        end
      rescue => e
        puts "Error loading metal: \#{e.class} \#{e.message}"
      end
    RUBY

    app_file "config/routes.rb", <<-RUBY
      Rails.application.routes.draw do
        get "/:controller(/:action)"
      end
    RUBY

    require "#{rails_root}/config/environment"

    require "rack/test"
    extend Rack::Test::Methods

    get "/omg/show"
    assert_equal "OK", last_response.body
  end

  def test_initialize_can_be_called_at_any_time
    require "#{app_path}/config/application"

    assert_not_predicate Rails, :initialized?
    assert_not_predicate Rails.application, :initialized?
    Rails.initialize!
    assert_predicate Rails, :initialized?
    assert_predicate Rails.application, :initialized?
  end

  test "frameworks aren't loaded during initialization" do
    app_file "config/initializers/raise_when_frameworks_load.rb", <<-RUBY
      %i(action_controller action_mailer active_job active_record).each do |framework|
        ActiveSupport.on_load(framework) { raise "\#{framework} loaded!" }
      end
    RUBY

    assert_nothing_raised do
      require "#{app_path}/config/environment"
    end
  end

  test "active record query cache hooks are installed before first request in production" do
    app_file "app/controllers/omg_controller.rb", <<-RUBY
      begin
        class OmgController < ActionController::Metal
          ActiveSupport.run_load_hooks(:action_controller, self)
          def show
            if ActiveRecord::Base.connection.query_cache_enabled
              self.response_body = ["Query cache is enabled."]
            else
              self.response_body = ["Expected ActiveRecord::Base.connection.query_cache_enabled to be true"]
            end
          end
        end
      rescue => e
        puts "Error loading metal: \#{e.class} \#{e.message}"
      end
    RUBY

    app_file "config/routes.rb", <<-RUBY
      Rails.application.routes.draw do
        get "/:controller(/:action)"
      end
    RUBY

    boot_app "production"

    require "rack/test"
    extend Rack::Test::Methods

    get "/omg/show"
    assert_equal "Query cache is enabled.", last_response.body
  end

  test "active record query cache hooks are installed before first request in development" do
    app_file "app/controllers/omg_controller.rb", <<-RUBY
      begin
        class OmgController < ActionController::Metal
          ActiveSupport.run_load_hooks(:action_controller, self)
          def show
            if ActiveRecord::Base.connection.query_cache_enabled
              self.response_body = ["Query cache is enabled."]
            else
              self.response_body = ["Expected ActiveRecord::Base.connection.query_cache_enabled to be true"]
            end
          end
        end
      rescue => e
        puts "Error loading metal: \#{e.class} \#{e.message}"
      end
    RUBY

    app_file "config/routes.rb", <<-RUBY
      Rails.application.routes.draw do
        get "/:controller(/:action)"
      end
    RUBY

    boot_app "development"

    require "rack/test"
    extend Rack::Test::Methods

    get "/omg/show"
    assert_equal "Query cache is enabled.", last_response.body
  end

  private
    def setup_ar!
      ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
      ActiveRecord::Migration.verbose = false
      ActiveRecord::Schema.define(version: 1) do
        create_table :posts do |t|
          t.string :title
        end
      end
    end

    def boot_app(env = "development")
      ENV["RAILS_ENV"] = env

      require "#{app_path}/config/environment"
    ensure
      ENV.delete "RAILS_ENV"
    end
end