aboutsummaryrefslogtreecommitdiffstats
path: root/railties/test
diff options
context:
space:
mode:
Diffstat (limited to 'railties/test')
-rw-r--r--railties/test/application/configuration_test.rb53
-rw-r--r--railties/test/application/console_test.rb8
-rw-r--r--railties/test/application/initializers/frameworks_test.rb4
-rw-r--r--railties/test/application/initializers/i18n_test.rb30
-rw-r--r--railties/test/application/middleware/show_exceptions_test.rb37
-rw-r--r--railties/test/application/middleware_test.rb18
-rw-r--r--railties/test/application/rake_test.rb36
-rw-r--r--railties/test/generators/app_generator_test.rb25
-rw-r--r--railties/test/generators/generators_test_helper.rb2
-rw-r--r--railties/test/generators/migration_generator_test.rb7
-rw-r--r--railties/test/generators/model_generator_test.rb51
-rw-r--r--railties/test/generators/named_base_test.rb5
-rw-r--r--railties/test/generators/plugin_new_generator_test.rb37
-rw-r--r--railties/test/generators/scaffold_generator_test.rb7
-rw-r--r--railties/test/generators_test.rb18
-rw-r--r--railties/test/isolation/abstract_unit.rb8
-rw-r--r--railties/test/railties/engine_test.rb214
-rw-r--r--railties/test/railties/railtie_test.rb6
-rw-r--r--railties/test/railties/shared_tests.rb10
19 files changed, 477 insertions, 99 deletions
diff --git a/railties/test/application/configuration_test.rb b/railties/test/application/configuration_test.rb
index c12c4a4660..044fd2a278 100644
--- a/railties/test/application/configuration_test.rb
+++ b/railties/test/application/configuration_test.rb
@@ -280,5 +280,58 @@ module ApplicationTests
get "/"
assert_equal "/omg/images/foo.jpg", last_response.body
end
+
+ test "config.action_view.cache_template_loading with cache_classes default" do
+ add_to_config "config.cache_classes = true"
+ require "#{app_path}/config/environment"
+ require 'action_view/base'
+
+ assert ActionView::Resolver.caching?
+ end
+
+ test "config.action_view.cache_template_loading without cache_classes default" do
+ add_to_config "config.cache_classes = false"
+ require "#{app_path}/config/environment"
+ require 'action_view/base'
+
+ assert !ActionView::Resolver.caching?
+ end
+
+ test "config.action_view.cache_template_loading = false" do
+ add_to_config <<-RUBY
+ config.cache_classes = true
+ config.action_view.cache_template_loading = false
+ RUBY
+ require "#{app_path}/config/environment"
+ require 'action_view/base'
+
+ assert !ActionView::Resolver.caching?
+ end
+
+ test "config.action_view.cache_template_loading = true" do
+ add_to_config <<-RUBY
+ config.cache_classes = false
+ config.action_view.cache_template_loading = true
+ RUBY
+ require "#{app_path}/config/environment"
+ require 'action_view/base'
+
+ assert ActionView::Resolver.caching?
+ end
+
+ test "config.action_dispatch.show_exceptions is sent in env" do
+ make_basic_app do |app|
+ app.config.action_dispatch.show_exceptions = true
+ end
+
+ class ::OmgController < ActionController::Base
+ def index
+ render :text => env["action_dispatch.show_exceptions"]
+ end
+ end
+
+ get "/"
+ assert_equal 'true', last_response.body
+ end
end
end
diff --git a/railties/test/application/console_test.rb b/railties/test/application/console_test.rb
index d4159dd0fd..793e73556c 100644
--- a/railties/test/application/console_test.rb
+++ b/railties/test/application/console_test.rb
@@ -26,14 +26,14 @@ class ConsoleTest < Test::Unit::TestCase
assert_instance_of ActionDispatch::Integration::Session, session
end
- def test_reload_should_fire_preparation_callbacks
+ def test_reload_should_fire_preparation_and_cleanup_callbacks
load_environment
a = b = c = nil
# TODO: These should be defined on the initializer
- ActionDispatch::Callbacks.to_prepare { a = b = c = 1 }
- ActionDispatch::Callbacks.to_prepare { b = c = 2 }
- ActionDispatch::Callbacks.to_prepare { c = 3 }
+ ActionDispatch::Reloader.to_cleanup { a = b = c = 1 }
+ ActionDispatch::Reloader.to_cleanup { b = c = 2 }
+ ActionDispatch::Reloader.to_prepare { c = 3 }
# Hide Reloading... output
silence_stream(STDOUT) { reload! }
diff --git a/railties/test/application/initializers/frameworks_test.rb b/railties/test/application/initializers/frameworks_test.rb
index 475091f789..19311a7fa0 100644
--- a/railties/test/application/initializers/frameworks_test.rb
+++ b/railties/test/application/initializers/frameworks_test.rb
@@ -1,7 +1,7 @@
require "isolation/abstract_unit"
module ApplicationTests
- class FrameworlsTest < Test::Unit::TestCase
+ class FrameworksTest < Test::Unit::TestCase
include ActiveSupport::Testing::Isolation
def setup
@@ -166,7 +166,7 @@ module ApplicationTests
require "#{app_path}/config/environment"
- expects = [ActiveRecord::ConnectionAdapters::ConnectionManagement, ActiveRecord::QueryCache, ActiveRecord::SessionStore]
+ expects = [ActiveRecord::IdentityMap::Middleware, ActiveRecord::ConnectionAdapters::ConnectionManagement, ActiveRecord::QueryCache, ActiveRecord::SessionStore]
middleware = Rails.application.config.middleware.map { |m| m.klass }
assert_equal expects, middleware & expects
end
diff --git a/railties/test/application/initializers/i18n_test.rb b/railties/test/application/initializers/i18n_test.rb
index 178b31cff3..390f65ab5c 100644
--- a/railties/test/application/initializers/i18n_test.rb
+++ b/railties/test/application/initializers/i18n_test.rb
@@ -63,6 +63,36 @@ module ApplicationTests
assert I18n.load_path.include?("#{app_path}/config/another_locale.yml")
end
+ test "load_path is populated before eager loaded models" do
+ add_to_config <<-RUBY
+ config.cache_classes = true
+ RUBY
+
+ app_file "config/locales/en.yml", <<-YAML
+en:
+ foo: "1"
+ YAML
+
+ app_file 'app/models/foo.rb', <<-RUBY
+ class Foo < ActiveRecord::Base
+ @foo = I18n.t(:foo)
+ end
+ RUBY
+
+ app_file 'config/routes.rb', <<-RUBY
+ AppTemplate::Application.routes.draw do
+ match '/i18n', :to => lambda { |env| [200, {}, [Foo.instance_variable_get('@foo')]] }
+ end
+ RUBY
+
+ require 'rack/test'
+ extend Rack::Test::Methods
+ load_app
+
+ get "/i18n"
+ assert_equal "1", last_response.body
+ end
+
test "locales are reloaded if they change between requests" do
add_to_config <<-RUBY
config.cache_classes = false
diff --git a/railties/test/application/middleware/show_exceptions_test.rb b/railties/test/application/middleware/show_exceptions_test.rb
new file mode 100644
index 0000000000..5487e41e0a
--- /dev/null
+++ b/railties/test/application/middleware/show_exceptions_test.rb
@@ -0,0 +1,37 @@
+require 'isolation/abstract_unit'
+
+module ApplicationTests
+ class ShowExceptionsTest < Test::Unit::TestCase
+ include ActiveSupport::Testing::Isolation
+
+ def setup
+ build_app
+ boot_rails
+ FileUtils.rm_rf "#{app_path}/config/environments"
+ end
+
+ def app
+ @app ||= Rails.application
+ end
+
+ test "unspecified route when set action_dispatch.show_exceptions to false" do
+ make_basic_app do |app|
+ app.config.action_dispatch.show_exceptions = false
+ end
+
+ assert_raise(ActionController::RoutingError) do
+ get '/foo'
+ end
+ end
+
+ test "unspecified route when set action_dispatch.show_exceptions to true" do
+ make_basic_app do |app|
+ app.config.action_dispatch.show_exceptions = true
+ end
+
+ assert_nothing_raised(ActionController::RoutingError) do
+ get '/foo'
+ end
+ end
+ end
+end
diff --git a/railties/test/application/middleware_test.rb b/railties/test/application/middleware_test.rb
index 173ac40b12..b314832685 100644
--- a/railties/test/application/middleware_test.rb
+++ b/railties/test/application/middleware_test.rb
@@ -27,7 +27,9 @@ module ApplicationTests
"ActionDispatch::ShowExceptions",
"ActionDispatch::RemoteIp",
"Rack::Sendfile",
+ "ActionDispatch::Reloader",
"ActionDispatch::Callbacks",
+ "ActiveRecord::IdentityMap::Middleware",
"ActiveRecord::ConnectionAdapters::ConnectionManagement",
"ActiveRecord::QueryCache",
"ActionDispatch::Cookies",
@@ -55,6 +57,7 @@ module ApplicationTests
boot!
assert !middleware.include?("ActiveRecord::ConnectionAdapters::ConnectionManagement")
assert !middleware.include?("ActiveRecord::QueryCache")
+ assert !middleware.include?("ActiveRecord::IdentityMap::Middleware")
end
test "removes lock if allow concurrency is set" do
@@ -75,10 +78,16 @@ module ApplicationTests
assert !middleware.include?("ActionDispatch::Static")
end
- test "removes show exceptions if action_dispatch.show_exceptions is disabled" do
+ test "includes show exceptions even action_dispatch.show_exceptions is disabled" do
add_to_config "config.action_dispatch.show_exceptions = false"
boot!
- assert !middleware.include?("ActionDispatch::ShowExceptions")
+ assert middleware.include?("ActionDispatch::ShowExceptions")
+ end
+
+ test "removes ActionDispatch::Reloader if cache_classes is true" do
+ add_to_config "config.cache_classes = true"
+ boot!
+ assert !middleware.include?("ActionDispatch::Reloader")
end
test "use middleware" do
@@ -105,6 +114,11 @@ module ApplicationTests
assert_equal "Rack::Runtime", middleware.fourth
end
+ test "identity map is inserted" do
+ boot!
+ assert middleware.include?("ActiveRecord::IdentityMap::Middleware")
+ end
+
test "insert middleware before" do
add_to_config "config.middleware.insert_before ActionDispatch::Static, Rack::Config"
boot!
diff --git a/railties/test/application/rake_test.rb b/railties/test/application/rake_test.rb
index 23cd2378c7..59e5ef4dee 100644
--- a/railties/test/application/rake_test.rb
+++ b/railties/test/application/rake_test.rb
@@ -63,5 +63,41 @@ module ApplicationTests
RUBY
assert_match 'cart GET /cart(.:format)', Dir.chdir(app_path){ `rake routes` }
end
+
+ def test_model_and_migration_generator_with_change_syntax
+ Dir.chdir(app_path) do
+ `rails generate model user username:string password:string`
+ `rails generate migration add_email_to_users email:string`
+ end
+
+ output = Dir.chdir(app_path){ `rake db:migrate` }
+ assert_match /create_table\(:users\)/, output
+ assert_match /CreateUsers: migrated/, output
+ assert_match /add_column\(:users, :email, :string\)/, output
+ assert_match /AddEmailToUsers: migrated/, output
+
+ output = Dir.chdir(app_path){ `rake db:rollback STEP=2` }
+ assert_match /drop_table\("users"\)/, output
+ assert_match /CreateUsers: reverted/, output
+ assert_match /remove_column\("users", :email\)/, output
+ assert_match /AddEmailToUsers: reverted/, output
+ end
+
+ def test_loading_specific_fixtures
+ Dir.chdir(app_path) do
+ `rails generate model user username:string password:string`
+ `rails generate model product name:string`
+ `rake db:migrate`
+ end
+
+ require "#{rails_root}/config/environment"
+
+ # loading a specific fixture
+ errormsg = Dir.chdir(app_path) { `rake db:fixtures:load FIXTURES=products` }
+ assert $?.success?, errormsg
+
+ assert_equal 2, ::AppTemplate::Application::Product.count
+ assert_equal 0, ::AppTemplate::Application::User.count
+ end
end
end
diff --git a/railties/test/generators/app_generator_test.rb b/railties/test/generators/app_generator_test.rb
index 7faa674a81..018c2fa6bf 100644
--- a/railties/test/generators/app_generator_test.rb
+++ b/railties/test/generators/app_generator_test.rb
@@ -64,6 +64,23 @@ class AppGeneratorTest < Rails::Generators::TestCase
assert_file "things-43/config/application.rb", /^module Things43$/
end
+ def test_application_new_exits_with_non_zero_code_on_invalid_application_name
+ # TODO: Suppress the output of this (it's because of a Thor::Error)
+ `rails new test`
+ assert_equal false, $?.success?
+ end
+
+ def test_application_new_exits_with_message_and_non_zero_code_when_generating_inside_existing_rails_directory
+ app_root = File.join(destination_root, 'myfirstapp')
+ run_generator [app_root]
+ output = nil
+ Dir.chdir(app_root) do
+ output = `rails new mysecondapp`
+ end
+ assert_equal "Can't initialize a new Rails application within the directory of another, please change to a non-Rails directory first.\nType 'rails' for help.\n", output
+ assert_equal false, $?.success?
+ end
+
def test_application_name_is_detected_if_it_exists_and_app_folder_renamed
app_root = File.join(destination_root, "myapp")
app_moved_root = File.join(destination_root, "myapp_moved")
@@ -106,7 +123,7 @@ class AppGeneratorTest < Rails::Generators::TestCase
def test_config_database_is_added_by_default
run_generator
assert_file "config/database.yml", /sqlite3/
- assert_file "Gemfile", /^gem\s+["']sqlite3-ruby["'],\s+:require\s+=>\s+["']sqlite3["']$/
+ assert_file "Gemfile", /^gem\s+["']sqlite3["']$/
end
def test_config_another_database
@@ -135,6 +152,9 @@ class AppGeneratorTest < Rails::Generators::TestCase
assert_file "public/javascripts/application.js"
assert_file "public/javascripts/prototype.js"
assert_file "public/javascripts/rails.js"
+ assert_file "public/javascripts/controls.js"
+ assert_file "public/javascripts/dragdrop.js"
+ assert_file "public/javascripts/effects.js"
assert_file "test"
end
@@ -151,6 +171,9 @@ class AppGeneratorTest < Rails::Generators::TestCase
assert_file "config/application.rb", /#\s+config\.action_view\.javascript_expansions\[:defaults\]\s+=\s+%w\(jquery rails\)/
assert_file "public/javascripts/application.js"
assert_file "public/javascripts/prototype.js"
+ assert_file "public/javascripts/controls.js"
+ assert_file "public/javascripts/dragdrop.js"
+ assert_file "public/javascripts/effects.js"
assert_file "public/javascripts/rails.js", /prototype/
end
diff --git a/railties/test/generators/generators_test_helper.rb b/railties/test/generators/generators_test_helper.rb
index 46a6da3568..1b9a8fd8a7 100644
--- a/railties/test/generators/generators_test_helper.rb
+++ b/railties/test/generators/generators_test_helper.rb
@@ -34,6 +34,6 @@ module GeneratorsTestHelper
routes = File.expand_path("../../../lib/rails/generators/rails/app/templates/config/routes.rb", __FILE__)
destination = File.join(destination_root, "config")
FileUtils.mkdir_p(destination)
- FileUtils.cp File.expand_path(routes), destination
+ FileUtils.cp routes, destination
end
end
diff --git a/railties/test/generators/migration_generator_test.rb b/railties/test/generators/migration_generator_test.rb
index 288ec30460..6eecfc8e2e 100644
--- a/railties/test/generators/migration_generator_test.rb
+++ b/railties/test/generators/migration_generator_test.rb
@@ -34,15 +34,10 @@ class MigrationGeneratorTest < Rails::Generators::TestCase
run_generator [migration, "title:string", "body:text"]
assert_migration "db/migrate/#{migration}.rb" do |content|
- assert_method :up, content do |up|
+ assert_method :change, content do |up|
assert_match /add_column :posts, :title, :string/, up
assert_match /add_column :posts, :body, :text/, up
end
-
- assert_method :down, content do |down|
- assert_match /remove_column :posts, :title/, down
- assert_match /remove_column :posts, :body/, down
- end
end
end
diff --git a/railties/test/generators/model_generator_test.rb b/railties/test/generators/model_generator_test.rb
index 552b7eb30a..3d773b4134 100644
--- a/railties/test/generators/model_generator_test.rb
+++ b/railties/test/generators/model_generator_test.rb
@@ -99,15 +99,11 @@ class ModelGeneratorTest < Rails::Generators::TestCase
run_generator ["product", "name:string", "supplier_id:integer"]
assert_migration "db/migrate/create_products.rb" do |m|
- assert_method :up, m do |up|
+ assert_method :change, m do |up|
assert_match /create_table :products/, up
assert_match /t\.string :name/, up
assert_match /t\.integer :supplier_id/, up
end
-
- assert_method :down, m do |down|
- assert_match /drop_table :products/, down
- end
end
end
@@ -141,7 +137,7 @@ class ModelGeneratorTest < Rails::Generators::TestCase
run_generator ["account", "--no-timestamps"]
assert_migration "db/migrate/create_accounts.rb" do |m|
- assert_method :up, m do |up|
+ assert_method :change, m do |up|
assert_no_match /t.timestamps/, up
end
end
@@ -181,7 +177,7 @@ class ModelGeneratorTest < Rails::Generators::TestCase
run_generator
old_migration = Dir["#{destination_root}/db/migrate/*_create_accounts.rb"].first
error = capture(:stderr) { run_generator ["Account", "--force"] }
- assert_no_match /Another migration is already named create_foos/, error
+ assert_no_match /Another migration is already named create_accounts/, error
assert_no_file old_migration
assert_migration 'db/migrate/create_accounts.rb'
end
@@ -207,4 +203,45 @@ class ModelGeneratorTest < Rails::Generators::TestCase
content = capture(:stderr){ run_generator ["object"] }
assert_match /The name 'Object' is either already used in your application or reserved/, content
end
+
+ def test_index_is_added_for_belongs_to_association
+ run_generator ["account", "supplier:belongs_to"]
+
+ assert_migration "db/migrate/create_accounts.rb" do |m|
+ assert_method :change, m do |up|
+ assert_match /add_index/, up
+ end
+ end
+ end
+
+ def test_index_is_added_for_references_association
+ run_generator ["account", "supplier:references"]
+
+ assert_migration "db/migrate/create_accounts.rb" do |m|
+ assert_method :change, m do |up|
+ assert_match /add_index/, up
+ end
+ end
+ end
+
+ def test_index_is_skipped_for_belongs_to_association
+ run_generator ["account", "supplier:belongs_to", "--no-indexes"]
+
+ assert_migration "db/migrate/create_accounts.rb" do |m|
+ assert_method :change, m do |up|
+ assert_no_match /add_index/, up
+ end
+ end
+ end
+
+ def test_index_is_skipped_for_references_association
+ run_generator ["account", "supplier:references", "--no-indexes"]
+
+ assert_migration "db/migrate/create_accounts.rb" do |m|
+ assert_method :change, m do |up|
+ assert_no_match /add_index/, up
+ end
+ end
+ end
+
end
diff --git a/railties/test/generators/named_base_test.rb b/railties/test/generators/named_base_test.rb
index 1badae0713..f23701e99e 100644
--- a/railties/test/generators/named_base_test.rb
+++ b/railties/test/generators/named_base_test.rb
@@ -98,6 +98,11 @@ class NamedBaseTest < Rails::Generators::TestCase
assert_name g, 'posts', :index_helper
end
+ def test_index_helper_to_pluralize_once
+ g = generator ['Stadium']
+ assert_name g, 'stadia', :index_helper
+ end
+
def test_index_helper_with_uncountable
g = generator ['Sheep']
assert_name g, 'sheep_index', :index_helper
diff --git a/railties/test/generators/plugin_new_generator_test.rb b/railties/test/generators/plugin_new_generator_test.rb
index 0d24821ff6..2a8b337144 100644
--- a/railties/test/generators/plugin_new_generator_test.rb
+++ b/railties/test/generators/plugin_new_generator_test.rb
@@ -38,8 +38,10 @@ class PluginNewGeneratorTest < Rails::Generators::TestCase
assert_file "things-43/lib/things-43.rb", /module Things43/
end
- def test_generating_test_files
+ def test_generating_without_options
run_generator
+ assert_file "README.rdoc", /Bukkits/
+ assert_no_file "config/routes.rb"
assert_file "test/test_helper.rb"
assert_file "test/bukkits_test.rb", /assert_kind_of Module, Bukkits/
end
@@ -66,7 +68,7 @@ class PluginNewGeneratorTest < Rails::Generators::TestCase
def test_database_entry_is_assed_by_default_in_full_mode
run_generator([destination_root, "--full"])
assert_file "test/dummy/config/database.yml", /sqlite/
- assert_file "Gemfile", /^gem\s+["']sqlite3-ruby["'],\s+:require\s+=>\s+["']sqlite3["']$/
+ assert_file "Gemfile", /^gem\s+["']sqlite3["']$/
end
def test_config_another_database
@@ -91,6 +93,35 @@ class PluginNewGeneratorTest < Rails::Generators::TestCase
assert_file "test/dummy/config/database.yml", /postgres/
end
+ def test_skipping_javascripts_without_mountable_option
+ run_generator
+ assert_no_file "public/javascripts/prototype.js"
+ assert_no_file "public/javascripts/rails.js"
+ assert_no_file "public/javascripts/controls.js"
+ assert_no_file "public/javascripts/dragdrop.js"
+ assert_no_file "public/javascripts/dragdrop.js"
+ assert_no_file "public/javascripts/application.js"
+ end
+
+ def test_javascripts_generation
+ run_generator [destination_root, "--mountable"]
+ assert_file "public/javascripts/rails.js"
+ assert_file "public/javascripts/prototype.js"
+ assert_file "public/javascripts/controls.js"
+ assert_file "public/javascripts/dragdrop.js"
+ assert_file "public/javascripts/dragdrop.js"
+ assert_file "public/javascripts/application.js"
+ end
+
+ def test_skip_javascripts
+ run_generator [destination_root, "--skip-javascript", "--mountable"]
+ assert_no_file "public/javascripts/prototype.js"
+ assert_no_file "public/javascripts/rails.js"
+ assert_no_file "public/javascripts/controls.js"
+ assert_no_file "public/javascripts/dragdrop.js"
+ assert_no_file "public/javascripts/dragdrop.js"
+ end
+
def test_ensure_that_javascript_option_is_passed_to_app_generator
run_generator [destination_root, "--javascript", "jquery"]
assert_file "test/dummy/public/javascripts/jquery.js"
@@ -122,6 +153,7 @@ class PluginNewGeneratorTest < Rails::Generators::TestCase
def test_creating_engine_in_full_mode
run_generator [destination_root, "--full"]
+ assert_file "config/routes.rb", /Rails.application.routes.draw do/
assert_file "lib/bukkits/engine.rb", /module Bukkits\n class Engine < Rails::Engine\n end\nend/
assert_file "lib/bukkits.rb", /require "bukkits\/engine"/
end
@@ -137,6 +169,7 @@ class PluginNewGeneratorTest < Rails::Generators::TestCase
assert_file "test/dummy/config/routes.rb", /mount Bukkits::Engine => "\/bukkits"/
assert_file "app/controllers/bukkits/application_controller.rb", /module Bukkits\n class ApplicationController < ActionController::Base/
assert_file "app/helpers/bukkits/application_helper.rb", /module Bukkits\n module ApplicationHelper/
+ assert_file "app/views/layouts/bukkits/application.html.erb", /<title>Bukkits<\/title>/
end
def test_passing_dummy_path_as_a_parameter
diff --git a/railties/test/generators/scaffold_generator_test.rb b/railties/test/generators/scaffold_generator_test.rb
index f366600b16..df787f61ba 100644
--- a/railties/test/generators/scaffold_generator_test.rb
+++ b/railties/test/generators/scaffold_generator_test.rb
@@ -3,7 +3,7 @@ require 'rails/generators/rails/scaffold/scaffold_generator'
class ScaffoldGeneratorTest < Rails::Generators::TestCase
include GeneratorsTestHelper
- arguments %w(product_line title:string price:integer)
+ arguments %w(product_line title:string product:belongs_to user:references)
setup :copy_routes
@@ -14,7 +14,10 @@ class ScaffoldGeneratorTest < Rails::Generators::TestCase
assert_file "app/models/product_line.rb", /class ProductLine < ActiveRecord::Base/
assert_file "test/unit/product_line_test.rb", /class ProductLineTest < ActiveSupport::TestCase/
assert_file "test/fixtures/product_lines.yml"
- assert_migration "db/migrate/create_product_lines.rb"
+ assert_migration "db/migrate/create_product_lines.rb", /belongs_to :product/
+ assert_migration "db/migrate/create_product_lines.rb", /add_index :product_lines, :product_id/
+ assert_migration "db/migrate/create_product_lines.rb", /references :user/
+ assert_migration "db/migrate/create_product_lines.rb", /add_index :product_lines, :user_id/
# Route
assert_file "config/routes.rb" do |route|
diff --git a/railties/test/generators_test.rb b/railties/test/generators_test.rb
index 346c9ceb9d..99c9d790eb 100644
--- a/railties/test/generators_test.rb
+++ b/railties/test/generators_test.rb
@@ -94,6 +94,14 @@ class GeneratorsTest < Rails::Generators::TestCase
assert_match /Rails 2\.x generator/, output
end
+ def test_invoke_with_nested_namespaces
+ model_generator = mock('ModelGenerator') do
+ expects(:start).with(["Account"], {})
+ end
+ Rails::Generators.expects(:find_by_namespace).with('namespace', 'my:awesome').returns(model_generator)
+ Rails::Generators.invoke 'my:awesome:namespace', ["Account"]
+ end
+
def test_rails_generators_help_with_builtin_information
output = capture(:stdout){ Rails::Generators.help }
assert_match /Rails:/, output
@@ -120,6 +128,16 @@ class GeneratorsTest < Rails::Generators::TestCase
assert_match /^ active_record:fixjour$/, output
end
+ def test_default_banner_should_show_generator_namespace
+ klass = Rails::Generators.find_by_namespace(:foobar)
+ assert_match /^rails generate foobar:foobar/, klass.banner
+ end
+
+ def test_default_banner_should_not_show_rails_generator_namespace
+ klass = Rails::Generators.find_by_namespace(:model)
+ assert_match /^rails generate model/, klass.banner
+ end
+
def test_no_color_sets_proper_shell
Rails::Generators.no_color!
assert_equal Thor::Shell::Basic, Thor::Base.shell
diff --git a/railties/test/isolation/abstract_unit.rb b/railties/test/isolation/abstract_unit.rb
index 3b03e4eb3d..c5b1cb9a80 100644
--- a/railties/test/isolation/abstract_unit.rb
+++ b/railties/test/isolation/abstract_unit.rb
@@ -215,6 +215,13 @@ module TestHelpers
end
end
+ def remove_from_config(str)
+ file = "#{app_path}/config/application.rb"
+ contents = File.read(file)
+ contents.sub!(/#{str}/, "")
+ File.open(file, "w+") { |f| f.puts contents }
+ end
+
def app_file(path, contents)
FileUtils.mkdir_p File.dirname("#{app_path}/#{path}")
File.open("#{app_path}/#{path}", 'w') do |f|
@@ -231,6 +238,7 @@ module TestHelpers
:activemodel,
:activerecord,
:activeresource] - arr
+ remove_from_config "config.active_record.identity_map = true" if to_remove.include? :activerecord
$:.reject! {|path| path =~ %r'/(#{to_remove.join('|')})/' }
end
diff --git a/railties/test/railties/engine_test.rb b/railties/test/railties/engine_test.rb
index 05bd0c36cd..0ce00db3c4 100644
--- a/railties/test/railties/engine_test.rb
+++ b/railties/test/railties/engine_test.rb
@@ -1,12 +1,15 @@
require "isolation/abstract_unit"
require "railties/shared_tests"
require 'stringio'
+require 'rack/test'
+require 'rack/file'
module RailtiesTest
class EngineTest < Test::Unit::TestCase
include ActiveSupport::Testing::Isolation
include SharedTests
+ include Rack::Test::Methods
def setup
build_app
@@ -87,10 +90,8 @@ module RailtiesTest
boot_rails
- env = Rack::MockRequest.env_for("/bukkits")
- response = Rails.application.call(env)
-
- assert_equal ["HELLO WORLD"], response[2]
+ get("/bukkits")
+ assert_equal "HELLO WORLD", last_response.body
end
test "it provides routes as default endpoint" do
@@ -115,9 +116,8 @@ module RailtiesTest
boot_rails
- env = Rack::MockRequest.env_for("/bukkits/foo")
- response = Rails.application.call(env)
- assert_equal ["foo"], response[2]
+ get("/bukkits/foo")
+ assert_equal "foo", last_response.body
end
test "engine can load its own plugins" do
@@ -191,13 +191,11 @@ module RailtiesTest
boot_rails
env = Rack::MockRequest.env_for("/")
- response = Bukkits::Engine.call(env)
-
+ Bukkits::Engine.call(env)
assert_equal Bukkits::Engine.routes, env['action_dispatch.routes']
env = Rack::MockRequest.env_for("/")
- response = Rails.application.call(env)
-
+ Rails.application.call(env)
assert_equal Rails.application.routes, env['action_dispatch.routes']
end
@@ -230,6 +228,12 @@ module RailtiesTest
<%= stylesheet_link_tag("foo") %>
ERB
+ app_file "config/routes.rb", <<-RUBY
+ Rails.application.routes.draw do
+ mount Bukkits::Engine => "/bukkits"
+ end
+ RUBY
+
add_to_config 'config.asset_path = "/omg%s"'
boot_rails
@@ -239,9 +243,8 @@ module RailtiesTest
::Bukkits::Engine.config.asset_path = "/bukkits%s"
- env = Rack::MockRequest.env_for("/foo")
- response = Bukkits::Engine.call(env)
- stripped_body = response[2].body.split("\n").map(&:strip).join
+ get("/bukkits/foo")
+ stripped_body = last_response.body.split("\n").map(&:strip).join
expected = "/omg/bukkits/images/foo.png" +
"<script src=\"/omg/bukkits/javascripts/foo.js\" type=\"text/javascript\"></script>" +
@@ -264,14 +267,16 @@ module RailtiesTest
end
RUBY
- boot_rails
+ app_file "config/routes.rb", <<-RUBY
+ AppTemplate::Application.routes.draw do
+ mount Bukkits::Engine => "/bukkits"
+ end
+ RUBY
- env = Rack::MockRequest.env_for("/foo")
- response = Bukkits::Engine.call(env)
- stripped_body = response[2].body.strip
+ boot_rails
- expected = "/bukkits/images/foo.png"
- assert_equal expected, stripped_body
+ get("/bukkits/foo")
+ assert_equal "/bukkits/images/foo.png", last_response.body.strip
end
test "engine's files are served via ActionDispatch::Static" do
@@ -291,25 +296,42 @@ module RailtiesTest
boot_rails
- env = Rack::MockRequest.env_for("/app.html")
- response = Rails.application.call(env)
- assert_equal rack_body(response[2]), rack_body(File.open(File.join(app_path, "public/app.html")))
+ get("/app.html")
+ assert_equal File.read(File.join(app_path, "public/app.html")), last_response.body
- env = Rack::MockRequest.env_for("/bukkits/bukkits.html")
- response = Rails.application.call(env)
- assert_equal rack_body(response[2]), rack_body(File.open(File.join(@plugin.path, "public/bukkits.html")))
+ get("/bukkits/bukkits.html")
+ assert_equal File.read(File.join(@plugin.path, "public/bukkits.html")), last_response.body
- env = Rack::MockRequest.env_for("/bukkits/file_from_app.html")
- response = Rails.application.call(env)
- assert_equal rack_body(response[2]), rack_body(File.open(File.join(app_path, "public/bukkits/file_from_app.html")))
+ get("/bukkits/file_from_app.html")
+ assert_equal File.read(File.join(app_path, "public/bukkits/file_from_app.html")), last_response.body
end
- def rack_body(obj)
- buffer = ""
- obj.each do |part|
- buffer << part
- end
- buffer
+ test "an applications files are given priority over an engines files when served via ActionDispatch::Static" do
+ add_to_config "config.serve_static_assets = true"
+
+ @plugin.write "lib/bukkits.rb", <<-RUBY
+ class Bukkits
+ class Engine < ::Rails::Engine
+ engine_name :bukkits
+ end
+ end
+ RUBY
+
+ app_file "config/routes.rb", <<-RUBY
+ AppTemplate::Application.routes.draw do
+ mount Bukkits::Engine => "/bukkits"
+ end
+ RUBY
+
+ @plugin.write "public/bukkits.html", "in engine"
+
+ app_file "public/bukkits/bukkits.html", "in app"
+
+ boot_rails
+
+ get('/bukkits/bukkits.html')
+
+ assert_equal 'in app', last_response.body.strip
end
test "shared engine should include application's helpers and own helpers" do
@@ -355,17 +377,14 @@ module RailtiesTest
boot_rails
- env = Rack::MockRequest.env_for("/foo")
- response = Rails.application.call(env)
- assert_equal ["Something... Something... Something..."], response[2]
+ get("/foo")
+ assert_equal "Something... Something... Something...", last_response.body
- env = Rack::MockRequest.env_for("/foo/show")
- response = Rails.application.call(env)
- assert_equal ["/foo"], response[2]
+ get("/foo/show")
+ assert_equal "/foo", last_response.body
- env = Rack::MockRequest.env_for("/foo/bar")
- response = Rails.application.call(env)
- assert_equal ["It's a bar."], response[2]
+ get("/foo/bar")
+ assert_equal "It's a bar.", last_response.body
end
test "isolated engine should include only its own routes and helpers" do
@@ -464,25 +483,20 @@ module RailtiesTest
assert ::Bukkits::MyMailer.method_defined?(:foo_path)
assert !::Bukkits::MyMailer.method_defined?(:bar_path)
- env = Rack::MockRequest.env_for("/bukkits/from_app")
- response = AppTemplate::Application.call(env)
- assert_equal ["false"], response[2]
+ get("/bukkits/from_app")
+ assert_equal "false", last_response.body
- env = Rack::MockRequest.env_for("/bukkits/foo/show")
- response = AppTemplate::Application.call(env)
- assert_equal ["/bukkits/foo"], response[2]
+ get("/bukkits/foo/show")
+ assert_equal "/bukkits/foo", last_response.body
- env = Rack::MockRequest.env_for("/bukkits/foo")
- response = AppTemplate::Application.call(env)
- assert_equal ["Helped."], response[2]
+ get("/bukkits/foo")
+ assert_equal "Helped.", last_response.body
- env = Rack::MockRequest.env_for("/bukkits/routes_helpers_in_view")
- response = AppTemplate::Application.call(env)
- assert_equal ["/bukkits/foo, /bar"], response[2]
+ get("/bukkits/routes_helpers_in_view")
+ assert_equal "/bukkits/foo, /bar", last_response.body
- env = Rack::MockRequest.env_for("/bukkits/polymorphic_path_without_namespace")
- response = AppTemplate::Application.call(env)
- assert_equal ["/bukkits/posts/1"], response[2]
+ get("/bukkits/polymorphic_path_without_namespace")
+ assert_equal "/bukkits/posts/1", last_response.body
end
test "isolated engine should avoid namespace in names if that's possible" do
@@ -541,9 +555,8 @@ module RailtiesTest
boot_rails
- env = Rack::MockRequest.env_for("/bukkits/posts/new")
- response = AppTemplate::Application.call(env)
- assert rack_body(response[2]) =~ /name="post\[title\]"/
+ get("/bukkits/posts/new")
+ assert_match /name="post\[title\]"/, last_response.body
end
test "loading seed data" do
@@ -612,16 +625,14 @@ module RailtiesTest
end
RUBY
- require 'rack/test'
- extend Rack::Test::Methods
-
boot_rails
require "#{rails_root}/config/environment"
- get "/foo"
+
+ get("/foo")
assert_equal "foo", last_response.body
- get "/bukkits/bar"
+ get("/bukkits/bar")
assert_equal "bar", last_response.body
end
@@ -721,5 +732,74 @@ module RailtiesTest
engine_path = File.join(@plugin.path, '..', engine_dir)
assert_equal Bukkits::Engine.instance, Rails::Engine.find(engine_path)
end
+
+ test "ensure that engine properly sets assets directories" do
+ add_to_config("config.action_dispatch.show_exceptions = false")
+ add_to_config("config.serve_static_assets = true")
+
+ @plugin.write "lib/bukkits.rb", <<-RUBY
+ module Bukkits
+ class Engine < ::Rails::Engine
+ isolate_namespace Bukkits
+ end
+ end
+ RUBY
+
+ @plugin.write "public/stylesheets/foo.css", ""
+ @plugin.write "public/javascripts/foo.js", ""
+
+ @plugin.write "app/views/layouts/bukkits/application.html.erb", <<-RUBY
+ <%= stylesheet_link_tag :all %>
+ <%= javascript_include_tag :all %>
+ <%= yield %>
+ RUBY
+
+ @plugin.write "app/controllers/bukkits/home_controller.rb", <<-RUBY
+ module Bukkits
+ class HomeController < ActionController::Base
+ def index
+ render :text => "Good morning!", :layout => "bukkits/application"
+ end
+ end
+ end
+ RUBY
+
+ @plugin.write "config/routes.rb", <<-RUBY
+ Bukkits::Engine.routes.draw do
+ match "/home" => "home#index"
+ end
+ RUBY
+
+ app_file "config/routes.rb", <<-RUBY
+ Rails.application.routes.draw do
+ mount Bukkits::Engine => "/bukkits"
+ end
+ RUBY
+
+ require 'rack/test'
+ extend Rack::Test::Methods
+
+ boot_rails
+
+ require "#{rails_root}/config/environment"
+
+ assert_equal File.join(@plugin.path, "public"), Bukkits::HomeController.assets_dir
+ assert_equal File.join(@plugin.path, "public/stylesheets"), Bukkits::HomeController.stylesheets_dir
+ assert_equal File.join(@plugin.path, "public/javascripts"), Bukkits::HomeController.javascripts_dir
+
+ assert_equal File.join(app_path, "public"), ActionController::Base.assets_dir
+ assert_equal File.join(app_path, "public/stylesheets"), ActionController::Base.stylesheets_dir
+ assert_equal File.join(app_path, "public/javascripts"), ActionController::Base.javascripts_dir
+
+ get "/bukkits/home"
+
+ assert_match %r{bukkits/stylesheets/foo.css}, last_response.body
+ assert_match %r{bukkits/javascripts/foo.js}, last_response.body
+ end
+
+ private
+ def app
+ Rails.application
+ end
end
end
diff --git a/railties/test/railties/railtie_test.rb b/railties/test/railties/railtie_test.rb
index 6d194eecba..7ea8364ae9 100644
--- a/railties/test/railties/railtie_test.rb
+++ b/railties/test/railties/railtie_test.rb
@@ -21,10 +21,10 @@ module RailtiesTest
test "Railtie provides railtie_name" do
begin
- class ::Foo < Rails::Railtie ; end
- assert_equal "foo", ::Foo.railtie_name
+ class ::FooBarBaz < Rails::Railtie ; end
+ assert_equal "foo_bar_baz", ::FooBarBaz.railtie_name
ensure
- Object.send(:remove_const, :"Foo")
+ Object.send(:remove_const, :"FooBarBaz")
end
end
diff --git a/railties/test/railties/shared_tests.rb b/railties/test/railties/shared_tests.rb
index 31ba2eaca2..3eb79d57c8 100644
--- a/railties/test/railties/shared_tests.rb
+++ b/railties/test/railties/shared_tests.rb
@@ -294,7 +294,7 @@ YAML
boot_rails
- assert_equal %W(
+ expected_locales = %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
@@ -302,7 +302,13 @@ YAML
#{@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) }
+ ).map { |path| File.expand_path(path) }
+
+ actual_locales = I18n.load_path.map { |path|
+ File.expand_path(path)
+ } & expected_locales # remove locales external to Rails
+
+ assert_equal expected_locales, actual_locales
assert_equal "2", I18n.t(:foo)
assert_equal "1", I18n.t(:bar)