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.rb31
-rw-r--r--railties/test/application/dbconsole_test.rb52
-rw-r--r--railties/test/application/loading_test.rb6
-rw-r--r--railties/test/application/rake/dbs_test.rb7
-rw-r--r--railties/test/application/rake_test.rb46
-rw-r--r--railties/test/application/routing_test.rb6
-rw-r--r--railties/test/application/runner_test.rb8
-rw-r--r--railties/test/application/server_test.rb31
-rw-r--r--railties/test/application/test_runner_test.rb28
-rw-r--r--railties/test/application/test_test.rb2
-rw-r--r--railties/test/commands/console_test.rb29
-rw-r--r--railties/test/commands/dbconsole_test.rb59
-rw-r--r--railties/test/console_helpers.rb23
-rw-r--r--railties/test/engine/commands_test.rb24
-rw-r--r--railties/test/generators/app_generator_test.rb31
-rw-r--r--railties/test/generators/application_record_generator_test.rb14
-rw-r--r--railties/test/generators/migration_generator_test.rb22
-rw-r--r--railties/test/generators/model_generator_test.rb19
-rw-r--r--railties/test/generators/namespaced_generators_test.rb11
-rw-r--r--railties/test/generators/plugin_generator_test.rb18
-rw-r--r--railties/test/generators/plugin_test_runner_test.rb2
-rw-r--r--railties/test/generators_test.rb2
-rw-r--r--railties/test/railties/engine_test.rb47
-rw-r--r--railties/test/railties/mounted_engine_test.rb9
-rw-r--r--railties/test/secrets_test.rb22
26 files changed, 441 insertions, 161 deletions
diff --git a/railties/test/application/configuration_test.rb b/railties/test/application/configuration_test.rb
index b8a19379e0..c2f6a5a95c 100644
--- a/railties/test/application/configuration_test.rb
+++ b/railties/test/application/configuration_test.rb
@@ -453,10 +453,8 @@ module ApplicationTests
secret_key_base: 123
YAML
- app "development"
-
assert_raise(ArgumentError) do
- app.key_generator
+ app "development"
end
end
@@ -1130,7 +1128,7 @@ module ApplicationTests
app "development"
- ActionController::Base.object_id # force lazy load hooks to run
+ force_lazy_load_hooks { ActionController::Base }
assert_equal :raise, ActionController::Parameters.action_on_unpermitted_parameters
@@ -1141,7 +1139,7 @@ module ApplicationTests
test "config.action_controller.always_permitted_parameters are: controller, action by default" do
app "development"
- ActionController::Base.object_id # force lazy load hooks to run
+ force_lazy_load_hooks { ActionController::Base }
assert_equal %w(controller action), ActionController::Parameters.always_permitted_parameters
end
@@ -1153,7 +1151,7 @@ module ApplicationTests
app "development"
- ActionController::Base.object_id # force lazy load hooks to run
+ force_lazy_load_hooks { ActionController::Base }
assert_equal %w( controller action format ), ActionController::Parameters.always_permitted_parameters
end
@@ -1177,7 +1175,7 @@ module ApplicationTests
app "development"
- ActionController::Base.object_id # force lazy load hooks to run
+ force_lazy_load_hooks { ActionController::Base }
assert_equal :raise, ActionController::Parameters.action_on_unpermitted_parameters
@@ -1188,7 +1186,7 @@ module ApplicationTests
test "config.action_controller.action_on_unpermitted_parameters is :log by default on development" do
app "development"
- ActionController::Base.object_id # force lazy load hooks to run
+ force_lazy_load_hooks { ActionController::Base }
assert_equal :log, ActionController::Parameters.action_on_unpermitted_parameters
end
@@ -1196,7 +1194,7 @@ module ApplicationTests
test "config.action_controller.action_on_unpermitted_parameters is :log by default on test" do
app "test"
- ActionController::Base.object_id # force lazy load hooks to run
+ force_lazy_load_hooks { ActionController::Base }
assert_equal :log, ActionController::Parameters.action_on_unpermitted_parameters
end
@@ -1204,7 +1202,7 @@ module ApplicationTests
test "config.action_controller.action_on_unpermitted_parameters is false by default on production" do
app "production"
- ActionController::Base.object_id # force lazy load hooks to run
+ force_lazy_load_hooks { ActionController::Base }
assert_equal false, ActionController::Parameters.action_on_unpermitted_parameters
end
@@ -1223,7 +1221,7 @@ module ApplicationTests
app "development"
- ActionController::Base.object_id # force lazy load hooks to run
+ force_lazy_load_hooks { ActionController::Base }
assert_equal true, ActionController::Parameters.permit_all_parameters
end
@@ -1234,7 +1232,7 @@ module ApplicationTests
app "development"
- ActionController::Base.object_id # force lazy load hooks to run
+ force_lazy_load_hooks { ActionController::Base }
assert_equal [], ActionController::Parameters.always_permitted_parameters
end
@@ -1245,7 +1243,7 @@ module ApplicationTests
app "development"
- ActionController::Base.object_id # force lazy load hooks to run
+ force_lazy_load_hooks { ActionController::Base }
assert_equal :raise, ActionController::Parameters.action_on_unpermitted_parameters
end
@@ -1452,8 +1450,8 @@ module ApplicationTests
test "raises with proper error message if no database configuration found" do
FileUtils.rm("#{app_path}/config/database.yml")
- app "development"
err = assert_raises RuntimeError do
+ app "development"
Rails.application.config.database_configuration
end
assert_match "config/database", err.message
@@ -1596,7 +1594,7 @@ module ApplicationTests
RUBY
app "development"
- Post.object_id # force lazy load hooks to run
+ force_lazy_load_hooks { Post }
assert_not ActiveRecord::ConnectionAdapters::SQLite3Adapter.represent_boolean_as_integer
end
@@ -1608,7 +1606,25 @@ module ApplicationTests
RUBY
app "development"
- Post.object_id # force lazy load hooks to run
+ force_lazy_load_hooks { Post }
+
+ assert ActiveRecord::ConnectionAdapters::SQLite3Adapter.represent_boolean_as_integer
+ end
+
+ test "represent_boolean_as_integer should be able to set via config.active_record.sqlite3.represent_boolean_as_integer" do
+ remove_from_config '.*config\.load_defaults.*\n'
+
+ app_file "config/initializers/new_framework_defaults_5_2.rb", <<-RUBY
+ Rails.application.config.active_record.sqlite3.represent_boolean_as_integer = true
+ RUBY
+
+ app_file "app/models/post.rb", <<-RUBY
+ class Post < ActiveRecord::Base
+ end
+ RUBY
+
+ app "development"
+ force_lazy_load_hooks { Post }
assert ActiveRecord::ConnectionAdapters::SQLite3Adapter.represent_boolean_as_integer
end
@@ -1719,5 +1735,10 @@ module ApplicationTests
assert_equal 301, last_response.status
assert_equal "https://example.org/", last_response.location
end
+
+ private
+ def force_lazy_load_hooks
+ yield # Tasty clarifying sugar, homie! We only need to reference a constant to load it.
+ end
end
end
diff --git a/railties/test/application/console_test.rb b/railties/test/application/console_test.rb
index 057d473870..31bef82ccc 100644
--- a/railties/test/application/console_test.rb
+++ b/railties/test/application/console_test.rb
@@ -1,4 +1,5 @@
require "isolation/abstract_unit"
+require "console_helpers"
class ConsoleTest < ActiveSupport::TestCase
include ActiveSupport::Testing::Isolation
@@ -93,14 +94,11 @@ class ConsoleTest < ActiveSupport::TestCase
end
end
-begin
- require "pty"
-rescue LoadError
-end
-
class FullStackConsoleTest < ActiveSupport::TestCase
+ include ConsoleHelpers
+
def setup
- skip "PTY unavailable" unless defined?(PTY) && PTY.respond_to?(:open)
+ skip "PTY unavailable" unless available_pty?
build_app
app_file "app/models/post.rb", <<-CODE
@@ -116,24 +114,11 @@ class FullStackConsoleTest < ActiveSupport::TestCase
teardown_app
end
- def assert_output(expected, timeout = 1)
- timeout = Time.now + timeout
-
- output = ""
- until output.include?(expected) || Time.now > timeout
- if IO.select([@master], [], [], 0.1)
- output << @master.read(1)
- end
- end
-
- assert_includes output, expected, "#{expected.inspect} expected, but got:\n\n#{output}"
- end
-
def write_prompt(command, expected_output = nil)
@master.puts command
- assert_output command
- assert_output expected_output if expected_output
- assert_output "> "
+ assert_output command, @master
+ assert_output expected_output, @master if expected_output
+ assert_output "> ", @master
end
def spawn_console(options)
@@ -142,7 +127,7 @@ class FullStackConsoleTest < ActiveSupport::TestCase
in: @slave, out: @slave, err: @slave
)
- assert_output "> ", 30
+ assert_output "> ", @master, 30
end
def test_sandbox
diff --git a/railties/test/application/dbconsole_test.rb b/railties/test/application/dbconsole_test.rb
index 7e5e9ea8aa..5d89d0e44d 100644
--- a/railties/test/application/dbconsole_test.rb
+++ b/railties/test/application/dbconsole_test.rb
@@ -1,14 +1,14 @@
require "isolation/abstract_unit"
-begin
- require "pty"
-rescue LoadError
-end
+require "console_helpers"
module ApplicationTests
class DBConsoleTest < ActiveSupport::TestCase
include ActiveSupport::Testing::Isolation
+ include ConsoleHelpers
def setup
+ skip "PTY unavailable" unless available_pty?
+
build_app
end
@@ -17,7 +17,6 @@ module ApplicationTests
end
def test_use_value_defined_in_environment_file_in_database_yml
- skip "PTY unavailable" unless available_pty?
Dir.chdir(app_path) do
app_file "config/database.yml", <<-YAML
development:
@@ -41,26 +40,37 @@ module ApplicationTests
master.puts ".exit"
end
- private
- def spawn_dbconsole(fd)
- Process.spawn("#{app_path}/bin/rails dbconsole", in: fd, out: fd, err: fd)
- end
-
- def assert_output(expected, io, timeout = 5)
- timeout = Time.now + timeout
+ def test_respect_environment_option
+ Dir.chdir(app_path) do
+ app_file "config/database.yml", <<-YAML
+ default: &default
+ adapter: sqlite3
+ pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
+ timeout: 5000
- output = ""
- until output.include?(expected) || Time.now > timeout
- if IO.select([io], [], [], 0.1)
- output << io.read(1)
- end
- end
+ development:
+ <<: *default
+ database: db/development.sqlite3
- assert_includes output, expected, "#{expected.inspect} expected, but got:\n\n#{output}"
+ production:
+ <<: *default
+ database: db/production.sqlite3
+ YAML
end
- def available_pty?
- defined?(PTY) && PTY.respond_to?(:open)
+ master, slave = PTY.open
+ spawn_dbconsole(slave, "-e production")
+ assert_output("sqlite>", master)
+
+ master.puts "pragma database_list;"
+ assert_output("production.sqlite3", master)
+ ensure
+ master.puts ".exit"
+ end
+
+ private
+ def spawn_dbconsole(fd, options = nil)
+ Process.spawn("#{app_path}/bin/rails dbconsole #{options}", in: fd, out: fd, err: fd)
end
end
end
diff --git a/railties/test/application/loading_test.rb b/railties/test/application/loading_test.rb
index c75a25bc6f..dd62907d18 100644
--- a/railties/test/application/loading_test.rb
+++ b/railties/test/application/loading_test.rb
@@ -115,11 +115,11 @@ class LoadingTest < ActiveSupport::TestCase
require "#{rails_root}/config/environment"
setup_ar!
- assert_equal [ActiveRecord::SchemaMigration, ActiveRecord::InternalMetadata], ActiveRecord::Base.descendants
+ assert_equal [ActiveStorage::Blob, ActiveStorage::Attachment, ActiveRecord::SchemaMigration, ActiveRecord::InternalMetadata].collect(&:to_s).sort, ActiveRecord::Base.descendants.collect(&:to_s).sort
get "/load"
- assert_equal [ActiveRecord::SchemaMigration, ActiveRecord::InternalMetadata, Post], ActiveRecord::Base.descendants
+ assert_equal [ActiveStorage::Blob, ActiveStorage::Attachment, ActiveRecord::SchemaMigration, ActiveRecord::InternalMetadata, Post].collect(&:to_s).sort, ActiveRecord::Base.descendants.collect(&:to_s).sort
get "/unload"
- assert_equal [ActiveRecord::SchemaMigration, ActiveRecord::InternalMetadata], ActiveRecord::Base.descendants
+ assert_equal [ActiveStorage::Blob, ActiveStorage::Attachment, ActiveRecord::SchemaMigration, ActiveRecord::InternalMetadata].collect(&:to_s).sort, ActiveRecord::Base.descendants.collect(&:to_s).sort
end
test "initialize cant be called twice" do
diff --git a/railties/test/application/rake/dbs_test.rb b/railties/test/application/rake/dbs_test.rb
index c63f23fa0a..3216121de3 100644
--- a/railties/test/application/rake/dbs_test.rb
+++ b/railties/test/application/rake/dbs_test.rb
@@ -259,6 +259,13 @@ module ApplicationTests
end
end
+ test "db:schema:load fails if schema.rb doesn't exist yet" do
+ Dir.chdir(app_path) do
+ stderr_output = capture(:stderr) { `bin/rails db:schema:load` }
+ assert_match(/Run `rails db:migrate` to create it/, stderr_output)
+ end
+ end
+
def db_test_load_structure
Dir.chdir(app_path) do
`bin/rails generate model book title:string;
diff --git a/railties/test/application/rake_test.rb b/railties/test/application/rake_test.rb
index 134106812d..6340f8f7b1 100644
--- a/railties/test/application/rake_test.rb
+++ b/railties/test/application/rake_test.rb
@@ -129,7 +129,15 @@ module ApplicationTests
RUBY
output = Dir.chdir(app_path) { `bin/rails routes` }
- assert_equal "Prefix Verb URI Pattern Controller#Action\n cart GET /cart(.:format) cart#show\n", output
+ assert_equal <<-MESSAGE.strip_heredoc, output
+ Prefix Verb URI Pattern Controller#Action
+ cart GET /cart(.:format) cart#show
+ rails_service_blob GET /rails/active_storage/blobs/:signed_id/*filename(.:format) active_storage/blobs#show
+ rails_blob_variation GET /rails/active_storage/variants/:signed_blob_id/:variation_key/*filename(.:format) active_storage/variants#show
+ rails_disk_service GET /rails/active_storage/disk/:encoded_key/*filename(.:format) active_storage/disk#show
+ update_rails_disk_service PUT /rails/active_storage/disk/:encoded_token(.:format) active_storage/disk#update
+ rails_direct_uploads POST /rails/active_storage/direct_uploads(.:format) active_storage/direct_uploads#create
+ MESSAGE
end
def test_singular_resource_output_in_rake_routes
@@ -162,10 +170,20 @@ module ApplicationTests
RUBY
output = Dir.chdir(app_path) { `bin/rails routes -g show` }
- assert_equal "Prefix Verb URI Pattern Controller#Action\n cart GET /cart(.:format) cart#show\n", output
+ assert_equal <<-MESSAGE.strip_heredoc, output
+ Prefix Verb URI Pattern Controller#Action
+ cart GET /cart(.:format) cart#show
+ rails_service_blob GET /rails/active_storage/blobs/:signed_id/*filename(.:format) active_storage/blobs#show
+ rails_blob_variation GET /rails/active_storage/variants/:signed_blob_id/:variation_key/*filename(.:format) active_storage/variants#show
+ rails_disk_service GET /rails/active_storage/disk/:encoded_key/*filename(.:format) active_storage/disk#show
+ MESSAGE
output = Dir.chdir(app_path) { `bin/rails routes -g POST` }
- assert_equal "Prefix Verb URI Pattern Controller#Action\n POST /cart(.:format) cart#create\n", output
+ assert_equal <<-MESSAGE.strip_heredoc, output
+ Prefix Verb URI Pattern Controller#Action
+ POST /cart(.:format) cart#create
+ rails_direct_uploads POST /rails/active_storage/direct_uploads(.:format) active_storage/direct_uploads#create
+ MESSAGE
output = Dir.chdir(app_path) { `bin/rails routes -g basketballs` }
assert_equal " Prefix Verb URI Pattern Controller#Action\n" \
@@ -221,11 +239,12 @@ module ApplicationTests
RUBY
assert_equal <<-MESSAGE.strip_heredoc, Dir.chdir(app_path) { `bin/rails routes` }
- You don't have any routes defined!
-
- Please add some routes in config/routes.rb.
-
- For more information about routes, see the Rails guide: http://guides.rubyonrails.org/routing.html.
+ Prefix Verb URI Pattern Controller#Action
+ rails_service_blob GET /rails/active_storage/blobs/:signed_id/*filename(.:format) active_storage/blobs#show
+ rails_blob_variation GET /rails/active_storage/variants/:signed_blob_id/:variation_key/*filename(.:format) active_storage/variants#show
+ rails_disk_service GET /rails/active_storage/disk/:encoded_key/*filename(.:format) active_storage/disk#show
+ update_rails_disk_service PUT /rails/active_storage/disk/:encoded_token(.:format) active_storage/disk#update
+ rails_direct_uploads POST /rails/active_storage/direct_uploads(.:format) active_storage/direct_uploads#create
MESSAGE
end
@@ -237,7 +256,16 @@ module ApplicationTests
RUBY
output = Dir.chdir(app_path) { `bin/rake --rakefile Rakefile routes` }
- assert_equal "Prefix Verb URI Pattern Controller#Action\n cart GET /cart(.:format) cart#show\n", output
+
+ assert_equal <<-MESSAGE.strip_heredoc, output
+ Prefix Verb URI Pattern Controller#Action
+ cart GET /cart(.:format) cart#show
+ rails_service_blob GET /rails/active_storage/blobs/:signed_id/*filename(.:format) active_storage/blobs#show
+ rails_blob_variation GET /rails/active_storage/variants/:signed_blob_id/:variation_key/*filename(.:format) active_storage/variants#show
+ rails_disk_service GET /rails/active_storage/disk/:encoded_key/*filename(.:format) active_storage/disk#show
+ update_rails_disk_service PUT /rails/active_storage/disk/:encoded_token(.:format) active_storage/disk#update
+ rails_direct_uploads POST /rails/active_storage/direct_uploads(.:format) active_storage/direct_uploads#create
+ MESSAGE
end
def test_logger_is_flushed_when_exiting_production_rake_tasks
diff --git a/railties/test/application/routing_test.rb b/railties/test/application/routing_test.rb
index 6742da20cc..bc7580d6f4 100644
--- a/railties/test/application/routing_test.rb
+++ b/railties/test/application/routing_test.rb
@@ -293,7 +293,7 @@ module ApplicationTests
extend ActiveModel::Naming
include ActiveModel::Conversion
- def model_name
+ def self.model_name
@_model_name ||= ActiveModel::Name.new(self.class, nil, "User")
end
@@ -430,7 +430,7 @@ module ApplicationTests
extend ActiveModel::Naming
include ActiveModel::Conversion
- def model_name
+ def self.model_name
@_model_name ||= ActiveModel::Name.new(self.class, nil, "User")
end
@@ -542,7 +542,7 @@ module ApplicationTests
extend ActiveModel::Naming
include ActiveModel::Conversion
- def model_name
+ def self.model_name
@_model_name ||= ActiveModel::Name.new(self.class, nil, "User")
end
diff --git a/railties/test/application/runner_test.rb b/railties/test/application/runner_test.rb
index 0c45bc398a..81f717b2c3 100644
--- a/railties/test/application/runner_test.rb
+++ b/railties/test/application/runner_test.rb
@@ -84,6 +84,14 @@ module ApplicationTests
assert_match %w( a b ).to_s, Dir.chdir(app_path) { `bin/rails runner "bin/program_name.rb" a b` }
end
+ def test_should_run_stdin
+ app_file "bin/count_users.rb", <<-SCRIPT
+ puts User.count
+ SCRIPT
+
+ assert_match "42", Dir.chdir(app_path) { `cat bin/count_users.rb | bin/rails runner -` }
+ end
+
def test_with_hook
add_to_config <<-RUBY
runner do |app|
diff --git a/railties/test/application/server_test.rb b/railties/test/application/server_test.rb
new file mode 100644
index 0000000000..07880a5025
--- /dev/null
+++ b/railties/test/application/server_test.rb
@@ -0,0 +1,31 @@
+require "isolation/abstract_unit"
+require "rails/command"
+require "rails/commands/server/server_command"
+
+module ApplicationTests
+ class ServerTest < ActiveSupport::TestCase
+ include ActiveSupport::Testing::Isolation
+
+ def setup
+ build_app
+ end
+
+ def teardown
+ teardown_app
+ end
+
+ test "deprecate support of older `config.ru`" do
+ remove_file "config.ru"
+ app_file "config.ru", <<-RUBY
+ require_relative 'config/environment'
+ run AppTemplate::Application
+ RUBY
+
+ server = Rails::Server.new(config: "#{app_path}/config.ru")
+ server.app
+
+ log = File.read(Rails.application.config.paths["log"].first)
+ assert_match(/DEPRECATION WARNING: Use `Rails::Application` subclass to start the server is deprecated/, log)
+ end
+ end
+end
diff --git a/railties/test/application/test_runner_test.rb b/railties/test/application/test_runner_test.rb
index c0027ab9a2..be50fdb49b 100644
--- a/railties/test/application/test_runner_test.rb
+++ b/railties/test/application/test_runner_test.rb
@@ -31,12 +31,26 @@ module ApplicationTests
assert_match "1 runs, 1 assertions, 0 failures", run_test_command("test/models/foo_test.rb")
end
+ def test_run_single_file_with_absolute_path
+ create_test_file :models, "foo"
+ create_test_file :models, "bar"
+ assert_match "1 runs, 1 assertions, 0 failures", run_test_command("#{app_path}/test/models/foo_test.rb")
+ end
+
def test_run_multiple_files
create_test_file :models, "foo"
create_test_file :models, "bar"
assert_match "2 runs, 2 assertions, 0 failures", run_test_command("test/models/foo_test.rb test/models/bar_test.rb")
end
+ def test_run_multiple_files_with_absolute_paths
+ create_test_file :models, "foo"
+ create_test_file :controllers, "foobar_controller"
+ create_test_file :models, "bar"
+
+ assert_match "2 runs, 2 assertions, 0 failures", run_test_command("#{app_path}/test/models/foo_test.rb #{app_path}/test/controllers/foobar_controller_test.rb")
+ end
+
def test_run_file_with_syntax_error
app_file "test/models/error_test.rb", <<-RUBY
require 'test_helper'
@@ -264,6 +278,18 @@ module ApplicationTests
end
end
+ def test_run_multiple_folders_with_absolute_paths
+ create_test_file :models, "account"
+ create_test_file :controllers, "accounts_controller"
+ create_test_file :helpers, "foo_helper"
+
+ run_test_command("#{app_path}/test/models #{app_path}/test/controllers").tap do |output|
+ assert_match "AccountTest", output
+ assert_match "AccountsControllerTest", output
+ assert_match "2 runs, 2 assertions, 0 failures, 0 errors, 0 skips", output
+ end
+ end
+
def test_run_with_ruby_command
app_file "test/models/post_test.rb", <<-RUBY
require 'test_helper'
@@ -489,7 +515,7 @@ module ApplicationTests
create_test_file :models, "post", pass: false
output = run_test_command("test/models/post_test.rb")
- assert_match %r{Finished in.*\n\n1 runs, 1 assertions}, output
+ assert_match %r{Finished in.*\n1 runs, 1 assertions}, output
end
def test_fail_fast
diff --git a/railties/test/application/test_test.rb b/railties/test/application/test_test.rb
index 32d2a6857c..2a1a6bcbfd 100644
--- a/railties/test/application/test_test.rb
+++ b/railties/test/application/test_test.rb
@@ -55,7 +55,7 @@ module ApplicationTests
end
RUBY
- assert_unsuccessful_run "unit/foo_test.rb", "Failed assertion"
+ assert_unsuccessful_run "unit/foo_test.rb", "Failure:\nFooTest#test_truth"
end
test "integration test" do
diff --git a/railties/test/commands/console_test.rb b/railties/test/commands/console_test.rb
index 4fc082e4ca..a7169e16fb 100644
--- a/railties/test/commands/console_test.rb
+++ b/railties/test/commands/console_test.rb
@@ -47,7 +47,7 @@ class Rails::ConsoleTest < ActiveSupport::TestCase
end
def test_console_with_environment
- start ["-e production"]
+ start ["-e", "production"]
assert_match(/\sproduction\s/, output)
end
@@ -82,24 +82,35 @@ class Rails::ConsoleTest < ActiveSupport::TestCase
assert_match(/\sspecial-production\s/, output)
end
+ def test_e_option_is_properly_expanded
+ start ["-e", "prod"]
+ assert_match(/\sproduction\s/, output)
+ end
+
def test_environment_option
start ["--environment=special-production"]
assert_match(/\sspecial-production\s/, output)
end
def test_rails_env_is_production_when_first_argument_is_p
- start ["p"]
- assert_match(/\sproduction\s/, output)
+ assert_deprecated do
+ start ["p"]
+ assert_match(/\sproduction\s/, output)
+ end
end
def test_rails_env_is_test_when_first_argument_is_t
- start ["t"]
- assert_match(/\stest\s/, output)
+ assert_deprecated do
+ start ["t"]
+ assert_match(/\stest\s/, output)
+ end
end
def test_rails_env_is_development_when_argument_is_d
- start ["d"]
- assert_match(/\sdevelopment\s/, output)
+ assert_deprecated do
+ start ["d"]
+ assert_match(/\sdevelopment\s/, output)
+ end
end
def test_rails_env_is_dev_when_argument_is_dev_and_dev_env_is_present
@@ -111,7 +122,9 @@ class Rails::ConsoleTest < ActiveSupport::TestCase
end
end
- assert_match("dev", parse_arguments(["dev"])[:environment])
+ assert_deprecated do
+ assert_match("dev", parse_arguments(["dev"])[:environment])
+ end
ensure
Rails::Command::ConsoleCommand.class_eval do
undef_method :available_environments
diff --git a/railties/test/commands/dbconsole_test.rb b/railties/test/commands/dbconsole_test.rb
index 0f8c5dbb79..4f55eb9aa6 100644
--- a/railties/test/commands/dbconsole_test.rb
+++ b/railties/test/commands/dbconsole_test.rb
@@ -98,14 +98,24 @@ class Rails::DBConsoleTest < ActiveSupport::TestCase
end
def test_rails_env_is_development_when_argument_is_dev
+ assert_deprecated do
+ stub_available_environments([ "development", "test" ]) do
+ assert_match("development", parse_arguments([ "dev" ])[:environment])
+ end
+ end
+ end
+
+ def test_rails_env_is_development_when_environment_option_is_dev
stub_available_environments([ "development", "test" ]) do
- assert_match("development", parse_arguments([ "dev" ])[:environment])
+ assert_match("development", parse_arguments([ "-e", "dev" ])[:environment])
end
end
def test_rails_env_is_dev_when_argument_is_dev_and_dev_env_is_present
- stub_available_environments([ "dev" ]) do
- assert_match("dev", parse_arguments([ "dev" ])[:environment])
+ assert_deprecated do
+ stub_available_environments([ "dev" ]) do
+ assert_match("dev", parse_arguments([ "dev" ])[:environment])
+ end
end
end
@@ -200,6 +210,49 @@ class Rails::DBConsoleTest < ActiveSupport::TestCase
assert_match(/Unknown command-line client for db/, output)
end
+ def test_primary_is_automatically_picked_with_3_level_configuration
+ sample_config = {
+ "test" => {
+ "primary" => {
+ "adapter" => "postgresql"
+ }
+ }
+ }
+
+ app_db_config(sample_config) do
+ assert_equal "postgresql", Rails::DBConsole.new.config["adapter"]
+ end
+ end
+
+ def test_specifying_a_custom_connection_and_environment
+ stub_available_environments(["development"]) do
+ dbconsole = parse_arguments(["-c", "custom", "-e", "development"])
+
+ assert_equal "development", dbconsole[:environment]
+ assert_equal "custom", dbconsole.connection
+ end
+ end
+
+ def test_specifying_a_missing_connection
+ app_db_config({}) do
+ e = assert_raises(ActiveRecord::AdapterNotSpecified) do
+ Rails::Command.invoke(:dbconsole, ["-c", "i_do_not_exist"])
+ end
+
+ assert_includes e.message, "'i_do_not_exist' connection is not configured."
+ end
+ end
+
+ def test_specifying_a_missing_environment
+ app_db_config({}) do
+ e = assert_raises(ActiveRecord::AdapterNotSpecified) do
+ Rails::Command.invoke(:dbconsole)
+ end
+
+ assert_includes e.message, "'test' database is not configured."
+ end
+ end
+
def test_print_help_short
stdout = capture(:stdout) do
Rails::Command.invoke(:dbconsole, ["-h"])
diff --git a/railties/test/console_helpers.rb b/railties/test/console_helpers.rb
new file mode 100644
index 0000000000..4b11afa511
--- /dev/null
+++ b/railties/test/console_helpers.rb
@@ -0,0 +1,23 @@
+begin
+ require "pty"
+rescue LoadError
+end
+
+module ConsoleHelpers
+ def assert_output(expected, io, timeout = 10)
+ timeout = Time.now + timeout
+
+ output = ""
+ until output.include?(expected) || Time.now > timeout
+ if IO.select([io], [], [], 0.1)
+ output << io.read(1)
+ end
+ end
+
+ assert_includes output, expected, "#{expected.inspect} expected, but got:\n\n#{output}"
+ end
+
+ def available_pty?
+ defined?(PTY) && PTY.respond_to?(:open)
+ end
+end
diff --git a/railties/test/engine/commands_test.rb b/railties/test/engine/commands_test.rb
index b1c937143f..018c7c949e 100644
--- a/railties/test/engine/commands_test.rb
+++ b/railties/test/engine/commands_test.rb
@@ -1,10 +1,9 @@
require "abstract_unit"
-begin
- require "pty"
-rescue LoadError
-end
+require "console_helpers"
class Rails::Engine::CommandsTest < ActiveSupport::TestCase
+ include ConsoleHelpers
+
def setup
@destination_root = Dir.mktmpdir("bukkits")
Dir.chdir(@destination_root) { `bundle exec rails plugin new bukkits --mountable` }
@@ -64,19 +63,6 @@ class Rails::Engine::CommandsTest < ActiveSupport::TestCase
"#{@destination_root}/bukkits"
end
- def assert_output(expected, io, timeout = 10)
- timeout = Time.now + timeout
-
- output = ""
- until output.include?(expected) || Time.now > timeout
- if IO.select([io], [], [], 0.1)
- output << io.read(1)
- end
- end
-
- assert_includes output, expected, "#{expected.inspect} expected, but got:\n\n#{output}"
- end
-
def spawn_command(command, fd)
Process.spawn(
"#{plugin_path}/bin/rails #{command}",
@@ -84,10 +70,6 @@ class Rails::Engine::CommandsTest < ActiveSupport::TestCase
)
end
- def available_pty?
- defined?(PTY) && PTY.respond_to?(:open)
- end
-
def kill(pid)
Process.kill("TERM", pid)
Process.wait(pid)
diff --git a/railties/test/generators/app_generator_test.rb b/railties/test/generators/app_generator_test.rb
index 059c2692be..44c4688aa4 100644
--- a/railties/test/generators/app_generator_test.rb
+++ b/railties/test/generators/app_generator_test.rb
@@ -4,6 +4,7 @@ require "generators/shared_generator_tests"
DEFAULT_APP_FILES = %w(
.gitignore
+ .ruby-version
README.md
Gemfile
Rakefile
@@ -63,6 +64,7 @@ DEFAULT_APP_FILES = %w(
config/routes.rb
config/secrets.yml
config/spring.rb
+ config/storage.yml
db
db/seeds.rb
lib
@@ -278,6 +280,22 @@ class AppGeneratorTest < Rails::Generators::TestCase
end
end
+ def test_app_update_does_not_generate_action_cable_contents_when_skip_action_cable_is_given
+ app_root = File.join(destination_root, "myapp")
+ run_generator [app_root, "--skip-action-cable"]
+
+ FileUtils.cd(app_root) do
+ # For avoid conflict file
+ FileUtils.rm("#{app_root}/config/secrets.yml")
+ quietly { system("bin/rails app:update") }
+ end
+
+ assert_no_file "#{app_root}/config/cable.yml"
+ assert_file "#{app_root}/config/environments/production.rb" do |content|
+ assert_no_match(/config\.action_cable/, content)
+ end
+ end
+
def test_application_names_are_not_singularized
run_generator [File.join(destination_root, "hats")]
assert_file "hats/config/environment.rb", /Rails\.application\.initialize!/
@@ -505,6 +523,8 @@ class AppGeneratorTest < Rails::Generators::TestCase
run_generator
if defined?(JRUBY_VERSION)
assert_gem "therubyrhino"
+ elsif RUBY_PLATFORM =~ /mingw|mswin/
+ assert_gem "duktape"
else
assert_file "Gemfile", /# gem 'mini_racer', platforms: :ruby/
end
@@ -789,6 +809,17 @@ class AppGeneratorTest < Rails::Generators::TestCase
end
end
+ def test_inclusion_of_ruby_version
+ run_generator
+
+ assert_file "Gemfile" do |content|
+ assert_match(/ruby '#{RUBY_VERSION}'/, content)
+ end
+ assert_file ".ruby-version" do |content|
+ assert_match(/#{RUBY_VERSION}/, content)
+ end
+ end
+
def test_version_control_initializes_git_repo
run_generator [destination_root]
assert_directory ".git"
diff --git a/railties/test/generators/application_record_generator_test.rb b/railties/test/generators/application_record_generator_test.rb
new file mode 100644
index 0000000000..b734d786c0
--- /dev/null
+++ b/railties/test/generators/application_record_generator_test.rb
@@ -0,0 +1,14 @@
+require "generators/generators_test_helper"
+require "rails/generators/rails/application_record/application_record_generator"
+
+class ApplicationRecordGeneratorTest < Rails::Generators::TestCase
+ include GeneratorsTestHelper
+
+ def test_application_record_skeleton_is_created
+ run_generator
+ assert_file "app/models/application_record.rb" do |record|
+ assert_match(/class ApplicationRecord < ActiveRecord::Base/, record)
+ assert_match(/self\.abstract_class = true/, record)
+ end
+ end
+end
diff --git a/railties/test/generators/migration_generator_test.rb b/railties/test/generators/migration_generator_test.rb
index 6fe6e4ca07..db0808750e 100644
--- a/railties/test/generators/migration_generator_test.rb
+++ b/railties/test/generators/migration_generator_test.rb
@@ -48,6 +48,17 @@ class MigrationGeneratorTest < Rails::Generators::TestCase
end
end
+ def test_add_migration_with_table_having_from_in_title
+ migration = "add_email_address_to_blacklisted_from_campaign"
+ run_generator [migration, "email_address:string"]
+
+ assert_migration "db/migrate/#{migration}.rb" do |content|
+ assert_method :change, content do |change|
+ assert_match(/add_column :blacklisted_from_campaigns, :email_address, :string/, change)
+ end
+ end
+ end
+
def test_remove_migration_with_indexed_attribute
migration = "remove_title_body_from_posts"
run_generator [migration, "title:string:index", "body:text"]
@@ -73,6 +84,17 @@ class MigrationGeneratorTest < Rails::Generators::TestCase
end
end
+ def test_remove_migration_with_table_having_to_in_title
+ migration = "remove_email_address_from_sent_to_user"
+ run_generator [migration, "email_address:string"]
+
+ assert_migration "db/migrate/#{migration}.rb" do |content|
+ assert_method :change, content do |change|
+ assert_match(/remove_column :sent_to_users, :email_address, :string/, change)
+ end
+ end
+ end
+
def test_remove_migration_with_references_options
migration = "remove_references_from_books"
run_generator [migration, "author:belongs_to", "distributor:references{polymorphic}"]
diff --git a/railties/test/generators/model_generator_test.rb b/railties/test/generators/model_generator_test.rb
index f41969fc46..651713d3c0 100644
--- a/railties/test/generators/model_generator_test.rb
+++ b/railties/test/generators/model_generator_test.rb
@@ -6,14 +6,6 @@ class ModelGeneratorTest < Rails::Generators::TestCase
include GeneratorsTestHelper
arguments %w(Account name:string age:integer)
- def test_application_record_skeleton_is_created
- run_generator
- assert_file "app/models/application_record.rb" do |record|
- assert_match(/class ApplicationRecord < ActiveRecord::Base/, record)
- assert_match(/self\.abstract_class = true/, record)
- end
- end
-
def test_help_shows_invoked_generators_options
content = run_generator ["--help"]
assert_match(/ActiveRecord options:/, content)
@@ -43,17 +35,6 @@ class ModelGeneratorTest < Rails::Generators::TestCase
assert_no_migration "db/migrate/create_accounts.rb"
end
- def test_model_with_existent_application_record
- mkdir_p "#{destination_root}/app/models"
- touch "#{destination_root}/app/models/application_record.rb"
-
- Dir.chdir(destination_root) do
- run_generator ["account"]
- end
-
- assert_file "app/models/account.rb", /class Account < ApplicationRecord/
- end
-
def test_plural_names_are_singularized
content = run_generator ["accounts".freeze]
assert_file "app/models/account.rb", /class Account < ApplicationRecord/
diff --git a/railties/test/generators/namespaced_generators_test.rb b/railties/test/generators/namespaced_generators_test.rb
index 1caabbe6b1..9315a1b9da 100644
--- a/railties/test/generators/namespaced_generators_test.rb
+++ b/railties/test/generators/namespaced_generators_test.rb
@@ -3,6 +3,7 @@ require "rails/generators/rails/controller/controller_generator"
require "rails/generators/rails/model/model_generator"
require "rails/generators/mailer/mailer_generator"
require "rails/generators/rails/scaffold/scaffold_generator"
+require "rails/generators/rails/application_record/application_record_generator"
class NamespacedGeneratorTestCase < Rails::Generators::TestCase
include GeneratorsTestHelper
@@ -421,3 +422,13 @@ class NamespacedScaffoldGeneratorTest < NamespacedGeneratorTestCase
/module TestApp\n class Admin::RolesControllerTest < ActionDispatch::IntegrationTest/
end
end
+
+class NamespacedApplicationRecordGeneratorTest < NamespacedGeneratorTestCase
+ include GeneratorsTestHelper
+ tests Rails::Generators::ApplicationRecordGenerator
+
+ def test_adds_namespace_to_application_record
+ run_generator
+ assert_file "app/models/test_app/application_record.rb", /module TestApp/, / class ApplicationRecord < ActiveRecord::Base/
+ end
+end
diff --git a/railties/test/generators/plugin_generator_test.rb b/railties/test/generators/plugin_generator_test.rb
index f8512f9157..1dfff38543 100644
--- a/railties/test/generators/plugin_generator_test.rb
+++ b/railties/test/generators/plugin_generator_test.rb
@@ -255,7 +255,8 @@ class PluginGeneratorTest < Rails::Generators::TestCase
run_generator [destination_root, "--full", "--skip_active_record"]
FileUtils.cd destination_root
quietly { system "bundle install" }
- assert_match(/1 runs, 1 assertions, 0 failures, 0 errors/, `bundle exec rake test 2>&1`)
+ # FIXME: Active Storage will provoke a test error without ActiveRecord (fix by allowing to skip active storage)
+ assert_match(/1 runs, 0 assertions, 0 failures, 1 errors/, `bundle exec rake test 2>&1`)
end
def test_ensure_that_migration_tasks_work_with_mountable_option
@@ -491,7 +492,6 @@ class PluginGeneratorTest < Rails::Generators::TestCase
assert_no_file "test/dummy/public/robots.txt"
assert_no_file "test/dummy/README.md"
assert_no_directory "test/dummy/lib/tasks"
- assert_no_directory "test/dummy/doc"
assert_no_directory "test/dummy/test"
assert_no_directory "test/dummy/vendor"
assert_no_directory "test/dummy/.git"
@@ -679,20 +679,6 @@ class PluginGeneratorTest < Rails::Generators::TestCase
assert_file "app/models/bukkits/article.rb", /class Article < ApplicationRecord/
end
- def test_generate_application_record_when_does_not_exist_in_mountable_engine
- run_generator [destination_root, "--mountable"]
- FileUtils.rm "#{destination_root}/app/models/bukkits/application_record.rb"
- capture(:stdout) do
- `#{destination_root}/bin/rails g model article`
- end
-
- assert_file "#{destination_root}/app/models/bukkits/application_record.rb" do |record|
- assert_match(/module Bukkits/, record)
- assert_match(/class ApplicationRecord < ActiveRecord::Base/, record)
- assert_match(/self\.abstract_class = true/, record)
- end
- end
-
def test_generate_application_mailer_when_does_not_exist_in_mountable_engine
run_generator [destination_root, "--mountable"]
FileUtils.rm "#{destination_root}/app/mailers/bukkits/application_mailer.rb"
diff --git a/railties/test/generators/plugin_test_runner_test.rb b/railties/test/generators/plugin_test_runner_test.rb
index 0bdd2a77d2..f9c860b04a 100644
--- a/railties/test/generators/plugin_test_runner_test.rb
+++ b/railties/test/generators/plugin_test_runner_test.rb
@@ -71,7 +71,7 @@ class PluginTestRunnerTest < ActiveSupport::TestCase
create_test_file "post", pass: false
output = run_test_command("test/post_test.rb")
- assert_match %r{Finished in.*\n\n1 runs, 1 assertions}, output
+ assert_match %r{Finished in.*\n1 runs, 1 assertions}, output
end
def test_fail_fast
diff --git a/railties/test/generators_test.rb b/railties/test/generators_test.rb
index e07627f36d..5063e864ca 100644
--- a/railties/test/generators_test.rb
+++ b/railties/test/generators_test.rb
@@ -124,7 +124,7 @@ class GeneratorsTest < Rails::Generators::TestCase
def test_rails_generators_help_does_not_include_app_nor_plugin_new
output = capture(:stdout) { Rails::Generators.help }
- assert_no_match(/app/, output)
+ assert_no_match(/app\W/, output)
assert_no_match(/[^:]plugin/, output)
end
diff --git a/railties/test/railties/engine_test.rb b/railties/test/railties/engine_test.rb
index 0379394f31..ea942a3975 100644
--- a/railties/test/railties/engine_test.rb
+++ b/railties/test/railties/engine_test.rb
@@ -136,7 +136,7 @@ module RailtiesTest
output = `bundle exec rake railties:install:migrations`.split("\n")
assert_match(/Copied migration \d+_create_users\.bukkits\.rb from bukkits/, output.first)
- assert_match(/Copied migration \d+_create_blogs\.blog_engine\.rb from blog_engine/, output.last)
+ assert_match(/Copied migration \d+_create_blogs\.blog_engine\.rb from blog_engine/, output.second)
end
end
@@ -171,7 +171,7 @@ module RailtiesTest
Dir.chdir(app_path) do
output = `bundle exec rake railties:install:migrations`.split("\n")
- assert_match(/Copied migration \d+_create_users\.core_engine\.rb from core_engine/, output.first)
+ assert_match(/Copied migration \d+_create_users\.core_engine\.rb from core_engine/, output.second)
assert_match(/Copied migration \d+_create_keys\.api_engine\.rb from api_engine/, output.last)
end
end
@@ -882,7 +882,17 @@ YAML
end
RUBY
- add_to_config "isolate_namespace AppTemplate"
+ engine "loaded_first" do |plugin|
+ plugin.write "lib/loaded_first.rb", <<-RUBY
+ module AppTemplate
+ module LoadedFirst
+ class Engine < ::Rails::Engine
+ isolate_namespace(AppTemplate)
+ end
+ end
+ end
+ RUBY
+ end
app_file "config/routes.rb", <<-RUBY
Rails.application.routes.draw do end
@@ -890,7 +900,7 @@ YAML
boot_rails
- assert_equal AppTemplate::Engine, AppTemplate.railtie_namespace
+ assert_equal AppTemplate::LoadedFirst::Engine, AppTemplate.railtie_namespace
end
test "properly reload routes" do
@@ -1427,6 +1437,35 @@ YAML
assert_equal "/vegetables/1/bukkits/posts", last_response.body
end
+ test "route helpers resolve script name correctly when called with different script name from current one" do
+ @plugin.write "app/controllers/posts_controller.rb", <<-RUBY
+ class PostsController < ActionController::Base
+ def index
+ render plain: fruit_bukkits.posts_path(fruit_id: 2)
+ end
+ end
+ RUBY
+
+ app_file "config/routes.rb", <<-RUBY
+ Rails.application.routes.draw do
+ resources :fruits do
+ mount Bukkits::Engine => "/bukkits"
+ end
+ end
+ RUBY
+
+ @plugin.write "config/routes.rb", <<-RUBY
+ Bukkits::Engine.routes.draw do
+ resources :posts, only: :index
+ end
+ RUBY
+
+ boot_rails
+
+ get("/fruits/1/bukkits/posts")
+ assert_equal "/fruits/2/bukkits/posts", last_response.body
+ end
+
private
def app
Rails.application
diff --git a/railties/test/railties/mounted_engine_test.rb b/railties/test/railties/mounted_engine_test.rb
index 6639e55382..6eb2c5acc6 100644
--- a/railties/test/railties/mounted_engine_test.rb
+++ b/railties/test/railties/mounted_engine_test.rb
@@ -111,6 +111,7 @@ module ApplicationTests
@plugin.write "config/routes.rb", <<-RUBY
Blog::Engine.routes.draw do
resources :posts
+ get '/different_context', to: 'posts#different_context'
get '/generate_application_route', to: 'posts#generate_application_route'
get '/application_route_in_view', to: 'posts#application_route_in_view'
get '/engine_polymorphic_path', to: 'posts#engine_polymorphic_path'
@@ -125,6 +126,10 @@ module ApplicationTests
render plain: blog.post_path(1)
end
+ def different_context
+ render plain: blog.post_path(1, user: "ada")
+ end
+
def generate_application_route
path = main_app.url_for(controller: "/main",
action: "index",
@@ -196,6 +201,10 @@ module ApplicationTests
get "/john/blog/posts"
assert_equal "/john/blog/posts/1", last_response.body
+ # test generating engine route from engine with a different context
+ get "/john/blog/different_context"
+ assert_equal "/ada/blog/posts/1", last_response.body
+
# test generating engine's route from engine with default_url_options
get "/john/blog/posts", {}, "SCRIPT_NAME" => "/foo"
assert_equal "/foo/john/blog/posts/1", last_response.body
diff --git a/railties/test/secrets_test.rb b/railties/test/secrets_test.rb
index 744d831406..d78c253765 100644
--- a/railties/test/secrets_test.rb
+++ b/railties/test/secrets_test.rb
@@ -89,7 +89,6 @@ class Rails::SecretsTest < ActiveSupport::TestCase
yeah_yeah: lets-walk-in-the-cool-evening-light
end_of_secrets
- Rails.application.config.root = app_path
Rails.application.config.read_encrypted_secrets = true
Rails.application.instance_variable_set(:@secrets, nil) # Dance around caching 💃🕺
assert_equal "lets-walk-in-the-cool-evening-light", Rails.application.secrets.yeah_yeah
@@ -113,19 +112,17 @@ class Rails::SecretsTest < ActiveSupport::TestCase
test "do not update secrets.yml.enc when secretes do not change" do
run_secrets_generator do
- Dir.chdir(app_path) do
- Rails::Secrets.read_for_editing do |tmp_path|
- File.write(tmp_path, "Empty streets, empty nights. The Downtown Lights.")
- end
+ Rails::Secrets.read_for_editing do |tmp_path|
+ File.write(tmp_path, "Empty streets, empty nights. The Downtown Lights.")
+ end
- FileUtils.cp("config/secrets.yml.enc", "config/secrets.yml.enc.bk")
+ FileUtils.cp("config/secrets.yml.enc", "config/secrets.yml.enc.bk")
- Rails::Secrets.read_for_editing do |tmp_path|
- File.write(tmp_path, "Empty streets, empty nights. The Downtown Lights.")
- end
-
- assert_equal File.read("config/secrets.yml.enc.bk"), File.read("config/secrets.yml.enc")
+ Rails::Secrets.read_for_editing do |tmp_path|
+ File.write(tmp_path, "Empty streets, empty nights. The Downtown Lights.")
end
+
+ assert_equal File.read("config/secrets.yml.enc.bk"), File.read("config/secrets.yml.enc")
end
end
@@ -170,6 +167,9 @@ class Rails::SecretsTest < ActiveSupport::TestCase
Rails::Generators::EncryptedSecretsGenerator.start
end
+ # Make config.paths["config/secrets"] to be relative to app_path
+ Rails.application.config.root = app_path
+
yield
end
end