Skip to content

Commit

Permalink
Got tests working once again.
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Jan 22, 2010
1 parent 02c5137 commit 4ae7936
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 48 deletions.
4 changes: 1 addition & 3 deletions railties/lib/rails/application.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
28 changes: 17 additions & 11 deletions railties/lib/rails/configuration.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -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"
Expand Down Expand Up @@ -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/")
Expand Down
4 changes: 3 additions & 1 deletion railties/lib/rails/engine.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -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


Expand Down
18 changes: 8 additions & 10 deletions railties/lib/rails/paths.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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]
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions railties/test/application/configuration_test.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 8 additions & 15 deletions railties/test/initializer/path_test.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -36,47 +37,39 @@ 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"
assert_path @paths.tmp, "tmp" assert_path @paths.tmp, "tmp"
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,
Pathname.new(@paths.app.controllers.to_a[1]).expand_path Pathname.new(@paths.app.controllers.to_a[1]).expand_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"
Expand All @@ -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
Expand Down
7 changes: 1 addition & 6 deletions railties/test/paths_test.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4ae7936

Please sign in to comment.