diff options
Diffstat (limited to 'railties/test')
29 files changed, 646 insertions, 51 deletions
diff --git a/railties/test/abstract_unit.rb b/railties/test/abstract_unit.rb index 0addcb8bf3..9a640bdbc5 100644 --- a/railties/test/abstract_unit.rb +++ b/railties/test/abstract_unit.rb @@ -1,7 +1,10 @@ +ORIG_ARGV = ARGV.dup + $:.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" @@ -9,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' diff --git a/railties/test/gem_dependency_test.rb b/railties/test/gem_dependency_test.rb index ff27af5572..92132be992 100644 --- a/railties/test/gem_dependency_test.rb +++ b/railties/test/gem_dependency_test.rb @@ -166,12 +166,25 @@ class GemDependencyTest < Test::Unit::TestCase dummy_gem.unpack end + def test_gem_from_directory_name_attempts_to_load_specification + assert_raises RuntimeError do + dummy_gem = Rails::GemDependency.from_directory_name('dummy-gem-1.1') + end + end + def test_gem_from_directory_name - dummy_gem = Rails::GemDependency.from_directory_name('dummy-gem-1.1') + dummy_gem = Rails::GemDependency.from_directory_name('dummy-gem-1.1', false) assert_equal 'dummy-gem', dummy_gem.name assert_equal '= 1.1', dummy_gem.version_requirements.to_s end + def test_gem_from_directory_name_loads_specification_successfully + assert_nothing_raised do + dummy_gem = Rails::GemDependency.from_directory_name(File.join(Rails::GemDependency.unpacked_path, 'dummy-gem-g-1.0.0')) + assert_not_nil dummy_gem.specification + end + end + def test_gem_from_invalid_directory_name assert_raises RuntimeError do dummy_gem = Rails::GemDependency.from_directory_name('dummy-gem') @@ -186,5 +199,22 @@ class GemDependencyTest < Test::Unit::TestCase assert_equal true, Rails::GemDependency.new("dummy-gem-i").built? assert_equal false, Rails::GemDependency.new("dummy-gem-j").built? end + + def test_gem_determines_build_status_only_on_vendor_gems + framework_gem = Rails::GemDependency.new('dummy-framework-gem') + framework_gem.stubs(:framework_gem?).returns(true) # already loaded + framework_gem.stubs(:vendor_rails?).returns(false) # but not in vendor/rails + framework_gem.stubs(:vendor_gem?).returns(false) # and not in vendor/gems + framework_gem.add_load_paths # freeze framework gem early + assert framework_gem.built? + end + + def test_gem_build_passes_options_to_dependencies + start_gem = Rails::GemDependency.new("dummy-gem-g") + dep_gem = Rails::GemDependency.new("dummy-gem-f") + start_gem.stubs(:dependencies).returns([dep_gem]) + dep_gem.expects(:build).with({ :force => true }).once + start_gem.build(:force => true) + end end 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 diff --git a/railties/test/initializer/check_ruby_version_test.rb b/railties/test/initializer/check_ruby_version_test.rb new file mode 100644 index 0000000000..33de653906 --- /dev/null +++ b/railties/test/initializer/check_ruby_version_test.rb @@ -0,0 +1,51 @@ +require "initializer/test_helper" + +module InitializerTests + class PathsTest < ActiveSupport::TestCase + include ActiveSupport::Testing::Isolation + + test "rails does not initialize with ruby version 1.8.1" do + assert_rails_does_not_boot "1.8.1" + end + + test "rails initializes with ruby version 1.8.2" do + assert_rails_boots "1.8.2" + end + + test "rails does not initialize with ruby version 1.8.3" do + assert_rails_does_not_boot "1.8.3" + end + + test "rails initializes with ruby version 1.8.4" do + assert_rails_boots "1.8.4" + end + + test "rails initializes with ruby version 1.8.5" do + assert_rails_boots "1.8.5" + end + + test "rails initializes with ruby version 1.8.6" do + assert_rails_boots "1.8.6" + end + + def set_ruby_version(version) + $-w = nil + Object.const_set(:RUBY_VERSION, version.freeze) + end + + def assert_rails_boots(version) + set_ruby_version(version) + assert_nothing_raised "It appears that rails does not boot" do + Rails::Initializer.run { |c| c.frameworks = [] } + end + end + + def assert_rails_does_not_boot(version) + set_ruby_version(version) + $stderr = File.open("/dev/null", "w") + assert_raises(SystemExit) do + Rails::Initializer.run { |c| c.frameworks = [] } + end + end + end +end diff --git a/railties/test/initializer/install_gem_spec_stubs_test.rb b/railties/test/initializer/install_gem_spec_stubs_test.rb new file mode 100644 index 0000000000..2e94c9968f --- /dev/null +++ b/railties/test/initializer/install_gem_spec_stubs_test.rb @@ -0,0 +1,85 @@ +require "initializer/test_helper" + +module InitializerTests + class GemSpecStubsTest < ActiveSupport::TestCase + include ActiveSupport::Testing::Isolation + + def setup + $stderr = StringIO.new + end + + test "user has an old boot.rb (defined by having no Rails.vendor_rails?)" do + class << Rails + undef vendor_rails? + end + + assert_stderr(/outdated/) do + assert_raises(SystemExit) do + Rails::Initializer.run { |c| c.frameworks = [] } + end + end + end + + test "requires rubygems" do + Kernel.module_eval do + alias old_require require + def require(name) + $rubygems_required = true if name == "rubygems" + old_require(name) + end + end + + Rails.vendor_rails = true + Rails::Initializer.run { |c| c.frameworks = [] } + assert $rubygems_required + end + + test "does not fail if rubygems does not exist" do + Kernel.module_eval do + alias old_require require + def require(name) + raise LoadError if name == "rubygems" + old_require(name) + end + end + + assert_nothing_raised do + Rails::Initializer.run { |c| c.frameworks = [] } + end + end + + test "adds fake Rubygems stubs if a framework is not loaded in Rubygems and we've vendored" do + Rails.vendor_rails = true + + Rails::Initializer.run { |c| c.frameworks = [] } + + %w(rails activesupport activerecord actionpack actionmailer activeresource).each do |stub| + gem_spec = Gem.loaded_specs[stub] + assert_equal Gem::Version.new(Rails::VERSION::STRING), gem_spec.version + assert_equal stub, gem_spec.name + assert_equal "", gem_spec.loaded_from + end + end + + test "doesn't replace gem specs that are already loaded" do + Rails.vendor_rails = true + + Gem.loaded_specs["rails"] = Gem::Specification.new do |s| + s.name = "rails" + s.version = Rails::VERSION::STRING + s.loaded_from = "/foo/bar/baz" + end + + Rails::Initializer.run { |c| c.frameworks = [] } + + assert_equal "/foo/bar/baz", Gem.loaded_specs["rails"].loaded_from + + %w(activesupport activerecord actionpack actionmailer activeresource).each do |stub| + gem_spec = Gem.loaded_specs[stub] + assert_equal Gem::Version.new(Rails::VERSION::STRING), gem_spec.version + assert_equal stub, gem_spec.name + assert_equal "", gem_spec.loaded_from + end + end + end +end
\ No newline at end of file diff --git a/railties/test/initializer/path_test.rb b/railties/test/initializer/path_test.rb new file mode 100644 index 0000000000..26f796f93d --- /dev/null +++ b/railties/test/initializer/path_test.rb @@ -0,0 +1,96 @@ +require "initializer/test_helper" + +class PathsTest < ActiveSupport::TestCase + include ActiveSupport::Testing::Isolation + + def self.setup + Rails::Initializer.run do |config| + config.frameworks = [:action_controller, :action_view, :action_mailer, :active_record] + end + end + + 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 + + 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 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 --- /dev/null +++ b/railties/test/initializer/root/app/controllers/.keep 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 --- /dev/null +++ b/railties/test/initializer/root/app/helpers/.keep 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 --- /dev/null +++ b/railties/test/initializer/root/app/metal/.keep 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 --- /dev/null +++ b/railties/test/initializer/root/app/models/.keep 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 --- /dev/null +++ b/railties/test/initializer/root/app/views/.keep 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 --- /dev/null +++ b/railties/test/initializer/root/config/environments/.keep 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 --- /dev/null +++ b/railties/test/initializer/root/config/locales/.keep 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 --- /dev/null +++ b/railties/test/initializer/root/config/routes.rb diff --git a/railties/test/initializer/root/lib/.keep b/railties/test/initializer/root/lib/.keep new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/railties/test/initializer/root/lib/.keep diff --git a/railties/test/initializer/root/tmp/.keep b/railties/test/initializer/root/tmp/.keep new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/railties/test/initializer/root/tmp/.keep 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 --- /dev/null +++ b/railties/test/initializer/root/tmp/cache/.keep diff --git a/railties/test/initializer/root/vendor/.keep b/railties/test/initializer/root/vendor/.keep new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/railties/test/initializer/root/vendor/.keep diff --git a/railties/test/initializer/test_helper.rb b/railties/test/initializer/test_helper.rb new file mode 100644 index 0000000000..ddb03397ab --- /dev/null +++ b/railties/test/initializer/test_helper.rb @@ -0,0 +1,24 @@ +require 'abstract_unit' +require 'active_support/ruby/shim' +require 'initializer' + +RAILS_ROOT.replace File.join(File.dirname(__FILE__), "root") + +module Rails + class << self + attr_accessor :vendor_rails + def vendor_rails?() @vendor_rails end + end +end + +class ActiveSupport::TestCase + def assert_stderr(match) + $stderr = StringIO.new + yield + $stderr.rewind + err = $stderr.read + assert_match match, err + ensure + $stderr = STDERR + end +end
\ No newline at end of file diff --git a/railties/test/initializer_test.rb b/railties/test/initializer_test.rb index bdda319916..550cb7de76 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,101 +115,99 @@ 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 -require File.dirname(__FILE__) + '/plugin_test_helper' +require 'plugin_test_helper' class InitializerPluginLoadingTests < Test::Unit::TestCase def setup @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 - assert_equal [], @initializer.loaded_plugins + @initializer.run :load_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 @@ -216,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 @@ -224,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 @@ -232,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 @@ -251,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 @@ -265,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')) @@ -274,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 @@ -312,9 +311,10 @@ class InitializerSetupI18nTests < Test::Unit::TestCase assert_equal [ File.expand_path(File.dirname(__FILE__) + "/../../activesupport/lib/active_support/locale/en.yml"), File.expand_path(File.dirname(__FILE__) + "/../../actionpack/lib/action_view/locale/en.yml"), + 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 diff --git a/railties/test/new_initializer_test.rb b/railties/test/new_initializer_test.rb new file mode 100644 index 0000000000..8d9ef7bee3 --- /dev/null +++ b/railties/test/new_initializer_test.rb @@ -0,0 +1,165 @@ +require 'abstract_unit' +require 'active_support/ruby/shim' +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 "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 + + 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 diff --git a/railties/test/paths_test.rb b/railties/test/paths_test.rb new file mode 100644 index 0000000000..fa2f6ceee2 --- /dev/null +++ b/railties/test/paths_test.rb @@ -0,0 +1,126 @@ +require 'abstract_unit' +require 'rails/paths' + +class PathsTest < ActiveSupport::TestCase + + def setup + @root = Rails::Application::Root.new("/foo/bar") + end + + test "the paths object is initialized with the root path" do + 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_a + end + + test "relative paths are relative to the paths root" do + @root.app = "app" + 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_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_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 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" + 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.concat ["/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.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.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.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.paths.first }.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 + + 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 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') 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/plugin_test_helper.rb b/railties/test/plugin_test_helper.rb index 55d1a1fa96..893095fa66 100644 --- a/railties/test/plugin_test_helper.rb +++ b/railties/test/plugin_test_helper.rb @@ -4,7 +4,7 @@ $:.unshift File.dirname(__FILE__) + "/../../activesupport/lib" require 'test/unit' require 'active_support' require 'initializer' -require File.join(File.dirname(__FILE__), 'abstract_unit') +require 'abstract_unit' # We need to set RAILS_ROOT if it isn't already set RAILS_ROOT = '.' unless defined?(RAILS_ROOT) 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 diff --git a/railties/test/rails_info_controller_test.rb b/railties/test/rails_info_controller_test.rb index ab31f3a487..6a60908859 100644 --- a/railties/test/rails_info_controller_test.rb +++ b/railties/test/rails_info_controller_test.rb @@ -9,6 +9,12 @@ ActionController::Routing::Routes.draw do |map| map.connect ':controller/:action/:id' end +module ActionController + class Base + include ActionController::Testing + end +end + class InfoControllerTest < ActionController::TestCase tests Rails::InfoController |