Browse files

Add tests for explicit engines.

  • Loading branch information...
1 parent 7adb1ff commit f8bf1982dff9cf0f35fb7a121932c794ecdc1cb1 @josevalim josevalim committed Jan 26, 2010
View
5 railties/lib/rails/application.rb
@@ -6,6 +6,7 @@
module Rails
class Application < Engine
autoload :Bootstrap, 'rails/application/bootstrap'
+ autoload :Configurable, 'rails/application/configurable'
autoload :Configuration, 'rails/application/configuration'
autoload :Finisher, 'rails/application/finisher'
autoload :Railties, 'rails/application/railties'
@@ -41,10 +42,6 @@ def require_environment!
require environment if environment
end
- def config
- @config ||= Application::Configuration.new(self.class.find_root_with_flag("config.ru", Dir.pwd))
- end
-
def routes
::ActionController::Routing::Routes
end
View
19 railties/lib/rails/application/configurable.rb
@@ -0,0 +1,19 @@
+module Rails
+ class Application
+ module Configurable
+ def self.included(base)
+ base.extend ClassMethods
+ end
+
+ module ClassMethods
+ def inherited(base)
+ raise "You cannot inherit from a Rails::Application child"
+ end
+ end
+
+ def config
+ @config ||= Application::Configuration.new(self.class.find_root_with_flag("config.ru", Dir.pwd))
+ end
+ end
+ end
+end
View
10 railties/lib/rails/railtie.rb
@@ -17,7 +17,7 @@ def subclasses
def inherited(base)
unless abstract_railtie?(base)
- base.send(:include, self::Configurable) if add_configurable?(base)
+ base.send(:include, self::Configurable)
subclasses << base
end
end
@@ -53,14 +53,6 @@ def generators(&blk)
def abstract_railtie?(base)
ABSTRACT_RAILTIES.include?(base.name)
end
-
- # Just add configurable behavior if a Configurable module is defined
- # and the class is a direct child from self. This is required to avoid
- # application or plugins getting class configuration method from Railties
- # and/or Engines.
- def add_configurable?(base)
- defined?(self::Configurable) && base.ancestors[1] == self
- end
end
def rake_tasks
View
21 railties/test/isolation/abstract_unit.rb
@@ -100,6 +100,8 @@ def build_app(options = {})
end
class Bukkit
+ attr_reader :path
+
def initialize(path)
@path = path
end
@@ -118,10 +120,29 @@ def delete(file)
def plugin(name, string = "")
dir = "#{app_path}/vendor/plugins/#{name}"
FileUtils.mkdir_p(dir)
+
File.open("#{dir}/init.rb", 'w') do |f|
f.puts "::#{name.upcase} = 'loaded'"
f.puts string
end
+
+ Bukkit.new(dir).tap do |bukkit|
+ yield bukkit if block_given?
+ end
+ end
+
+ def engine(name)
+ dir = "#{app_path}/random/#{name}"
+ FileUtils.mkdir_p(dir)
+
+ app = File.readlines("#{app_path}/config/application.rb")
+ app.insert(2, "$:.unshift(\"#{dir}/lib\")")
+ app.insert(3, "require #{name.inspect}")
+
+ File.open("#{app_path}/config/application.rb", 'r+') do |f|
+ f.puts app
+ end
+
Bukkit.new(dir).tap do |bukkit|
yield bukkit if block_given?
end
View
23 railties/test/railties/engine_test.rb
@@ -0,0 +1,23 @@
+require "isolation/abstract_unit"
+require "railties/shared_tests"
+
+module RailtiesTest
+ class EngineTest < Test::Unit::TestCase
+ include ActiveSupport::Testing::Isolation
+ include SharedTests
+
+ def setup
+ build_app
+
+ @plugin = engine "bukkits" do |plugin|
+ plugin.write "lib/bukkits.rb", <<-RUBY
+ class Bukkits
+ class Engine < ::Rails::Engine
+ end
+ end
+ RUBY
+ plugin.write "lib/another.rb", "class Another; end"
+ end
+ end
+ end
+end
View
41 railties/test/railties/plugin_test.rb
@@ -2,7 +2,7 @@
require "railties/shared_tests"
module RailtiesTest
- class PluginSpecificTest < Test::Unit::TestCase
+ class PluginTest < Test::Unit::TestCase
include ActiveSupport::Testing::Isolation
include SharedTests
@@ -11,9 +11,16 @@ def setup
@plugin = plugin "bukkits", "::LEVEL = config.log_level" do |plugin|
plugin.write "lib/bukkits.rb", "class Bukkits; end"
+ plugin.write "lib/another.rb", "class Another; end"
end
end
+ test "plugin can load the file with the same name in lib" do
+ boot_rails
+ require "bukkits"
+ assert_equal "Bukkits", Bukkits.name
+ end
+
test "it loads the plugin's init.rb file" do
boot_rails
assert_equal "loaded", BUKKITS
@@ -24,6 +31,20 @@ def setup
assert_equal :debug, LEVEL
end
+ test "plugin_init_is_ran_before_application_ones" do
+ plugin "foo", "$foo = true" do |plugin|
+ plugin.write "lib/foo.rb", "module Foo; end"
+ end
+
+ app_file 'config/initializers/foo.rb', <<-RUBY
+ raise "no $foo" unless $foo
+ raise "no Foo" unless Foo
+ RUBY
+
+ boot_rails
+ assert $foo
+ end
+
test "plugin should work without init.rb" do
@plugin.delete("init.rb")
@@ -56,5 +77,23 @@ class Engine < Rails::Engine
assert rescued, "Expected boot rails to fail"
end
+
+ test "deprecated tasks are also loaded" do
+ $executed = false
+ @plugin.write "tasks/foo.rake", <<-RUBY
+ task :foo do
+ $executed = true
+ end
+ RUBY
+
+ boot_rails
+ require 'rake'
+ require 'rake/rdoctask'
+ require 'rake/testtask'
+ Rails.application.load_tasks
+ Rake::Task[:foo].invoke
+ assert $executed
+ end
+
end
end
View
51 railties/test/railties/shared_tests.rb
@@ -12,35 +12,21 @@ def app
def test_plugin_puts_its_lib_directory_on_load_path
boot_rails
- require "bukkits"
- assert_equal "Bukkits", Bukkits.name
- end
-
- def test_plugin_init_is_ran_before_application_ones
- plugin "foo", "$foo = true" do |plugin|
- plugin.write "lib/foo.rb", "module Foo; end"
- end
-
- app_file 'config/initializers/foo.rb', <<-RUBY
- raise "no $foo" unless $foo
- raise "no Foo" unless Foo
- RUBY
-
- boot_rails
- assert $foo
+ require "another"
+ assert_equal "Another", Another.name
end
def test_plugin_paths_get_added_to_as_dependency_list
boot_rails
- assert_equal "Bukkits", Bukkits.name
+ assert_equal "Another", Another.name
end
def test_plugins_constants_are_not_reloaded_by_default
boot_rails
- assert_equal "Bukkits", Bukkits.name
+ assert_equal "Another", Another.name
ActiveSupport::Dependencies.clear
- @plugin.delete("lib/bukkits.rb")
- assert_nothing_raised { Bukkits }
+ @plugin.delete("lib/another.rb")
+ assert_nothing_raised { Another }
end
def test_plugin_constants_get_reloaded_if_config_reload_plugins
@@ -50,10 +36,10 @@ def test_plugin_constants_get_reloaded_if_config_reload_plugins
boot_rails
- assert_equal "Bukkits", Bukkits.name
+ assert_equal "Another", Another.name
ActiveSupport::Dependencies.clear
- @plugin.delete("lib/bukkits.rb")
- assert_raises(NameError) { Bukkits }
+ @plugin.delete("lib/another.rb")
+ assert_raises(NameError) { Another }
end
def test_plugin_puts_its_models_directory_on_load_path
@@ -190,23 +176,6 @@ def test_rake_tasks_lib_tasks_are_loaded
assert $executed
end
- def test_deprecated_tasks_are_also_loaded
- $executed = false
- @plugin.write "tasks/foo.rake", <<-RUBY
- task :foo do
- $executed = true
- end
- RUBY
-
- boot_rails
- require 'rake'
- require 'rake/rdoctask'
- require 'rake/testtask'
- Rails.application.load_tasks
- Rake::Task[:foo].invoke
- assert $executed
- end
-
def test_i18n_files_have_lower_priority_than_application_ones
add_to_config <<-RUBY
config.i18n.load_path << "#{app_path}/app/locales/en.yml"
@@ -235,7 +204,7 @@ def test_i18n_files_have_lower_priority_than_application_ones
#{RAILS_FRAMEWORK_ROOT}/activemodel/lib/active_model/locale/en.yml
#{RAILS_FRAMEWORK_ROOT}/activerecord/lib/active_record/locale/en.yml
#{RAILS_FRAMEWORK_ROOT}/actionpack/lib/action_view/locale/en.yml
- #{app_path}/vendor/plugins/bukkits/config/locales/en.yml
+ #{@plugin.path}/config/locales/en.yml
#{app_path}/config/locales/en.yml
#{app_path}/app/locales/en.yml
).map { |path| File.expand_path(path) }, I18n.load_path.map { |path| File.expand_path(path) }

0 comments on commit f8bf198

Please sign in to comment.