tests pass with requiring the frameworks in rails.rb
This commit is contained in:
parent
24e1b55608
commit
dc677f7665
@ -1,33 +1,9 @@
|
||||
require "pathname"
|
||||
require "rails/core"
|
||||
|
||||
require 'active_support'
|
||||
require 'active_support/core_ext/kernel/reporting'
|
||||
require 'active_support/core_ext/logger'
|
||||
require 'action_dispatch'
|
||||
|
||||
require 'rails/initializable'
|
||||
require 'rails/application'
|
||||
require 'rails/plugin'
|
||||
require 'rails/railties_path'
|
||||
require 'rails/version'
|
||||
require 'rails/rack'
|
||||
require 'rails/paths'
|
||||
require 'rails/core'
|
||||
require 'rails/configuration'
|
||||
require 'rails/deprecation'
|
||||
require 'rails/initializer'
|
||||
require 'rails/ruby_version_check'
|
||||
|
||||
# For Ruby 1.8, this initialization sets $KCODE to 'u' to enable the
|
||||
# multibyte safe operations. Plugin authors supporting other encodings
|
||||
# should override this behaviour and set the relevant +default_charset+
|
||||
# on ActionController::Base.
|
||||
#
|
||||
# For Ruby 1.9, UTF-8 is the default internal and external encoding.
|
||||
if RUBY_VERSION < '1.9'
|
||||
$KCODE='u'
|
||||
else
|
||||
Encoding.default_external = Encoding::UTF_8
|
||||
end
|
||||
|
||||
RAILS_ENV = (ENV["RAILS_ENV"] || ENV["RACK_ENV"] || "development").dup unless defined?(RAILS_ENV)
|
||||
%w(active_model active_record action_controller action_view action_mailer active_resource).each do |framework|
|
||||
begin
|
||||
require framework
|
||||
require "#{framework}/rails"
|
||||
rescue LoadError
|
||||
end
|
||||
end
|
@ -1,3 +1,5 @@
|
||||
require "fileutils"
|
||||
|
||||
module Rails
|
||||
class Application
|
||||
include Initializable
|
||||
@ -140,13 +142,13 @@ def call(env)
|
||||
$LOAD_PATH.uniq!
|
||||
end
|
||||
|
||||
# Requires all frameworks specified by the Configuration#frameworks
|
||||
# list. By default, all frameworks (Active Record, Active Support,
|
||||
# Action Pack, Action Mailer, and Active Resource) are loaded.
|
||||
initializer :require_frameworks do
|
||||
require 'active_support/all' unless config.active_support.bare
|
||||
config.frameworks.each { |framework| require(framework.to_s) }
|
||||
end
|
||||
# # Requires all frameworks specified by the Configuration#frameworks
|
||||
# # list. By default, all frameworks (Active Record, Active Support,
|
||||
# # Action Pack, Action Mailer, and Active Resource) are loaded.
|
||||
# initializer :require_frameworks do
|
||||
# require 'active_support/all' unless config.active_support.bare
|
||||
# config.frameworks.each { |framework| require(framework.to_s) }
|
||||
# end
|
||||
|
||||
# Set the paths from which Rails will automatically load source files, and
|
||||
# the load_once paths.
|
||||
@ -192,7 +194,7 @@ def call(env)
|
||||
# this sets the database configuration from Configuration#database_configuration
|
||||
# and then establishes the connection.
|
||||
initializer :initialize_database do
|
||||
if config.frameworks.include?(:active_record)
|
||||
if defined?(ActiveRecord)
|
||||
ActiveRecord::Base.configurations = config.database_configuration
|
||||
ActiveRecord::Base.establish_connection
|
||||
end
|
||||
@ -206,7 +208,7 @@ def call(env)
|
||||
end
|
||||
|
||||
initializer :initialize_middleware_stack do
|
||||
if config.frameworks.include?(:action_controller)
|
||||
if defined?(ActionController)
|
||||
config.middleware.use(::Rack::Lock, :if => lambda { ActionController::Base.allow_concurrency })
|
||||
config.middleware.use(::Rack::Runtime)
|
||||
config.middleware.use(ActionDispatch::ShowExceptions, lambda { ActionController::Base.consider_all_requests_local })
|
||||
@ -231,7 +233,7 @@ def call(env)
|
||||
end
|
||||
|
||||
initializer :initialize_framework_caches do
|
||||
if config.frameworks.include?(:action_controller)
|
||||
if defined?(ActionController)
|
||||
ActionController::Base.cache_store ||= RAILS_CACHE
|
||||
end
|
||||
end
|
||||
@ -266,8 +268,12 @@ def call(env)
|
||||
# logger is already set, it is not changed, otherwise it is set to use
|
||||
# RAILS_DEFAULT_LOGGER.
|
||||
initializer :initialize_framework_logging do
|
||||
for framework in ([ :active_record, :action_controller, :action_mailer ] & config.frameworks)
|
||||
framework.to_s.camelize.constantize.const_get("Base").logger ||= Rails.logger
|
||||
for framework in [ :active_record, :action_controller, :action_mailer ]
|
||||
# TODO BEFORE PUSHING: REMOVEZ
|
||||
begin
|
||||
framework.to_s.camelize.constantize.const_get("Base").logger ||= Rails.logger
|
||||
rescue Exception
|
||||
end
|
||||
end
|
||||
|
||||
ActiveSupport::Dependencies.logger ||= Rails.logger
|
||||
@ -302,7 +308,7 @@ def call(env)
|
||||
|
||||
Time.zone_default = zone_default
|
||||
|
||||
if config.frameworks.include?(:active_record)
|
||||
if defined?(ActiveRecord)
|
||||
ActiveRecord::Base.time_zone_aware_attributes = true
|
||||
ActiveRecord::Base.default_timezone = :utc
|
||||
end
|
||||
@ -326,10 +332,14 @@ def call(env)
|
||||
# on each of the corresponding Base classes.
|
||||
initializer :initialize_framework_settings do
|
||||
config.frameworks.each do |framework|
|
||||
base_class = framework.to_s.camelize.constantize.const_get("Base")
|
||||
# TODO BEFORE PUSHING: This needs to work differently
|
||||
begin
|
||||
base_class = framework.to_s.camelize.constantize.const_get("Base")
|
||||
|
||||
config.send(framework).each do |setting, value|
|
||||
base_class.send("#{setting}=", value)
|
||||
config.send(framework).each do |setting, value|
|
||||
base_class.send("#{setting}=", value)
|
||||
end
|
||||
rescue Exception
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -339,16 +349,16 @@ def call(env)
|
||||
# paths have already been set, it is not changed, otherwise it is
|
||||
# set to use Configuration#view_path.
|
||||
initializer :initialize_framework_views do
|
||||
if config.frameworks.include?(:action_view)
|
||||
if defined?(ActionView)
|
||||
view_path = ActionView::PathSet.type_cast(config.view_path, config.cache_classes)
|
||||
ActionMailer::Base.template_root = view_path if config.frameworks.include?(:action_mailer) && ActionMailer::Base.view_paths.blank?
|
||||
ActionController::Base.view_paths = view_path if config.frameworks.include?(:action_controller) && ActionController::Base.view_paths.blank?
|
||||
|
||||
ActionMailer::Base.template_root = view_path if defined?(ActionMailer) && ActionMailer::Base.view_paths.blank?
|
||||
ActionController::Base.view_paths = view_path if defined?(ActionController) && ActionController::Base.view_paths.blank?
|
||||
end
|
||||
end
|
||||
|
||||
initializer :initialize_metal do
|
||||
# TODO: Make Rails and metal work without ActionController
|
||||
if config.frameworks.include?(:action_controller)
|
||||
if defined?(ActionController)
|
||||
Rails::Rack::Metal.requested_metals = config.metals
|
||||
|
||||
config.middleware.insert_before(
|
||||
@ -375,8 +385,8 @@ def call(env)
|
||||
|
||||
# # Setup database middleware after initializers have run
|
||||
initializer :initialize_database_middleware do
|
||||
if configuration.frameworks.include?(:active_record)
|
||||
if configuration.frameworks.include?(:action_controller) && ActionController::Base.session_store &&
|
||||
if defined?(ActiveRecord)
|
||||
if defined?(ActionController) && ActionController::Base.session_store &&
|
||||
ActionController::Base.session_store.name == 'ActiveRecord::SessionStore'
|
||||
configuration.middleware.insert_before :"ActiveRecord::SessionStore", ActiveRecord::ConnectionAdapters::ConnectionManagement
|
||||
configuration.middleware.insert_before :"ActiveRecord::SessionStore", ActiveRecord::QueryCache
|
||||
@ -422,7 +432,7 @@ def call(env)
|
||||
#
|
||||
# # Observers are loaded after plugins in case Observers or observed models are modified by plugins.
|
||||
initializer :load_observers do
|
||||
if configuration.frameworks.include?(:active_record)
|
||||
if defined?(ActiveRecord)
|
||||
ActiveRecord::Base.instantiate_observers
|
||||
end
|
||||
end
|
||||
|
@ -1,3 +1,37 @@
|
||||
require "pathname"
|
||||
|
||||
require 'active_support'
|
||||
require 'active_support/core_ext/kernel/reporting'
|
||||
require 'active_support/core_ext/logger'
|
||||
require 'action_dispatch'
|
||||
|
||||
require 'rails/initializable'
|
||||
require 'rails/application'
|
||||
require 'rails/plugin'
|
||||
require 'rails/railties_path'
|
||||
require 'rails/version'
|
||||
require 'rails/rack'
|
||||
require 'rails/paths'
|
||||
require 'rails/core'
|
||||
require 'rails/configuration'
|
||||
require 'rails/deprecation'
|
||||
require 'rails/initializer'
|
||||
require 'rails/ruby_version_check'
|
||||
|
||||
# For Ruby 1.8, this initialization sets $KCODE to 'u' to enable the
|
||||
# multibyte safe operations. Plugin authors supporting other encodings
|
||||
# should override this behaviour and set the relevant +default_charset+
|
||||
# on ActionController::Base.
|
||||
#
|
||||
# For Ruby 1.9, UTF-8 is the default internal and external encoding.
|
||||
if RUBY_VERSION < '1.9'
|
||||
$KCODE='u'
|
||||
else
|
||||
Encoding.default_external = Encoding::UTF_8
|
||||
end
|
||||
|
||||
RAILS_ENV = (ENV["RAILS_ENV"] || ENV["RACK_ENV"] || "development").dup unless defined?(RAILS_ENV)
|
||||
|
||||
module Rails
|
||||
# Needs to be duplicated from Active Support since its needed before Active
|
||||
# Support is available. Here both Options and Hash are namespaced to prevent
|
||||
|
@ -4,7 +4,16 @@ module ApplicationTests
|
||||
class InitializerTest < Test::Unit::TestCase
|
||||
include ActiveSupport::Testing::Isolation
|
||||
|
||||
def new_app
|
||||
File.expand_path("#{app_path}/../new_app")
|
||||
end
|
||||
|
||||
def copy_app
|
||||
FileUtils.cp_r(app_path, new_app)
|
||||
end
|
||||
|
||||
def setup
|
||||
FileUtils.rm_rf(new_app) if File.directory?(new_app)
|
||||
build_app
|
||||
boot_rails
|
||||
end
|
||||
@ -15,42 +24,36 @@ def setup
|
||||
end
|
||||
|
||||
test "the application root can be set" do
|
||||
FileUtils.mkdir_p("#{app_path}/hello")
|
||||
copy_app
|
||||
add_to_config <<-RUBY
|
||||
config.frameworks = []
|
||||
config.root = '#{app_path}/hello'
|
||||
config.root = '#{new_app}'
|
||||
RUBY
|
||||
require "#{app_path}/config/environment"
|
||||
assert_equal Pathname.new("#{app_path}/hello"), Rails.application.root
|
||||
end
|
||||
|
||||
test "the application root is detected as where config.ru is located" do
|
||||
add_to_config <<-RUBY
|
||||
config.frameworks = []
|
||||
RUBY
|
||||
FileUtils.mv "#{app_path}/config.ru", "#{app_path}/config/config.ru"
|
||||
use_frameworks []
|
||||
|
||||
require "#{app_path}/config/environment"
|
||||
assert_equal Pathname.new("#{app_path}/config"), Rails.application.root
|
||||
assert_equal Pathname.new(new_app), Rails.application.root
|
||||
end
|
||||
|
||||
test "the application root is Dir.pwd if there is no config.ru" do
|
||||
File.delete("#{app_path}/config.ru")
|
||||
add_to_config <<-RUBY
|
||||
config.frameworks = []
|
||||
RUBY
|
||||
|
||||
Dir.chdir("#{app_path}/app") do
|
||||
use_frameworks []
|
||||
|
||||
Dir.chdir("#{app_path}") do
|
||||
require "#{app_path}/config/environment"
|
||||
assert_equal Pathname.new("#{app_path}/app"), Rails.application.root
|
||||
assert_equal Pathname.new("#{app_path}"), Rails.application.root
|
||||
end
|
||||
end
|
||||
|
||||
test "config.active_support.bare does not require all of ActiveSupport" do
|
||||
add_to_config "config.frameworks = []; config.active_support.bare = true"
|
||||
add_to_config "config.active_support.bare = true"
|
||||
|
||||
use_frameworks []
|
||||
|
||||
Dir.chdir("#{app_path}/app") do
|
||||
require "#{app_path}/config/environment"
|
||||
assert_raises(NoMethodError) { 1.day }
|
||||
assert_raises(NoMethodError) { [1,2,3].rand }
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -7,8 +7,6 @@ class GeneratorsTest < Test::Unit::TestCase
|
||||
def setup
|
||||
build_app
|
||||
boot_rails
|
||||
require "rails"
|
||||
require "rails/generators"
|
||||
end
|
||||
|
||||
def app_const
|
||||
@ -16,6 +14,8 @@ def app_const
|
||||
end
|
||||
|
||||
def with_config
|
||||
require "rails"
|
||||
require "rails/generators"
|
||||
yield app_const.config
|
||||
end
|
||||
|
||||
@ -46,14 +46,15 @@ def with_config
|
||||
end
|
||||
|
||||
test "generators aliases and options on initialization" do
|
||||
application = with_config do |c|
|
||||
c.frameworks = []
|
||||
c.generators.rails :aliases => { :test_framework => "-w" }
|
||||
c.generators.orm :datamapper
|
||||
c.generators.test_framework :rspec
|
||||
end
|
||||
add_to_config <<-RUBY
|
||||
config.generators.rails :aliases => { :test_framework => "-w" }
|
||||
config.generators.orm :datamapper
|
||||
config.generators.test_framework :rspec
|
||||
RUBY
|
||||
|
||||
require "#{app_path}/config/environment"
|
||||
# Initialize the application
|
||||
app_const.initialize!
|
||||
require "rails/generators"
|
||||
Rails::Generators.configure!
|
||||
|
||||
assert_equal :rspec, Rails::Generators.options[:rails][:test_framework]
|
||||
@ -61,12 +62,13 @@ def with_config
|
||||
end
|
||||
|
||||
test "generators no color on initialization" do
|
||||
with_config do |c|
|
||||
c.frameworks = []
|
||||
c.generators.colorize_logging = false
|
||||
end
|
||||
add_to_config <<-RUBY
|
||||
config.generators.colorize_logging = false
|
||||
RUBY
|
||||
|
||||
# Initialize the application
|
||||
app_const.initialize!
|
||||
require "#{app_path}/config/environment"
|
||||
require "rails/generators"
|
||||
Rails::Generators.configure!
|
||||
|
||||
assert_equal Thor::Base.shell, Thor::Shell::Basic
|
||||
|
@ -19,19 +19,6 @@ def setup
|
||||
assert $:.include?("#{app_path}/app/models")
|
||||
end
|
||||
|
||||
test "adding an unknown framework raises an error" do
|
||||
add_to_config <<-RUBY
|
||||
config.root = "#{app_path}"
|
||||
config.frameworks << :action_foo
|
||||
RUBY
|
||||
|
||||
require "active_support/core_ext/load_error"
|
||||
|
||||
assert_raises MissingSourceFile do
|
||||
require "#{app_path}/config/environment"
|
||||
end
|
||||
end
|
||||
|
||||
test "eager loading loads parent classes before children" do
|
||||
app_file "lib/zoo.rb", <<-ZOO
|
||||
class Zoo ; include ReptileHouse ; end
|
||||
@ -180,19 +167,6 @@ module Zoo::ReptileHouse ; end
|
||||
|
||||
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
|
||||
add_to_config <<-RUBY
|
||||
config.root = "#{app_path}"
|
||||
config.frameworks -= [:action_view]
|
||||
RUBY
|
||||
require "#{app_path}/config/environment"
|
||||
|
||||
assert_equal nil, ActionMailer::Base.template_root
|
||||
assert_equal [], ActionController::Base.view_paths
|
||||
end
|
||||
|
||||
test "Rails.root should be a Pathname" do
|
||||
add_to_config <<-RUBY
|
||||
config.root = "#{app_path}"
|
||||
|
@ -1,6 +1,4 @@
|
||||
require "isolation/abstract_unit"
|
||||
# require "rails"
|
||||
# require 'action_dispatch'
|
||||
|
||||
module ApplicationTests
|
||||
class LoadTest < Test::Unit::TestCase
|
||||
|
@ -153,6 +153,14 @@ def controller(name, contents)
|
||||
app_file("app/controllers/#{name}_controller.rb", contents)
|
||||
end
|
||||
|
||||
def use_frameworks(arr)
|
||||
to_remove = [:actionmailer,
|
||||
:activemodel,
|
||||
:activerecord,
|
||||
:activeresource] - arr
|
||||
$:.reject! {|path| path =~ %r'/(#{to_remove.join('|')})/' }
|
||||
end
|
||||
|
||||
def boot_rails
|
||||
root = File.expand_path('../../../..', __FILE__)
|
||||
begin
|
||||
|
Loading…
Reference in New Issue
Block a user