提交 4ae79367 编写于 作者: J José Valim

Got tests working once again.

上级 02c5137e
...@@ -2,8 +2,6 @@ ...@@ -2,8 +2,6 @@
module Rails module Rails
class Application < Engine class Application < Engine
include Initializable
class << self class << self
alias :configure :class_eval alias :configure :class_eval
delegate :initialize!, :load_tasks, :load_generators, :root, :to => :instance delegate :initialize!, :load_tasks, :load_generators, :root, :to => :instance
...@@ -122,7 +120,7 @@ def call(env) ...@@ -122,7 +120,7 @@ def call(env)
app.call(env) app.call(env)
end end
initializer :add_builtin_route do |app| initializer :add_builtin_route, :before => :build_middleware_stack do |app|
if Rails.env.development? if Rails.env.development?
app.route_configuration_files << File.join(RAILTIES_PATH, 'builtin', 'routes.rb') app.route_configuration_files << File.join(RAILTIES_PATH, 'builtin', 'routes.rb')
end end
......
...@@ -81,15 +81,13 @@ def initialize(root) ...@@ -81,15 +81,13 @@ def initialize(root)
def paths def paths
@paths ||= begin @paths ||= begin
paths = Rails::Application::Root.new(@root) paths = Rails::Application::Root.new(@root)
paths.app "app", :load_path => true paths.app "app", :eager_load => true, :glob => "*"
paths.app_glob "app/*", :load_path => true, :eager_load => true paths.app.controllers "app/controllers", :eager_load => true
paths.app.controllers "app/controllers", :eager_load => true paths.app.metals "app/metal", :eager_load => true
paths.app.metals "app/metal"
paths.app.views "app/views" paths.app.views "app/views"
paths.lib "lib", :load_path => true paths.lib "lib", :load_path => true
paths.config "config" paths.config "config"
paths.config.environment "config/environments/#{Rails.env}.rb" paths.config.environment "config/environments", :glob => "#{Rails.env}.rb"
paths.config.environments "config/environments", :glob => "#{Rails.env}.rb"
paths.config.initializers "config/initializers" paths.config.initializers "config/initializers"
paths.config.locales "config/locales" paths.config.locales "config/locales"
paths.config.routes "config/routes.rb" paths.config.routes "config/routes.rb"
...@@ -112,10 +110,6 @@ def load_once_paths ...@@ -112,10 +110,6 @@ def load_once_paths
def load_paths def load_paths
@load_paths ||= paths.load_paths @load_paths ||= paths.load_paths
end end
def controller_paths
paths.app.controllers.to_a.uniq
end
end end
class Configuration < Engine::Configuration class Configuration < Engine::Configuration
...@@ -142,7 +136,7 @@ def after_initialize(&blk) ...@@ -142,7 +136,7 @@ def after_initialize(&blk)
def paths def paths
@paths ||= begin @paths ||= begin
paths = super paths = super
paths.app.controllers << builtin_directories paths.app.controllers.concat(builtin_directories)
paths.config.database "config/database.yml" paths.config.database "config/database.yml"
paths.log "log/#{Rails.env}.log" paths.log "log/#{Rails.env}.log"
paths.tmp "tmp" paths.tmp "tmp"
...@@ -238,6 +232,18 @@ def log_path ...@@ -238,6 +232,18 @@ def log_path
paths.config.log.to_a.first paths.config.log.to_a.first
end end
def controller_paths=(value)
ActiveSupport::Deprecation.warn "config.controller_paths= is deprecated, " <<
"please do config.paths.app.controllers= instead", caller
paths.app.controllers = value
end
def controller_paths
ActiveSupport::Deprecation.warn "config.controller_paths is deprecated, " <<
"please do config.paths.app.controllers instead", caller
paths.app.controllers.to_a.uniq
end
def cache_store def cache_store
@cache_store ||= begin @cache_store ||= begin
if File.exist?("#{root}/tmp/cache/") if File.exist?("#{root}/tmp/cache/")
......
...@@ -50,7 +50,9 @@ def find_root_with_file_flag(flag, default=nil) ...@@ -50,7 +50,9 @@ def find_root_with_file_flag(flag, default=nil)
# Add configured load paths to ruby load paths and remove duplicates. # Add configured load paths to ruby load paths and remove duplicates.
initializer :set_load_path, :before => :container do initializer :set_load_path, :before => :container do
config.paths.add_to_load_path expand_load_path(config.load_paths).reverse_each do |path|
$LOAD_PATH.unshift(path) if File.directory?(path)
end
$LOAD_PATH.uniq! $LOAD_PATH.uniq!
end end
......
...@@ -39,6 +39,7 @@ def eager_load ...@@ -39,6 +39,7 @@ def eager_load
all_paths.map { |path| path.paths if path.eager_load? }.compact.flatten.uniq all_paths.map { |path| path.paths if path.eager_load? }.compact.flatten.uniq
end end
# TODO Discover why do we need to call uniq! here
def all_paths def all_paths
@all_paths.uniq! @all_paths.uniq!
@all_paths @all_paths
...@@ -48,12 +49,6 @@ def load_paths ...@@ -48,12 +49,6 @@ def load_paths
all_paths.map { |path| path.paths if path.load_path? }.compact.flatten.uniq all_paths.map { |path| path.paths if path.load_path? }.compact.flatten.uniq
end end
def add_to_load_path
load_paths.reverse_each do |path|
$LOAD_PATH.unshift(path) if File.directory?(path)
end
end
def push(*) def push(*)
raise "Application root can only have one physical path" raise "Application root can only have one physical path"
end end
...@@ -74,7 +69,7 @@ def initialize(root, *paths) ...@@ -74,7 +69,7 @@ def initialize(root, *paths)
@children = {} @children = {}
@root = root @root = root
@paths = paths.flatten @paths = paths.flatten
@glob = @options[:glob] || "**/*.rb" @glob = @options.delete(:glob)
@load_once = @options[:load_once] @load_once = @options[:load_once]
@eager_load = @options[:eager_load] @eager_load = @options[:eager_load]
...@@ -128,10 +123,13 @@ def load_path? ...@@ -128,10 +123,13 @@ def load_path?
def paths def paths
raise "You need to set a path root" unless @root.path raise "You need to set a path root" unless @root.path
result = @paths.map do |p|
@paths.map do |path| path = File.expand_path(p, @root.path)
path.index('/') == 0 ? path : File.expand_path(File.join(@root.path, path)) @glob ? Dir[File.join(path, @glob)] : path
end end
result.flatten!
result.uniq!
result
end end
alias to_a paths alias to_a paths
......
...@@ -34,9 +34,9 @@ def setup ...@@ -34,9 +34,9 @@ def setup
add_to_config <<-RUBY add_to_config <<-RUBY
config.root = '#{new_app}' config.root = '#{new_app}'
RUBY RUBY
use_frameworks [] use_frameworks []
require "#{app_path}/config/environment" require "#{app_path}/config/environment"
assert_equal Pathname.new(new_app), Rails.application.root assert_equal Pathname.new(new_app), Rails.application.root
end end
......
...@@ -8,6 +8,7 @@ def setup ...@@ -8,6 +8,7 @@ def setup
build_app build_app
boot_rails boot_rails
FileUtils.rm_rf("#{app_path}/config/environments") FileUtils.rm_rf("#{app_path}/config/environments")
app_file "config/environments/development.rb", ""
add_to_config <<-RUBY add_to_config <<-RUBY
config.root = "#{app_path}" config.root = "#{app_path}"
config.after_initialize do config.after_initialize do
...@@ -36,11 +37,8 @@ def assert_not_in_load_path(*path) ...@@ -36,11 +37,8 @@ def assert_not_in_load_path(*path)
end end
test "booting up Rails yields a valid paths object" do test "booting up Rails yields a valid paths object" do
assert_path @paths.app, "app"
assert_path @paths.app.metals, "app", "metal" assert_path @paths.app.metals, "app", "metal"
assert_path @paths.app.models, "app", "models" assert_path @paths.app.views, "app", "views"
assert_path @paths.app.helpers, "app", "helpers"
assert_path @paths.app.services, "app", "services"
assert_path @paths.lib, "lib" assert_path @paths.lib, "lib"
assert_path @paths.vendor, "vendor" assert_path @paths.vendor, "vendor"
assert_path @paths.vendor.plugins, "vendor", "plugins" assert_path @paths.vendor.plugins, "vendor", "plugins"
...@@ -48,7 +46,7 @@ def assert_not_in_load_path(*path) ...@@ -48,7 +46,7 @@ def assert_not_in_load_path(*path)
assert_path @paths.tmp.cache, "tmp", "cache" assert_path @paths.tmp.cache, "tmp", "cache"
assert_path @paths.config, "config" assert_path @paths.config, "config"
assert_path @paths.config.locales, "config", "locales" assert_path @paths.config.locales, "config", "locales"
assert_path @paths.config.environments, "config", "environments" assert_path @paths.config.environment, "config", "environments", "development.rb"
assert_equal root("app", "controllers"), @paths.app.controllers.to_a.first assert_equal root("app", "controllers"), @paths.app.controllers.to_a.first
assert_equal Pathname.new(File.dirname(__FILE__)).join("..", "..", "builtin", "rails_info").expand_path, assert_equal Pathname.new(File.dirname(__FILE__)).join("..", "..", "builtin", "rails_info").expand_path,
...@@ -56,27 +54,22 @@ def assert_not_in_load_path(*path) ...@@ -56,27 +54,22 @@ def assert_not_in_load_path(*path)
end end
test "booting up Rails yields a list of paths that are eager" do test "booting up Rails yields a list of paths that are eager" do
assert @paths.app.models.eager_load? assert @paths.app.eager_load?
assert @paths.app.controllers.eager_load? assert @paths.app.controllers.eager_load?
assert @paths.app.helpers.eager_load?
assert @paths.app.metals.eager_load? assert @paths.app.metals.eager_load?
end end
test "environments has a glob equal to the current environment" do test "environments has a glob equal to the current environment" do
assert_equal "#{Rails.env}.rb", @paths.config.environments.glob assert_equal "#{Rails.env}.rb", @paths.config.environment.glob
end end
test "load path includes each of the paths in config.paths as long as the directories exist" do test "load path includes each of the paths in config.paths as long as the directories exist" do
assert_in_load_path "app"
assert_in_load_path "app", "controllers" assert_in_load_path "app", "controllers"
assert_in_load_path "app", "models" assert_in_load_path "app", "models"
assert_in_load_path "app", "helpers" assert_in_load_path "app", "helpers"
assert_in_load_path "lib" assert_in_load_path "lib"
assert_in_load_path "vendor" assert_in_load_path "vendor"
assert_not_in_load_path "app", "views"
assert_not_in_load_path "app", "metal"
assert_not_in_load_path "app", "services"
assert_not_in_load_path "config" assert_not_in_load_path "config"
assert_not_in_load_path "config", "locales" assert_not_in_load_path "config", "locales"
assert_not_in_load_path "config", "environments" assert_not_in_load_path "config", "environments"
...@@ -86,17 +79,17 @@ def assert_not_in_load_path(*path) ...@@ -86,17 +79,17 @@ def assert_not_in_load_path(*path)
test "controller paths include builtin in development mode" do test "controller paths include builtin in development mode" do
Rails.env.replace "development" Rails.env.replace "development"
assert Rails::Configuration.new.paths.app.controllers.paths.any? { |p| p =~ /builtin/ } assert Rails::Configuration.new("/").paths.app.controllers.paths.any? { |p| p =~ /builtin/ }
end end
test "controller paths does not have builtin_directories in test mode" do test "controller paths does not have builtin_directories in test mode" do
Rails.env.replace "test" Rails.env.replace "test"
assert !Rails::Configuration.new.paths.app.controllers.paths.any? { |p| p =~ /builtin/ } assert !Rails::Configuration.new("/").paths.app.controllers.paths.any? { |p| p =~ /builtin/ }
end end
test "controller paths does not have builtin_directories in production mode" do test "controller paths does not have builtin_directories in production mode" do
Rails.env.replace "production" Rails.env.replace "production"
assert !Rails::Configuration.new.paths.app.controllers.paths.any? { |p| p =~ /builtin/ } assert !Rails::Configuration.new("/").paths.app.controllers.paths.any? { |p| p =~ /builtin/ }
end end
end end
......
...@@ -193,12 +193,7 @@ def setup ...@@ -193,12 +193,7 @@ def setup
assert_equal 2, @root.eager_load.size assert_equal 2, @root.eager_load.size
end end
test "a path should have a glob that defaults to **/*.rb" do test "it should be possible to add a path's default glob" do
@root.app = "/app"
assert_equal "**/*.rb", @root.app.glob
end
test "it should be possible to override a path's default glob" do
@root.app = "/app" @root.app = "/app"
@root.app.glob = "*.rb" @root.app.glob = "*.rb"
assert_equal "*.rb", @root.app.glob assert_equal "*.rb", @root.app.glob
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册