diff options
Diffstat (limited to 'railties/test')
33 files changed, 507 insertions, 464 deletions
diff --git a/railties/test/abstract_unit.rb b/railties/test/abstract_unit.rb index 2ea1d2aff4..491faf4af9 100644 --- a/railties/test/abstract_unit.rb +++ b/railties/test/abstract_unit.rb @@ -3,11 +3,10 @@ ENV["RAILS_ENV"] ||= "test" require File.expand_path("../../../load_paths", __FILE__) require 'stringio' -require 'minitest/autorun' +require 'active_support/testing/autorun' require 'fileutils' require 'active_support' - require 'action_controller' require 'rails/all' diff --git a/railties/test/application/assets_test.rb b/railties/test/application/assets_test.rb index f98915d1cc..638df8ca23 100644 --- a/railties/test/application/assets_test.rb +++ b/railties/test/application/assets_test.rb @@ -45,7 +45,7 @@ module ApplicationTests app_file 'config/routes.rb', <<-RUBY AppTemplate::Application.routes.draw do - get '*path', to: lambda { |env| [200, { "Content-Type" => "text/html" }, "Not an asset"] } + get '*path', to: lambda { |env| [200, { "Content-Type" => "text/html" }, ["Not an asset"]] } end RUBY diff --git a/railties/test/application/configuration_test.rb b/railties/test/application/configuration_test.rb index b9d18f4582..654a44e648 100644 --- a/railties/test/application/configuration_test.rb +++ b/railties/test/application/configuration_test.rb @@ -1,5 +1,6 @@ require "isolation/abstract_unit" require 'rack/test' +require 'env_helpers' class ::MyMailInterceptor def self.delivering_email(email); email; end @@ -17,6 +18,7 @@ module ApplicationTests class ConfigurationTest < ActiveSupport::TestCase include ActiveSupport::Testing::Isolation include Rack::Test::Methods + include EnvHelpers def new_app File.expand_path("#{app_path}/../new_app") @@ -41,6 +43,16 @@ module ApplicationTests FileUtils.rm_rf(new_app) if File.directory?(new_app) end + test "Rails.env does not set the RAILS_ENV environment variable which would leak out into rake tasks" do + require "rails" + + switch_env "RAILS_ENV", nil do + Rails.env = "development" + assert_equal "development", Rails.env + assert_nil ENV['RAILS_ENV'] + end + end + test "a renders exception on pending migration" do add_to_config <<-RUBY config.active_record.migration_error = :page_load @@ -180,6 +192,16 @@ module ApplicationTests end end + test "filter_parameters should be able to set via config.filter_parameters in an initializer" do + app_file 'config/initializers/filter_parameters_logging.rb', <<-RUBY + Rails.application.config.filter_parameters += [ :password, :foo, 'bar' ] + RUBY + + require "#{app_path}/config/environment" + + assert_equal [:password, :foo, 'bar'], Rails.application.env_config['action_dispatch.parameter_filter'] + end + test "config.to_prepare is forwarded to ActionDispatch" do $prepared = false @@ -395,7 +417,17 @@ module ApplicationTests require "#{app_path}/config/environment" - assert_equal "Wellington", Rails.application.config.time_zone + assert_equal Time.find_zone!("Wellington"), Time.zone_default + end + + test "timezone can be set on initializers" do + app_file "config/initializers/locale.rb", <<-RUBY + Rails.application.config.time_zone = "Central Time (US & Canada)" + RUBY + + require "#{app_path}/config/environment" + + assert_equal Time.find_zone!("Central Time (US & Canada)"), Time.zone_default end test "raises when an invalid timezone is defined in the config" do @@ -545,6 +577,54 @@ module ApplicationTests assert_equal 'permitted', last_response.body end + test "config.action_controller.raise_on_unpermitted_parameters = true" do + app_file 'app/controllers/posts_controller.rb', <<-RUBY + class PostsController < ActionController::Base + def create + render text: params.require(:post).permit(:name) + end + end + RUBY + + add_to_config <<-RUBY + routes.prepend do + resources :posts + end + config.action_controller.raise_on_unpermitted_parameters = true + RUBY + + require "#{app_path}/config/environment" + + assert_equal true, ActionController::Parameters.raise_on_unpermitted_parameters + + post "/posts", {post: {"title" =>"zomg"}} + assert_match "We're sorry, but something went wrong", last_response.body + end + + test "config.action_controller.raise_on_unpermitted_parameters is true by default on development" do + ENV["RAILS_ENV"] = "development" + + require "#{app_path}/config/environment" + + assert_equal true, ActionController::Parameters.raise_on_unpermitted_parameters + end + + test "config.action_controller.raise_on_unpermitted_parameters is true by defaul on test" do + ENV["RAILS_ENV"] = "test" + + require "#{app_path}/config/environment" + + assert_equal true, ActionController::Parameters.raise_on_unpermitted_parameters + end + + test "config.action_controller.raise_on_unpermitted_parameters is false by default on production" do + ENV["RAILS_ENV"] = "production" + + require "#{app_path}/config/environment" + + assert_equal false, ActionController::Parameters.raise_on_unpermitted_parameters + end + test "config.action_dispatch.ignore_accept_header" do make_basic_app do |app| app.config.action_dispatch.ignore_accept_header = true @@ -582,27 +662,6 @@ module ApplicationTests assert app.config.colorize_logging end - test "config.active_record.observers" do - add_to_config <<-RUBY - config.active_record.observers = :foo_observer - RUBY - - app_file 'app/models/foo.rb', <<-RUBY - class Foo < ActiveRecord::Base - end - RUBY - - app_file 'app/models/foo_observer.rb', <<-RUBY - class FooObserver < ActiveRecord::Observer - end - RUBY - - require "#{app_path}/config/environment" - - ActiveRecord::Base - assert defined?(FooObserver) - end - test "config.session_store with :active_record_store with activerecord-session_store gem" do begin make_basic_app do |app| diff --git a/railties/test/application/console_test.rb b/railties/test/application/console_test.rb index f372afa51c..3cb3643e3a 100644 --- a/railties/test/application/console_test.rb +++ b/railties/test/application/console_test.rb @@ -95,21 +95,4 @@ class ConsoleTest < ActiveSupport::TestCase load_environment(true) assert value end - - def test_active_record_does_not_panic_when_referencing_an_observed_constant - add_to_config "config.active_record.observers = :user_observer" - - app_file "app/models/user.rb", <<-MODEL - class User < ActiveRecord::Base - end - MODEL - - app_file "app/models/user_observer.rb", <<-MODEL - class UserObserver < ActiveRecord::Observer - end - MODEL - - load_environment - assert_nothing_raised { User } - end end diff --git a/railties/test/application/initializers/frameworks_test.rb b/railties/test/application/initializers/frameworks_test.rb index 40d1655c9b..bc794e1602 100644 --- a/railties/test/application/initializers/frameworks_test.rb +++ b/railties/test/application/initializers/frameworks_test.rb @@ -50,22 +50,6 @@ module ApplicationTests assert_equal "test.rails", ActionMailer::Base.default_url_options[:host] end - test "uses the default queue for ActionMailer" do - require "#{app_path}/config/environment" - assert_kind_of ActiveSupport::Queue, ActionMailer::Base.queue - end - - test "allows me to configure queue for ActionMailer" do - app_file "config/environments/development.rb", <<-RUBY - AppTemplate::Application.configure do - config.action_mailer.queue = ActiveSupport::TestQueue.new - end - RUBY - - require "#{app_path}/config/environment" - assert_kind_of ActiveSupport::TestQueue, ActionMailer::Base.queue - end - test "does not include url helpers as action methods" do app_file "config/routes.rb", <<-RUBY AppTemplate::Application.routes.draw do diff --git a/railties/test/application/middleware/remote_ip_test.rb b/railties/test/application/middleware/remote_ip_test.rb index fde13eeb94..f0d3438aa4 100644 --- a/railties/test/application/middleware/remote_ip_test.rb +++ b/railties/test/application/middleware/remote_ip_test.rb @@ -40,7 +40,7 @@ module ApplicationTests end assert_nothing_raised(ActionDispatch::RemoteIp::IpSpoofAttackError) do - assert_equal "1.1.1.2", remote_ip("HTTP_X_FORWARDED_FOR" => "1.1.1.1", "HTTP_CLIENT_IP" => "1.1.1.2") + assert_equal "1.1.1.1", remote_ip("HTTP_X_FORWARDED_FOR" => "1.1.1.1", "HTTP_CLIENT_IP" => "1.1.1.2") end end diff --git a/railties/test/application/paths_test.rb b/railties/test/application/paths_test.rb index 2265d220ab..4029984ce9 100644 --- a/railties/test/application/paths_test.rb +++ b/railties/test/application/paths_test.rb @@ -59,8 +59,6 @@ module ApplicationTests assert eager_load.include?(root("app/controllers")) assert eager_load.include?(root("app/helpers")) assert eager_load.include?(root("app/models")) - assert !eager_load.include?(root("app/views")), "expected to not be in the eager_load_path" - assert !eager_load.include?(root("app/assets")), "expected to not be in the eager_load_path" end test "environments has a glob equal to the current environment" do @@ -75,18 +73,11 @@ module ApplicationTests assert_in_load_path "vendor" assert_not_in_load_path "app", "views" - assert_not_in_load_path "app", "assets" 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 "deprecated children method" do - assert_deprecated "children is deprecated and will be removed from Rails 4.1." do - @paths["app/assets"].children - end - end end end diff --git a/railties/test/application/queue_test.rb b/railties/test/application/queue_test.rb deleted file mode 100644 index 219a35da35..0000000000 --- a/railties/test/application/queue_test.rb +++ /dev/null @@ -1,154 +0,0 @@ -require 'isolation/abstract_unit' - -module ApplicationTests - class QueueTest < ActiveSupport::TestCase - include ActiveSupport::Testing::Isolation - - def setup - build_app - boot_rails - end - - def teardown - teardown_app - end - - def app_const - @app_const ||= Class.new(Rails::Application) - end - - test "the queue is a SynchronousQueue in test mode" do - app("test") - assert_kind_of ActiveSupport::SynchronousQueue, Rails.application.queue - assert_kind_of ActiveSupport::SynchronousQueue, Rails.queue - end - - test "the queue is a SynchronousQueue in development mode" do - app("development") - assert_kind_of ActiveSupport::SynchronousQueue, Rails.application.queue - assert_kind_of ActiveSupport::SynchronousQueue, Rails.queue - end - - class ThreadTrackingJob - def initialize - @origin = Thread.current.object_id - end - - def run - @target = Thread.current.object_id - end - - def ran_in_different_thread? - @origin != @target - end - - def ran? - @target - end - end - - test "in development mode, an enqueued job will be processed in the same thread" do - app("development") - - job = ThreadTrackingJob.new - Rails.queue.push job - sleep 0.1 - - assert job.ran?, "Expected job to be run" - refute job.ran_in_different_thread?, "Expected job to run in the same thread" - end - - test "in test mode, an enqueued job will be processed in the same thread" do - app("test") - - job = ThreadTrackingJob.new - Rails.queue.push job - sleep 0.1 - - assert job.ran?, "Expected job to be run" - refute job.ran_in_different_thread?, "Expected job to run in the same thread" - end - - test "in production, automatically spawn a queue consumer in a background thread" do - add_to_env_config "production", <<-RUBY - config.queue = ActiveSupport::Queue.new - RUBY - - app("production") - - assert_nil Rails.application.config.queue_consumer - assert_kind_of ActiveSupport::ThreadedQueueConsumer, Rails.application.queue_consumer - assert_equal Rails.logger, Rails.application.queue_consumer.logger - end - - test "attempting to marshal a queue will raise an exception" do - app("test") - assert_raises TypeError do - Marshal.dump Rails.queue - end - end - - def setup_custom_queue - add_to_env_config "production", <<-RUBY - require "my_queue" - config.queue = MyQueue.new - RUBY - - app_file "lib/my_queue.rb", <<-RUBY - class MyQueue - def push(job) - job.run - end - end - RUBY - - app("production") - end - - test "a custom queue implementation can be provided" do - setup_custom_queue - - assert_kind_of MyQueue, Rails.queue - - job = Struct.new(:id, :ran) do - def run - self.ran = true - end - end - - job1 = job.new(1) - Rails.queue.push job1 - - assert_equal true, job1.ran - end - - test "a custom consumer implementation can be provided" do - add_to_env_config "production", <<-RUBY - require "my_queue_consumer" - config.queue = ActiveSupport::Queue.new - config.queue_consumer = MyQueueConsumer.new - RUBY - - app_file "lib/my_queue_consumer.rb", <<-RUBY - class MyQueueConsumer - attr_reader :started - - def start - @started = true - end - end - RUBY - - app("production") - - assert_kind_of MyQueueConsumer, Rails.application.queue_consumer - assert Rails.application.queue_consumer.started - end - - test "default consumer is not used with custom queue implementation" do - setup_custom_queue - - assert_nil Rails.application.queue_consumer - end - end -end diff --git a/railties/test/application/rake/dbs_test.rb b/railties/test/application/rake/dbs_test.rb index 03798d572a..ccb47663d4 100644 --- a/railties/test/application/rake/dbs_test.rb +++ b/railties/test/application/rake/dbs_test.rb @@ -55,8 +55,8 @@ module ApplicationTests def db_migrate_and_status Dir.chdir(app_path) do - `rails generate model book title:string` - `bundle exec rake db:migrate` + `rails generate model book title:string; + bundle exec rake db:migrate` output = `bundle exec rake db:migrate:status` assert_match(/database:\s+\S+#{expected[:database]}/, output) assert_match(/up\s+\d{14}\s+Create books/, output) @@ -78,9 +78,8 @@ module ApplicationTests def db_schema_dump Dir.chdir(app_path) do - `rails generate model book title:string` - `rake db:migrate` - `rake db:schema:dump` + `rails generate model book title:string; + rake db:migrate db:schema:dump` schema_dump = File.read("db/schema.rb") assert_match(/create_table \"books\"/, schema_dump) end @@ -97,9 +96,8 @@ module ApplicationTests def db_fixtures_load Dir.chdir(app_path) do - `rails generate model book title:string` - `bundle exec rake db:migrate` - `bundle exec rake db:fixtures:load` + `rails generate model book title:string; + bundle exec rake db:migrate db:fixtures:load` assert_match(/#{expected[:database]}/, ActiveRecord::Base.connection_config[:database]) require "#{app_path}/app/models/book" @@ -122,13 +120,11 @@ module ApplicationTests def db_structure_dump_and_load Dir.chdir(app_path) do - `rails generate model book title:string` - `bundle exec rake db:migrate` - `bundle exec rake db:structure:dump` + `rails generate model book title:string; + bundle exec rake db:migrate db:structure:dump` structure_dump = File.read("db/structure.sql") assert_match(/CREATE TABLE \"books\"/, structure_dump) - `bundle exec rake db:drop` - `bundle exec rake db:structure:load` + `bundle exec rake db:drop db:structure:load` assert_match(/#{expected[:database]}/, ActiveRecord::Base.connection_config[:database]) require "#{app_path}/app/models/book" @@ -152,10 +148,8 @@ module ApplicationTests def db_test_load_structure Dir.chdir(app_path) do - `rails generate model book title:string` - `bundle exec rake db:migrate` - `bundle exec rake db:structure:dump` - `bundle exec rake db:test:load_structure` + `rails generate model book title:string; + bundle exec rake db:migrate db:structure:dump db:test:load_structure` ActiveRecord::Base.configurations = Rails.application.config.database_configuration ActiveRecord::Base.establish_connection 'test' require "#{app_path}/app/models/book" @@ -178,4 +172,4 @@ module ApplicationTests end end end -end
\ No newline at end of file +end diff --git a/railties/test/application/rake/migrations_test.rb b/railties/test/application/rake/migrations_test.rb index 0a47fd014c..33c753868c 100644 --- a/railties/test/application/rake/migrations_test.rb +++ b/railties/test/application/rake/migrations_test.rb @@ -50,9 +50,9 @@ module ApplicationTests assert_match(/AddEmailToUsers: migrated/, output) output = `rake db:rollback STEP=2` - assert_match(/drop_table\("users"\)/, output) + assert_match(/drop_table\(:users\)/, output) assert_match(/CreateUsers: reverted/, output) - assert_match(/remove_column\("users", :email\)/, output) + assert_match(/remove_column\(:users, :email, :string\)/, output) assert_match(/AddEmailToUsers: reverted/, output) end end diff --git a/railties/test/application/rake/notes_test.rb b/railties/test/application/rake/notes_test.rb index 7a227098ba..744bb93671 100644 --- a/railties/test/application/rake/notes_test.rb +++ b/railties/test/application/rake/notes_test.rb @@ -60,6 +60,7 @@ module ApplicationTests test 'notes finds notes in default directories' do app_file "app/controllers/some_controller.rb", "# TODO: note in app directory" app_file "config/initializers/some_initializer.rb", "# TODO: note in config directory" + app_file "db/some_seeds.rb", "# TODO: note in db directory" app_file "lib/some_file.rb", "# TODO: note in lib directory" app_file "script/run_something.rb", "# TODO: note in script directory" app_file "test/some_test.rb", 1000.times.map { "" }.join("\n") << "# TODO: note in test directory" @@ -80,12 +81,13 @@ module ApplicationTests assert_match(/note in app directory/, output) assert_match(/note in config directory/, output) + assert_match(/note in db directory/, output) assert_match(/note in lib directory/, output) assert_match(/note in script directory/, output) assert_match(/note in test directory/, output) assert_no_match(/note in some_other directory/, output) - assert_equal 5, lines.size + assert_equal 6, lines.size lines.each do |line_number| assert_equal 4, line_number.size @@ -96,6 +98,7 @@ module ApplicationTests test 'notes finds notes in custom directories' do app_file "app/controllers/some_controller.rb", "# TODO: note in app directory" app_file "config/initializers/some_initializer.rb", "# TODO: note in config directory" + app_file "db/some_seeds.rb", "# TODO: note in db directory" app_file "lib/some_file.rb", "# TODO: note in lib directory" app_file "script/run_something.rb", "# TODO: note in script directory" app_file "test/some_test.rb", 1000.times.map { "" }.join("\n") << "# TODO: note in test directory" @@ -116,13 +119,14 @@ module ApplicationTests assert_match(/note in app directory/, output) assert_match(/note in config directory/, output) + assert_match(/note in db directory/, output) assert_match(/note in lib directory/, output) assert_match(/note in script directory/, output) assert_match(/note in test directory/, output) assert_match(/note in some_other directory/, output) - assert_equal 6, lines.size + assert_equal 7, lines.size lines.each do |line_number| assert_equal 4, line_number.size diff --git a/railties/test/application/rake_test.rb b/railties/test/application/rake_test.rb index c6aea03d8c..a8275a2e76 100644 --- a/railties/test/application/rake_test.rb +++ b/railties/test/application/rake_test.rb @@ -110,7 +110,6 @@ module ApplicationTests app_name = File.basename(app_path) app_dir = File.dirname(app_path) moved_app_name = app_name + '_moved' - moved_app_path = "#{app_path}/#{moved_app_name}" Dir.chdir(app_dir) do # Go from "./app/" to "./app/app_moved" @@ -196,6 +195,16 @@ module ApplicationTests assert_no_match(/Errors running/, output) end + def test_scaffold_with_references_columns_tests_pass_by_default + output = Dir.chdir(app_path) do + `rails generate scaffold LineItems product:references cart:belongs_to; + bundle exec rake db:migrate db:test:clone test` + end + + assert_match(/7 tests, 13 assertions, 0 failures, 0 errors/, output) + assert_no_match(/Errors running/, output) + end + def test_db_test_clone_when_using_sql_format add_to_config "config.active_record.schema_format = :sql" output = Dir.chdir(app_path) do @@ -250,28 +259,6 @@ module ApplicationTests assert !File.exists?(File.join(app_path, 'db', 'schema_cache.dump')) end - def test_load_activerecord_base_when_we_use_observers - Dir.chdir(app_path) do - `bundle exec rails g model user; - bundle exec rake db:migrate; - bundle exec rails g observer user;` - - add_to_config "config.active_record.observers = :user_observer" - - assert_equal "0", `bundle exec rails r "puts User.count"`.strip - - app_file "lib/tasks/count_user.rake", <<-RUBY - namespace :user do - task count: :environment do - puts User.count - end - end - RUBY - - assert_equal "0", `bundle exec rake user:count`.strip - end - end - def test_copy_templates Dir.chdir(app_path) do `bundle exec rake rails:templates:copy` diff --git a/railties/test/application/routing_test.rb b/railties/test/application/routing_test.rb index ffcdeac7f0..22de640236 100644 --- a/railties/test/application/routing_test.rb +++ b/railties/test/application/routing_test.rb @@ -15,6 +15,12 @@ module ApplicationTests teardown_app end + test "rails/welcome in development" do + app("development") + get "/" + assert_equal 200, last_response.status + end + test "rails/info/routes in development" do app("development") get "/rails/info/routes" @@ -27,6 +33,36 @@ module ApplicationTests assert_equal 200, last_response.status end + test "root takes precedence over internal welcome controller" do + app("development") + + get '/' + assert_match %r{<h1>Getting started</h1>} , last_response.body + + controller :foo, <<-RUBY + class FooController < ApplicationController + def index + render text: "foo" + end + end + RUBY + + app_file 'config/routes.rb', <<-RUBY + AppTemplate::Application.routes.draw do + root to: "foo#index" + end + RUBY + + get '/' + assert_equal 'foo', last_response.body + end + + test "rails/welcome in production" do + app("production") + get "/" + assert_equal 404, last_response.status + end + test "rails/info/routes in production" do app("production") get "/rails/info/routes" @@ -241,6 +277,77 @@ module ApplicationTests end end + test 'routes are added and removed when reloading' do + app('development') + + controller :foo, <<-RUBY + class FooController < ApplicationController + def index + render text: "foo" + end + end + RUBY + + controller :bar, <<-RUBY + class BarController < ApplicationController + def index + render text: "bar" + end + end + RUBY + + app_file 'config/routes.rb', <<-RUBY + AppTemplate::Application.routes.draw do + get 'foo', to: 'foo#index' + end + RUBY + + get '/foo' + assert_equal 'foo', last_response.body + assert_equal '/foo', Rails.application.routes.url_helpers.foo_path + + get '/bar' + assert_equal 404, last_response.status + assert_raises NoMethodError do + assert_equal '/bar', Rails.application.routes.url_helpers.bar_path + end + + app_file 'config/routes.rb', <<-RUBY + AppTemplate::Application.routes.draw do + get 'foo', to: 'foo#index' + get 'bar', to: 'bar#index' + end + RUBY + + Rails.application.reload_routes! + + get '/foo' + assert_equal 'foo', last_response.body + assert_equal '/foo', Rails.application.routes.url_helpers.foo_path + + get '/bar' + assert_equal 'bar', last_response.body + assert_equal '/bar', Rails.application.routes.url_helpers.bar_path + + app_file 'config/routes.rb', <<-RUBY + AppTemplate::Application.routes.draw do + get 'foo', to: 'foo#index' + end + RUBY + + Rails.application.reload_routes! + + get '/foo' + assert_equal 'foo', last_response.body + assert_equal '/foo', Rails.application.routes.url_helpers.foo_path + + get '/bar' + assert_equal 404, last_response.status + assert_raises NoMethodError do + assert_equal '/bar', Rails.application.routes.url_helpers.bar_path + end + end + test 'resource routing with irregular inflection' do app_file 'config/initializers/inflection.rb', <<-RUBY ActiveSupport::Inflector.inflections do |inflect| diff --git a/railties/test/application/runner_test.rb b/railties/test/application/runner_test.rb index 81ed5873a5..f65b5e2f2d 100644 --- a/railties/test/application/runner_test.rb +++ b/railties/test/application/runner_test.rb @@ -1,8 +1,10 @@ require 'isolation/abstract_unit' +require 'env_helpers' module ApplicationTests class RunnerTest < ActiveSupport::TestCase include ActiveSupport::Testing::Isolation + include EnvHelpers def setup build_app @@ -67,5 +69,21 @@ module ApplicationTests assert_match "true", Dir.chdir(app_path) { `bundle exec rails runner "puts Rails.application.config.ran"` } end + + def test_default_environment + assert_match "development", Dir.chdir(app_path) { `bundle exec rails runner "puts Rails.env"` } + end + + def test_environment_with_rails_env + with_rails_env "production" do + assert_match "production", Dir.chdir(app_path) { `bundle exec rails runner "puts Rails.env"` } + end + end + + def test_environment_with_rack_env + with_rack_env "production" do + assert_match "production", Dir.chdir(app_path) { `bundle exec rails runner "puts Rails.env"` } + end + end end end diff --git a/railties/test/commands/console_test.rb b/railties/test/commands/console_test.rb index f99ea13022..6be4a5fe89 100644 --- a/railties/test/commands/console_test.rb +++ b/railties/test/commands/console_test.rb @@ -1,14 +1,14 @@ require 'abstract_unit' +require 'env_helpers' require 'rails/commands/console' class Rails::ConsoleTest < ActiveSupport::TestCase + include EnvHelpers + class FakeConsole def self.start; end end - def setup - end - def test_sandbox_option console = Rails::Console.new(app, parse_arguments(["--sandbox"])) assert console.sandbox? @@ -78,7 +78,14 @@ class Rails::ConsoleTest < ActiveSupport::TestCase assert_match(/\sspecial-production\s/, output) end end - + + def test_default_environment_with_rack_env + with_rack_env 'production' do + start + assert_match(/\sproduction\s/, output) + end + end + def test_e_option start ['-e', 'special-production'] assert_match(/\sspecial-production\s/, output) @@ -104,6 +111,12 @@ class Rails::ConsoleTest < ActiveSupport::TestCase assert_match(/\sdevelopment\s/, output) end + def test_rails_env_is_dev_when_argument_is_dev_and_dev_env_is_present + Rails::Console.stubs(:available_environments).returns(['dev']) + options = Rails::Console.parse_arguments(['dev']) + assert_match('dev', options[:environment]) + end + private attr_reader :output @@ -126,12 +139,4 @@ class Rails::ConsoleTest < ActiveSupport::TestCase def parse_arguments(args) Rails::Console.parse_arguments(args) end - - def with_rails_env(env) - original_rails_env = ENV['RAILS_ENV'] - ENV['RAILS_ENV'] = env - yield - ensure - ENV['RAILS_ENV'] = original_rails_env - end end diff --git a/railties/test/commands/dbconsole_test.rb b/railties/test/commands/dbconsole_test.rb index d45bdaabf5..38fe8ca544 100644 --- a/railties/test/commands/dbconsole_test.rb +++ b/railties/test/commands/dbconsole_test.rb @@ -45,6 +45,18 @@ class Rails::DBConsoleTest < ActiveSupport::TestCase ENV['RAILS_ENV'] = "test" end + def test_rails_env_is_development_when_argument_is_dev + Rails::DBConsole.stubs(:available_environments).returns(['development', 'test']) + options = Rails::DBConsole.new.send(:parse_arguments, ['dev']) + assert_match('development', options[:environment]) + end + + def test_rails_env_is_dev_when_argument_is_dev_and_dev_env_is_present + Rails::DBConsole.stubs(:available_environments).returns(['dev']) + options = Rails::DBConsole.new.send(:parse_arguments, ['dev']) + assert_match('dev', options[:environment]) + end + def test_mysql dbconsole.expects(:find_cmd_and_exec).with(%w[mysql mysql5], 'db') start(adapter: 'mysql', database: 'db') @@ -116,6 +128,14 @@ class Rails::DBConsoleTest < ActiveSupport::TestCase assert !aborted end + def test_sqlite3_db_without_defined_rails_root + Rails.stubs(:respond_to?) + Rails.expects(:respond_to?).with(:root).once.returns(false) + dbconsole.expects(:find_cmd_and_exec).with('sqlite3', Rails.root.join('../config/db.sqlite3').to_s) + start(adapter: 'sqlite3', database: 'config/db.sqlite3') + assert !aborted + end + def test_oracle dbconsole.expects(:find_cmd_and_exec).with('sqlplus', 'user@db') start(adapter: 'oracle', database: 'db', username: 'user', password: 'secret') diff --git a/railties/test/commands/server_test.rb b/railties/test/commands/server_test.rb index 4a3ea82e3d..cb57b3c0cd 100644 --- a/railties/test/commands/server_test.rb +++ b/railties/test/commands/server_test.rb @@ -1,7 +1,9 @@ require 'abstract_unit' +require 'env_helpers' require 'rails/commands/server' class Rails::ServerTest < ActiveSupport::TestCase + include EnvHelpers def test_environment_with_server_option args = ["thin", "-e", "production"] @@ -23,4 +25,18 @@ class Rails::ServerTest < ActiveSupport::TestCase assert_nil options[:environment] assert_equal 'thin', options[:server] end + + def test_environment_with_rails_env + with_rails_env 'production' do + server = Rails::Server.new + assert_equal 'production', server.options[:environment] + end + end + + def test_environment_with_rack_env + with_rack_env 'production' do + server = Rails::Server.new + assert_equal 'production', server.options[:environment] + end + end end diff --git a/railties/test/configuration/middleware_stack_proxy_test.rb b/railties/test/configuration/middleware_stack_proxy_test.rb index 5984c0b425..2442cb995d 100644 --- a/railties/test/configuration/middleware_stack_proxy_test.rb +++ b/railties/test/configuration/middleware_stack_proxy_test.rb @@ -1,6 +1,7 @@ -require 'minitest/autorun' +require 'active_support/testing/autorun' require 'rails/configuration' require 'active_support/test_case' +require 'minitest/mock' module Rails module Configuration diff --git a/railties/test/engine_test.rb b/railties/test/engine_test.rb index addf49cdb6..7970913d21 100644 --- a/railties/test/engine_test.rb +++ b/railties/test/engine_test.rb @@ -1,7 +1,7 @@ require 'abstract_unit' class EngineTest < ActiveSupport::TestCase - it "reports routes as available only if they're actually present" do + test "reports routes as available only if they're actually present" do engine = Class.new(Rails::Engine) do def initialize(*args) @routes = nil diff --git a/railties/test/env_helpers.rb b/railties/test/env_helpers.rb new file mode 100644 index 0000000000..6223c85bbf --- /dev/null +++ b/railties/test/env_helpers.rb @@ -0,0 +1,26 @@ +module EnvHelpers + private + + def with_rails_env(env) + switch_env 'RAILS_ENV', env do + switch_env 'RACK_ENV', nil do + yield + end + end + end + + def with_rack_env(env) + switch_env 'RACK_ENV', env do + switch_env 'RAILS_ENV', nil do + yield + end + end + end + + def switch_env(key, value) + old, ENV[key] = ENV[key], value + yield + ensure + ENV[key] = old + end +end diff --git a/railties/test/generators/actions_test.rb b/railties/test/generators/actions_test.rb index 8af92479c3..54734ed260 100644 --- a/railties/test/generators/actions_test.rb +++ b/railties/test/generators/actions_test.rb @@ -1,8 +1,11 @@ require 'generators/generators_test_helper' require 'rails/generators/rails/app/app_generator' +require 'env_helpers' class ActionsTest < Rails::Generators::TestCase include GeneratorsTestHelper + include EnvHelpers + tests Rails::Generators::AppGenerator arguments [destination_root] @@ -154,10 +157,9 @@ class ActionsTest < Rails::Generators::TestCase def test_rake_should_run_rake_command_with_default_env generator.expects(:run).once.with("rake log:clear RAILS_ENV=development", verbose: false) - old_env, ENV['RAILS_ENV'] = ENV["RAILS_ENV"], nil - action :rake, 'log:clear' - ensure - ENV["RAILS_ENV"] = old_env + with_rails_env nil do + action :rake, 'log:clear' + end end def test_rake_with_env_option_should_run_rake_command_in_env @@ -167,26 +169,23 @@ class ActionsTest < Rails::Generators::TestCase def test_rake_with_rails_env_variable_should_run_rake_command_in_env generator.expects(:run).once.with('rake log:clear RAILS_ENV=production', verbose: false) - old_env, ENV["RAILS_ENV"] = ENV["RAILS_ENV"], "production" - action :rake, 'log:clear' - ensure - ENV["RAILS_ENV"] = old_env + with_rails_env "production" do + action :rake, 'log:clear' + end end def test_env_option_should_win_over_rails_env_variable_when_running_rake generator.expects(:run).once.with('rake log:clear RAILS_ENV=production', verbose: false) - old_env, ENV["RAILS_ENV"] = ENV["RAILS_ENV"], "staging" - action :rake, 'log:clear', env: 'production' - ensure - ENV["RAILS_ENV"] = old_env + with_rails_env "staging" do + action :rake, 'log:clear', env: 'production' + end end def test_rake_with_sudo_option_should_run_rake_command_with_sudo generator.expects(:run).once.with("sudo rake log:clear RAILS_ENV=development", verbose: false) - old_env, ENV['RAILS_ENV'] = ENV["RAILS_ENV"], nil - action :rake, 'log:clear', sudo: true - ensure - ENV["RAILS_ENV"] = old_env + with_rails_env nil do + action :rake, 'log:clear', sudo: true + end end def test_capify_should_run_the_capify_command diff --git a/railties/test/generators/app_generator_test.rb b/railties/test/generators/app_generator_test.rb index 5ea31f2e0f..945cb61bc1 100644 --- a/railties/test/generators/app_generator_test.rb +++ b/railties/test/generators/app_generator_test.rb @@ -11,9 +11,11 @@ DEFAULT_APP_FILES = %w( app/assets/stylesheets app/assets/images app/controllers + app/controllers/concerns app/helpers app/mailers app/models + app/models/concerns app/views/layouts config/environments config/initializers @@ -54,8 +56,6 @@ class AppGeneratorTest < Rails::Generators::TestCase assert_file "app/views/layouts/application.html.erb", /stylesheet_link_tag\s+"application"/ assert_file "app/views/layouts/application.html.erb", /javascript_include_tag\s+"application"/ assert_file "app/assets/stylesheets/application.css" - assert_file "config/application.rb", /config\.assets\.enabled = true/ - assert_file "public/index.html", /url\("assets\/rails.png"\);/ end def test_invalid_application_name_raises_an_error @@ -224,7 +224,7 @@ class AppGeneratorTest < Rails::Generators::TestCase run_generator [destination_root, "--skip-sprockets"] assert_file "config/application.rb" do |content| assert_match(/#\s+require\s+["']sprockets\/railtie["']/, content) - assert_no_match(/config\.assets\.enabled = true/, content) + assert_match(/config\.assets\.enabled = false/, content) end assert_file "Gemfile" do |content| assert_no_match(/sass-rails/, content) @@ -238,6 +238,7 @@ class AppGeneratorTest < Rails::Generators::TestCase assert_no_match(/config\.assets\.digest = true/, content) assert_no_match(/config\.assets\.js_compressor = :uglifier/, content) assert_no_match(/config\.assets\.css_compressor = :sass/, content) + assert_no_match(/config\.assets\.version = '1\.0'/, content) end assert_file "test/performance/browsing_test.rb" end @@ -251,13 +252,6 @@ class AppGeneratorTest < Rails::Generators::TestCase end end - def test_generator_if_skip_index_html_is_given - run_generator [destination_root, '--skip-index-html'] - assert_no_file 'public/index.html' - assert_no_file 'app/assets/images/rails.png' - assert_file 'app/assets/images/.keep' - end - def test_creation_of_a_test_directory run_generator assert_file 'test' diff --git a/railties/test/generators/generated_attribute_test.rb b/railties/test/generators/generated_attribute_test.rb index 6ab1cd58c7..c48bc20899 100644 --- a/railties/test/generators/generated_attribute_test.rb +++ b/railties/test/generators/generated_attribute_test.rb @@ -117,13 +117,13 @@ class GeneratedAttributeTest < Rails::Generators::TestCase assert create_generated_attribute("#{attribute_type}{polymorphic}").polymorphic? end end - + def test_polymorphic_reference_is_false %w(foo bar baz).each do |attribute_type| assert !create_generated_attribute("#{attribute_type}{polymorphic}").polymorphic? end end - + def test_blank_type_defaults_to_string_raises_exception assert_equal :string, create_generated_attribute(nil, 'title').type assert_equal :string, create_generated_attribute("", 'title').type @@ -132,6 +132,13 @@ class GeneratedAttributeTest < Rails::Generators::TestCase def test_handles_index_names_for_references assert_equal "post", create_generated_attribute('string', 'post').index_name assert_equal "post_id", create_generated_attribute('references', 'post').index_name + assert_equal "post_id", create_generated_attribute('belongs_to', 'post').index_name assert_equal ["post_id", "post_type"], create_generated_attribute('references{polymorphic}', 'post').index_name end + + def test_handles_column_names_for_references + assert_equal "post", create_generated_attribute('string', 'post').column_name + assert_equal "post_id", create_generated_attribute('references', 'post').column_name + assert_equal "post_id", create_generated_attribute('belongs_to', 'post').column_name + end end diff --git a/railties/test/generators/migration_generator_test.rb b/railties/test/generators/migration_generator_test.rb index 15e5a0b92b..62d9d1f06a 100644 --- a/railties/test/generators/migration_generator_test.rb +++ b/railties/test/generators/migration_generator_test.rb @@ -28,7 +28,7 @@ class MigrationGeneratorTest < Rails::Generators::TestCase run_generator [migration] assert_migration "db/migrate/change_title_body_from_posts.rb", /class #{migration} < ActiveRecord::Migration/ end - + def test_migration_with_invalid_file_name migration = "add_something:datetime" assert_raise ActiveRecord::IllegalMigrationNameError do @@ -41,9 +41,9 @@ class MigrationGeneratorTest < Rails::Generators::TestCase run_generator [migration, "title:string", "body:text"] assert_migration "db/migrate/#{migration}.rb" do |content| - assert_method :change, content do |up| - assert_match(/add_column :posts, :title, :string/, up) - assert_match(/add_column :posts, :body, :text/, up) + assert_method :change, content do |change| + assert_match(/add_column :posts, :title, :string/, change) + assert_match(/add_column :posts, :body, :text/, change) end end end @@ -53,15 +53,10 @@ class MigrationGeneratorTest < Rails::Generators::TestCase run_generator [migration, "title:string:index", "body:text"] assert_migration "db/migrate/#{migration}.rb" do |content| - assert_method :up, content do |up| - assert_match(/remove_column :posts, :title/, up) - assert_match(/remove_column :posts, :body/, up) - end - - assert_method :down, content do |down| - assert_match(/add_column :posts, :title, :string/, down) - assert_match(/add_column :posts, :body, :text/, down) - assert_match(/add_index :posts, :title/, down) + assert_method :change, content do |change| + assert_match(/remove_column :posts, :title, :string/, change) + assert_match(/remove_column :posts, :body, :text/, change) + assert_match(/remove_index :posts, :title/, change) end end end @@ -71,14 +66,9 @@ 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_match(/remove_column :posts, :title/, up) - assert_match(/remove_column :posts, :body/, up) - end - - assert_method :down, content do |down| - assert_match(/add_column :posts, :title, :string/, down) - assert_match(/add_column :posts, :body, :text/, down) + assert_method :change, content do |change| + assert_match(/remove_column :posts, :title, :string/, change) + assert_match(/remove_column :posts, :body, :text/, change) end end end @@ -88,14 +78,9 @@ class MigrationGeneratorTest < Rails::Generators::TestCase run_generator [migration, "author:belongs_to", "distributor:references{polymorphic}"] assert_migration "db/migrate/#{migration}.rb" do |content| - assert_method :up, content do |up| - assert_match(/remove_reference :books, :author/, up) - assert_match(/remove_reference :books, :distributor, polymorphic: true/, up) - end - - assert_method :down, content do |down| - assert_match(/add_reference :books, :author, index: true/, down) - assert_match(/add_reference :books, :distributor, polymorphic: true, index: true/, down) + assert_method :change, content do |change| + assert_match(/remove_reference :books, :author, index: true/, change) + assert_match(/remove_reference :books, :distributor, polymorphic: true, index: true/, change) end end end @@ -105,13 +90,13 @@ class MigrationGeneratorTest < Rails::Generators::TestCase run_generator [migration, "title:string:index", "body:text", "user_id:integer:uniq"] assert_migration "db/migrate/#{migration}.rb" do |content| - assert_method :change, content do |up| - assert_match(/add_column :posts, :title, :string/, up) - assert_match(/add_column :posts, :body, :text/, up) - assert_match(/add_column :posts, :user_id, :integer/, up) + assert_method :change, content do |change| + assert_match(/add_column :posts, :title, :string/, change) + assert_match(/add_column :posts, :body, :text/, change) + assert_match(/add_column :posts, :user_id, :integer/, change) + assert_match(/add_index :posts, :title/, change) + assert_match(/add_index :posts, :user_id, unique: true/, change) end - assert_match(/add_index :posts, :title/, content) - assert_match(/add_index :posts, :user_id, unique: true/, content) end end @@ -120,10 +105,10 @@ class MigrationGeneratorTest < Rails::Generators::TestCase run_generator [migration, "title:string:inex", "content:text", "user_id:integer:unik"] assert_migration "db/migrate/#{migration}.rb" do |content| - assert_method :change, content do |up| - assert_match(/add_column :books, :title, :string/, up) - assert_match(/add_column :books, :content, :text/, up) - assert_match(/add_column :books, :user_id, :integer/, up) + assert_method :change, content do |change| + assert_match(/add_column :books, :title, :string/, change) + assert_match(/add_column :books, :content, :text/, change) + assert_match(/add_column :books, :user_id, :integer/, change) end assert_no_match(/add_index :books, :title/, content) assert_no_match(/add_index :books, :user_id/, content) @@ -135,13 +120,13 @@ class MigrationGeneratorTest < Rails::Generators::TestCase run_generator [migration, "title:index", "body:text", "user_uuid:uniq"] assert_migration "db/migrate/#{migration}.rb" do |content| - assert_method :change, content do |up| - assert_match(/add_column :posts, :title, :string/, up) - assert_match(/add_column :posts, :body, :text/, up) - assert_match(/add_column :posts, :user_uuid, :string/, up) + assert_method :change, content do |change| + assert_match(/add_column :posts, :title, :string/, change) + assert_match(/add_column :posts, :body, :text/, change) + assert_match(/add_column :posts, :user_uuid, :string/, change) + assert_match(/add_index :posts, :title/, change) + assert_match(/add_index :posts, :user_uuid, unique: true/, change) end - assert_match(/add_index :posts, :title/, content) - assert_match(/add_index :posts, :user_uuid, unique: true/, content) end end @@ -150,11 +135,11 @@ class MigrationGeneratorTest < Rails::Generators::TestCase run_generator [migration, "title:string{40}:index", "content:string{255}", "price:decimal{1,2}:index", "discount:decimal{3.4}:uniq"] assert_migration "db/migrate/#{migration}.rb" do |content| - assert_method :change, content do |up| - assert_match(/add_column :books, :title, :string, limit: 40/, up) - assert_match(/add_column :books, :content, :string, limit: 255/, up) - assert_match(/add_column :books, :price, :decimal, precision: 1, scale: 2/, up) - assert_match(/add_column :books, :discount, :decimal, precision: 3, scale: 4/, up) + assert_method :change, content do |change| + assert_match(/add_column :books, :title, :string, limit: 40/, change) + assert_match(/add_column :books, :content, :string, limit: 255/, change) + assert_match(/add_column :books, :price, :decimal, precision: 1, scale: 2/, change) + assert_match(/add_column :books, :discount, :decimal, precision: 3, scale: 4/, change) end assert_match(/add_index :books, :title/, content) assert_match(/add_index :books, :price/, content) @@ -167,9 +152,9 @@ class MigrationGeneratorTest < Rails::Generators::TestCase run_generator [migration, "author:belongs_to", "distributor:references{polymorphic}"] assert_migration "db/migrate/#{migration}.rb" do |content| - assert_method :change, content do |up| - assert_match(/add_reference :books, :author, index: true/, up) - assert_match(/add_reference :books, :distributor, polymorphic: true, index: true/, up) + assert_method :change, content do |change| + assert_match(/add_reference :books, :author, index: true/, change) + assert_match(/add_reference :books, :distributor, polymorphic: true, index: true/, change) end end end @@ -179,10 +164,10 @@ class MigrationGeneratorTest < Rails::Generators::TestCase run_generator [migration, "artist_id", "musics:uniq"] assert_migration "db/migrate/#{migration}.rb" do |content| - assert_method :change, content do |up| - assert_match(/create_join_table :artists, :musics/, up) - assert_match(/# t.index \[:artist_id, :music_id\]/, up) - assert_match(/ t.index \[:music_id, :artist_id\], unique: true/, up) + assert_method :change, content do |change| + assert_match(/create_join_table :artists, :musics/, change) + assert_match(/# t.index \[:artist_id, :music_id\]/, change) + assert_match(/ t.index \[:music_id, :artist_id\], unique: true/, change) end end end @@ -192,12 +177,8 @@ class MigrationGeneratorTest < Rails::Generators::TestCase run_generator [migration, "title:string", "content:text"] assert_migration "db/migrate/#{migration}.rb" do |content| - assert_method :up, content do |up| - assert_match(/^\s*$/, up) - end - - assert_method :down, content do |down| - assert_match(/^\s*$/, down) + assert_method :change, content do |change| + assert_match(/^\s*$/, change) end end end diff --git a/railties/test/generators/model_generator_test.rb b/railties/test/generators/model_generator_test.rb index 0c7ff0ebe7..01ab77ee20 100644 --- a/railties/test/generators/model_generator_test.rb +++ b/railties/test/generators/model_generator_test.rb @@ -264,13 +264,40 @@ class ModelGeneratorTest < Rails::Generators::TestCase error = capture(:stderr) { run_generator ["Account", "--force"] } assert_no_match(/Another migration is already named create_accounts/, error) assert_no_file old_migration - assert_migration 'db/migrate/create_accounts.rb' + assert_migration "db/migrate/create_accounts.rb" end def test_invokes_default_test_framework run_generator assert_file "test/models/account_test.rb", /class AccountTest < ActiveSupport::TestCase/ + assert_file "test/fixtures/accounts.yml", /name: MyString/, /age: 1/ + assert_generated_fixture("test/fixtures/accounts.yml", + {"one"=>{"name"=>"MyString", "age"=>1}, "two"=>{"name"=>"MyString", "age"=>1}}) + end + + def test_fixtures_use_the_references_ids + run_generator ["LineItem", "product:references", "cart:belongs_to"] + + assert_file "test/fixtures/line_items.yml", /product_id: \n cart_id: / + assert_generated_fixture("test/fixtures/line_items.yml", + {"one"=>{"product_id"=>nil, "cart_id"=>nil}, "two"=>{"product_id"=>nil, "cart_id"=>nil}}) + end + + def test_fixtures_use_the_references_ids_and_type + run_generator ["LineItem", "product:references{polymorphic}", "cart:belongs_to"] + + assert_file "test/fixtures/line_items.yml", /product_id: \n product_type: Product\n cart_id: / + assert_generated_fixture("test/fixtures/line_items.yml", + {"one"=>{"product_id"=>nil, "product_type"=>"Product", "cart_id"=>nil}, + "two"=>{"product_id"=>nil, "product_type"=>"Product", "cart_id"=>nil}}) + end + + def test_fixtures_respect_reserved_yml_keywords + run_generator ["LineItem", "no:integer", "Off:boolean", "ON:boolean"] + + assert_generated_fixture("test/fixtures/line_items.yml", + {"one"=>{"no"=>1, "Off"=>false, "ON"=>false}, "two"=>{"no"=>1, "Off"=>false, "ON"=>false}}) end def test_fixture_is_skipped @@ -328,4 +355,10 @@ class ModelGeneratorTest < Rails::Generators::TestCase end end end + + private + def assert_generated_fixture(path, parsed_contents) + fixture_file = File.new File.expand_path(path, destination_root) + assert_equal(parsed_contents, YAML.load(fixture_file)) + end end diff --git a/railties/test/generators/namespaced_generators_test.rb b/railties/test/generators/namespaced_generators_test.rb index 9e7626647e..a4d8b3d1b0 100644 --- a/railties/test/generators/namespaced_generators_test.rb +++ b/railties/test/generators/namespaced_generators_test.rb @@ -1,18 +1,19 @@ require 'generators/generators_test_helper' require 'rails/generators/rails/controller/controller_generator' require 'rails/generators/rails/model/model_generator' -require 'rails/generators/rails/observer/observer_generator' require 'rails/generators/mailer/mailer_generator' require 'rails/generators/rails/scaffold/scaffold_generator' class NamespacedGeneratorTestCase < Rails::Generators::TestCase + include GeneratorsTestHelper + def setup + super Rails::Generators.namespace = TestApp end end class NamespacedControllerGeneratorTest < NamespacedGeneratorTestCase - include GeneratorsTestHelper arguments %w(Account foo bar) tests Rails::Generators::ControllerGenerator @@ -81,7 +82,6 @@ class NamespacedControllerGeneratorTest < NamespacedGeneratorTestCase end class NamespacedModelGeneratorTest < NamespacedGeneratorTestCase - include GeneratorsTestHelper arguments %w(Account name:string age:integer) tests Rails::Generators::ModelGenerator @@ -141,29 +141,7 @@ class NamespacedModelGeneratorTest < NamespacedGeneratorTestCase end end -class NamespacedObserverGeneratorTest < NamespacedGeneratorTestCase - include GeneratorsTestHelper - arguments %w(account) - tests Rails::Generators::ObserverGenerator - - def test_invokes_default_orm - run_generator - assert_file "app/models/test_app/account_observer.rb", /module TestApp/, / class AccountObserver < ActiveRecord::Observer/ - end - - def test_invokes_default_orm_with_class_path - run_generator ["admin/account"] - assert_file "app/models/test_app/admin/account_observer.rb", /module TestApp/, / class Admin::AccountObserver < ActiveRecord::Observer/ - end - - def test_invokes_default_test_framework - run_generator - assert_file "test/models/test_app/account_observer_test.rb", /module TestApp/, / class AccountObserverTest < ActiveSupport::TestCase/ - end -end - class NamespacedMailerGeneratorTest < NamespacedGeneratorTestCase - include GeneratorsTestHelper arguments %w(notifier foo bar) tests Rails::Generators::MailerGenerator diff --git a/railties/test/generators/observer_generator_test.rb b/railties/test/generators/observer_generator_test.rb deleted file mode 100644 index 1231827466..0000000000 --- a/railties/test/generators/observer_generator_test.rb +++ /dev/null @@ -1,27 +0,0 @@ -require 'generators/generators_test_helper' -require 'rails/generators/rails/observer/observer_generator' - -class ObserverGeneratorTest < Rails::Generators::TestCase - include GeneratorsTestHelper - arguments %w(account) - - def test_invokes_default_orm - run_generator - assert_file "app/models/account_observer.rb", /class AccountObserver < ActiveRecord::Observer/ - end - - def test_invokes_default_orm_with_class_path - run_generator ["admin/account"] - assert_file "app/models/admin/account_observer.rb", /class Admin::AccountObserver < ActiveRecord::Observer/ - end - - def test_invokes_default_test_framework - run_generator - assert_file "test/models/account_observer_test.rb", /class AccountObserverTest < ActiveSupport::TestCase/ - end - - def test_logs_if_the_test_framework_cannot_be_found - content = run_generator ["account", "--test-framework=rspec"] - assert_match(/rspec \[not found\]/, content) - end -end diff --git a/railties/test/generators/scaffold_controller_generator_test.rb b/railties/test/generators/scaffold_controller_generator_test.rb index 8cacca668f..ab00586a64 100644 --- a/railties/test/generators/scaffold_controller_generator_test.rb +++ b/railties/test/generators/scaffold_controller_generator_test.rb @@ -20,17 +20,13 @@ class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase assert_match(/@users = User\.all/, m) end - assert_instance_method :show, content do |m| - assert_match(/@user = User\.find\(params\[:id\]\)/, m) - end + assert_instance_method :show, content assert_instance_method :new, content do |m| assert_match(/@user = User\.new/, m) end - assert_instance_method :edit, content do |m| - assert_match(/@user = User\.find\(params\[:id\]\)/, m) - end + assert_instance_method :edit, content assert_instance_method :create, content do |m| assert_match(/@user = User\.new\(user_params\)/, m) @@ -39,21 +35,50 @@ class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase end assert_instance_method :update, content do |m| - assert_match(/@user = User\.find\(params\[:id\]\)/, m) - assert_match(/@user\.update_attributes\(user_params\)/, m) + assert_match(/@user\.update\(user_params\)/, m) assert_match(/@user\.errors/, m) end assert_instance_method :destroy, content do |m| - assert_match(/@user = User\.find\(params\[:id\]\)/, m) assert_match(/@user\.destroy/, m) end + assert_instance_method :set_user, content do |m| + assert_match(/@user = User\.find\(params\[:id\]\)/, m) + end + assert_match(/def user_params/, content) assert_match(/params\.require\(:user\)\.permit\(:name, :age\)/, content) end end + def test_dont_use_require_or_permit_if_there_are_no_attributes + run_generator ["User"] + + assert_file "app/controllers/users_controller.rb" do |content| + assert_match(/def user_params/, content) + assert_match(/params\[:user\]/, content) + end + end + + def test_controller_permit_references_attributes + run_generator ["LineItem", "product:references", "cart:belongs_to"] + + assert_file "app/controllers/line_items_controller.rb" do |content| + assert_match(/def line_item_params/, content) + assert_match(/params\.require\(:line_item\)\.permit\(:product_id, :cart_id\)/, content) + end + end + + def test_controller_permit_polymorphic_references_attributes + run_generator ["LineItem", "product:references{polymorphic}"] + + assert_file "app/controllers/line_items_controller.rb" do |content| + assert_match(/def line_item_params/, content) + assert_match(/params\.require\(:line_item\)\.permit\(:product_id, :product_type\)/, content) + end + end + def test_helper_are_invoked_with_a_pluralized_name run_generator assert_file "app/helpers/users_helper.rb", /module UsersHelper/ @@ -70,13 +95,13 @@ class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase end def test_functional_tests - run_generator + run_generator ["User", "name:string", "age:integer", "organization:references{polymorphic}"] assert_file "test/controllers/users_controller_test.rb" do |content| assert_match(/class UsersControllerTest < ActionController::TestCase/, content) assert_match(/test "should get index"/, content) - assert_match(/post :create, user: \{ age: @user.age, name: @user.name \}/, content) - assert_match(/put :update, id: @user, user: \{ age: @user.age, name: @user.name \}/, content) + assert_match(/post :create, user: \{ age: @user\.age, name: @user\.name, organization_id: @user\.organization_id, organization_type: @user\.organization_type \}/, content) + assert_match(/patch :update, id: @user, user: \{ age: @user\.age, name: @user\.name, organization_id: @user\.organization_id, organization_type: @user\.organization_type \}/, content) end end @@ -87,7 +112,7 @@ class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase assert_match(/class UsersControllerTest < ActionController::TestCase/, content) assert_match(/test "should get index"/, content) assert_match(/post :create, user: \{ \}/, content) - assert_match(/put :update, id: @user, user: \{ \}/, content) + assert_match(/patch :update, id: @user, user: \{ \}/, content) end end @@ -102,6 +127,18 @@ class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase assert_no_file "app/views/layouts/users.html.erb" end + def test_skip_html_if_required + run_generator [ "User", "name:string", "age:integer", "--no-html" ] + assert_no_file "app/helpers/users_helper.rb" + assert_no_file "app/views/users" + + assert_file "app/controllers/users_controller.rb" do |content| + assert_no_match(/format\.html/, content) + assert_no_match(/def edit/, content) + assert_no_match(/def new/, content) + end + end + def test_default_orm_is_used run_generator ["User", "--orm=unknown"] diff --git a/railties/test/generators/scaffold_generator_test.rb b/railties/test/generators/scaffold_generator_test.rb index 54d5a9db6f..431b23b014 100644 --- a/railties/test/generators/scaffold_generator_test.rb +++ b/railties/test/generators/scaffold_generator_test.rb @@ -30,17 +30,13 @@ class ScaffoldGeneratorTest < Rails::Generators::TestCase assert_match(/@product_lines = ProductLine\.all/, m) end - assert_instance_method :show, content do |m| - assert_match(/@product_line = ProductLine\.find\(params\[:id\]\)/, m) - end + assert_instance_method :show, content assert_instance_method :new, content do |m| assert_match(/@product_line = ProductLine\.new/, m) end - assert_instance_method :edit, content do |m| - assert_match(/@product_line = ProductLine\.find\(params\[:id\]\)/, m) - end + assert_instance_method :edit, content assert_instance_method :create, content do |m| assert_match(/@product_line = ProductLine\.new\(product_line_params\)/, m) @@ -49,21 +45,23 @@ class ScaffoldGeneratorTest < Rails::Generators::TestCase end assert_instance_method :update, content do |m| - assert_match(/@product_line = ProductLine\.find\(params\[:id\]\)/, m) - assert_match(/@product_line\.update_attributes\(product_line_params\)/, m) + assert_match(/@product_line\.update\(product_line_params\)/, m) assert_match(/@product_line\.errors/, m) end assert_instance_method :destroy, content do |m| - assert_match(/@product_line = ProductLine\.find\(params\[:id\]\)/, m) assert_match(/@product_line\.destroy/, m) end + + assert_instance_method :set_product_line, content do |m| + assert_match(/@product_line = ProductLine\.find\(params\[:id\]\)/, m) + end end assert_file "test/controllers/product_lines_controller_test.rb" do |test| assert_match(/class ProductLinesControllerTest < ActionController::TestCase/, test) - assert_match(/post :create, product_line: \{ title: @product_line.title \}/, test) - assert_match(/put :update, id: @product_line, product_line: \{ title: @product_line.title \}/, test) + assert_match(/post :create, product_line: \{ product_id: @product_line\.product_id, title: @product_line\.title, user_id: @product_line\.user_id \}/, test) + assert_match(/patch :update, id: @product_line, product_line: \{ product_id: @product_line\.product_id, title: @product_line\.title, user_id: @product_line\.user_id \}/, test) end # Views @@ -89,7 +87,7 @@ class ScaffoldGeneratorTest < Rails::Generators::TestCase assert_match(/class ProductLinesControllerTest < ActionController::TestCase/, content) assert_match(/test "should get index"/, content) assert_match(/post :create, product_line: \{ \}/, content) - assert_match(/put :update, id: @product_line, product_line: \{ \}/, content) + assert_match(/patch :update, id: @product_line, product_line: \{ \}/, content) end end @@ -149,17 +147,13 @@ class ScaffoldGeneratorTest < Rails::Generators::TestCase assert_match(/@admin_roles = Admin::Role\.all/, m) end - assert_instance_method :show, content do |m| - assert_match(/@admin_role = Admin::Role\.find\(params\[:id\]\)/, m) - end + assert_instance_method :show, content assert_instance_method :new, content do |m| assert_match(/@admin_role = Admin::Role\.new/, m) end - assert_instance_method :edit, content do |m| - assert_match(/@admin_role = Admin::Role\.find\(params\[:id\]\)/, m) - end + assert_instance_method :edit, content assert_instance_method :create, content do |m| assert_match(/@admin_role = Admin::Role\.new\(admin_role_params\)/, m) @@ -168,15 +162,17 @@ class ScaffoldGeneratorTest < Rails::Generators::TestCase end assert_instance_method :update, content do |m| - assert_match(/@admin_role = Admin::Role\.find\(params\[:id\]\)/, m) - assert_match(/@admin_role\.update_attributes\(admin_role_params\)/, m) + assert_match(/@admin_role\.update\(admin_role_params\)/, m) assert_match(/@admin_role\.errors/, m) end assert_instance_method :destroy, content do |m| - assert_match(/@admin_role = Admin::Role\.find\(params\[:id\]\)/, m) assert_match(/@admin_role\.destroy/, m) end + + assert_instance_method :set_admin_role, content do |m| + assert_match(/@admin_role = Admin::Role\.find\(params\[:id\]\)/, m) + end end assert_file "test/controllers/admin/roles_controller_test.rb", @@ -203,7 +199,7 @@ class ScaffoldGeneratorTest < Rails::Generators::TestCase run_generator [ "admin/role" ], :behavior => :revoke # Model - assert_file "app/models/admin.rb" # ( should not be remove ) + assert_file "app/models/admin.rb" # ( should not be remove ) assert_no_file "app/models/admin/role.rb" assert_no_file "test/models/admin/role_test.rb" assert_no_file "test/fixtures/admin/roles.yml" @@ -261,6 +257,11 @@ class ScaffoldGeneratorTest < Rails::Generators::TestCase assert_no_file "app/assets/stylesheets/posts.css" end + def test_scaffold_generator_no_html + run_generator [ "posts", "--no-html" ] + assert_no_file "app/assets/stylesheets/scaffold.css" + end + def test_scaffold_generator_no_javascripts run_generator [ "posts", "--no-javascripts" ] assert_file "app/assets/stylesheets/scaffold.css" diff --git a/railties/test/generators/shared_generator_tests.rb b/railties/test/generators/shared_generator_tests.rb index 1e5a4545a1..e4924c8386 100644 --- a/railties/test/generators/shared_generator_tests.rb +++ b/railties/test/generators/shared_generator_tests.rb @@ -48,7 +48,7 @@ module SharedGeneratorTests def test_options_before_application_name_raises_an_error content = capture(:stderr){ run_generator(["--pretend", destination_root]) } - assert_match(/Options should be given after the \w+ name. For details run: rails( plugin)? --help\n/, content) + assert_match(/Options should be given after the \w+ name. For details run: rails( plugin new)? --help\n/, content) end def test_name_collision_raises_an_error diff --git a/railties/test/isolation/abstract_unit.rb b/railties/test/isolation/abstract_unit.rb index 0cb65f8e0d..172a42a549 100644 --- a/railties/test/isolation/abstract_unit.rb +++ b/railties/test/isolation/abstract_unit.rb @@ -9,7 +9,7 @@ require 'fileutils' require 'bundler/setup' unless defined?(Bundler) -require 'minitest/autorun' +require 'active_support/testing/autorun' require 'active_support/test_case' RAILS_FRAMEWORK_ROOT = File.expand_path("#{File.dirname(__FILE__)}/../../..") diff --git a/railties/test/rails_info_controller_test.rb b/railties/test/rails_info_controller_test.rb index 08fcddd4bf..a9b237d0a5 100644 --- a/railties/test/rails_info_controller_test.rb +++ b/railties/test/rails_info_controller_test.rb @@ -50,7 +50,7 @@ class InfoControllerTest < ActionController::TestCase test "info controller renders with routes" do get :routes - assert_select 'pre' + assert_response :success end end diff --git a/railties/test/railties/engine_test.rb b/railties/test/railties/engine_test.rb index fcbe7b6cda..a4a75fe459 100644 --- a/railties/test/railties/engine_test.rb +++ b/railties/test/railties/engine_test.rb @@ -568,7 +568,7 @@ YAML @plugin.write "lib/bukkits.rb", <<-RUBY module Bukkits class Engine < ::Rails::Engine - endpoint lambda { |env| [200, {'Content-Type' => 'text/html'}, 'hello'] } + endpoint lambda { |env| [200, {'Content-Type' => 'text/html'}, ['hello']] } end end RUBY |