diff options
Diffstat (limited to 'railties/test')
-rw-r--r-- | railties/test/application/configuration_test.rb | 47 | ||||
-rw-r--r-- | railties/test/application/dbconsole_test.rb | 35 | ||||
-rw-r--r-- | railties/test/application/runner_test.rb | 8 | ||||
-rw-r--r-- | railties/test/commands/console_test.rb | 29 | ||||
-rw-r--r-- | railties/test/commands/dbconsole_test.rb | 59 | ||||
-rw-r--r-- | railties/test/generators/app_generator_test.rb | 16 |
6 files changed, 168 insertions, 26 deletions
diff --git a/railties/test/application/configuration_test.rb b/railties/test/application/configuration_test.rb index b8a19379e0..9f62ca8eb8 100644 --- a/railties/test/application/configuration_test.rb +++ b/railties/test/application/configuration_test.rb @@ -1130,7 +1130,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 +1141,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 +1153,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 +1177,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 +1188,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 +1196,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 +1204,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 +1223,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 +1234,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 +1245,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 @@ -1596,7 +1596,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 +1608,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 +1737,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/dbconsole_test.rb b/railties/test/application/dbconsole_test.rb index 7e5e9ea8aa..12d1cfb089 100644 --- a/railties/test/application/dbconsole_test.rb +++ b/railties/test/application/dbconsole_test.rb @@ -9,6 +9,8 @@ module ApplicationTests include ActiveSupport::Testing::Isolation def setup + skip "PTY unavailable" unless available_pty? + build_app end @@ -17,7 +19,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,9 +42,37 @@ module ApplicationTests master.puts ".exit" end + 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 + + development: + <<: *default + database: db/development.sqlite3 + + production: + <<: *default + database: db/production.sqlite3 + YAML + end + + master, slave = PTY.open + spawn_dbconsole(slave, "-e production") + assert_output("sqlite>", master) + + master.puts ".databases" + assert_output("production.sqlite3", master) + ensure + master.puts ".exit" + end + private - def spawn_dbconsole(fd) - Process.spawn("#{app_path}/bin/rails dbconsole", in: fd, out: fd, err: fd) + def spawn_dbconsole(fd, options = nil) + Process.spawn("#{app_path}/bin/rails dbconsole #{options}", in: fd, out: fd, err: fd) end def assert_output(expected, io, timeout = 5) 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/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/generators/app_generator_test.rb b/railties/test/generators/app_generator_test.rb index 059c2692be..ffdee3a6b5 100644 --- a/railties/test/generators/app_generator_test.rb +++ b/railties/test/generators/app_generator_test.rb @@ -278,6 +278,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!/ |