Deprecate RAILS_ROOT in favor of Rails.root (which proxies to the application's object root)

This commit is contained in:
Carl Lerche 2009-10-16 12:49:39 -07:00
parent 6094e65169
commit 2110a524a4
40 changed files with 138 additions and 141 deletions

View File

@ -54,7 +54,7 @@ module ActionController
included do
# Set the default directory for helpers
extlib_inheritable_accessor(:helpers_dir) do
defined?(RAILS_ROOT) ? "#{RAILS_ROOT}/app/helpers" : "app/helpers"
defined?(Rails) ? "#{Rails.root}/app/helpers" : "app/helpers"
end
end

View File

@ -1,3 +1,5 @@
require "active_support/core_ext/load_error"
module ActiveSupport
module Testing
class ProxyTestResult

View File

@ -19,6 +19,7 @@ end
ARGV << "--help" if ARGV.empty?
require 'rails'
require 'rails/generators'
require 'rails/generators/rails/app/app_generator'

View File

@ -75,7 +75,7 @@ module Rails
protected
def rails_vendor_root
@rails_vendor_root ||= "#{RAILS_ROOT}/vendor/rails"
@rails_vendor_root ||= "#{Rails.root}/vendor/rails"
end
def git_info
@ -124,7 +124,7 @@ module Rails
# The application's location on the filesystem.
property 'Application root' do
File.expand_path(RAILS_ROOT)
File.expand_path(Rails.root)
end
# The current Rails environment (development, test, or production).

View File

@ -51,7 +51,7 @@ If you want to generate these guides locally, inside your application:
rake doc:guides
</ruby>
This will put the guides inside +RAILS_ROOT/doc/guides+ and you may start surfing straight away by opening +RAILS_ROOT/doc/guides/index.html+ in your favourite browser.
This will put the guides inside +Rails.root/doc/guides+ and you may start surfing straight away by opening +Rails.root/doc/guides/index.html+ in your favourite browser.
* Lead Contributors: "Rails Documentation Team":http://guides.rails.info/credits.html
* Major contributions from "Xavier Noria":http://advogato.org/person/fxn/diary.html and "Hongli Lai":http://izumi.plan99.net/blog/.

View File

@ -653,7 +653,7 @@ class ClientsController < ApplicationController
# Stream a file that has already been generated and stored on disk.
def download_pdf
client = Client.find(params[:id])
send_data("#{RAILS_ROOT}/files/clients/#{client.id}.pdf",
send_data("#{Rails.root}/files/clients/#{client.id}.pdf",
:filename => "#{client.name}.pdf",
:type => "application/pdf")
end

View File

@ -424,10 +424,10 @@ INFO: For a good rundown on generators, see "Understanding Generators":http://wi
Generators are code that generates code. Let's experiment by building one. Our generator will generate a text file.
The Rails generator by default looks in these places for available generators, where RAILS_ROOT is the root of your Rails application, like /home/foobar/commandsapp:
The Rails generator by default looks in these places for available generators, where Rails.root is the root of your Rails application, like /home/foobar/commandsapp:
* RAILS_ROOT/lib/generators
* RAILS_ROOT/vendor/generators
* Rails.root/lib/generators
* Rails.root/vendor/generators
* Inside any plugin with a directory like "generators" or "rails_generators"
* ~/.rails/generators
* Inside any Gem you have installed with a name ending in "_generator"
@ -465,7 +465,7 @@ We take whatever args are supplied, save them to an instance variable, and liter
* Check there's a *public* directory. You bet there is.
* Run the ERb template called "tutorial.erb".
* Save it into "RAILS_ROOT/public/tutorial.txt".
* Save it into "Rails.root/public/tutorial.txt".
* Pass in the arguments we saved through the +:assign+ parameter.
Next we'll build the template:

View File

@ -18,7 +18,7 @@ module Rails
def initialize
super
add_filter { |line| line.sub("#{RAILS_ROOT}/", '') }
add_filter { |line| line.sub("#{Rails.root}/", '') }
add_filter { |line| line.sub(ERB_METHOD_SIG, '') }
add_filter { |line| line.sub('./', '/') } # for tests
@ -37,7 +37,7 @@ module Rails
add_filter { |line| line.sub(/(#{path})\/gems\/([a-z]+)-([0-9.]+)\/(.*)/, '\2 (\3) \4')}
end
vendor_gems_path = Rails::GemDependency.unpacked_path.sub("#{RAILS_ROOT}/",'')
vendor_gems_path = Rails::GemDependency.unpacked_path.sub("#{Rails.root}/",'')
add_filter { |line| line.sub(/(#{vendor_gems_path})\/([a-z]+)-([0-9.]+)\/(.*)/, '\2 (\3) [v] \4')}
end
end

View File

@ -1,4 +1,3 @@
require "#{RAILS_ROOT}/config/application"
Rails.application.new
require 'rails/info'
puts Rails::Info

View File

@ -12,7 +12,7 @@ OptionParser.new do |opt|
end
libs = " -r irb/completion"
libs << %( -r "#{RAILS_ROOT}/config/environment")
libs << %( -r "#{Rails.root}/config/environment")
libs << " -r rails/console_app"
libs << " -r rails/console_sandbox" if options[:sandbox]
libs << " -r rails/console_with_helpers"

View File

@ -25,7 +25,7 @@ OptionParser.new do |opt|
end
env = ARGV.first || ENV['RAILS_ENV'] || 'development'
unless config = YAML::load(ERB.new(IO.read(RAILS_ROOT + "/config/database.yml")).result)[env]
unless config = YAML::load(ERB.new(IO.read(Rails.root + "/config/database.yml")).result)[env]
abort "No database is configured for the environment '#{env}'"
end

View File

@ -1,5 +1,4 @@
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'generators'))
require "#{RAILS_ROOT}/config/application"
if ARGV.size == 0
Rails::Generators.help

View File

@ -7,7 +7,7 @@ options = {
:Port => 3000,
:Host => "0.0.0.0",
:environment => (ENV['RAILS_ENV'] || "development").dup,
:config => RAILS_ROOT + "/config.ru",
:config => Rails.root + "/config.ru",
:detach => false,
:debugger => false
}
@ -46,7 +46,7 @@ puts "=> Rails #{Rails.version} application starting on http://#{options[:Host]}
if options[:detach]
Process.daemon
pid = "#{RAILS_ROOT}/tmp/pids/server.pid"
pid = "#{Rails.root}/tmp/pids/server.pid"
File.open(pid, 'w'){ |f| f.write(Process.pid) }
at_exit { File.delete(pid) if File.exist?(pid) }
end

View File

@ -34,37 +34,25 @@ module Rails
def root
@root ||= begin
if defined?(RAILS_ROOT)
root = RAILS_ROOT
else
call_stack = caller.map { |p| p.split(':').first }
root_path = call_stack.detect { |p| p !~ %r[railties/lib/rails] }
root_path = File.dirname(root_path)
call_stack = caller.map { |p| p.split(':').first }
root_path = call_stack.detect { |p| p !~ %r[railties/lib/rails] }
root_path = File.dirname(root_path)
while root_path && File.directory?(root_path) && !File.exist?("#{root_path}/config.ru")
parent = File.dirname(root_path)
root_path = parent != root_path && parent
end
Object.class_eval("RAILS_ROOT = ''")
root = File.exist?("#{root_path}/config.ru") ? root_path : Dir.pwd
while root_path && File.directory?(root_path) && !File.exist?("#{root_path}/config.ru")
parent = File.dirname(root_path)
root_path = parent != root_path && parent
end
root = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ?
Pathname.new(root).expand_path.to_s :
Pathname.new(root).realpath.to_s
root = File.exist?("#{root_path}/config.ru") ? root_path : Dir.pwd
# TODO: Remove RAILS_ROOT
RAILS_ROOT.replace(root)
root
RUBY_PLATFORM =~ /(:?mswin|mingw)/ ?
Pathname.new(root).expand_path :
Pathname.new(root).realpath
end
end
def root=(root)
Object.class_eval("RAILS_ROOT = ''") unless defined?(RAILS_ROOT)
RAILS_ROOT.replace(root)
@root = root
@root = Pathname.new(root).expand_path
end
def paths

View File

@ -6,7 +6,7 @@ module Rails
# TODO: w0t?
class << self
def application
@@application
@@application ||= nil
end
def application=(application)
@ -43,7 +43,7 @@ module Rails
end
def root
Pathname.new(RAILS_ROOT) if defined?(RAILS_ROOT)
application && application.config.root
end
def env

View File

@ -0,0 +1,17 @@
require "active_support/string_inquirer"
require "active_support/deprecation"
RAILS_ROOT = (Class.new(ActiveSupport::Deprecation::DeprecationProxy) do
def target
Rails.root
end
def replace(val)
puts OMG
end
def warn(callstack, called, args)
msg = "RAILS_ROOT is deprecated! Use Rails.root instead."
ActiveSupport::Deprecation.warn(msg, callstack)
end
end).new

View File

@ -11,7 +11,7 @@ module Rails
attr_accessor :lib, :source, :dep
def self.unpacked_path
@unpacked_path ||= File.join(RAILS_ROOT, 'vendor', 'gems')
@unpacked_path ||= File.join(Rails.root, 'vendor', 'gems')
end
@@framework_gems = {}

View File

@ -92,8 +92,8 @@ module Rails
generator_path = File.join(spec.full_gem_path, "lib/generators")
paths << generator_path if File.exist?(generator_path)
end
elsif defined?(RAILS_ROOT)
paths += Dir[File.join(RAILS_ROOT, "vendor", "gems", "gems", "*", "lib", "generators")]
elsif defined?(Rails.root)
paths += Dir[File.join(Rails.root, "vendor", "gems", "gems", "*", "lib", "generators")]
end
paths
@ -102,8 +102,8 @@ module Rails
# Load paths from plugin.
#
def self.plugins_generators_paths
return [] unless defined?(RAILS_ROOT)
Dir[File.join(RAILS_ROOT, "vendor", "plugins", "*", "lib", "generators")]
return [] unless Rails.root
Dir[File.join(Rails.root, "vendor", "plugins", "*", "lib", "generators")]
end
# Hold configured generators fallbacks. If a plugin developer wants a
@ -143,7 +143,7 @@ module Rails
def self.load_paths
@load_paths ||= begin
paths = []
paths << File.join(RAILS_ROOT, "lib", "generators") if defined?(RAILS_ROOT)
paths << File.join(Rails.root, "lib", "generators") if Rails.root
paths << File.join(Thor::Util.user_home, ".rails", "generators")
paths += self.plugins_generators_paths
paths += self.gems_generators_paths

View File

@ -203,8 +203,8 @@ module Rails
super
base.source_root # Cache source root
if defined?(RAILS_ROOT) && base.name !~ /Base$/
path = File.expand_path(File.join(RAILS_ROOT, 'lib', 'templates'))
if Rails.root && base.name !~ /Base$/
path = File.expand_path(File.join(Rails.root, 'lib', 'templates'))
if base.name.include?('::')
base.source_paths << File.join(path, base.base_name, base.generator_name)
else

View File

@ -7,7 +7,7 @@ Rails::Initializer.run do |config|
# -- all .rb files in that directory are automatically loaded.
# Add additional load paths for your own custom dirs
# config.load_paths += %W( #{RAILS_ROOT}/extras )
# config.load_paths += %W( #{root}/extras )
# Specify gems that this application depends on and have them installed with rake gems:install
# config.gem "bj"

View File

@ -1,8 +1,6 @@
# Don't change this file!
# Configure your app in config/environment.rb and config/environments/*.rb
RAILS_ROOT = "#{File.dirname(__FILE__)}/.." unless defined?(RAILS_ROOT)
module Rails
# Mark the version of Rails that generated the boot.rb file. This is
# a temporary solution and will most likely be removed as Rails 3.0
@ -70,7 +68,6 @@ module Rails
def load_initializer
require "rails"
install_gem_spec_stubs
Rails::GemDependency.add_frozen_gem_path
end
def install_gem_spec_stubs

View File

@ -9,6 +9,7 @@ require 'rails/rack'
require 'rails/paths'
require 'rails/core'
require 'rails/configuration'
require 'rails/deprecation'
RAILS_ENV = (ENV['RAILS_ENV'] || 'development').dup unless defined?(RAILS_ENV)

View File

@ -147,7 +147,7 @@ module Rails
end
def application_lib_index
$LOAD_PATH.index(File.join(RAILS_ROOT, 'lib')) || 0
$LOAD_PATH.index(File.join(Rails.root, 'lib')) || 0
end
def enabled?(plugin)

View File

@ -20,5 +20,5 @@ end
# Load any custom rakefile extensions
# TODO: Don't hardcode these paths.
Dir["#{RAILS_ROOT}/vendor/plugins/*/**/tasks/**/*.rake"].sort.each { |ext| load ext }
Dir["#{RAILS_ROOT}/lib/tasks/**/*.rake"].sort.each { |ext| load ext }
Dir["#{Rails.root}/vendor/plugins/*/**/tasks/**/*.rake"].sort.each { |ext| load ext }
Dir["#{Rails.root}/lib/tasks/**/*.rake"].sort.each { |ext| load ext }

View File

@ -283,7 +283,7 @@ namespace :db do
desc "Create a db/schema.rb file that can be portably used against any DB supported by AR"
task :dump => :environment do
require 'active_record/schema_dumper'
File.open(ENV['SCHEMA'] || "#{RAILS_ROOT}/db/schema.rb", "w") do |file|
File.open(ENV['SCHEMA'] || "#{Rails.root}/db/schema.rb", "w") do |file|
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)
end
Rake::Task["db:schema:dump"].reenable
@ -291,11 +291,11 @@ namespace :db do
desc "Load a schema.rb file into the database"
task :load => :environment do
file = ENV['SCHEMA'] || "#{RAILS_ROOT}/db/schema.rb"
file = ENV['SCHEMA'] || "#{Rails.root}/db/schema.rb"
if File.exists?(file)
load(file)
else
abort %{#{file} doesn't exist yet. Run "rake db:migrate" to create it then try again. If you do not intend to use a database, you should instead alter #{RAILS_ROOT}/config/environment.rb to prevent active_record from loading: config.frameworks -= [ :active_record ]}
abort %{#{file} doesn't exist yet. Run "rake db:migrate" to create it then try again. If you do not intend to use a database, you should instead alter #{Rails.root}/config/environment.rb to prevent active_record from loading: config.frameworks -= [ :active_record ]}
end
end
end
@ -307,7 +307,7 @@ namespace :db do
case abcs[RAILS_ENV]["adapter"]
when "mysql", "oci", "oracle"
ActiveRecord::Base.establish_connection(abcs[RAILS_ENV])
File.open("#{RAILS_ROOT}/db/#{RAILS_ENV}_structure.sql", "w+") { |f| f << ActiveRecord::Base.connection.structure_dump }
File.open("#{Rails.root}/db/#{RAILS_ENV}_structure.sql", "w+") { |f| f << ActiveRecord::Base.connection.structure_dump }
when "postgresql"
ENV['PGHOST'] = abcs[RAILS_ENV]["host"] if abcs[RAILS_ENV]["host"]
ENV['PGPORT'] = abcs[RAILS_ENV]["port"].to_s if abcs[RAILS_ENV]["port"]
@ -327,13 +327,13 @@ namespace :db do
when "firebird"
set_firebird_env(abcs[RAILS_ENV])
db_string = firebird_db_string(abcs[RAILS_ENV])
sh "isql -a #{db_string} > #{RAILS_ROOT}/db/#{RAILS_ENV}_structure.sql"
sh "isql -a #{db_string} > #{Rails.root}/db/#{RAILS_ENV}_structure.sql"
else
raise "Task not supported by '#{abcs["test"]["adapter"]}'"
end
if ActiveRecord::Base.connection.supports_migrations?
File.open("#{RAILS_ROOT}/db/#{RAILS_ENV}_structure.sql", "a") { |f| f << ActiveRecord::Base.connection.dump_schema_information }
File.open("#{Rails.root}/db/#{RAILS_ENV}_structure.sql", "a") { |f| f << ActiveRecord::Base.connection.dump_schema_information }
end
end
end
@ -356,28 +356,28 @@ namespace :db do
when "mysql"
ActiveRecord::Base.establish_connection(:test)
ActiveRecord::Base.connection.execute('SET foreign_key_checks = 0')
IO.readlines("#{RAILS_ROOT}/db/#{RAILS_ENV}_structure.sql").join.split("\n\n").each do |table|
IO.readlines("#{Rails.root}/db/#{RAILS_ENV}_structure.sql").join.split("\n\n").each do |table|
ActiveRecord::Base.connection.execute(table)
end
when "postgresql"
ENV['PGHOST'] = abcs["test"]["host"] if abcs["test"]["host"]
ENV['PGPORT'] = abcs["test"]["port"].to_s if abcs["test"]["port"]
ENV['PGPASSWORD'] = abcs["test"]["password"].to_s if abcs["test"]["password"]
`psql -U "#{abcs["test"]["username"]}" -f #{RAILS_ROOT}/db/#{RAILS_ENV}_structure.sql #{abcs["test"]["database"]}`
`psql -U "#{abcs["test"]["username"]}" -f #{Rails.root}/db/#{RAILS_ENV}_structure.sql #{abcs["test"]["database"]}`
when "sqlite", "sqlite3"
dbfile = abcs["test"]["database"] || abcs["test"]["dbfile"]
`#{abcs["test"]["adapter"]} #{dbfile} < #{RAILS_ROOT}/db/#{RAILS_ENV}_structure.sql`
`#{abcs["test"]["adapter"]} #{dbfile} < #{Rails.root}/db/#{RAILS_ENV}_structure.sql`
when "sqlserver"
`osql -E -S #{abcs["test"]["host"]} -d #{abcs["test"]["database"]} -i db\\#{RAILS_ENV}_structure.sql`
when "oci", "oracle"
ActiveRecord::Base.establish_connection(:test)
IO.readlines("#{RAILS_ROOT}/db/#{RAILS_ENV}_structure.sql").join.split(";\n\n").each do |ddl|
IO.readlines("#{Rails.root}/db/#{RAILS_ENV}_structure.sql").join.split(";\n\n").each do |ddl|
ActiveRecord::Base.connection.execute(ddl)
end
when "firebird"
set_firebird_env(abcs["test"])
db_string = firebird_db_string(abcs["test"])
sh "isql -i #{RAILS_ROOT}/db/#{RAILS_ENV}_structure.sql #{db_string}"
sh "isql -i #{Rails.root}/db/#{RAILS_ENV}_structure.sql #{db_string}"
else
raise "Task not supported by '#{abcs["test"]["adapter"]}'"
end
@ -446,7 +446,7 @@ def drop_database(config)
when /^sqlite/
require 'pathname'
path = Pathname.new(config['database'])
file = path.absolute? ? path.to_s : File.join(RAILS_ROOT, path)
file = path.absolute? ? path.to_s : File.join(Rails.root, path)
FileUtils.rm(file)
when 'postgresql'

View File

@ -86,7 +86,7 @@ namespace :rails do
template = File.expand_path(template) if template !~ %r{\A[A-Za-z][A-Za-z0-9+\-\.]*://}
require 'generators'
generator = Rails::Generators::App.new [ RAILS_ROOT ], {}, :destination_root => RAILS_ROOT
generator = Rails::Generators::App.new [ Rails.root ], {}, :destination_root => Rails.root
generator.apply template, :verbose => false
end
@ -96,7 +96,7 @@ namespace :rails do
require 'rails/generators/rails/app/app_generator'
generator = Rails::Generators::AppGenerator.new ["rails"], { :with_dispatchers => true },
:destination_root => RAILS_ROOT
:destination_root => Rails.root
generator.invoke(method)
end
@ -117,8 +117,8 @@ namespace :rails do
desc "Rename application.rb to application_controller.rb"
task :application_controller do
old_style = RAILS_ROOT + '/app/controllers/application.rb'
new_style = RAILS_ROOT + '/app/controllers/application_controller.rb'
old_style = Rails.root + '/app/controllers/application.rb'
new_style = Rails.root + '/app/controllers/application_controller.rb'
if File.exists?(old_style) && !File.exists?(new_style)
FileUtils.mv(old_style, new_style)
puts "#{old_style} has been renamed to #{new_style}, update your SCM as necessary"

View File

@ -61,7 +61,7 @@ def current_gems
end
def frozen_gems(load_specs=true)
Dir[File.join(RAILS_ROOT, 'vendor', 'gems', '*-*')].map do |gem_dir|
Dir[File.join(Rails.root, 'vendor', 'gems', '*-*')].map do |gem_dir|
Rails::GemDependency.from_directory_name(gem_dir, load_specs)
end
end

View File

@ -1,7 +1,7 @@
task :default => :test
task :environment do
$rails_rake_task = true
require(File.join(RAILS_ROOT, 'config', 'environment'))
require(File.join(Rails.root, 'config', 'environment'))
end
task :rails_env do

View File

@ -17,7 +17,7 @@ if defined?(ActiveRecord)
class ActiveSupport::TestCase
include ActiveRecord::TestFixtures
self.fixture_path = "#{RAILS_ROOT}/test/fixtures/"
self.fixture_path = "#{Rails.root}/test/fixtures/"
self.use_instantiated_fixtures = false
self.use_transactional_fixtures = true
end

View File

@ -14,15 +14,15 @@ $:.unshift File.dirname(__FILE__) + "/../builtin/rails_info"
require 'stringio'
require 'test/unit'
require 'fileutils'
require 'active_support'
require 'active_support/core_ext/logger'
require 'active_support/test_case'
require 'action_controller'
require 'rails'
if defined?(RAILS_ROOT)
RAILS_ROOT.replace File.dirname(__FILE__)
else
RAILS_ROOT = File.dirname(__FILE__)
end
Rails::Initializer.run do |config|
config.root = File.dirname(__FILE__)
end

View File

@ -12,7 +12,7 @@ module ApplicationTests
test "the application root is set correctly" do
require "#{app_path}/config/environment"
assert_equal app_path, Rails.application.root
assert_equal Pathname.new(app_path), Rails.application.root
end
test "the application root can be set" do
@ -22,7 +22,7 @@ module ApplicationTests
config.root = '#{app_path}/hello'
RUBY
require "#{app_path}/config/environment"
assert_equal "#{app_path}/hello", Rails.application.root
assert_equal Pathname.new("#{app_path}/hello"), Rails.application.root
end
test "the application root is detected as where config.ru is located" do
@ -31,7 +31,7 @@ module ApplicationTests
RUBY
FileUtils.mv "#{app_path}/config.ru", "#{app_path}/config/config.ru"
require "#{app_path}/config/environment"
assert_equal "#{app_path}/config", Rails.application.root
assert_equal Pathname.new("#{app_path}/config"), Rails.application.root
end
test "the application root is Dir.pwd if there is no config.ru" do
@ -42,7 +42,7 @@ module ApplicationTests
Dir.chdir("#{app_path}/app") do
require "#{app_path}/config/environment"
assert_equal "#{app_path}/app", Rails.application.root
assert_equal Pathname.new("#{app_path}/app"), Rails.application.root
end
end
end

View File

@ -37,6 +37,7 @@ module ApplicationTests
test "generators aliases and options on initialization" do
Rails::Initializer.run do |c|
c.frameworks = []
c.generators.rails :aliases => { :test_framework => "-w" }
c.generators.orm :datamapper
c.generators.test_framework :rspec
@ -50,6 +51,7 @@ module ApplicationTests
test "generators no color on initialization" do
Rails::Initializer.run do |c|
c.frameworks = []
c.generators.colorize_logging = false
end
# Initialize the application

View File

@ -10,33 +10,38 @@ module ApplicationTests
end
test "initializing an application initializes rails" do
class MyApp < Rails::Application ; end
Rails::Initializer.run do |config|
config.root = app_path
end
if RUBY_VERSION < '1.9'
$KCODE = ''
MyApp.new
Rails.application.new
assert_equal 'UTF8', $KCODE
else
Encoding.default_external = Encoding::US_ASCII
MyApp.new
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
class MyApp < Rails::Application ; end
Rails::Initializer.run do |config|
config.root = app_path
end
MyApp.new
Rails.application.new
assert $:.include?("#{app_path}/app/models")
end
test "adding an unknown framework raises an error" do
class MyApp < Rails::Application
Rails::Initializer.run do |config|
config.root = app_path
config.frameworks << :action_foo
end
assert_raises RuntimeError do
MyApp.new
Rails.application.new
end
end
@ -49,6 +54,7 @@ module ApplicationTests
ZOO
Rails::Initializer.run do |config|
config.root = app_path
config.eager_load_paths = "#{app_path}/lib"
end
@ -60,7 +66,7 @@ module ApplicationTests
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 { }
Rails::Initializer.run { |config| config.root = app_path }
Rails.application.new
assert_equal "success", $initialize_test_set_from_env
end
@ -68,6 +74,7 @@ module ApplicationTests
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
@ -76,6 +83,7 @@ module ApplicationTests
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
@ -85,6 +93,7 @@ module ApplicationTests
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
@ -94,6 +103,7 @@ module ApplicationTests
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
@ -105,6 +115,7 @@ module ApplicationTests
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" }
@ -118,6 +129,7 @@ module ApplicationTests
# 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
@ -128,18 +140,21 @@ module ApplicationTests
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
@ -151,6 +166,7 @@ module ApplicationTests
# 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
@ -160,6 +176,7 @@ module ApplicationTests
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
@ -167,6 +184,7 @@ module ApplicationTests
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
@ -178,6 +196,7 @@ module ApplicationTests
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
@ -189,6 +208,7 @@ module ApplicationTests
# 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
@ -197,12 +217,11 @@ module ApplicationTests
assert_equal [], ActionController::Base.view_paths
end
# Rails root test
test "Rails.root == RAILS_ROOT" do
assert_equal RAILS_ROOT, Rails.root.to_s
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

View File

@ -17,7 +17,7 @@ module ApplicationTests
end
test "all plugins are loaded when registered plugin list is untouched" do
Rails::Initializer.run { }
Rails::Initializer.run { |c| c.root = app_path }
Rails.application.new
assert_plugins [
:a, :acts_as_chunky_bacon, :engine, :gemlike, :plugin_with_no_lib_dir, :stubby
@ -25,19 +25,20 @@ module ApplicationTests
end
test "no plugins are loaded if the configuration has an empty plugin list" do
Rails::Initializer.run { |c| c.plugins = [] }
Rails::Initializer.run { |c| c.root = app_path; c.plugins = [] }
assert_plugins [], Rails.application.config.loaded_plugins
end
test "only the specified plugins are located in the order listed" do
plugin_names = [:plugin_with_no_lib_dir, :acts_as_chunky_bacon]
Rails::Initializer.run { |c| c.plugins = plugin_names }
Rails::Initializer.run { |c| c.root = app_path; c.plugins = plugin_names }
Rails.application.new
assert_plugins plugin_names, Rails.application.config.loaded_plugins
end
test "all plugins loaded after all" do
Rails::Initializer.run do |config|
config.root = app_path
config.plugins = [:stubby, :all, :acts_as_chunky_bacon]
end
Rails.application.new
@ -47,6 +48,7 @@ module ApplicationTests
test "plugin names may be strings" do
plugin_names = ['stubby', 'acts_as_chunky_bacon', :a, :plugin_with_no_lib_dir]
Rails::Initializer.run do |config|
config.root = app_path
config.plugins = ['stubby', 'acts_as_chunky_bacon', :a, :plugin_with_no_lib_dir]
end
Rails.application.new
@ -56,6 +58,7 @@ module ApplicationTests
test "all plugins loaded when all is used" do
Rails::Initializer.run do |config|
config.root = app_path
config.plugins = [:stubby, :acts_as_chunky_bacon, :all]
end
Rails.application.new
@ -65,6 +68,7 @@ module ApplicationTests
test "all loaded plugins are added to the load paths" do
Rails::Initializer.run do |config|
config.root = app_path
config.plugins = [:stubby, :acts_as_chunky_bacon]
end
Rails.application.new
@ -75,6 +79,7 @@ module ApplicationTests
test "registering a plugin name that does not exist raises a load error" do
Rails::Initializer.run do |config|
config.root = app_path
config.plugins = [:stubby, :acts_as_a_non_existant_plugin]
end
@ -89,6 +94,7 @@ module ApplicationTests
begin
Rails::Initializer.run do |config|
config.root = app_path
config.plugins = [:stubby, :acts_as_chunky_bacon, :non_existant_plugin1, :non_existant_plugin2]
end
Rails.application.new

View File

@ -1,6 +1,4 @@
require 'abstract_unit'
require 'rails/initializer'
require 'rails/backtrace_cleaner'
if defined? Test::Unit::Util::BacktraceFilter

View File

@ -57,7 +57,6 @@ class VendorBootTest < Test::Unit::TestCase
boot = VendorBoot.new
boot.expects(:require).with("rails")
boot.expects(:install_gem_spec_stubs)
Rails::GemDependency.expects(:add_frozen_gem_path)
boot.load_initializer
end
end

View File

@ -1,24 +1,10 @@
require 'test/unit'
require 'fileutils'
fixtures = File.expand_path(File.join(File.dirname(__FILE__), '..', 'fixtures'))
if defined?(RAILS_ROOT)
RAILS_ROOT.replace fixtures
else
RAILS_ROOT = fixtures
end
$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../../../activemodel/lib"
$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../../../activerecord/lib"
$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../../../actionpack/lib"
$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../../lib"
# TODO: Fix this RAILS_ENV stuff
RAILS_ENV = 'test'
require "rails/core"
require 'abstract_unit'
Rails.application.config.root = File.expand_path(File.join(File.dirname(__FILE__), '..', 'fixtures'))
require 'rails/generators'
require 'rubygems'
require 'active_record'
require 'action_dispatch'

View File

@ -12,6 +12,7 @@ module InitializerTests
# test_config_defaults_and_settings_should_be_added_to_i18n_defaults
test "i18n config defaults and settings should be added to i18n defaults" do
Rails::Initializer.run do |c|
c.root = app_path
c.i18n.load_path << "my/other/locale.yml"
end
Rails.application.new
@ -34,6 +35,7 @@ module InitializerTests
app_file "vendor/plugins/engine/config/locales/en.yml", "hello:"
Rails::Initializer.run do |c|
c.root = app_path
c.i18n.load_path << "my/other/locale.yml"
end
Rails.application.new

View File

@ -7,6 +7,7 @@ class PathsTest < Test::Unit::TestCase
build_app
boot_rails
Rails::Initializer.run do |config|
config.root = app_path
config.frameworks = [:action_controller, :action_view, :action_mailer, :active_record]
config.after_initialize do
ActionController::Base.session_store = nil

View File

@ -1,20 +0,0 @@
require 'abstract_unit'
require 'rails/initializer'
require 'rails/generators'
require 'action_view'
require 'action_mailer'
require 'active_record'
require 'plugin_test_helper'
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