Refactor railties test, break huge files in smaller chunks and move initializers to application folder.

This commit is contained in:
José Valim
2010-01-26 13:57:11 +01:00
parent e6da2f651f
commit 1dca7ebc93
16 changed files with 632 additions and 552 deletions

View File

@@ -1,7 +1,7 @@
require "isolation/abstract_unit"
module ApplicationTests
class InitializerTest < Test::Unit::TestCase
class ConfigurationTest < Test::Unit::TestCase
include ActiveSupport::Testing::Isolation
def new_app
@@ -59,21 +59,12 @@ module ApplicationTests
end
end
test "if there's no config.active_support.bare, all of ActiveSupport is required" do
use_frameworks []
test "Rails.root should be a Pathname" do
add_to_config <<-RUBY
config.root = "#{app_path}"
RUBY
require "#{app_path}/config/environment"
assert_nothing_raised { [1,2,3].rand }
end
test "config.active_support.bare does not require all of ActiveSupport" do
add_to_config "config.active_support.bare = true"
use_frameworks []
Dir.chdir("#{app_path}/app") do
require "#{app_path}/config/environment"
assert_raises(NoMethodError) { [1,2,3].rand }
end
assert_instance_of Pathname, Rails.root
end
test "marking the application as threadsafe sets the correct config variables" do
@@ -136,7 +127,7 @@ module ApplicationTests
value = value.reverse if key =~ /baz/
}]
RUBY
assert_nothing_raised do
require "#{app_path}/config/application"
end

View File

@@ -92,18 +92,5 @@ module ApplicationTests
assert_equal({ :plugin => { :generator => "-g" } }, c.generators.aliases)
end
end
test "generators with hashes are deep merged" do
with_config do |c|
c.generators do |g|
g.orm :datamapper, :migration => false
g.plugin :aliases => { :generator => "-g" },
:generator => true
end
end
assert Rails::Generators.aliases.size >= 1
assert Rails::Generators.options.size >= 1
end
end
end

View File

@@ -1,196 +0,0 @@
require "isolation/abstract_unit"
module ApplicationTests
class InitializerTest < Test::Unit::TestCase
include ActiveSupport::Testing::Isolation
def setup
build_app
boot_rails
FileUtils.rm_rf "#{app_path}/config/environments"
end
test "initializing an application adds the application paths to the load path" do
add_to_config <<-RUBY
config.root = "#{app_path}"
RUBY
require "#{app_path}/config/environment"
assert $:.include?("#{app_path}/app/models")
end
test "eager loading loads parent classes before children" do
app_file "lib/zoo.rb", <<-ZOO
class Zoo ; include ReptileHouse ; end
ZOO
app_file "lib/zoo/reptile_house.rb", <<-ZOO
module Zoo::ReptileHouse ; end
ZOO
add_to_config <<-RUBY
config.root = "#{app_path}"
config.eager_load_paths << "#{app_path}/lib"
RUBY
require "#{app_path}/config/environment"
assert Zoo
end
test "load environment with global" do
app_file "config/environments/development.rb", <<-RUBY
$initialize_test_set_from_env = 'success'
AppTemplate::Application.configure do
config.cache_classes = true
config.time_zone = "Brasilia"
end
RUBY
assert_nil $initialize_test_set_from_env
add_to_config <<-RUBY
config.root = "#{app_path}"
config.time_zone = "UTC"
RUBY
require "#{app_path}/config/environment"
assert_equal "success", $initialize_test_set_from_env
assert AppTemplate::Application.config.cache_classes
assert_equal "Brasilia", AppTemplate::Application.config.time_zone
end
test "action_controller load paths set only if action controller in use" do
assert_nothing_raised NameError do
add_to_config <<-RUBY
config.root = "#{app_path}"
RUBY
use_frameworks []
require "#{app_path}/config/environment"
end
end
test "after_initialize block works correctly" do
add_to_config <<-RUBY
config.root = "#{app_path}"
config.after_initialize { $test_after_initialize_block1 = "success" }
config.after_initialize { $test_after_initialize_block2 = "congratulations" }
RUBY
require "#{app_path}/config/environment"
assert_equal "success", $test_after_initialize_block1
assert_equal "congratulations", $test_after_initialize_block2
end
test "after_initialize block works correctly when no block is passed" do
add_to_config <<-RUBY
config.root = "#{app_path}"
config.after_initialize { $test_after_initialize_block1 = "success" }
config.after_initialize # don't pass a block, this is what we're testing!
config.after_initialize { $test_after_initialize_block2 = "congratulations" }
RUBY
require "#{app_path}/config/environment"
assert_equal "success", $test_after_initialize_block1
assert_equal "congratulations", $test_after_initialize_block2
end
test "after_initialize runs after frameworks have been initialized" do
$activerecord_configurations = nil
add_to_config <<-RUBY
config.after_initialize { $activerecord_configurations = ActiveRecord::Base.configurations }
RUBY
require "#{app_path}/config/environment"
assert $activerecord_configurations
assert $activerecord_configurations['development']
end
# i18n
test "setting another default locale" do
add_to_config <<-RUBY
config.root = "#{app_path}"
config.i18n.default_locale = :de
RUBY
require "#{app_path}/config/environment"
assert_equal :de, I18n.default_locale
end
test "no config locales dir present should return empty load path" do
FileUtils.rm_rf "#{app_path}/config/locales"
add_to_config <<-RUBY
config.root = "#{app_path}"
RUBY
require "#{app_path}/config/environment"
assert_equal [], Rails.application.config.i18n.load_path
end
test "config locales dir present should be added to load path" do
add_to_config <<-RUBY
config.root = "#{app_path}"
RUBY
require "#{app_path}/config/environment"
assert_equal ["#{app_path}/config/locales/en.yml"], Rails.application.config.i18n.load_path
end
test "config defaults should be added with config settings" do
add_to_config <<-RUBY
config.root = "#{app_path}"
config.i18n.load_path << "my/other/locale.yml"
RUBY
require "#{app_path}/config/environment"
assert_equal [
"#{app_path}/config/locales/en.yml", "my/other/locale.yml"
], Rails.application.config.i18n.load_path
end
# DB middleware
test "database middleware doesn't initialize when session store is not active_record" do
add_to_config <<-RUBY
config.root = "#{app_path}"
config.action_controller.session_store = :cookie_store
RUBY
require "#{app_path}/config/environment"
assert !Rails.application.config.middleware.include?(ActiveRecord::SessionStore)
end
test "database middleware initializes when session store is active record" do
add_to_config "config.action_controller.session_store = :active_record_store"
require "#{app_path}/config/environment"
expects = [ActiveRecord::ConnectionAdapters::ConnectionManagement, ActiveRecord::QueryCache, ActiveRecord::SessionStore]
middleware = Rails.application.config.middleware.map { |m| m.klass }
assert_equal expects, middleware & expects
end
test "Rails.root should be a Pathname" do
add_to_config <<-RUBY
config.root = "#{app_path}"
RUBY
require "#{app_path}/config/environment"
assert_instance_of Pathname, Rails.root
end
end
class InitializerCustomFrameworkExtensionsTest < Test::Unit::TestCase
include ActiveSupport::Testing::Isolation
def setup
build_app
boot_rails
FileUtils.rm_rf "#{app_path}/config/environments"
end
test "database middleware doesn't initialize when activerecord is not in frameworks" do
use_frameworks []
require "#{app_path}/config/environment"
assert_nil defined?(ActiveRecord)
end
end
end

View File

@@ -1,6 +1,6 @@
require "isolation/abstract_unit"
module BootTests
module ApplicationTests
class GemBooting < Test::Unit::TestCase
include ActiveSupport::Testing::Isolation

View File

@@ -1,6 +1,6 @@
require "isolation/abstract_unit"
module InitializerTests
module ApplicationTests
class CheckRubyVersionTest < Test::Unit::TestCase
include ActiveSupport::Testing::Isolation

View File

@@ -0,0 +1,80 @@
require "isolation/abstract_unit"
module ApplicationTests
class FrameworlsTest < Test::Unit::TestCase
include ActiveSupport::Testing::Isolation
def setup
build_app
boot_rails
FileUtils.rm_rf "#{app_path}/config/environments"
end
# AC & AM
test "set load paths set only if action controller or action mailer are in use" do
assert_nothing_raised NameError do
add_to_config <<-RUBY
config.root = "#{app_path}"
RUBY
use_frameworks []
require "#{app_path}/config/environment"
end
end
test "sets action_controller and action_mailer load paths" do
add_to_config <<-RUBY
config.root = "#{app_path}"
RUBY
require "#{app_path}/config/environment"
ActionController::Base.view_paths.include?(File.expand_path("app/views", app_path))
ActionMailer::Base.view_paths.include?(File.expand_path("app/views", app_path))
end
# AS
test "if there's no config.active_support.bare, all of ActiveSupport is required" do
use_frameworks []
require "#{app_path}/config/environment"
assert_nothing_raised { [1,2,3].rand }
end
test "config.active_support.bare does not require all of ActiveSupport" do
add_to_config "config.active_support.bare = true"
use_frameworks []
Dir.chdir("#{app_path}/app") do
require "#{app_path}/config/environment"
assert_raises(NoMethodError) { [1,2,3].rand }
end
end
# AR
test "database middleware doesn't initialize when session store is not active_record" do
add_to_config <<-RUBY
config.root = "#{app_path}"
config.action_controller.session_store = :cookie_store
RUBY
require "#{app_path}/config/environment"
assert !Rails.application.config.middleware.include?(ActiveRecord::SessionStore)
end
test "database middleware initializes when session store is active record" do
add_to_config "config.action_controller.session_store = :active_record_store"
require "#{app_path}/config/environment"
expects = [ActiveRecord::ConnectionAdapters::ConnectionManagement, ActiveRecord::QueryCache, ActiveRecord::SessionStore]
middleware = Rails.application.config.middleware.map { |m| m.klass }
assert_equal expects, middleware & expects
end
test "database middleware doesn't initialize when activerecord is not in frameworks" do
use_frameworks []
require "#{app_path}/config/environment"
assert_nil defined?(ActiveRecord)
end
end
end

View File

@@ -0,0 +1,55 @@
require "isolation/abstract_unit"
module ApplicationTests
class I18nTest < Test::Unit::TestCase
include ActiveSupport::Testing::Isolation
def setup
build_app
boot_rails
FileUtils.rm_rf "#{app_path}/config/environments"
end
# i18n
test "setting another default locale" do
add_to_config <<-RUBY
config.root = "#{app_path}"
config.i18n.default_locale = :de
RUBY
require "#{app_path}/config/environment"
assert_equal :de, I18n.default_locale
end
test "no config locales dir present should return empty load path" do
FileUtils.rm_rf "#{app_path}/config/locales"
add_to_config <<-RUBY
config.root = "#{app_path}"
RUBY
require "#{app_path}/config/environment"
assert_equal [], Rails.application.config.i18n.load_path
end
test "config locales dir present should be added to load path" do
add_to_config <<-RUBY
config.root = "#{app_path}"
RUBY
require "#{app_path}/config/environment"
assert_equal ["#{app_path}/config/locales/en.yml"], Rails.application.config.i18n.load_path
end
test "config defaults should be added with config settings" do
add_to_config <<-RUBY
config.root = "#{app_path}"
config.i18n.load_path << "my/other/locale.yml"
RUBY
require "#{app_path}/config/environment"
assert_equal [
"#{app_path}/config/locales/en.yml", "my/other/locale.yml"
], Rails.application.config.i18n.load_path
end
end
end

View File

@@ -0,0 +1,55 @@
require "isolation/abstract_unit"
module ApplicationTests
class InitializersTest < Test::Unit::TestCase
include ActiveSupport::Testing::Isolation
def setup
build_app
boot_rails
FileUtils.rm_rf "#{app_path}/config/environments"
end
test "load initializers" do
app_file "config/initializers/foo.rb", "$foo = true"
require "#{app_path}/config/environment"
assert $foo
end
test "after_initialize block works correctly" do
add_to_config <<-RUBY
config.root = "#{app_path}"
config.after_initialize { $test_after_initialize_block1 = "success" }
config.after_initialize { $test_after_initialize_block2 = "congratulations" }
RUBY
require "#{app_path}/config/environment"
assert_equal "success", $test_after_initialize_block1
assert_equal "congratulations", $test_after_initialize_block2
end
test "after_initialize block works correctly when no block is passed" do
add_to_config <<-RUBY
config.root = "#{app_path}"
config.after_initialize { $test_after_initialize_block1 = "success" }
config.after_initialize # don't pass a block, this is what we're testing!
config.after_initialize { $test_after_initialize_block2 = "congratulations" }
RUBY
require "#{app_path}/config/environment"
assert_equal "success", $test_after_initialize_block1
assert_equal "congratulations", $test_after_initialize_block2
end
test "after_initialize runs after frameworks have been initialized" do
$activerecord_configurations = nil
add_to_config <<-RUBY
config.after_initialize { $activerecord_configurations = ActiveRecord::Base.configurations }
RUBY
require "#{app_path}/config/environment"
assert $activerecord_configurations
assert $activerecord_configurations['development']
end
end
end

View File

@@ -0,0 +1,62 @@
require "isolation/abstract_unit"
module ApplicationTests
class LoadPathTest < Test::Unit::TestCase
include ActiveSupport::Testing::Isolation
def setup
build_app
boot_rails
FileUtils.rm_rf "#{app_path}/config/environments"
end
# General
test "initializing an application adds the application paths to the load path" do
add_to_config <<-RUBY
config.root = "#{app_path}"
RUBY
require "#{app_path}/config/environment"
assert $:.include?("#{app_path}/app/models")
end
test "eager loading loads parent classes before children" do
app_file "lib/zoo.rb", <<-ZOO
class Zoo ; include ReptileHouse ; end
ZOO
app_file "lib/zoo/reptile_house.rb", <<-ZOO
module Zoo::ReptileHouse ; end
ZOO
add_to_config <<-RUBY
config.root = "#{app_path}"
config.eager_load_paths << "#{app_path}/lib"
RUBY
require "#{app_path}/config/environment"
assert Zoo
end
test "load environment with global" do
app_file "config/environments/development.rb", <<-RUBY
$initialize_test_set_from_env = 'success'
AppTemplate::Application.configure do
config.cache_classes = true
config.time_zone = "Brasilia"
end
RUBY
assert_nil $initialize_test_set_from_env
add_to_config <<-RUBY
config.root = "#{app_path}"
config.time_zone = "UTC"
RUBY
require "#{app_path}/config/environment"
assert_equal "success", $initialize_test_set_from_env
assert AppTemplate::Application.config.cache_classes
assert_equal "Brasilia", AppTemplate::Application.config.time_zone
end
end
end

View File

@@ -1,7 +1,7 @@
require "isolation/abstract_unit"
module InitializerTests
class PathTest < Test::Unit::TestCase
module ApplicationTests
class PathsTest < Test::Unit::TestCase
include ActiveSupport::Testing::Isolation
def setup

View File

@@ -1,7 +1,7 @@
require "isolation/abstract_unit"
module ApplicationTests
class LoadTest < Test::Unit::TestCase
class RackupTest < Test::Unit::TestCase
include ActiveSupport::Testing::Isolation
def rackup

View File

@@ -14,7 +14,6 @@ module ApplicationTests
def app
@app ||= begin
require "#{app_path}/config/environment"
Rails.application
end
end
@@ -26,7 +25,7 @@ module ApplicationTests
test "simple controller" do
controller :foo, <<-RUBY
class FooController < ActionController::Base
class FooController < ApplicationController
def index
render :text => "foo"
end
@@ -43,9 +42,36 @@ module ApplicationTests
assert_equal 'foo', last_response.body
end
test "simple controller with helper" do
controller :foo, <<-RUBY
class FooController < ApplicationController
def index
render :inline => "<%= foo_or_bar? %>"
end
end
RUBY
app_file 'app/helpers/bar_helper.rb', <<-RUBY
module BarHelper
def foo_or_bar?
"bar"
end
end
RUBY
app_file 'config/routes.rb', <<-RUBY
AppTemplate::Application.routes.draw do |map|
match ':controller(/:action)'
end
RUBY
get '/foo'
assert_equal 'bar', last_response.body
end
test "multiple controllers" do
controller :foo, <<-RUBY
class FooController < ActionController::Base
class FooController < ApplicationController
def index
render :text => "foo"
end
@@ -75,7 +101,7 @@ module ApplicationTests
test "nested controller" do
controller 'foo', <<-RUBY
class FooController < ActionController::Base
class FooController < ApplicationController
def index
render :text => "foo"
end
@@ -84,7 +110,7 @@ module ApplicationTests
controller 'admin/foo', <<-RUBY
module Admin
class FooController < ActionController::Base
class FooController < ApplicationController
def index
render :text => "admin::foo"
end
@@ -105,47 +131,9 @@ module ApplicationTests
assert_equal 'admin::foo', last_response.body
end
test "merges with plugin routes" do
controller 'foo', <<-RUBY
class FooController < ActionController::Base
def index
render :text => "foo"
end
end
RUBY
app_file 'config/routes.rb', <<-RUBY
AppTemplate::Application.routes.draw do |map|
match 'foo', :to => 'foo#index'
end
RUBY
plugin 'bar', 'require File.dirname(__FILE__) + "/app/controllers/bar"' do |plugin|
plugin.write 'app/controllers/bar.rb', <<-RUBY
class BarController < ActionController::Base
def index
render :text => "bar"
end
end
RUBY
plugin.write 'config/routes.rb', <<-RUBY
AppTemplate::Application.routes.draw do |map|
match 'bar', :to => 'bar#index'
end
RUBY
end
get '/foo'
assert_equal 'foo', last_response.body
get '/bar'
assert_equal 'bar', last_response.body
end
test "reloads routes when configuration is changed" do
controller :foo, <<-RUBY
class FooController < ActionController::Base
class FooController < ApplicationController
def bar
render :text => "bar"
end
@@ -191,7 +179,7 @@ module ApplicationTests
RUBY
controller 'yazilar', <<-RUBY
class YazilarController < ActionController::Base
class YazilarController < ApplicationController
def index
render :text => 'yazilar#index'
end

View File

@@ -46,6 +46,20 @@ module RailtiesTest
AppTemplate::Application.load_generators
assert $ran_block
end
test "railtie initializer" do
$ran_block = false
class MyTie < Rails::Railtie
initializer :something_nice do
$ran_block = true
end
end
assert !$ran_block
require "#{app_path}/config/environment"
assert $ran_block
end
end
class ActiveRecordExtensionTest < Test::Unit::TestCase

View File

@@ -1,8 +1,10 @@
require "isolation/abstract_unit"
require "railties/shared_tests"
module RailtiesTest
class PluginTest < Test::Unit::TestCase
class PluginSpecificTest < Test::Unit::TestCase
include ActiveSupport::Testing::Isolation
include SharedTests
def setup
build_app
@@ -12,15 +14,6 @@ module RailtiesTest
end
end
def boot_rails
super
require "#{app_path}/config/environment"
end
def app
@app ||= Rails.application
end
test "it loads the plugin's init.rb file" do
boot_rails
assert_equal "loaded", BUKKITS
@@ -31,52 +24,6 @@ module RailtiesTest
assert_equal :debug, LEVEL
end
test "the plugin puts its lib directory on the load path" do
boot_rails
require "bukkits"
assert_equal "Bukkits", Bukkits.name
end
test "plugin init is ran before application initializers" 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 paths get added to the AS::Dependency list" do
boot_rails
assert_equal "Bukkits", Bukkits.name
end
test "plugin constants do not get reloaded by default" do
boot_rails
assert_equal "Bukkits", Bukkits.name
ActiveSupport::Dependencies.clear
@plugin.delete("lib/bukkits.rb")
assert_nothing_raised { Bukkits }
end
test "plugin constants get reloaded if config.reload_plugins is set" do
add_to_config <<-RUBY
config.reload_plugins = true
RUBY
boot_rails
assert_equal "Bukkits", Bukkits.name
ActiveSupport::Dependencies.clear
@plugin.delete("lib/bukkits.rb")
assert_raises(NameError) { Bukkits }
end
test "plugin should work without init.rb" do
@plugin.delete("init.rb")
@@ -86,210 +33,6 @@ module RailtiesTest
assert_nothing_raised { Bukkits }
end
test "the plugin puts its models directory on the load path" do
@plugin.write "app/models/my_bukkit.rb", "class MyBukkit ; end"
boot_rails
assert_nothing_raised { MyBukkit }
end
test "the plugin puts is controllers directory on the load path" do
@plugin.write "app/controllers/bukkit_controller.rb", "class BukkitController ; end"
boot_rails
assert_nothing_raised { BukkitController }
end
test "the plugin adds its view to the load path" do
@plugin.write "app/controllers/bukkit_controller.rb", <<-RUBY
class BukkitController < ActionController::Base
def index
end
end
RUBY
@plugin.write "app/views/bukkit/index.html.erb", "Hello bukkits"
boot_rails
require "action_controller"
require "rack/mock"
response = BukkitController.action(:index).call(Rack::MockRequest.env_for("/"))
assert_equal "Hello bukkits\n", response[2].body
end
test "the plugin adds helpers to the controller's views" do
@plugin.write "app/controllers/bukkit_controller.rb", <<-RUBY
class BukkitController < ActionController::Base
def index
end
end
RUBY
@plugin.write "app/helpers/bukkit_helper.rb", <<-RUBY
module BukkitHelper
def bukkits
"bukkits"
end
end
RUBY
@plugin.write "app/views/bukkit/index.html.erb", "Hello <%= bukkits %>"
boot_rails
require "rack/mock"
response = BukkitController.action(:index).call(Rack::MockRequest.env_for("/"))
assert_equal "Hello bukkits\n", response[2].body
end
test "routes.rb are added to the router" do
@plugin.write "config/routes.rb", <<-RUBY
class Sprokkit
def self.call(env)
[200, {'Content-Type' => 'text/html'}, ["I am a Sprokkit"]]
end
end
ActionController::Routing::Routes.draw do
match "/sprokkit", :to => Sprokkit
end
RUBY
boot_rails
require "rack/mock"
response = Rails.application.call(Rack::MockRequest.env_for("/sprokkit"))
assert_equal "I am a Sprokkit", response[2].join
end
test "tasks are loaded by default" do
$executed = false
@plugin.write "lib/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
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
test "i18n files are added with lower priority than application ones" do
add_to_config <<-RUBY
config.i18n.load_path << "#{app_path}/app/locales/en.yml"
RUBY
app_file 'app/locales/en.yml', <<-YAML
en:
bar: "1"
YAML
app_file 'config/locales/en.yml', <<-YAML
en:
foo: "2"
bar: "2"
YAML
@plugin.write 'config/locales/en.yml', <<-YAML
en:
foo: "3"
YAML
boot_rails
assert_equal %W(
#{RAILS_FRAMEWORK_ROOT}/activesupport/lib/active_support/locale/en.yml
#{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
#{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) }
assert_equal "2", I18n.t(:foo)
assert_equal "1", I18n.t(:bar)
end
test "plugin metals are added to the middleware stack" do
@plugin.write 'app/metal/foo_metal.rb', <<-RUBY
class FooMetal
def self.call(env)
[200, { "Content-Type" => "text/html"}, ["FooMetal"]]
end
end
RUBY
boot_rails
require 'rack/test'
extend Rack::Test::Methods
get "/"
assert_equal 200, last_response.status
assert_equal "FooMetal", last_response.body
end
test "namespaced controllers with namespaced routes" do
@plugin.write "config/routes.rb", <<-RUBY
ActionController::Routing::Routes.draw do
namespace :admin do
match "index", :to => "admin/foo#index"
end
end
RUBY
@plugin.write "app/controllers/admin/foo_controller.rb", <<-RUBY
class Admin::FooController < ApplicationController
def index
render :text => "Rendered from namespace"
end
end
RUBY
boot_rails
require 'rack/test'
extend Rack::Test::Methods
get "/admin/index"
assert_equal 200, last_response.status
assert_equal "Rendered from namespace", last_response.body
end
test "plugin with initializers" do
$plugin_initializer = false
@plugin.write "config/initializers/foo.rb", <<-RUBY
$plugin_initializer = true
RUBY
boot_rails
assert $plugin_initializer
end
test "plugin cannot declare an engine for it" do
@plugin.write "lib/bukkits.rb", <<-RUBY
class Bukkits
@@ -313,22 +56,5 @@ YAML
assert rescued, "Expected boot rails to fail"
end
test "use plugin middleware in application config" do
@plugin.write "lib/bukkits.rb", <<-RUBY
class Bukkits
def initialize(app)
@app = app
end
def call(env)
@app.call(env)
end
end
RUBY
add_to_config "config.middleware.use \"Bukkits\""
boot_rails
end
end
end

View File

@@ -0,0 +1,318 @@
module RailtiesTest
# Holds tests shared between plugin and engines
module SharedTests
def boot_rails
super
require "#{app_path}/config/environment"
end
def app
@app ||= Rails.application
end
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
end
def test_plugin_paths_get_added_to_as_dependency_list
boot_rails
assert_equal "Bukkits", Bukkits.name
end
def test_plugins_constants_are_not_reloaded_by_default
boot_rails
assert_equal "Bukkits", Bukkits.name
ActiveSupport::Dependencies.clear
@plugin.delete("lib/bukkits.rb")
assert_nothing_raised { Bukkits }
end
def test_plugin_constants_get_reloaded_if_config_reload_plugins
add_to_config <<-RUBY
config.reload_plugins = true
RUBY
boot_rails
assert_equal "Bukkits", Bukkits.name
ActiveSupport::Dependencies.clear
@plugin.delete("lib/bukkits.rb")
assert_raises(NameError) { Bukkits }
end
def test_plugin_puts_its_models_directory_on_load_path
@plugin.write "app/models/my_bukkit.rb", "class MyBukkit ; end"
boot_rails
assert_nothing_raised { MyBukkit }
end
def test_plugin_puts_its_controllers_directory_on_the_load_path
@plugin.write "app/controllers/bukkit_controller.rb", "class BukkitController ; end"
boot_rails
assert_nothing_raised { BukkitController }
end
def test_plugin_adds_its_views_to_view_paths
@plugin.write "app/controllers/bukkit_controller.rb", <<-RUBY
class BukkitController < ActionController::Base
def index
end
end
RUBY
@plugin.write "app/views/bukkit/index.html.erb", "Hello bukkits"
boot_rails
require "action_controller"
require "rack/mock"
response = BukkitController.action(:index).call(Rack::MockRequest.env_for("/"))
assert_equal "Hello bukkits\n", response[2].body
end
def test_plugin_adds_helpers_to_controller_views
@plugin.write "app/controllers/bukkit_controller.rb", <<-RUBY
class BukkitController < ActionController::Base
def index
end
end
RUBY
@plugin.write "app/helpers/bukkit_helper.rb", <<-RUBY
module BukkitHelper
def bukkits
"bukkits"
end
end
RUBY
@plugin.write "app/views/bukkit/index.html.erb", "Hello <%= bukkits %>"
boot_rails
require "rack/mock"
response = BukkitController.action(:index).call(Rack::MockRequest.env_for("/"))
assert_equal "Hello bukkits\n", response[2].body
end
def test_routes_are_added_to_router
@plugin.write "config/routes.rb", <<-RUBY
class Sprokkit
def self.call(env)
[200, {'Content-Type' => 'text/html'}, ["I am a Sprokkit"]]
end
end
ActionController::Routing::Routes.draw do
match "/sprokkit", :to => Sprokkit
end
RUBY
boot_rails
require 'rack/test'
extend Rack::Test::Methods
get "/sprokkit"
assert_equal "I am a Sprokkit", last_response.body
end
def test_routes_in_plugins_have_lower_priority_than_application_ones
controller "foo", <<-RUBY
class FooController < ActionController::Base
def index
render :text => "foo"
end
end
RUBY
app_file "config/routes.rb", <<-RUBY
AppTemplate::Application.routes.draw do |map|
match 'foo', :to => 'foo#index'
end
RUBY
@plugin.write "app/controllers/bar_controller.rb", <<-RUBY
class BarController < ActionController::Base
def index
render :text => "bar"
end
end
RUBY
@plugin.write "config/routes.rb", <<-RUBY
ActionController::Routing::Routes.draw do |map|
match 'foo', :to => 'bar#index'
match 'bar', :to => 'bar#index'
end
RUBY
boot_rails
require 'rack/test'
extend Rack::Test::Methods
get '/foo'
assert_equal 'foo', last_response.body
get '/bar'
assert_equal 'bar', last_response.body
end
def test_rake_tasks_lib_tasks_are_loaded
$executed = false
@plugin.write "lib/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_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"
RUBY
app_file 'app/locales/en.yml', <<-YAML
en:
bar: "1"
YAML
app_file 'config/locales/en.yml', <<-YAML
en:
foo: "2"
bar: "2"
YAML
@plugin.write 'config/locales/en.yml', <<-YAML
en:
foo: "3"
YAML
boot_rails
assert_equal %W(
#{RAILS_FRAMEWORK_ROOT}/activesupport/lib/active_support/locale/en.yml
#{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
#{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) }
assert_equal "2", I18n.t(:foo)
assert_equal "1", I18n.t(:bar)
end
def test_plugin_metals_added_to_middleware_stack
@plugin.write 'app/metal/foo_metal.rb', <<-RUBY
class FooMetal
def self.call(env)
[200, { "Content-Type" => "text/html"}, ["FooMetal"]]
end
end
RUBY
boot_rails
require 'rack/test'
extend Rack::Test::Methods
get "/"
assert_equal 200, last_response.status
assert_equal "FooMetal", last_response.body
end
def test_namespaced_controllers_with_namespaced_routes
@plugin.write "config/routes.rb", <<-RUBY
ActionController::Routing::Routes.draw do
namespace :admin do
match "index", :to => "admin/foo#index"
end
end
RUBY
@plugin.write "app/controllers/admin/foo_controller.rb", <<-RUBY
class Admin::FooController < ApplicationController
def index
render :text => "Rendered from namespace"
end
end
RUBY
boot_rails
require 'rack/test'
extend Rack::Test::Methods
get "/admin/index"
assert_equal 200, last_response.status
assert_equal "Rendered from namespace", last_response.body
end
def test_plugin_initializers
$plugin_initializer = false
@plugin.write "config/initializers/foo.rb", <<-RUBY
$plugin_initializer = true
RUBY
boot_rails
assert $plugin_initializer
end
def test_plugin_midleware_referenced_in_configuration
@plugin.write "lib/bukkits.rb", <<-RUBY
class Bukkits
def initialize(app)
@app = app
end
def call(env)
@app.call(env)
end
end
RUBY
add_to_config "config.middleware.use \"Bukkits\""
boot_rails
end
end
end