2010-06-19 11:51:29 -04:00
|
|
|
require 'isolation/abstract_unit'
|
|
|
|
|
2012-01-05 20:30:17 -05:00
|
|
|
class LoadingTest < ActiveSupport::TestCase
|
2010-06-19 11:51:29 -04:00
|
|
|
include ActiveSupport::Testing::Isolation
|
|
|
|
|
|
|
|
def setup
|
|
|
|
build_app
|
|
|
|
boot_rails
|
|
|
|
end
|
|
|
|
|
2011-06-05 15:25:24 -04:00
|
|
|
def teardown
|
2011-06-06 08:54:05 -04:00
|
|
|
teardown_app
|
2011-06-05 15:25:24 -04:00
|
|
|
end
|
|
|
|
|
2010-06-19 11:51:29 -04:00
|
|
|
def app
|
|
|
|
@app ||= Rails.application
|
|
|
|
end
|
|
|
|
|
2011-12-13 03:19:58 -05:00
|
|
|
test "constants in app are autoloaded" do
|
2010-06-19 11:51:29 -04:00
|
|
|
app_file "app/models/post.rb", <<-MODEL
|
|
|
|
class Post < ActiveRecord::Base
|
2012-10-14 06:03:39 -04:00
|
|
|
validates_acceptance_of :title, accept: "omg"
|
2010-06-19 11:51:29 -04:00
|
|
|
end
|
|
|
|
MODEL
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2010-06-19 11:51:29 -04:00
|
|
|
require "#{rails_root}/config/environment"
|
|
|
|
setup_ar!
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2012-10-14 06:03:39 -04:00
|
|
|
p = Post.create(title: 'omg')
|
2010-06-19 11:51:29 -04:00
|
|
|
assert_equal 1, Post.count
|
|
|
|
assert_equal 'omg', p.title
|
|
|
|
p = Post.first
|
|
|
|
assert_equal 'omg', p.title
|
|
|
|
end
|
|
|
|
|
2011-12-13 03:19:58 -05:00
|
|
|
test "models without table do not panic on scope definitions when loaded" do
|
2010-06-29 13:47:04 -04:00
|
|
|
app_file "app/models/user.rb", <<-MODEL
|
|
|
|
class User < ActiveRecord::Base
|
2013-07-01 23:24:51 -04:00
|
|
|
default_scope { where(published: true) }
|
2010-06-29 13:47:04 -04:00
|
|
|
end
|
|
|
|
MODEL
|
|
|
|
|
|
|
|
require "#{rails_root}/config/environment"
|
|
|
|
setup_ar!
|
|
|
|
|
|
|
|
User
|
|
|
|
end
|
|
|
|
|
2010-07-18 17:23:54 -04:00
|
|
|
test "load config/environments/environment before Bootstrap initializers" do
|
|
|
|
app_file "config/environments/development.rb", <<-RUBY
|
2013-06-03 23:57:01 -04:00
|
|
|
Rails.application.configure do
|
2010-07-18 17:23:54 -04:00
|
|
|
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"
|
2013-06-03 23:57:01 -04:00
|
|
|
assert ::Rails.application.config.loaded
|
2010-07-18 17:23:54 -04:00
|
|
|
end
|
|
|
|
|
2012-01-15 10:37:46 -05:00
|
|
|
test "descendants loaded after framework initialization are cleaned on each request without cache classes" do
|
2010-06-19 11:51:29 -04:00
|
|
|
add_to_config <<-RUBY
|
|
|
|
config.cache_classes = false
|
2011-12-12 16:51:33 -05:00
|
|
|
config.reload_classes_only_on_change = false
|
2010-06-19 11:51:29 -04:00
|
|
|
RUBY
|
|
|
|
|
|
|
|
app_file "app/models/post.rb", <<-MODEL
|
|
|
|
class Post < ActiveRecord::Base
|
|
|
|
end
|
|
|
|
MODEL
|
|
|
|
|
|
|
|
app_file 'config/routes.rb', <<-RUBY
|
2013-06-03 23:57:01 -04:00
|
|
|
Rails.application.routes.draw do
|
2012-10-14 06:03:39 -04:00
|
|
|
get '/load', to: lambda { |env| [200, {}, Post.all] }
|
|
|
|
get '/unload', to: lambda { |env| [200, {}, []] }
|
2010-06-19 11:51:29 -04:00
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
require 'rack/test'
|
|
|
|
extend Rack::Test::Methods
|
|
|
|
|
|
|
|
require "#{rails_root}/config/environment"
|
|
|
|
setup_ar!
|
|
|
|
|
2012-01-15 10:37:46 -05:00
|
|
|
assert_equal [ActiveRecord::SchemaMigration], ActiveRecord::Base.descendants
|
2010-06-19 11:51:29 -04:00
|
|
|
get "/load"
|
2012-01-15 10:37:46 -05:00
|
|
|
assert_equal [ActiveRecord::SchemaMigration, Post], ActiveRecord::Base.descendants
|
2010-06-19 11:51:29 -04:00
|
|
|
get "/unload"
|
2012-01-15 10:37:46 -05:00
|
|
|
assert_equal [ActiveRecord::SchemaMigration], ActiveRecord::Base.descendants
|
2010-06-19 11:51:29 -04:00
|
|
|
end
|
|
|
|
|
2012-03-15 03:48:38 -04:00
|
|
|
test "initialize cant be called twice" do
|
2010-07-22 06:07:45 -04:00
|
|
|
require "#{app_path}/config/environment"
|
2013-06-03 23:57:01 -04:00
|
|
|
assert_raise(RuntimeError) { Rails.application.initialize! }
|
2010-07-22 06:07:45 -04:00
|
|
|
end
|
|
|
|
|
2011-12-13 03:19:58 -05:00
|
|
|
test "reload constants on development" do
|
|
|
|
add_to_config <<-RUBY
|
|
|
|
config.cache_classes = false
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
app_file 'config/routes.rb', <<-RUBY
|
2013-06-03 23:57:01 -04:00
|
|
|
Rails.application.routes.draw do
|
2012-10-14 06:03:39 -04:00
|
|
|
get '/c', to: lambda { |env| [200, {"Content-Type" => "text/plain"}, [User.counter.to_s]] }
|
2011-12-13 03:19:58 -05:00
|
|
|
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
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
app_file 'config/routes.rb', <<-RUBY
|
2013-06-03 23:57:01 -04:00
|
|
|
Rails.application.routes.draw do
|
2012-10-14 06:03:39 -04:00
|
|
|
get '/c', to: lambda { |env| [200, {"Content-Type" => "text/plain"}, [User.counter.to_s]] }
|
2011-12-13 03:19:58 -05:00
|
|
|
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
|
|
|
|
|
2011-12-15 12:48:10 -05:00
|
|
|
test "added files (like db/schema.rb) also trigger reloading" do
|
2011-12-13 05:23:21 -05:00
|
|
|
add_to_config <<-RUBY
|
|
|
|
config.cache_classes = false
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
app_file 'config/routes.rb', <<-RUBY
|
2013-06-06 17:04:26 -04:00
|
|
|
$counter ||= 0
|
2013-06-03 23:57:01 -04:00
|
|
|
Rails.application.routes.draw do
|
2012-10-14 06:03:39 -04:00
|
|
|
get '/c', to: lambda { |env| User; [200, {"Content-Type" => "text/plain"}, [$counter.to_s]] }
|
2011-12-13 05:23:21 -05:00
|
|
|
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
|
|
|
|
|
2013-06-06 17:04:26 -04:00
|
|
|
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
|
2013-06-09 22:47:07 -04:00
|
|
|
Rails.application.routes.draw do
|
2013-06-06 17:04:26 -04:00
|
|
|
get '/c', to: lambda { |env| User; [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
|
|
|
|
|
2011-12-15 12:48:10 -05:00
|
|
|
test "columns migrations also trigger reloading" do
|
|
|
|
add_to_config <<-RUBY
|
|
|
|
config.cache_classes = false
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
app_file 'config/routes.rb', <<-RUBY
|
2013-06-03 23:57:01 -04:00
|
|
|
Rails.application.routes.draw do
|
2012-10-14 06:03:39 -04:00
|
|
|
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]] }
|
2011-12-15 12:48:10 -05:00
|
|
|
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
|
|
|
|
def change
|
|
|
|
create_table :posts do |t|
|
2012-10-14 06:03:39 -04:00
|
|
|
t.string :title, default: "TITLE"
|
2011-12-15 12:48:10 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
MIGRATION
|
|
|
|
|
|
|
|
Dir.chdir(app_path) { `rake 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
|
|
|
|
def change
|
2012-10-14 06:03:39 -04:00
|
|
|
add_column :posts, :body, :text, default: "BODY"
|
2011-12-15 12:48:10 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
MIGRATION
|
|
|
|
|
|
|
|
Dir.chdir(app_path) { `rake db:migrate` }
|
|
|
|
|
|
|
|
get "/body"
|
|
|
|
assert_equal "BODY", last_response.body
|
|
|
|
end
|
|
|
|
|
2012-03-15 03:48:38 -04:00
|
|
|
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
|
2013-06-03 23:57:01 -04:00
|
|
|
Rails.application.routes.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get "/:controller(/:action)"
|
2012-03-15 03:48:38 -04:00
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
require "#{rails_root}/config/environment"
|
|
|
|
|
|
|
|
require 'rack/test'
|
|
|
|
extend Rack::Test::Methods
|
|
|
|
|
|
|
|
get '/omg/show'
|
|
|
|
assert_equal 'OK', last_response.body
|
|
|
|
end
|
|
|
|
|
2011-08-15 04:11:11 -04:00
|
|
|
def test_initialize_can_be_called_at_any_time
|
|
|
|
require "#{app_path}/config/application"
|
|
|
|
|
|
|
|
assert !Rails.initialized?
|
2013-06-03 23:57:01 -04:00
|
|
|
assert !Rails.application.initialized?
|
2011-08-15 04:11:11 -04:00
|
|
|
Rails.initialize!
|
|
|
|
assert Rails.initialized?
|
2013-06-03 23:57:01 -04:00
|
|
|
assert Rails.application.initialized?
|
2011-08-15 04:11:11 -04:00
|
|
|
end
|
|
|
|
|
2010-06-19 11:51:29 -04:00
|
|
|
protected
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2010-06-19 11:51:29 -04:00
|
|
|
def setup_ar!
|
2012-10-14 06:03:39 -04:00
|
|
|
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
|
2010-06-19 11:51:29 -04:00
|
|
|
ActiveRecord::Migration.verbose = false
|
2012-10-14 06:03:39 -04:00
|
|
|
ActiveRecord::Schema.define(version: 1) do
|
2010-06-19 11:51:29 -04:00
|
|
|
create_table :posts do |t|
|
|
|
|
t.string :title
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|