From 805369c8c84a15115e4f7a2fe9b3d4de07e2c7f2 Mon Sep 17 00:00:00 2001 From: Yehuda Katz + Carl Lerche Date: Thu, 18 Jun 2009 12:43:43 -0700 Subject: Starting to write the new initializer --- railties/test/new_initializer_test.rb | 118 ++++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 railties/test/new_initializer_test.rb (limited to 'railties/test') diff --git a/railties/test/new_initializer_test.rb b/railties/test/new_initializer_test.rb new file mode 100644 index 0000000000..166e7e613f --- /dev/null +++ b/railties/test/new_initializer_test.rb @@ -0,0 +1,118 @@ +require 'abstract_unit' +require 'active_support/ruby/shim' + +module Rails + class Initializer + class Error < StandardError ; end + class Runner + def initialize + @names = {} + @initializers = [] + end + + def add(name, options = {}, &block) + if other = options[:before] || options[:after] and !@names[other] + raise Error, "The #{other.inspect} initializer does not exist" + end + + Class.new(Initializer, &block).new.tap do |initializer| + index = if options[:before] + @initializers.index(@names[other]) + elsif options[:after] + @initializers.index(@names[other]) + 1 + else + -1 + end + + @initializers.insert(index, initializer) + @names[name] = initializer + end + end + + def run + @initializers.each { |init| init.run } + end + end + + def self.run(&blk) + define_method(:run, &blk) + end + end +end + + +class InitializerRunnerTest < ActiveSupport::TestCase + + def setup + @runner = Rails::Initializer::Runner.new + end + + test "A new runner can be created" do + assert @runner + end + + test "You can create initializers" do + init = @runner.add :foo do + + end + + assert_kind_of Rails::Initializer, init + end + + test "The initializers actually get run when the runner is run" do + state = nil + + @runner.add :foo do + run { state = true } + end + + @runner.run + assert state + end + + test "By default, initializers get run in the order that they are added" do + state = [] + + @runner.add :first do + run { state << :first } + end + + @runner.add :second do + run { state << :second } + end + + @runner.run + assert_equal [:first, :second], state + end + + test "Raises an exception if :before or :after are specified, but don't exist" do + assert_raise(Rails::Initializer::Error) do + @runner.add(:fail, :before => :whale) { 1 } + end + + assert_raise(Rails::Initializer::Error) do + @runner.add(:fail, :after => :whale) { 1 } + end + end + + test "When adding an initializer, specifying :after allows you to move an initializer after another" do + state = [] + + @runner.add :first do + run { state << :first } + end + + @runner.add :second do + run { state << :second } + end + + @runner.add :third, :after => :first do + run { state << :third } + end + + @runner.run + assert_equal [:first, :third, :second], state + end + +end + -- cgit v1.2.3 From 99b8248f6e83b87fc7f09e43a11c68aebcb2712e Mon Sep 17 00:00:00 2001 From: Yehuda Katz + Carl Lerche Date: Thu, 18 Jun 2009 12:51:15 -0700 Subject: Initial initializer impl --- railties/test/new_initializer_test.rb | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) (limited to 'railties/test') diff --git a/railties/test/new_initializer_test.rb b/railties/test/new_initializer_test.rb index 166e7e613f..ffa10060ce 100644 --- a/railties/test/new_initializer_test.rb +++ b/railties/test/new_initializer_test.rb @@ -11,20 +11,15 @@ module Rails end def add(name, options = {}, &block) - if other = options[:before] || options[:after] and !@names[other] - raise Error, "The #{other.inspect} initializer does not exist" + # If :before or :after is specified, set the index to the right spot + if other = options[:before] || options[:after] + raise Error, "The #{other.inspect} initializer does not exist" unless @names[other] + index = @initializers.index(@names[other]) + index += 1 if options[:after] end Class.new(Initializer, &block).new.tap do |initializer| - index = if options[:before] - @initializers.index(@names[other]) - elsif options[:after] - @initializers.index(@names[other]) + 1 - else - -1 - end - - @initializers.insert(index, initializer) + @initializers.insert(index || -1, initializer) @names[name] = initializer end end -- cgit v1.2.3 From a3309e4d70101ec1b32064a99042622c43619d09 Mon Sep 17 00:00:00 2001 From: Yehuda Katz + Carl Lerche Date: Thu, 18 Jun 2009 17:44:35 -0700 Subject: Started making progress on implementing a new initializer. Class.any_instance.expects(:require).raises(LoadError) ... w0t --- railties/test/initializer_test.rb | 55 ++++++----- railties/test/new_initializer_test.rb | 174 ++++++++++++++++++++++------------ 2 files changed, 140 insertions(+), 89 deletions(-) (limited to 'railties/test') diff --git a/railties/test/initializer_test.rb b/railties/test/initializer_test.rb index 987a5ada86..744b0d32d1 100644 --- a/railties/test/initializer_test.rb +++ b/railties/test/initializer_test.rb @@ -38,14 +38,15 @@ class Initializer_eager_loading_Test < Test::Unit::TestCase @config.cache_classes = true @config.load_paths = [File.expand_path(File.dirname(__FILE__) + "/fixtures/eager")] @config.eager_load_paths = [File.expand_path(File.dirname(__FILE__) + "/fixtures/eager")] - @initializer = Rails::Initializer.new(@config) - @initializer.set_load_path - @initializer.set_autoload_paths + @initializer = Rails::Initializer.default + @initializer.config = @config + @initializer.run(:set_load_path) + @initializer.run(:set_autoload_paths) end def test_eager_loading_loads_parent_classes_before_children assert_nothing_raised do - @initializer.load_application_classes + @initializer.run(:load_application_classes) end end end @@ -62,7 +63,7 @@ class Initializer_after_initialize_with_blocks_environment_Test < Test::Unit::Te assert_nil $test_after_initialize_block1 assert_nil $test_after_initialize_block2 - Rails::Initializer.any_instance.expects(:gems_dependencies_loaded).returns(true) + config.expects(:gems_dependencies_loaded).returns(true) Rails::Initializer.run(:after_initialize, config) end @@ -92,7 +93,7 @@ class Initializer_after_initialize_with_no_block_environment_Test < Test::Unit:: end assert_nil $test_after_initialize_block1 - Rails::Initializer.any_instance.expects(:gems_dependencies_loaded).returns(true) + config.expects(:gems_dependencies_loaded).returns(true) Rails::Initializer.run(:after_initialize, config) end @@ -114,68 +115,65 @@ class ConfigurationFrameworkPathsTests < Test::Unit::TestCase def setup @config = Rails::Configuration.new @config.frameworks.clear + @initializer = Rails::Initializer.default + @initializer.config = @config File.stubs(:directory?).returns(true) - @config.stubs(:framework_root_path).returns('') + Rails::Initializer.run(:set_root_path, @config) end def test_minimal - expected = %w( - /railties - /railties/lib - /activesupport/lib - ) - assert_equal expected, @config.framework_paths + expected = %w(railties railties/lib activesupport/lib) + assert_equal expected.map {|e| "#{@config.framework_root_path}/#{e}"}, @config.framework_paths end def test_actioncontroller_or_actionview_add_actionpack @config.frameworks << :action_controller - assert_framework_path '/actionpack/lib' + assert_framework_path "actionpack/lib" @config.frameworks = [:action_view] - assert_framework_path '/actionpack/lib' + assert_framework_path 'actionpack/lib' end def test_paths_for_ar_ares_and_mailer [:active_record, :action_mailer, :active_resource, :action_web_service].each do |framework| @config.frameworks = [framework] - assert_framework_path "/#{framework.to_s.gsub('_', '')}/lib" + assert_framework_path "#{framework.to_s.gsub('_', '')}/lib" end end def test_unknown_framework_raises_error @config.frameworks << :action_foo - initializer = Rails::Initializer.new @config - initializer.expects(:require).raises(LoadError) + + Class.any_instance.expects(:require).raises(LoadError) assert_raise RuntimeError do - initializer.send :require_frameworks + @initializer.run(:require_frameworks) end end def test_action_mailer_load_paths_set_only_if_action_mailer_in_use @config.frameworks = [:action_controller] - initializer = Rails::Initializer.new @config - initializer.send :require_frameworks + @initializer.config = @config + @initializer.run :require_frameworks assert_nothing_raised NameError do - initializer.send :load_view_paths + @initializer.run :load_view_paths end end def test_action_controller_load_paths_set_only_if_action_controller_in_use @config.frameworks = [] - initializer = Rails::Initializer.new @config - initializer.send :require_frameworks + @initializer.run :require_frameworks assert_nothing_raised NameError do - initializer.send :load_view_paths + @initializer.run :load_view_paths end end protected def assert_framework_path(path) - assert @config.framework_paths.include?(path), + assert @config.framework_paths.include?("#{@config.framework_root_path}/#{path}"), "<#{path.inspect}> not found among <#{@config.framework_paths.inspect}>" end end @@ -187,14 +185,15 @@ class InitializerPluginLoadingTests < Test::Unit::TestCase @configuration = Rails::Configuration.new @configuration.frameworks -= [:action_mailer] @configuration.plugin_paths << plugin_fixture_root_path - @initializer = Rails::Initializer.new(@configuration) + @initializer = Rails::Initializer.default + @initializer.config = @configuration @valid_plugin_path = plugin_fixture_path('default/stubby') @empty_plugin_path = plugin_fixture_path('default/empty') end def test_no_plugins_are_loaded_if_the_configuration_has_an_empty_plugin_list only_load_the_following_plugins! [] - @initializer.load_plugins + @initializer.run :load_plugins assert_equal [], @initializer.loaded_plugins end diff --git a/railties/test/new_initializer_test.rb b/railties/test/new_initializer_test.rb index ffa10060ce..8d9ef7bee3 100644 --- a/railties/test/new_initializer_test.rb +++ b/railties/test/new_initializer_test.rb @@ -1,113 +1,165 @@ require 'abstract_unit' require 'active_support/ruby/shim' - -module Rails - class Initializer - class Error < StandardError ; end - class Runner - def initialize - @names = {} - @initializers = [] - end - - def add(name, options = {}, &block) - # If :before or :after is specified, set the index to the right spot - if other = options[:before] || options[:after] - raise Error, "The #{other.inspect} initializer does not exist" unless @names[other] - index = @initializers.index(@names[other]) - index += 1 if options[:after] - end - - Class.new(Initializer, &block).new.tap do |initializer| - @initializers.insert(index || -1, initializer) - @names[name] = initializer - end - end - - def run - @initializers.each { |init| init.run } - end - end - - def self.run(&blk) - define_method(:run, &blk) - end - end -end - +require 'initializer' class InitializerRunnerTest < ActiveSupport::TestCase - + def setup @runner = Rails::Initializer::Runner.new end - + test "A new runner can be created" do assert @runner end - - test "You can create initializers" do - init = @runner.add :foo do - - end - - assert_kind_of Rails::Initializer, init - end - + test "The initializers actually get run when the runner is run" do state = nil - + @runner.add :foo do run { state = true } end - + @runner.run assert state end - + test "By default, initializers get run in the order that they are added" do state = [] - + @runner.add :first do run { state << :first } end - + @runner.add :second do run { state << :second } end - + @runner.run assert_equal [:first, :second], state end - + test "Raises an exception if :before or :after are specified, but don't exist" do assert_raise(Rails::Initializer::Error) do @runner.add(:fail, :before => :whale) { 1 } end - + assert_raise(Rails::Initializer::Error) do @runner.add(:fail, :after => :whale) { 1 } end end - + test "When adding an initializer, specifying :after allows you to move an initializer after another" do state = [] - + @runner.add :first do run { state << :first } end - + @runner.add :second do run { state << :second } end - + @runner.add :third, :after => :first do run { state << :third } end - + @runner.run assert_equal [:first, :third, :second], state end - -end + test "An initializer can be deleted" do + state = [] + + @runner.add :first do + run { state << :first } + end + + @runner.add :second do + run { state << :second } + end + + @runner.delete(:second) + + @runner.run + assert_equal [:first], state + end + + test "A runner can be initialized with an existing runner, which it copies" do + state = [] + + @runner.add :first do + run { state << :first } + end + + @runner.add :second do + run { state << :second } + end + + Rails::Initializer::Runner.new(@runner).run + assert_equal [:first, :second], state + end + + test "A child runner can be still be modified without modifying the parent" do + state = [] + + @runner.add :first do + run { state << :first } + end + + @runner.add :second do + run { state << :second } + end + + new_runner = Rails::Initializer::Runner.new(@runner) + new_runner.add :trois do + run { state << :trois } + end + new_runner.delete(:second) + + new_runner.run + assert_equal [:first, :trois], state + state.clear + @runner.run + assert_equal [:first, :second], state + end + + test "A child runner that is modified does not modify any other children of the same parent" do + state = [] + + @runner.add :first do + run { state << :first } + end + + @runner.add :second do + run { state << :second } + end + + child_one = Rails::Initializer::Runner.new(@runner) + child_two = Rails::Initializer::Runner.new(@runner) + + child_one.delete(:second) + child_two.run + + assert_equal [:first, :second], state + end + + test "It does not run the initializer block immediately" do + state = [] + @runner.add :first do + state << :first + end + + assert_equal [], state + end + + test "It runs the block when the runner is run" do + state = [] + @runner.add :first do + state << :first + end + + @runner.run + assert_equal [:first], state + end + +end \ No newline at end of file -- cgit v1.2.3 From f2aea4d3eac467b85a63593ba7e2de28b2a2eb0a Mon Sep 17 00:00:00 2001 From: Yehuda Katz + Carl Lerche Date: Fri, 19 Jun 2009 11:13:38 -0700 Subject: Get initializer_test.rb to pass with the new initializer. --- railties/test/initializer_test.rb | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) (limited to 'railties/test') diff --git a/railties/test/initializer_test.rb b/railties/test/initializer_test.rb index 744b0d32d1..5caa5858a4 100644 --- a/railties/test/initializer_test.rb +++ b/railties/test/initializer_test.rb @@ -194,20 +194,20 @@ class InitializerPluginLoadingTests < Test::Unit::TestCase def test_no_plugins_are_loaded_if_the_configuration_has_an_empty_plugin_list only_load_the_following_plugins! [] @initializer.run :load_plugins - assert_equal [], @initializer.loaded_plugins + assert_equal [], @configuration.loaded_plugins end def test_only_the_specified_plugins_are_located_in_the_order_listed plugin_names = [:plugin_with_no_lib_dir, :acts_as_chunky_bacon] only_load_the_following_plugins! plugin_names load_plugins! - assert_plugins plugin_names, @initializer.loaded_plugins + assert_plugins plugin_names, @configuration.loaded_plugins end def test_all_plugins_are_loaded_when_registered_plugin_list_is_untouched failure_tip = "It's likely someone has added a new plugin fixture without updating this list" load_plugins! - assert_plugins [:a, :acts_as_chunky_bacon, :engine, :gemlike, :plugin_with_no_lib_dir, :stubby], @initializer.loaded_plugins, failure_tip + assert_plugins [:a, :acts_as_chunky_bacon, :engine, :gemlike, :plugin_with_no_lib_dir, :stubby], @configuration.loaded_plugins, failure_tip end def test_all_plugins_loaded_when_all_is_used @@ -215,7 +215,7 @@ class InitializerPluginLoadingTests < Test::Unit::TestCase only_load_the_following_plugins! plugin_names load_plugins! failure_tip = "It's likely someone has added a new plugin fixture without updating this list" - assert_plugins [:stubby, :acts_as_chunky_bacon, :a, :engine, :gemlike, :plugin_with_no_lib_dir], @initializer.loaded_plugins, failure_tip + assert_plugins [:stubby, :acts_as_chunky_bacon, :a, :engine, :gemlike, :plugin_with_no_lib_dir], @configuration.loaded_plugins, failure_tip end def test_all_plugins_loaded_after_all @@ -223,7 +223,7 @@ class InitializerPluginLoadingTests < Test::Unit::TestCase only_load_the_following_plugins! plugin_names load_plugins! failure_tip = "It's likely someone has added a new plugin fixture without updating this list" - assert_plugins [:stubby, :a, :engine, :gemlike, :plugin_with_no_lib_dir, :acts_as_chunky_bacon], @initializer.loaded_plugins, failure_tip + assert_plugins [:stubby, :a, :engine, :gemlike, :plugin_with_no_lib_dir, :acts_as_chunky_bacon], @configuration.loaded_plugins, failure_tip end def test_plugin_names_may_be_strings @@ -231,7 +231,7 @@ class InitializerPluginLoadingTests < Test::Unit::TestCase only_load_the_following_plugins! plugin_names load_plugins! failure_tip = "It's likely someone has added a new plugin fixture without updating this list" - assert_plugins plugin_names, @initializer.loaded_plugins, failure_tip + assert_plugins plugin_names, @configuration.loaded_plugins, failure_tip end def test_registering_a_plugin_name_that_does_not_exist_raises_a_load_error @@ -250,7 +250,7 @@ class InitializerPluginLoadingTests < Test::Unit::TestCase flunk "Expected a LoadError but did not get one" rescue LoadError => e failure_tip = "It's likely someone renamed or deleted plugin fixtures without updating this test" - assert_plugins valid_plugin_names, @initializer.loaded_plugins, failure_tip + assert_plugins valid_plugin_names, @configuration.loaded_plugins, failure_tip invalid_plugin_names.each do |plugin| assert_match(/#{plugin.to_s}/, e.message, "LoadError message should mention plugin '#{plugin}'") end @@ -264,7 +264,7 @@ class InitializerPluginLoadingTests < Test::Unit::TestCase def test_should_ensure_all_loaded_plugins_load_paths_are_added_to_the_load_path only_load_the_following_plugins! [:stubby, :acts_as_chunky_bacon] - @initializer.add_plugin_load_paths + @initializer.run(:add_plugin_load_paths) assert $LOAD_PATH.include?(File.join(plugin_fixture_path('default/stubby'), 'lib')) assert $LOAD_PATH.include?(File.join(plugin_fixture_path('default/acts/acts_as_chunky_bacon'), 'lib')) @@ -273,8 +273,8 @@ class InitializerPluginLoadingTests < Test::Unit::TestCase private def load_plugins! - @initializer.add_plugin_load_paths - @initializer.load_plugins + @initializer.run(:add_plugin_load_paths) + @initializer.run(:load_plugins) end end @@ -314,7 +314,7 @@ class InitializerSetupI18nTests < Test::Unit::TestCase File.expand_path(File.dirname(__FILE__) + "/../../activemodel/lib/active_model/locale/en.yml"), File.expand_path(File.dirname(__FILE__) + "/../../activerecord/lib/active_record/locale/en.yml"), "my/test/locale.yml", - "my/other/locale.yml" ], I18n.load_path.collect { |path| path =~ /^\./ ? File.expand_path(path) : path } + "my/other/locale.yml" ], I18n.load_path.collect { |path| path =~ /\.\./ ? File.expand_path(path) : path } end def test_setting_another_default_locale -- cgit v1.2.3 From 9cfd1d44915f4615bbb760198cd01bf4dfc69f5a Mon Sep 17 00:00:00 2001 From: Yehuda Katz + Carl Lerche Date: Fri, 19 Jun 2009 11:27:24 -0700 Subject: Get more tests to pass --- railties/test/generator_lookup_test.rb | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'railties/test') diff --git a/railties/test/generator_lookup_test.rb b/railties/test/generator_lookup_test.rb index b650f304ed..b67087e5ea 100644 --- a/railties/test/generator_lookup_test.rb +++ b/railties/test/generator_lookup_test.rb @@ -7,9 +7,11 @@ class GeneratorLookupTest < Test::Unit::TestCase # We need to add our testing plugin directory to the plugin paths so # the locator knows where to look for our plugins @configuration.plugin_paths += @fixture_dirs.map{|fd| plugin_fixture_path(fd)} - @initializer = Rails::Initializer.new(@configuration) - @initializer.add_plugin_load_paths - @initializer.load_plugins + @initializer = Rails::Initializer.default + @initializer.config = @configuration + @initializer.run(:add_plugin_load_paths) + @initializer.run(:load_plugins) + @initializer.run(:set_root_path) load 'rails_generator.rb' require 'rails_generator/scripts' end -- cgit v1.2.3 From 30baaac5465a352e78dac6330407a7b3460db180 Mon Sep 17 00:00:00 2001 From: Yehuda Katz + Carl Lerche Date: Fri, 19 Jun 2009 11:41:02 -0700 Subject: Fix the default frameworks --- railties/test/plugin_loader_test.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'railties/test') diff --git a/railties/test/plugin_loader_test.rb b/railties/test/plugin_loader_test.rb index c647d7b478..873e000222 100644 --- a/railties/test/plugin_loader_test.rb +++ b/railties/test/plugin_loader_test.rb @@ -20,7 +20,8 @@ class TestPluginLoader < Test::Unit::TestCase @configuration = Rails::Configuration.new @configuration.plugin_paths << plugin_fixture_root_path - @initializer = Rails::Initializer.new(@configuration) + @initializer = Rails::Initializer.default + @initializer.config = @configuration @valid_plugin_path = plugin_fixture_path('default/stubby') @empty_plugin_path = plugin_fixture_path('default/empty') -- cgit v1.2.3 From 9d398f48275a2740b0c7545da207ac2ebbe012ec Mon Sep 17 00:00:00 2001 From: Yehuda Katz + Carl Lerche Date: Fri, 19 Jun 2009 12:12:56 -0700 Subject: Got all the railties tests to pass, rails must boot! --- railties/test/plugin_locator_test.rb | 3 ++- railties/test/plugin_test.rb | 3 ++- railties/test/rails_generator_test.rb | 3 +++ 3 files changed, 7 insertions(+), 2 deletions(-) (limited to 'railties/test') diff --git a/railties/test/plugin_locator_test.rb b/railties/test/plugin_locator_test.rb index 855ac7d82f..da1548dee1 100644 --- a/railties/test/plugin_locator_test.rb +++ b/railties/test/plugin_locator_test.rb @@ -27,7 +27,8 @@ class PluginFileSystemLocatorTest < Test::Unit::TestCase # We need to add our testing plugin directory to the plugin paths so # the locator knows where to look for our plugins @configuration.plugin_paths << plugin_fixture_root_path - @initializer = Rails::Initializer.new(@configuration) + @initializer = Rails::Initializer.default + @initializer.config = @configuration @locator = Rails::Plugin::FileSystemLocator.new(@initializer) @valid_plugin_path = plugin_fixture_path('default/stubby') @empty_plugin_path = plugin_fixture_path('default/empty') diff --git a/railties/test/plugin_test.rb b/railties/test/plugin_test.rb index a6c390a45a..ae03ea4662 100644 --- a/railties/test/plugin_test.rb +++ b/railties/test/plugin_test.rb @@ -2,7 +2,8 @@ require 'plugin_test_helper' class PluginTest < Test::Unit::TestCase def setup - @initializer = Rails::Initializer.new(Rails::Configuration.new) + @initializer = Rails::Initializer.default + @initializer.config = Rails::Configuration.new @valid_plugin_path = plugin_fixture_path('default/stubby') @empty_plugin_path = plugin_fixture_path('default/empty') @gemlike_plugin_path = plugin_fixture_path('default/gemlike') diff --git a/railties/test/rails_generator_test.rb b/railties/test/rails_generator_test.rb index b2fc2f585d..38bd90dcc1 100644 --- a/railties/test/rails_generator_test.rb +++ b/railties/test/rails_generator_test.rb @@ -50,6 +50,9 @@ class RailsGeneratorTest < Test::Unit::TestCase def setup ActiveRecord::Base.pluralize_table_names = true + @initializer = Rails::Initializer.default + @initializer.config = Rails.configuration + @initializer.run(:set_root_path) end def test_sources -- cgit v1.2.3 From 728e3b4047a55ead0f5fbb3bc57095b2b273352b Mon Sep 17 00:00:00 2001 From: Yehuda Katz + Carl Lerche Date: Fri, 26 Jun 2009 11:21:17 -0700 Subject: Simple initial Paths impl --- railties/test/paths_test.rb | 68 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 railties/test/paths_test.rb (limited to 'railties/test') diff --git a/railties/test/paths_test.rb b/railties/test/paths_test.rb new file mode 100644 index 0000000000..0da31b467a --- /dev/null +++ b/railties/test/paths_test.rb @@ -0,0 +1,68 @@ +require 'abstract_unit' + +module Rails + class Application + class Path + attr_accessor :path, :root #, :glob, :load_once, :eager + + def initialize(path, root = nil) + @children = {} + @path = path + @root = root || self + end + + def method_missing(id, *args) + name = id.to_s + + if name =~ /^(.*)=$/ + @children[$1] = Path.new(args.first, @root) + elsif path = @children[name] + path + else + super + end + end + + def path + @path.index('/') == 0 ? @path : File.join(@root.path, @path) + end + + alias to_s path + end + end +end + +class PathsTest < ActiveSupport::TestCase + + def setup + @root = Rails::Application::Path.new("/foo/bar") + end + + test "the paths object is initialized with the root path" do + root = Rails::Application::Path.new("/fiz/baz") + assert_equal "/fiz/baz", root.to_s + end + + test "creating a root level path" do + @root.app = "/foo/bar" + assert_equal "/foo/bar", @root.app.to_s + end + + test "relative paths are relative to the paths root" do + @root.app = "app" + assert_equal "/foo/bar/app", @root.app.to_s + end + + test "creating a child level path" do + @root.app = "/foo/bar" + @root.app.models = "/foo/bar/baz" + assert_equal "/foo/bar/baz", @root.app.models.to_s + end + + test "child level paths are relative from the root" do + @root.app = "/app" + @root.app.models = "baz" + + assert_equal "/foo/bar/baz", @root.app.models.to_s + end +end \ No newline at end of file -- cgit v1.2.3 From b0774281ef4008f2c249b1e85bbdb1f28629d18f Mon Sep 17 00:00:00 2001 From: Yehuda Katz + Carl Lerche Date: Fri, 26 Jun 2009 12:12:20 -0700 Subject: Update paths to support an explicit root and multiple paths per category --- railties/test/paths_test.rb | 120 +++++++++++++++++++++++++++++++++----------- 1 file changed, 91 insertions(+), 29 deletions(-) (limited to 'railties/test') diff --git a/railties/test/paths_test.rb b/railties/test/paths_test.rb index 0da31b467a..1fad324015 100644 --- a/railties/test/paths_test.rb +++ b/railties/test/paths_test.rb @@ -2,18 +2,10 @@ require 'abstract_unit' module Rails class Application - class Path - attr_accessor :path, :root #, :glob, :load_once, :eager - - def initialize(path, root = nil) - @children = {} - @path = path - @root = root || self - end - + module PathParent def method_missing(id, *args) name = id.to_s - + if name =~ /^(.*)=$/ @children[$1] = Path.new(args.first, @root) elsif path = @children[name] @@ -22,47 +14,117 @@ module Rails super end end - - def path - @path.index('/') == 0 ? @path : File.join(@root.path, @path) + end + + class Root + include PathParent + + attr_reader :path + def initialize(path) + raise unless path.is_a?(String) + + @children = {} + + # TODO: Move logic from set_root_path initializer + @path = File.expand_path(path) + @root = self + end + end + + class Path + include PathParent + + attr_reader :path #, :glob, :load_once, :eager + + def initialize(path, root) + @children = {} + @root = root + @paths = [path].flatten + end + + def push(path) + @paths.push path + end + + alias << push + + def unshift(path) + @paths.unshift path + end + + + def paths + @paths.map do |path| + path.index('/') == 0 ? path : File.join(@root.path, path) + end end - - alias to_s path + + alias to_a paths end end end class PathsTest < ActiveSupport::TestCase - + def setup - @root = Rails::Application::Path.new("/foo/bar") + @root = Rails::Application::Root.new("/foo/bar") end - + test "the paths object is initialized with the root path" do - root = Rails::Application::Path.new("/fiz/baz") - assert_equal "/fiz/baz", root.to_s + root = Rails::Application::Root.new("/fiz/baz") + assert_equal "/fiz/baz", root.path end - + test "creating a root level path" do @root.app = "/foo/bar" - assert_equal "/foo/bar", @root.app.to_s + assert_equal ["/foo/bar"], @root.app.to_a end - + test "relative paths are relative to the paths root" do @root.app = "app" - assert_equal "/foo/bar/app", @root.app.to_s + assert_equal ["/foo/bar/app"], @root.app.to_a end - + test "creating a child level path" do @root.app = "/foo/bar" @root.app.models = "/foo/bar/baz" - assert_equal "/foo/bar/baz", @root.app.models.to_s + assert_equal ["/foo/bar/baz"], @root.app.models.to_a end - + test "child level paths are relative from the root" do @root.app = "/app" @root.app.models = "baz" - - assert_equal "/foo/bar/baz", @root.app.models.to_s + + assert_equal ["/foo/bar/baz"], @root.app.models.to_a + end + + test "adding multiple physical paths as an array" do + @root.app = ["/app", "/app2"] + assert_equal ["/app", "/app2"], @root.app.to_a + end + + test "adding multiple physical paths using #push" do + @root.app = "/app" + @root.app.push "/app2" + assert_equal ["/app", "/app2"], @root.app.to_a + end + + test "adding multiple physical paths using <<" do + @root.app = "/app" + @root.app << "/app2" + assert_equal ["/app", "/app2"], @root.app.to_a + end + + test "adding multiple physical paths using #unshift" do + @root.app = "/app" + @root.app.unshift "/app2" + assert_equal ["/app2", "/app"], @root.app.to_a + end + + test "the root can only have one physical path" do + assert_raise(RuntimeError) { Rails::Application::Root.new(["/fiz", "/biz"]) } + assert_raise(NoMethodError) { @root.push "/biz" } + assert_raise(NoMethodError) { @root.unshift "/biz" } + assert_raise(NoMethodError) { @root << "/biz" } end end \ No newline at end of file -- cgit v1.2.3 From 4153c6b720563e1c43bb96e95f0ff5fbd59d6be7 Mon Sep 17 00:00:00 2001 From: Yehuda Katz + Carl Lerche Date: Fri, 26 Jun 2009 15:36:38 -0700 Subject: Finished a first stab at the Rails application path object. --- railties/test/paths_test.rb | 104 +++++++++++++++++--------------------------- 1 file changed, 40 insertions(+), 64 deletions(-) (limited to 'railties/test') diff --git a/railties/test/paths_test.rb b/railties/test/paths_test.rb index 1fad324015..e7df58579f 100644 --- a/railties/test/paths_test.rb +++ b/railties/test/paths_test.rb @@ -1,68 +1,5 @@ require 'abstract_unit' - -module Rails - class Application - module PathParent - def method_missing(id, *args) - name = id.to_s - - if name =~ /^(.*)=$/ - @children[$1] = Path.new(args.first, @root) - elsif path = @children[name] - path - else - super - end - end - end - - class Root - include PathParent - - attr_reader :path - def initialize(path) - raise unless path.is_a?(String) - - @children = {} - - # TODO: Move logic from set_root_path initializer - @path = File.expand_path(path) - @root = self - end - end - - class Path - include PathParent - - attr_reader :path #, :glob, :load_once, :eager - - def initialize(path, root) - @children = {} - @root = root - @paths = [path].flatten - end - - def push(path) - @paths.push path - end - - alias << push - - def unshift(path) - @paths.unshift path - end - - - def paths - @paths.map do |path| - path.index('/') == 0 ? path : File.join(@root.path, path) - end - end - - alias to_a paths - end - end -end +require 'rails/paths' class PathsTest < ActiveSupport::TestCase @@ -127,4 +64,43 @@ class PathsTest < ActiveSupport::TestCase assert_raise(NoMethodError) { @root.unshift "/biz" } assert_raise(NoMethodError) { @root << "/biz" } end + + test "it is possible to add a path that should be loaded only once" do + @root.app = "/app" + @root.app.load_once! + assert @root.app.load_once? + assert @root.load_once.include?(@root.app) + end + + test "making a path load_once more than once only includes it once in @root.load_once" do + @root.app = "/app" + @root.app.load_once! + @root.app.load_once! + assert_equal 1, @root.load_once.select {|p| p == @root.app }.size + end + + test "it is possible to mark a path as eager" do + @root.app = "/app" + @root.app.eager_load! + assert @root.app.eager_load? + assert @root.eager_load.include?(@root.app) + end + + test "making a path eager more than once only includes it once in @root.eager_paths" do + @root.app = "/app" + @root.app.eager_load! + @root.app.eager_load! + assert_equal 1, @root.eager_load.select {|p| p == @root.app }.size + end + + test "a path should have a glob that defaults to **/*.rb" 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.glob = "*.rb" + assert_equal "*.rb", @root.app.glob + end end \ No newline at end of file -- cgit v1.2.3 From 188a892c5a097ee6d62249d048a6be7d2dfe9649 Mon Sep 17 00:00:00 2001 From: Yehuda Katz + Carl Lerche Date: Fri, 26 Jun 2009 17:32:05 -0700 Subject: Starting to replace scattered path configuration settings with the path object --- railties/test/abstract_unit.rb | 1 + railties/test/initializer/path_test.rb | 86 ++++++++++++++++++++++ .../test/initializer/root/app/controllers/.keep | 0 railties/test/initializer/root/app/helpers/.keep | 0 railties/test/initializer/root/app/metal/.keep | 0 railties/test/initializer/root/app/models/.keep | 0 railties/test/initializer/root/app/views/.keep | 0 railties/test/initializer/root/config/database.yml | 4 + .../initializer/root/config/environments/.keep | 0 .../test/initializer/root/config/locales/.keep | 0 railties/test/initializer/root/config/routes.rb | 0 railties/test/initializer/root/lib/.keep | 0 railties/test/initializer/root/tmp/.keep | 0 railties/test/initializer/root/tmp/cache/.keep | 0 railties/test/initializer/root/vendor/.keep | 0 railties/test/paths_test.rb | 21 +++++- 16 files changed, 108 insertions(+), 4 deletions(-) create mode 100644 railties/test/initializer/path_test.rb create mode 100644 railties/test/initializer/root/app/controllers/.keep create mode 100644 railties/test/initializer/root/app/helpers/.keep create mode 100644 railties/test/initializer/root/app/metal/.keep create mode 100644 railties/test/initializer/root/app/models/.keep create mode 100644 railties/test/initializer/root/app/views/.keep create mode 100644 railties/test/initializer/root/config/database.yml create mode 100644 railties/test/initializer/root/config/environments/.keep create mode 100644 railties/test/initializer/root/config/locales/.keep create mode 100644 railties/test/initializer/root/config/routes.rb create mode 100644 railties/test/initializer/root/lib/.keep create mode 100644 railties/test/initializer/root/tmp/.keep create mode 100644 railties/test/initializer/root/tmp/cache/.keep create mode 100644 railties/test/initializer/root/vendor/.keep (limited to 'railties/test') diff --git a/railties/test/abstract_unit.rb b/railties/test/abstract_unit.rb index 0addcb8bf3..cef69e90cb 100644 --- a/railties/test/abstract_unit.rb +++ b/railties/test/abstract_unit.rb @@ -2,6 +2,7 @@ $:.unshift File.dirname(__FILE__) + "/../../activesupport/lib" $:.unshift File.dirname(__FILE__) + "/../../activerecord/lib" $:.unshift File.dirname(__FILE__) + "/../../actionpack/lib" $:.unshift File.dirname(__FILE__) + "/../../actionmailer/lib" +$:.unshift File.dirname(__FILE__) + "/../../activeresource/lib" $:.unshift File.dirname(__FILE__) + "/../lib" $:.unshift File.dirname(__FILE__) + "/../builtin/rails_info" diff --git a/railties/test/initializer/path_test.rb b/railties/test/initializer/path_test.rb new file mode 100644 index 0000000000..37e1eebbc7 --- /dev/null +++ b/railties/test/initializer/path_test.rb @@ -0,0 +1,86 @@ +require 'abstract_unit' +require 'active_support/ruby/shim' +require 'initializer' + +RAILS_ROOT.replace File.join(File.dirname(__FILE__), "root") + +module Rails + def self.vendor_rails? ; false ; end +end + +# TODO: Can this be reset? +Rails::Initializer.run do |config| + config.frameworks = [:action_controller, :action_view, :action_mailer, :active_record] +end + +class PathsTest < ActiveSupport::TestCase + def setup + @paths = Rails::Initializer.default.config.paths + end + + def root(*path) + File.expand_path(File.join(File.dirname(__FILE__), "root", *path)) + end + + def assert_path(paths, *dir) + assert_equal [root(*dir)], paths.paths + end + + 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.models, "app", "models" + assert_path @paths.app.helpers, "app", "helpers" + assert_path @paths.app.services, "app", "services" + assert_path @paths.lib, "lib" + assert_path @paths.vendor, "vendor" + assert_path @paths.vendor.plugins, "vendor", "plugins" + assert_path @paths.cache, "tmp", "cache" + assert_path @paths.config, "config" + assert_path @paths.config.locales, "config", "locales" + assert_path @paths.config.environments, "config", "environments" + + assert_equal Pathname.new(File.dirname(__FILE__)).join("root", "app", "controllers").expand_path, + Pathname.new(@paths.app.controllers.to_a.first).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 + end + + test "booting up Rails yields a list of paths that are eager" do + assert @paths.app.models.eager_load? + assert @paths.app.controllers.eager_load? + assert @paths.app.helpers.eager_load? + assert @paths.app.metals.eager_load? + end + + test "environments has a glob equal to the current environment" do + assert_equal "#{RAILS_ENV}.rb", @paths.config.environments.glob + end + + def assert_in_load_path(*path) + assert $:.any? { |p| File.expand_path(p) == root(*path) }, "Load path does not include '#{root(*path)}'. They are:\n-----\n #{$:.join("\n")}\n-----" + end + + def assert_not_in_load_path(*path) + assert !$:.any? { |p| File.expand_path(p) == root(*path) }, "Load path includes '#{root(*path)}'. They are:\n-----\n #{$:.join("\n")}\n-----" + end + + 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", "metal" + assert_in_load_path "app", "models" + assert_in_load_path "app", "helpers" + assert_in_load_path "lib" + assert_in_load_path "vendor" + + assert_not_in_load_path "app", "views" + assert_not_in_load_path "app", "services" + assert_not_in_load_path "config" + assert_not_in_load_path "config", "locales" + assert_not_in_load_path "config", "environments" + assert_not_in_load_path "tmp" + assert_not_in_load_path "tmp", "cache" + end + +end \ No newline at end of file diff --git a/railties/test/initializer/root/app/controllers/.keep b/railties/test/initializer/root/app/controllers/.keep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/railties/test/initializer/root/app/helpers/.keep b/railties/test/initializer/root/app/helpers/.keep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/railties/test/initializer/root/app/metal/.keep b/railties/test/initializer/root/app/metal/.keep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/railties/test/initializer/root/app/models/.keep b/railties/test/initializer/root/app/models/.keep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/railties/test/initializer/root/app/views/.keep b/railties/test/initializer/root/app/views/.keep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/railties/test/initializer/root/config/database.yml b/railties/test/initializer/root/config/database.yml new file mode 100644 index 0000000000..ce3356be0c --- /dev/null +++ b/railties/test/initializer/root/config/database.yml @@ -0,0 +1,4 @@ +development: + adapter: sqlite3 + database: db/railties.db + timeout: 5000 \ No newline at end of file diff --git a/railties/test/initializer/root/config/environments/.keep b/railties/test/initializer/root/config/environments/.keep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/railties/test/initializer/root/config/locales/.keep b/railties/test/initializer/root/config/locales/.keep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/railties/test/initializer/root/config/routes.rb b/railties/test/initializer/root/config/routes.rb new file mode 100644 index 0000000000..e69de29bb2 diff --git a/railties/test/initializer/root/lib/.keep b/railties/test/initializer/root/lib/.keep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/railties/test/initializer/root/tmp/.keep b/railties/test/initializer/root/tmp/.keep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/railties/test/initializer/root/tmp/cache/.keep b/railties/test/initializer/root/tmp/cache/.keep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/railties/test/initializer/root/vendor/.keep b/railties/test/initializer/root/vendor/.keep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/railties/test/paths_test.rb b/railties/test/paths_test.rb index e7df58579f..a1ed43ad6d 100644 --- a/railties/test/paths_test.rb +++ b/railties/test/paths_test.rb @@ -69,28 +69,28 @@ class PathsTest < ActiveSupport::TestCase @root.app = "/app" @root.app.load_once! assert @root.app.load_once? - assert @root.load_once.include?(@root.app) + assert @root.load_once.include?(@root.app.paths.first) end test "making a path load_once more than once only includes it once in @root.load_once" do @root.app = "/app" @root.app.load_once! @root.app.load_once! - assert_equal 1, @root.load_once.select {|p| p == @root.app }.size + assert_equal 1, @root.load_once.select {|p| p == @root.app.paths.first }.size end test "it is possible to mark a path as eager" do @root.app = "/app" @root.app.eager_load! assert @root.app.eager_load? - assert @root.eager_load.include?(@root.app) + assert @root.eager_load.include?(@root.app.paths.first) end test "making a path eager more than once only includes it once in @root.eager_paths" do @root.app = "/app" @root.app.eager_load! @root.app.eager_load! - assert_equal 1, @root.eager_load.select {|p| p == @root.app }.size + assert_equal 1, @root.eager_load.select {|p| p == @root.app.paths.first }.size end test "a path should have a glob that defaults to **/*.rb" do @@ -103,4 +103,17 @@ class PathsTest < ActiveSupport::TestCase @root.app.glob = "*.rb" assert_equal "*.rb", @root.app.glob end + + test "a path can be added to the load path" do + @root.app = "app" + @root.app.load_path! + @root.app.models = "app/models" + assert_equal ["/foo/bar/app"], @root.load_paths + end + + test "adding a path to the eager paths also adds it to the load path" do + @root.app = "app" + @root.app.eager_load! + assert_equal ["/foo/bar/app"], @root.load_paths + end end \ No newline at end of file -- cgit v1.2.3 From f281745056cd67c967c5a695694061f515385be8 Mon Sep 17 00:00:00 2001 From: Carl Lerche Date: Tue, 30 Jun 2009 13:53:03 -0700 Subject: Test that builtin_directories is only used in development mode --- railties/test/initializer/path_test.rb | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'railties/test') diff --git a/railties/test/initializer/path_test.rb b/railties/test/initializer/path_test.rb index 37e1eebbc7..8fbad24a73 100644 --- a/railties/test/initializer/path_test.rb +++ b/railties/test/initializer/path_test.rb @@ -14,6 +14,8 @@ Rails::Initializer.run do |config| end class PathsTest < ActiveSupport::TestCase + include ActiveSupport::Testing::Isolation + def setup @paths = Rails::Initializer.default.config.paths end @@ -83,4 +85,19 @@ class PathsTest < ActiveSupport::TestCase assert_not_in_load_path "tmp", "cache" end + test "controller paths include builtin in development mode" do + RAILS_ENV.replace "development" + assert Rails::Configuration.new.paths.app.controllers.paths.any? { |p| p =~ /builtin/ } + end + + test "controller paths does not have builtin_directories in test mode" do + RAILS_ENV.replace "test" + assert !Rails::Configuration.new.paths.app.controllers.paths.any? { |p| p =~ /builtin/ } + end + + test "controller paths does not have builtin_directories in production mode" do + RAILS_ENV.replace "production" + assert !Rails::Configuration.new.paths.app.controllers.paths.any? { |p| p =~ /builtin/ } + end + end \ No newline at end of file -- cgit v1.2.3 From 132e6d00638dc6370fafa0f1377d3bca17eee2d1 Mon Sep 17 00:00:00 2001 From: Carl Lerche Date: Tue, 30 Jun 2009 13:55:11 -0700 Subject: Add #concat to Rails::Application::Path --- railties/test/paths_test.rb | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'railties/test') diff --git a/railties/test/paths_test.rb b/railties/test/paths_test.rb index a1ed43ad6d..fa2f6ceee2 100644 --- a/railties/test/paths_test.rb +++ b/railties/test/paths_test.rb @@ -52,6 +52,12 @@ class PathsTest < ActiveSupport::TestCase assert_equal ["/app", "/app2"], @root.app.to_a end + test "adding multiple physical paths using concat" do + @root.app = "/app" + @root.app.concat ["/app2", "/app3"] + assert_equal ["/app", "/app2", "/app3"], @root.app.to_a + end + test "adding multiple physical paths using #unshift" do @root.app = "/app" @root.app.unshift "/app2" @@ -62,6 +68,7 @@ class PathsTest < ActiveSupport::TestCase assert_raise(RuntimeError) { Rails::Application::Root.new(["/fiz", "/biz"]) } assert_raise(NoMethodError) { @root.push "/biz" } assert_raise(NoMethodError) { @root.unshift "/biz" } + assert_raise(NoMethodError) { @root.concat ["/biz"]} assert_raise(NoMethodError) { @root << "/biz" } end -- cgit v1.2.3 From eea7b5db1db5d7e6020c5bcb6b4d85afcbc2e696 Mon Sep 17 00:00:00 2001 From: Yehuda Katz + Carl Lerche Date: Tue, 30 Jun 2009 17:26:46 -0700 Subject: Crazy hacks to get the Isolation testing module to work on non forking environments --- railties/test/abstract_unit.rb | 2 ++ 1 file changed, 2 insertions(+) (limited to 'railties/test') diff --git a/railties/test/abstract_unit.rb b/railties/test/abstract_unit.rb index cef69e90cb..dd9d0448b2 100644 --- a/railties/test/abstract_unit.rb +++ b/railties/test/abstract_unit.rb @@ -1,3 +1,5 @@ +ORIG_ARGV = ARGV.dup + $:.unshift File.dirname(__FILE__) + "/../../activesupport/lib" $:.unshift File.dirname(__FILE__) + "/../../activerecord/lib" $:.unshift File.dirname(__FILE__) + "/../../actionpack/lib" -- cgit v1.2.3 From 7583a24ee0ea85d55a5e235c3082f1b67d3d7694 Mon Sep 17 00:00:00 2001 From: Yehuda Katz + Carl Lerche Date: Wed, 1 Jul 2009 11:53:17 -0700 Subject: Move mocha down below initial T::U require and bump version to 0.9.7 [#2858 state:resolved] --- railties/test/abstract_unit.rb | 3 --- 1 file changed, 3 deletions(-) (limited to 'railties/test') diff --git a/railties/test/abstract_unit.rb b/railties/test/abstract_unit.rb index dd9d0448b2..9a640bdbc5 100644 --- a/railties/test/abstract_unit.rb +++ b/railties/test/abstract_unit.rb @@ -12,9 +12,6 @@ require 'stringio' require 'rubygems' require 'test/unit' -gem 'mocha', '>= 0.9.5' -require 'mocha' - require 'active_support' require 'active_support/test_case' -- cgit v1.2.3