diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2017-08-01 17:34:14 -0400 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2017-08-01 17:34:14 -0400 |
commit | feb1ddae021c38174f9c22bbd1298c8b73431b45 (patch) | |
tree | 09a38695596cf80dc362dd4d93cf94d6f7415e7b /railties | |
parent | 3540e60c385286f3517ff56270b31a91ed3024a3 (diff) | |
parent | f9a43f28c087f8ffd35ff7c33a60c938b60f2be2 (diff) | |
download | rails-feb1ddae021c38174f9c22bbd1298c8b73431b45.tar.gz rails-feb1ddae021c38174f9c22bbd1298c8b73431b45.tar.bz2 rails-feb1ddae021c38174f9c22bbd1298c8b73431b45.zip |
Merge remote-tracking branch 'origin/master' into unlock-minitest
Diffstat (limited to 'railties')
59 files changed, 827 insertions, 490 deletions
diff --git a/railties/CHANGELOG.md b/railties/CHANGELOG.md index d793405da1..ec41d17a9f 100644 --- a/railties/CHANGELOG.md +++ b/railties/CHANGELOG.md @@ -1,3 +1,59 @@ +* Add `ruby x.x.x` version to `Gemfile` and create `.ruby-version` + root file containing current Ruby version when new Rails applications are + created. + + *Alberto Almagro* + +* Support `-` as a platform-agnostic way to run a script from stdin with + `rails runner` + + *Cody Cutrer* + +* Add `bootsnap` to default `Gemfile`. + + *Burke Libbey* + +* Properly expand shortcuts for environment's name running the `console` + and `dbconsole` commands. + + *Robin Dupret* + +* Passing the environment's name as a regular argument to the + `rails dbconsole` and `rails console` commands is deprecated. + The `-e` option should be used instead. + + Previously: + + $ bin/rails dbconsole production + + Now: + + $ bin/rails dbconsole -e production + + *Robin Dupret*, *Kasper Timm Hansen* + +* Allow to pass a custom connection name to the `rails dbconsole` + command when using a 3-level database configuration. + + $ bin/rails dbconsole -c replica + + *Robin Dupret*, *Jeremy Daer* + +* Skip unused components when running `bin/rails app:update`. + + If the initial app generation skipped Action Cable, Active Record etc., + the update task honors those skips too. + + *Yuji Yaginuma* + +* Make Rails' test runner work better with minitest plugins. + + By demoting the Rails test runner to just another minitest plugin — + and thereby not eager loading it — we can co-exist much better with + other minitest plugins such as pride and minitest-focus. + + *Kasper Timm Hansen* + * Load environment file in `dbconsole` command. Fixes #29717 diff --git a/railties/lib/minitest/rails_plugin.rb b/railties/lib/minitest/rails_plugin.rb new file mode 100644 index 0000000000..fec9ae64d8 --- /dev/null +++ b/railties/lib/minitest/rails_plugin.rb @@ -0,0 +1,49 @@ +require "active_support/core_ext/module/attribute_accessors" +require "rails/test_unit/reporter" + +module Minitest + class SuppressedSummaryReporter < SummaryReporter + # Disable extra failure output after a run if output is inline. + def aggregated_results(*) + super unless options[:output_inline] + end + end + + def self.plugin_rails_options(opts, options) + opts.on("-b", "--backtrace", "Show the complete backtrace") do + options[:full_backtrace] = true + end + + opts.on("-d", "--defer-output", "Output test failures and errors after the test run") do + options[:output_inline] = false + end + + opts.on("-f", "--fail-fast", "Abort test run on first failure or error") do + options[:fail_fast] = true + end + + opts.on("-c", "--[no-]color", "Enable color in the output") do |value| + options[:color] = value + end + + options[:color] = true + options[:output_inline] = true + end + + # Owes great inspiration to test runner trailblazers like RSpec, + # minitest-reporters, maxitest and others. + def self.plugin_rails_init(options) + unless options[:full_backtrace] || ENV["BACKTRACE"] + # Plugin can run without Rails loaded, check before filtering. + Minitest.backtrace_filter = ::Rails.backtrace_cleaner if ::Rails.respond_to?(:backtrace_cleaner) + end + + # Replace progress reporter for colors. + reporter.reporters.delete_if { |reporter| reporter.kind_of?(SummaryReporter) || reporter.kind_of?(ProgressReporter) } + reporter << SuppressedSummaryReporter.new(options[:io], options) + reporter << ::Rails::TestUnitReporter.new(options[:io], options) + end + + # Backwardscompatibility with Rails 5.0 generated plugin test scripts + mattr_reader :run_via, default: {} +end diff --git a/railties/lib/rails/app_updater.rb b/railties/lib/rails/app_updater.rb new file mode 100644 index 0000000000..e53d1cf093 --- /dev/null +++ b/railties/lib/rails/app_updater.rb @@ -0,0 +1,31 @@ +require "rails/generators" +require "rails/generators/rails/app/app_generator" + +module Rails + class AppUpdater # :nodoc: + class << self + def invoke_from_app_generator(method) + app_generator.send(method) + end + + def app_generator + @app_generator ||= begin + gen = Rails::Generators::AppGenerator.new ["rails"], generator_options, destination_root: Rails.root + File.exist?(Rails.root.join("config", "application.rb")) ? gen.send(:app_const) : gen.send(:valid_const?) + gen + end + end + + private + def generator_options + options = { api: !!Rails.application.config.api_only, update: true } + options[:skip_active_record] = !defined?(ActiveRecord::Railtie) + options[:skip_action_mailer] = !defined?(ActionMailer::Railtie) + options[:skip_action_cable] = !defined?(ActionCable::Engine) + options[:skip_sprockets] = !defined?(Sprockets::Railtie) + options[:skip_puma] = !defined?(Puma) + options + end + end + end +end diff --git a/railties/lib/rails/application/configuration.rb b/railties/lib/rails/application/configuration.rb index 7e1359c42b..4797223380 100644 --- a/railties/lib/rails/application/configuration.rb +++ b/railties/lib/rails/application/configuration.rb @@ -86,6 +86,10 @@ module Rails if respond_to?(:active_record) active_record.cache_versioning = true + # Remove the temporary load hook from SQLite3Adapter when this is removed + ActiveSupport.on_load(:active_record_sqlite3adapter) do + ActiveRecord::ConnectionAdapters::SQLite3Adapter.represent_boolean_as_integer = true + end end if respond_to?(:action_dispatch) @@ -96,6 +100,10 @@ module Rails active_support.use_authenticated_message_encryption = true end + if respond_to?(:action_controller) + action_controller.default_protect_from_forgery = true + end + else raise "Unknown version #{target_version.to_s.inspect}" end diff --git a/railties/lib/rails/command/environment_argument.rb b/railties/lib/rails/command/environment_argument.rb index 05eac34155..9582509840 100644 --- a/railties/lib/rails/command/environment_argument.rb +++ b/railties/lib/rails/command/environment_argument.rb @@ -7,13 +7,24 @@ module Rails included do argument :environment, optional: true, banner: "environment" + + class_option :environment, aliases: "-e", type: :string, + desc: "Specifies the environment to run this console under (test/development/production)." end private def extract_environment_option_from_argument if environment self.options = options.merge(environment: acceptable_environment(environment)) - elsif !options[:environment] + + ActiveSupport::Deprecation.warn "Passing the environment's name as a " \ + "regular argument is deprecated and " \ + "will be removed in the next Rails " \ + "version. Please, use the -e option " \ + "instead." + elsif options[:environment] + self.options = options.merge(environment: acceptable_environment(options[:environment])) + else self.options = options.merge(environment: Rails::Command.environment) end end diff --git a/railties/lib/rails/commands/console/console_command.rb b/railties/lib/rails/commands/console/console_command.rb index 1da1e331f1..6f9a1f022b 100644 --- a/railties/lib/rails/commands/console/console_command.rb +++ b/railties/lib/rails/commands/console/console_command.rb @@ -70,9 +70,6 @@ module Rails class_option :sandbox, aliases: "-s", type: :boolean, default: false, desc: "Rollback database modifications on exit." - class_option :environment, aliases: "-e", type: :string, - desc: "Specifies the environment to run this console under (test/development/production)." - def initialize(args = [], local_options = {}, config = {}) console_options = [] diff --git a/railties/lib/rails/commands/dbconsole/dbconsole_command.rb b/railties/lib/rails/commands/dbconsole/dbconsole_command.rb index b3df5191c6..71b3455473 100644 --- a/railties/lib/rails/commands/dbconsole/dbconsole_command.rb +++ b/railties/lib/rails/commands/dbconsole/dbconsole_command.rb @@ -11,7 +11,7 @@ module Rails end def start - ENV["RAILS_ENV"] = @options[:environment] || environment + ENV["RAILS_ENV"] ||= @options[:environment] || environment case config["adapter"] when /^(jdbc)?mysql/ @@ -87,10 +87,15 @@ module Rails def config @config ||= begin - if configurations[environment].blank? + # We need to check whether the user passed the connection the + # first time around to show a consistent error message to people + # relying on 2-level database configuration. + if @options["connection"] && configurations[connection].blank? + raise ActiveRecord::AdapterNotSpecified, "'#{connection}' connection is not configured. Available configuration: #{configurations.inspect}" + elsif configurations[environment].blank? && configurations[connection].blank? raise ActiveRecord::AdapterNotSpecified, "'#{environment}' database is not configured. Available configuration: #{configurations.inspect}" else - configurations[environment] + configurations[environment].presence || configurations[connection] end end end @@ -99,6 +104,10 @@ module Rails Rails.respond_to?(:env) ? Rails.env : Rails::Command.environment end + def connection + @options.fetch(:connection, "primary") + end + private def configurations # :doc: require APP_PATH @@ -142,12 +151,15 @@ module Rails class_option :header, type: :boolean - class_option :environment, aliases: "-e", type: :string, - desc: "Specifies the environment to run this console under (test/development/production)." + class_option :connection, aliases: "-c", type: :string, + desc: "Specifies the connection to use." def perform extract_environment_option_from_argument + # RAILS_ENV needs to be set before config/application is required. + ENV["RAILS_ENV"] = options[:environment] + require_application_and_environment! Rails::DBConsole.start(options) end diff --git a/railties/lib/rails/commands/runner/USAGE b/railties/lib/rails/commands/runner/USAGE index b2a6e8493d..24b60037f0 100644 --- a/railties/lib/rails/commands/runner/USAGE +++ b/railties/lib/rails/commands/runner/USAGE @@ -8,6 +8,9 @@ Run the Ruby file located at `path/to/filename.rb` after loading the app: <%= executable %> path/to/filename.rb +Run the Ruby script read from stdin after loading the app: + <%= executable %> - + <% unless Gem.win_platform? %> You can also use the runner command as a shebang line for your executables: diff --git a/railties/lib/rails/commands/runner/runner_command.rb b/railties/lib/rails/commands/runner/runner_command.rb index 6864a9726b..c931fc2152 100644 --- a/railties/lib/rails/commands/runner/runner_command.rb +++ b/railties/lib/rails/commands/runner/runner_command.rb @@ -13,7 +13,7 @@ module Rails end def self.banner(*) - "#{super} [<'Some.ruby(code)'> | <filename.rb>]" + "#{super} [<'Some.ruby(code)'> | <filename.rb> | -]" end def perform(code_or_file = nil, *command_argv) @@ -29,7 +29,9 @@ module Rails ARGV.replace(command_argv) - if File.exist?(code_or_file) + if code_or_file == "-" + eval($stdin.read, binding, "stdin") + elsif File.exist?(code_or_file) $0 = code_or_file Kernel.load code_or_file else diff --git a/railties/lib/rails/commands/server/server_command.rb b/railties/lib/rails/commands/server/server_command.rb index b607a63176..ce258341f6 100644 --- a/railties/lib/rails/commands/server/server_command.rb +++ b/railties/lib/rails/commands/server/server_command.rb @@ -64,9 +64,9 @@ module Rails end def print_boot_information - url = "#{options[:SSLEnable] ? 'https' : 'http'}://#{options[:Host]}:#{options[:Port]}" + url = "on #{options[:SSLEnable] ? 'https' : 'http'}://#{options[:Host]}:#{options[:Port]}" unless use_puma? puts "=> Booting #{ActiveSupport::Inflector.demodulize(server)}" - puts "=> Rails #{Rails.version} application starting in #{Rails.env} on #{url}" + puts "=> Rails #{Rails.version} application starting in #{Rails.env} #{url}" puts "=> Run `rails server -h` for more startup options" end @@ -91,6 +91,10 @@ module Rails def restart_command "bin/rails server #{ARGV.join(' ')}" end + + def use_puma? + server.to_s == "Rack::Handler::Puma" + end end module Command diff --git a/railties/lib/rails/commands/test/test_command.rb b/railties/lib/rails/commands/test/test_command.rb index ca0b6c00fe..5852f51a62 100644 --- a/railties/lib/rails/commands/test/test_command.rb +++ b/railties/lib/rails/commands/test/test_command.rb @@ -1,21 +1,41 @@ require_relative "../../command" -require_relative "../../test_unit/minitest_plugin" +require_relative "../../test_unit/runner" module Rails module Command class TestCommand < Base # :nodoc: no_commands do def help - perform # Hand over help printing to minitest. + require "optparse" + require "minitest/rails_plugin" + + opts = OptionParser.new + opts.banner = "Usage: #{Rails::TestUnitReporter.executable} [options] [files or directories]" + opts.separator "" + opts.separator "You can run a single test by appending a line number to a filename:" + opts.separator "" + opts.separator " #{Rails::TestUnitReporter.executable} test/models/user_test.rb:27" + opts.separator "" + opts.separator "You can run multiple files and directories at the same time:" + opts.separator "" + opts.separator " #{Rails::TestUnitReporter.executable} test/controllers test/integration/login_test.rb" + opts.separator "" + opts.separator "By default test failures and errors are reported inline during a run." + opts.separator "" + + opts.separator "Rails options:" + Rails::TestUnit::Runner.options(opts) + Minitest.plugin_rails_options(opts, {}) + + say opts end end def perform(*) $LOAD_PATH << Rails::Command.root.join("test").to_s - Minitest.run_via = :rails - - require "active_support/testing/autorun" + Rails::TestUnit::Runner.parse_options(ARGV) + Rails::TestUnit::Runner.run(ARGV) end end end diff --git a/railties/lib/rails/generators/actions.rb b/railties/lib/rails/generators/actions.rb index 5cf0985050..2792d7636f 100644 --- a/railties/lib/rails/generators/actions.rb +++ b/railties/lib/rails/generators/actions.rb @@ -216,9 +216,9 @@ module Rails # Runs the supplied rake task (invoked with 'rails ...') # - # rails("db:migrate") - # rails("db:migrate", env: "production") - # rails("gems:install", sudo: true) + # rails_command("db:migrate") + # rails_command("db:migrate", env: "production") + # rails_command("gems:install", sudo: true) def rails_command(command, options = {}) execute_command :rails, command, options end diff --git a/railties/lib/rails/generators/app_base.rb b/railties/lib/rails/generators/app_base.rb index 760cb2f81e..4899a63917 100644 --- a/railties/lib/rails/generators/app_base.rb +++ b/railties/lib/rails/generators/app_base.rb @@ -348,6 +348,8 @@ module Rails comment = "See https://github.com/rails/execjs#readme for more supported runtimes" if defined?(JRUBY_VERSION) GemfileEntry.version "therubyrhino", nil, comment + elsif RUBY_PLATFORM =~ /mingw|mswin/ + GemfileEntry.version "duktape", nil, comment else GemfileEntry.new "mini_racer", nil, comment, { platforms: :ruby }, true end diff --git a/railties/lib/rails/generators/base.rb b/railties/lib/rails/generators/base.rb index e7f51dba99..9705af8b40 100644 --- a/railties/lib/rails/generators/base.rb +++ b/railties/lib/rails/generators/base.rb @@ -16,6 +16,9 @@ module Rails include Thor::Actions include Rails::Generators::Actions + class_option :skip_namespace, type: :boolean, default: false, + desc: "Skip namespace (affects only isolated applications)" + add_runtime_options! strict_args_position! @@ -271,6 +274,40 @@ module Rails end end + # Wrap block with namespace of current application + # if namespace exists and is not skipped + def module_namespacing(&block) # :doc: + content = capture(&block) + content = wrap_with_namespace(content) if namespaced? + concat(content) + end + + def indent(content, multiplier = 2) # :doc: + spaces = " " * multiplier + content.each_line.map { |line| line.blank? ? line : "#{spaces}#{line}" }.join + end + + def wrap_with_namespace(content) # :doc: + content = indent(content).chomp + "module #{namespace.name}\n#{content}\nend\n" + end + + def namespace # :doc: + Rails::Generators.namespace + end + + def namespaced? # :doc: + !options[:skip_namespace] && namespace + end + + def namespace_dirs + @namespace_dirs ||= namespace.name.split("::").map(&:underscore) + end + + def namespaced_path # :doc: + @namespaced_path ||= namespace_dirs.join("/") + end + # Use Rails default banner. def self.banner # :doc: "rails generate #{namespace.sub(/^rails:/, '')} #{arguments.map(&:usage).join(' ')} [options]".gsub(/\s+/, " ") diff --git a/railties/lib/rails/generators/named_base.rb b/railties/lib/rails/generators/named_base.rb index d63a5b0c30..dc1d6cab9d 100644 --- a/railties/lib/rails/generators/named_base.rb +++ b/railties/lib/rails/generators/named_base.rb @@ -6,8 +6,6 @@ module Rails module Generators class NamedBase < Base argument :name, type: :string - class_option :skip_namespace, type: :boolean, default: false, - desc: "Skip namespace (affects only isolated applications)" def initialize(args, *options) #:nodoc: @inside_template = nil @@ -45,24 +43,6 @@ module Rails file_name end - # Wrap block with namespace of current application - # if namespace exists and is not skipped - def module_namespacing(&block) # :doc: - content = capture(&block) - content = wrap_with_namespace(content) if namespaced? - concat(content) - end - - def indent(content, multiplier = 2) # :doc: - spaces = " " * multiplier - content.each_line.map { |line| line.blank? ? line : "#{spaces}#{line}" }.join - end - - def wrap_with_namespace(content) # :doc: - content = indent(content).chomp - "module #{namespace.name}\n#{content}\nend\n" - end - def inside_template # :doc: @inside_template = true yield @@ -74,18 +54,6 @@ module Rails @inside_template end - def namespace # :doc: - Rails::Generators.namespace - end - - def namespaced? # :doc: - !options[:skip_namespace] && namespace - end - - def namespace_dirs - @namespace_dirs ||= namespace.name.split("::").map(&:underscore) - end - def file_path # :doc: @file_path ||= (class_path + [file_name]).join("/") end @@ -102,10 +70,6 @@ module Rails @namespaced_class_path ||= namespace_dirs + @class_path end - def namespaced_path # :doc: - @namespaced_path ||= namespace_dirs.join("/") - end - def class_name # :doc: (class_path + [file_name]).map!(&:camelize).join("::") end diff --git a/railties/lib/rails/generators/rails/app/app_generator.rb b/railties/lib/rails/generators/rails/app/app_generator.rb index 7614e6304b..507099ef57 100644 --- a/railties/lib/rails/generators/rails/app/app_generator.rb +++ b/railties/lib/rails/generators/rails/app/app_generator.rb @@ -49,6 +49,10 @@ module Rails copy_file "README.md", "README.md" end + def ruby_version + template "ruby-version", ".ruby-version" + end + def gemfile template "Gemfile" end @@ -128,7 +132,7 @@ module Rails gsub_file "config/initializers/cookies_serializer.rb", /json(?!,)/, "marshal" end - unless action_cable_config_exist + if !options[:skip_action_cable] && !action_cable_config_exist template "config/cable.yml" end @@ -242,6 +246,7 @@ module Rails def create_root_files build(:readme) build(:rakefile) + build(:ruby_version) build(:configru) build(:gitignore) unless options[:skip_git] build(:gemfile) unless options[:skip_gemfile] diff --git a/railties/lib/rails/generators/rails/app/templates/Gemfile b/railties/lib/rails/generators/rails/app/templates/Gemfile index 64e2062aea..4b2842ef46 100644 --- a/railties/lib/rails/generators/rails/app/templates/Gemfile +++ b/railties/lib/rails/generators/rails/app/templates/Gemfile @@ -1,5 +1,6 @@ source 'https://rubygems.org' git_source(:github) { |repo| "https://github.com/#{repo}.git" } +ruby <%= "'#{RUBY_VERSION}'" %> <% gemfile_entries.each do |gem| -%> <% if gem.comment -%> @@ -19,6 +20,9 @@ git_source(:github) { |repo| "https://github.com/#{repo}.git" } # Use Capistrano for deployment # gem 'capistrano-rails', group: :development +# Reduces boot times through caching; required in config/boot.rb +gem 'bootsnap', '>= 1.1.0', require: false + <%- if options.api? -%> # Use Rack CORS for handling Cross-Origin Resource Sharing (CORS), making cross-origin AJAX possible # gem 'rack-cors' diff --git a/railties/lib/rails/generators/rails/app/templates/app/controllers/application_controller.rb.tt b/railties/lib/rails/generators/rails/app/templates/app/controllers/application_controller.rb.tt index 413354186d..185c0017f1 100644 --- a/railties/lib/rails/generators/rails/app/templates/app/controllers/application_controller.rb.tt +++ b/railties/lib/rails/generators/rails/app/templates/app/controllers/application_controller.rb.tt @@ -1,5 +1,2 @@ class ApplicationController < ActionController::<%= options[:api] ? "API" : "Base" %> -<%- unless options[:api] -%> - protect_from_forgery with: :exception -<%- end -%> end diff --git a/railties/lib/rails/generators/rails/app/templates/bin/setup.tt b/railties/lib/rails/generators/rails/app/templates/bin/setup.tt index ee9d077c30..a53d1fb0a9 100644 --- a/railties/lib/rails/generators/rails/app/templates/bin/setup.tt +++ b/railties/lib/rails/generators/rails/app/templates/bin/setup.tt @@ -15,10 +15,11 @@ chdir APP_ROOT do puts '== Installing dependencies ==' system! 'gem install bundler --conservative' system('bundle check') || system!('bundle install') -<% unless options[:skip_yarn] %> +<% unless options[:skip_yarn] -%> + # Install JavaScript dependencies if using Yarn # system('bin/yarn') -<% end %> +<% end -%> <% unless options.skip_active_record -%> # puts "\n== Copying sample files ==" diff --git a/railties/lib/rails/generators/rails/app/templates/bin/yarn b/railties/lib/rails/generators/rails/app/templates/bin/yarn index 44f75c22a4..c2f9b6768a 100644 --- a/railties/lib/rails/generators/rails/app/templates/bin/yarn +++ b/railties/lib/rails/generators/rails/app/templates/bin/yarn @@ -1,7 +1,7 @@ VENDOR_PATH = File.expand_path('..', __dir__) Dir.chdir(VENDOR_PATH) do begin - exec "yarnpkg #{ARGV.join(" ")}" + exec "yarnpkg #{ARGV.join(' ')}" rescue Errno::ENOENT $stderr.puts "Yarn executable was not detected in the system." $stderr.puts "Download Yarn at https://yarnpkg.com/en/docs/install" diff --git a/railties/lib/rails/generators/rails/app/templates/config/boot.rb b/railties/lib/rails/generators/rails/app/templates/config/boot.rb index 30f5120df6..b9e460cef3 100644 --- a/railties/lib/rails/generators/rails/app/templates/config/boot.rb +++ b/railties/lib/rails/generators/rails/app/templates/config/boot.rb @@ -1,3 +1,4 @@ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__) require 'bundler/setup' # Set up gems listed in the Gemfile. +require 'bootsnap/setup' # Speed up boot time by caching expensive operations. diff --git a/railties/lib/rails/generators/rails/app/templates/config/initializers/new_framework_defaults_5_2.rb.tt b/railties/lib/rails/generators/rails/app/templates/config/initializers/new_framework_defaults_5_2.rb.tt index 3809936f9f..25dcddb27a 100644 --- a/railties/lib/rails/generators/rails/app/templates/config/initializers/new_framework_defaults_5_2.rb.tt +++ b/railties/lib/rails/generators/rails/app/templates/config/initializers/new_framework_defaults_5_2.rb.tt @@ -17,3 +17,11 @@ # Use AES-256-GCM authenticated encryption as default cipher for encrypting messages # instead of AES-256-CBC, when use_authenticated_message_encryption is set to true. # Rails.application.config.active_support.use_authenticated_message_encryption = true + +# Add default protection from forgery to ActionController::Base instead of in +# ApplicationController. +# Rails.application.config.action_controller.default_protect_from_forgery = true + +# Store boolean values are in sqlite3 databases as 1 and 0 instead of 't' and +# 'f' after migrating old data. +# Rails.application.config.active_record.sqlite3.represent_boolean_as_integer = true diff --git a/railties/lib/rails/generators/rails/app/templates/config/spring.rb b/railties/lib/rails/generators/rails/app/templates/config/spring.rb index c9119b40c0..9fa7863f99 100644 --- a/railties/lib/rails/generators/rails/app/templates/config/spring.rb +++ b/railties/lib/rails/generators/rails/app/templates/config/spring.rb @@ -1,6 +1,6 @@ -%w( +%w[ .ruby-version .rbenv-vars tmp/restart.txt tmp/caching-dev.txt -).each { |path| Spring.watch(path) } +].each { |path| Spring.watch(path) } diff --git a/railties/lib/rails/generators/rails/app/templates/ruby-version b/railties/lib/rails/generators/rails/app/templates/ruby-version new file mode 100644 index 0000000000..c444f33b0f --- /dev/null +++ b/railties/lib/rails/generators/rails/app/templates/ruby-version @@ -0,0 +1 @@ +<%= RUBY_VERSION -%> diff --git a/railties/lib/rails/generators/rails/application_record/application_record_generator.rb b/railties/lib/rails/generators/rails/application_record/application_record_generator.rb new file mode 100644 index 0000000000..f6b6e76b1d --- /dev/null +++ b/railties/lib/rails/generators/rails/application_record/application_record_generator.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +module Rails + module Generators + class ApplicationRecordGenerator < Base # :nodoc: + hook_for :orm, required: true, desc: "ORM to be invoked" + end + end +end diff --git a/railties/lib/rails/generators/rails/plugin/plugin_generator.rb b/railties/lib/rails/generators/rails/plugin/plugin_generator.rb index a1209e4624..61c54b4222 100644 --- a/railties/lib/rails/generators/rails/plugin/plugin_generator.rb +++ b/railties/lib/rails/generators/rails/plugin/plugin_generator.rb @@ -120,7 +120,6 @@ task default: :test def test_dummy_clean inside dummy_path do remove_file "db/seeds.rb" - remove_file "doc" remove_file "Gemfile" remove_file "lib/tasks" remove_file "public/robots.txt" diff --git a/railties/lib/rails/plugin/test.rb b/railties/lib/rails/plugin/test.rb index bd43cba688..40070c0eb3 100644 --- a/railties/lib/rails/plugin/test.rb +++ b/railties/lib/rails/plugin/test.rb @@ -1,7 +1,7 @@ -require_relative "../test_unit/minitest_plugin" +require_relative "../test_unit/runner" +require_relative "../test_unit/reporter" Rails::TestUnitReporter.executable = "bin/test" -Minitest.run_via = :rails - -require "active_support/testing/autorun" +Rails::TestUnit::Runner.parse_options(ARGV) +Rails::TestUnit::Runner.run(ARGV) diff --git a/railties/lib/rails/secrets.rb b/railties/lib/rails/secrets.rb index 46e21185d7..0883857c55 100644 --- a/railties/lib/rails/secrets.rb +++ b/railties/lib/rails/secrets.rb @@ -100,7 +100,8 @@ module Rails end def writing(contents) - tmp_path = File.join(Dir.tmpdir, File.basename(path)) + tmp_file = "#{File.basename(path)}.#{Process.pid}" + tmp_path = File.join(Dir.tmpdir, tmp_file) IO.binwrite(tmp_path, contents) yield tmp_path diff --git a/railties/lib/rails/tasks/framework.rake b/railties/lib/rails/tasks/framework.rake index f7e8f4f340..6c6e86d78d 100644 --- a/railties/lib/rails/tasks/framework.rake +++ b/railties/lib/rails/tasks/framework.rake @@ -36,38 +36,21 @@ namespace :app do end namespace :update do - class RailsUpdate - def self.invoke_from_app_generator(method) - app_generator.send(method) - end - - def self.app_generator - @app_generator ||= begin - require_relative "../generators" - require_relative "../generators/rails/app/app_generator" - gen = Rails::Generators::AppGenerator.new ["rails"], - { api: !!Rails.application.config.api_only, update: true }, - destination_root: Rails.root - File.exist?(Rails.root.join("config", "application.rb")) ? - gen.send(:app_const) : gen.send(:valid_const?) - gen - end - end - end + require_relative "../app_updater" # desc "Update config/boot.rb from your current rails install" task :configs do - RailsUpdate.invoke_from_app_generator :create_boot_file - RailsUpdate.invoke_from_app_generator :update_config_files + Rails::AppUpdater.invoke_from_app_generator :create_boot_file + Rails::AppUpdater.invoke_from_app_generator :update_config_files end # desc "Adds new executables to the application bin/ directory" task :bin do - RailsUpdate.invoke_from_app_generator :update_bin_files + Rails::AppUpdater.invoke_from_app_generator :update_bin_files end task :upgrade_guide_info do - RailsUpdate.invoke_from_app_generator :display_upgrade_guide_info + Rails::AppUpdater.invoke_from_app_generator :display_upgrade_guide_info end end end diff --git a/railties/lib/rails/tasks/yarn.rake b/railties/lib/rails/tasks/yarn.rake index 87476b1b8c..6cb19b7282 100644 --- a/railties/lib/rails/tasks/yarn.rake +++ b/railties/lib/rails/tasks/yarn.rake @@ -1,7 +1,7 @@ namespace :yarn do desc "Install all JavaScript dependencies as specified via Yarn" task :install do - system("./bin/yarn install --no-progress") + system("./bin/yarn install --no-progress --production") end end diff --git a/railties/lib/rails/test_help.rb b/railties/lib/rails/test_help.rb index 05048eb2e1..33b6740cf1 100644 --- a/railties/lib/rails/test_help.rb +++ b/railties/lib/rails/test_help.rb @@ -2,7 +2,6 @@ # so fixtures aren't loaded into that environment abort("Abort testing: Your Rails environment is running in production mode!") if Rails.env.production? -require_relative "test_unit/minitest_plugin" require "active_support/test_case" require "action_controller" require "action_controller/test_case" diff --git a/railties/lib/rails/test_unit/line_filtering.rb b/railties/lib/rails/test_unit/line_filtering.rb index 32ba744701..289afae331 100644 --- a/railties/lib/rails/test_unit/line_filtering.rb +++ b/railties/lib/rails/test_unit/line_filtering.rb @@ -1,78 +1,12 @@ require "method_source" +require "rails/test_unit/runner" module Rails module LineFiltering # :nodoc: def run(reporter, options = {}) - if options[:patterns] && options[:patterns].any? { |p| p =~ /:\d+/ } - options[:filter] = \ - CompositeFilter.new(self, options[:filter], options[:patterns]) - end + options[:filter] = Rails::TestUnit::Runner.compose_filter(self, options[:filter]) super end end - - class CompositeFilter # :nodoc: - attr_reader :named_filter - - def initialize(runnable, filter, patterns) - @runnable = runnable - @named_filter = derive_named_filter(filter) - @filters = [ @named_filter, *derive_line_filters(patterns) ].compact - end - - # Minitest uses === to find matching filters. - def ===(method) - @filters.any? { |filter| filter === method } - end - - private - def derive_named_filter(filter) - if filter.respond_to?(:named_filter) - filter.named_filter - elsif filter =~ %r%/(.*)/% # Regexp filtering copied from Minitest. - Regexp.new $1 - elsif filter.is_a?(String) - filter - end - end - - def derive_line_filters(patterns) - patterns.flat_map do |file_and_line| - file, *lines = file_and_line.split(":") - - if lines.empty? - Filter.new(@runnable, file, nil) if file - else - lines.map { |line| Filter.new(@runnable, file, line) } - end - end - end - end - - class Filter # :nodoc: - def initialize(runnable, file, line) - @runnable, @file = runnable, File.expand_path(file) - @line = line.to_i if line - end - - def ===(method) - return unless @runnable.method_defined?(method) - - if @line - test_file, test_range = definition_for(@runnable.instance_method(method)) - test_file == @file && test_range.include?(@line) - else - @runnable.instance_method(method).source_location.first == @file - end - end - - private - def definition_for(method) - file, start_line = method.source_location - end_line = method.source.count("\n") + start_line - 1 - - return file, start_line..end_line - end - end end diff --git a/railties/lib/rails/test_unit/minitest_plugin.rb b/railties/lib/rails/test_unit/minitest_plugin.rb deleted file mode 100644 index 3571274bcd..0000000000 --- a/railties/lib/rails/test_unit/minitest_plugin.rb +++ /dev/null @@ -1,145 +0,0 @@ -require "active_support/core_ext/module/attribute_accessors" -require_relative "reporter" -require_relative "test_requirer" -require "shellwords" - -module Minitest - class SuppressedSummaryReporter < SummaryReporter - # Disable extra failure output after a run if output is inline. - def aggregated_results(*) - super unless options[:output_inline] - end - end - - def self.plugin_rails_options(opts, options) - executable = ::Rails::TestUnitReporter.executable - opts.separator "" - opts.separator "Usage: #{executable} [options] [files or directories]" - opts.separator "You can run a single test by appending a line number to a filename:" - opts.separator "" - opts.separator " #{executable} test/models/user_test.rb:27" - opts.separator "" - opts.separator "You can run multiple files and directories at the same time:" - opts.separator "" - opts.separator " #{executable} test/controllers test/integration/login_test.rb" - opts.separator "" - opts.separator "By default test failures and errors are reported inline during a run." - opts.separator "" - - opts.separator "Rails options:" - opts.on("-e", "--environment ENV", - "Run tests in the ENV environment") do |env| - options[:environment] = env.strip - end - - opts.on("-b", "--backtrace", - "Show the complete backtrace") do - options[:full_backtrace] = true - end - - opts.on("-d", "--defer-output", - "Output test failures and errors after the test run") do - options[:output_inline] = false - end - - opts.on("-f", "--fail-fast", - "Abort test run on first failure or error") do - options[:fail_fast] = true - end - - opts.on("-c", "--[no-]color", - "Enable color in the output") do |value| - options[:color] = value - end - - opts.on("-w", "--warnings", - "Enable ruby warnings") do - $VERBOSE = true - end - - options[:color] = true - options[:output_inline] = true - options[:patterns] = opts.order! unless run_via.rake? - end - - def self.rake_run(patterns, exclude_patterns = []) # :nodoc: - self.run_via = :rake unless run_via.set? - ::Rails::TestRequirer.require_files(patterns, exclude_patterns) - autorun - end - - module RunRespectingRakeTestopts - def run(args = []) - if run_via.rake? - args = Shellwords.split(ENV["TESTOPTS"] || "") - end - - super - end - end - - singleton_class.prepend RunRespectingRakeTestopts - - # Owes great inspiration to test runner trailblazers like RSpec, - # minitest-reporters, maxitest and others. - def self.plugin_rails_init(options) - ENV["RAILS_ENV"] = options[:environment] || "test" - - # If run via `ruby` we've been passed the files to run directly, or if run - # via `rake` then they have already been eagerly required. - unless run_via.ruby? || run_via.rake? - # If there are no given patterns, we can assume that the user - # simply runs the `bin/rails test` command without extra arguments. - if options[:patterns].empty? - ::Rails::TestRequirer.require_files(options[:patterns], ["test/system/**/*"]) - else - ::Rails::TestRequirer.require_files(options[:patterns]) - end - end - - unless options[:full_backtrace] || ENV["BACKTRACE"] - # Plugin can run without Rails loaded, check before filtering. - Minitest.backtrace_filter = ::Rails.backtrace_cleaner if ::Rails.respond_to?(:backtrace_cleaner) - end - - # Replace progress reporter for colors. - reporter.reporters.delete_if { |reporter| reporter.kind_of?(SummaryReporter) || reporter.kind_of?(ProgressReporter) } - reporter << SuppressedSummaryReporter.new(options[:io], options) - reporter << ::Rails::TestUnitReporter.new(options[:io], options) - end - - def self.run_via=(runner) - if run_via.set? - raise ArgumentError, "run_via already assigned" - else - run_via.runner = runner - end - end - - class RunVia - attr_accessor :runner - alias set? runner - - # Backwardscompatibility with Rails 5.0 generated plugin test scripts. - def []=(runner, *) - @runner = runner - end - - def ruby? - runner == :ruby - end - - def rake? - runner == :rake - end - end - - mattr_reader :run_via, default: RunVia.new -end - -# Put Rails as the first plugin minitest initializes so other plugins -# can override or replace our default reporter setup. -# Since minitest only loads plugins if its extensions are empty we have -# to call `load_plugins` first. -Minitest.load_plugins -Minitest.extensions.unshift "rails" diff --git a/railties/lib/rails/test_unit/reporter.rb b/railties/lib/rails/test_unit/reporter.rb index 1cc27f7b6c..ab28034d27 100644 --- a/railties/lib/rails/test_unit/reporter.rb +++ b/railties/lib/rails/test_unit/reporter.rb @@ -71,7 +71,12 @@ module Rails end def app_root - @app_root ||= defined?(ENGINE_ROOT) ? ENGINE_ROOT : Rails.root + @app_root ||= + if defined?(ENGINE_ROOT) + ENGINE_ROOT + elsif Rails.respond_to?(:root) + Rails.root + end end def colored_output? diff --git a/railties/lib/rails/test_unit/runner.rb b/railties/lib/rails/test_unit/runner.rb new file mode 100644 index 0000000000..1f3a248bcb --- /dev/null +++ b/railties/lib/rails/test_unit/runner.rb @@ -0,0 +1,143 @@ +# frozen_string_literal: true + +require "shellwords" +require "method_source" +require "rake/file_list" +require "active_support/core_ext/module/attribute_accessors" + +module Rails + module TestUnit + class Runner + mattr_reader :filters, default: [] + + class << self + def options(opts) + opts.on("--warnings", "-w", "Run with Ruby warnings enabled") {} + opts.on("--environment", "-e", "Run tests in the ENV environment") {} + end + + def parse_options(argv) + # Perform manual parsing and cleanup since option parser raises on unknown options. + env_index = argv.index("--environment") || argv.index("-e") + if env_index + argv.delete_at(env_index) + environment = argv.delete_at(env_index).strip + end + ENV["RAILS_ENV"] = environment || "test" + + w_index = argv.index("--warnings") || argv.index("-w") + $VERBOSE = argv.delete_at(w_index) if w_index + end + + def rake_run(argv = []) + ARGV.replace Shellwords.split(ENV["TESTOPTS"] || "") + + run(argv) + end + + def run(argv = []) + load_tests(argv) + + require "active_support/testing/autorun" + end + + def load_tests(argv) + patterns = extract_filters(argv) + + tests = Rake::FileList[patterns.any? ? patterns : "test/**/*_test.rb"] + tests.exclude("test/system/**/*") if patterns.empty? + + tests.to_a.each { |path| require File.expand_path(path) } + end + + def compose_filter(runnable, filter) + if filters.any? { |_, lines| lines.any? } + CompositeFilter.new(runnable, filter, filters) + else + filter + end + end + + private + def extract_filters(argv) + # Extract absolute and relative paths but skip -n /.*/ regexp filters. + argv.select { |arg| arg =~ %r%^/?\w+/% && !arg.end_with?("/") }.map do |path| + case + when path =~ /(:\d+)+$/ + file, *lines = path.split(":") + filters << [ file, lines ] + file + when Dir.exist?(path) + "#{path}/**/*_test.rb" + else + filters << [ path, [] ] + path + end + end + end + end + end + + class CompositeFilter # :nodoc: + attr_reader :named_filter + + def initialize(runnable, filter, patterns) + @runnable = runnable + @named_filter = derive_named_filter(filter) + @filters = [ @named_filter, *derive_line_filters(patterns) ].compact + end + + # Minitest uses === to find matching filters. + def ===(method) + @filters.any? { |filter| filter === method } + end + + private + def derive_named_filter(filter) + if filter.respond_to?(:named_filter) + filter.named_filter + elsif filter =~ %r%/(.*)/% # Regexp filtering copied from Minitest. + Regexp.new $1 + elsif filter.is_a?(String) + filter + end + end + + def derive_line_filters(patterns) + patterns.flat_map do |file, lines| + if lines.empty? + Filter.new(@runnable, file, nil) if file + else + lines.map { |line| Filter.new(@runnable, file, line) } + end + end + end + end + + class Filter # :nodoc: + def initialize(runnable, file, line) + @runnable, @file = runnable, File.expand_path(file) + @line = line.to_i if line + end + + def ===(method) + return unless @runnable.method_defined?(method) + + if @line + test_file, test_range = definition_for(@runnable.instance_method(method)) + test_file == @file && test_range.include?(@line) + else + @runnable.instance_method(method).source_location.first == @file + end + end + + private + def definition_for(method) + file, start_line = method.source_location + end_line = method.source.count("\n") + start_line - 1 + + return file, start_line..end_line + end + end + end +end diff --git a/railties/lib/rails/test_unit/test_requirer.rb b/railties/lib/rails/test_unit/test_requirer.rb deleted file mode 100644 index 92e5fcf0bc..0000000000 --- a/railties/lib/rails/test_unit/test_requirer.rb +++ /dev/null @@ -1,31 +0,0 @@ -require "active_support/core_ext/object/blank" -require "rake/file_list" - -module Rails - class TestRequirer # :nodoc: - class << self - def require_files(patterns, exclude_patterns = []) - patterns = expand_patterns(patterns) - - file_list = Rake::FileList[patterns.compact.presence || "test/**/*_test.rb"] - file_list.exclude(exclude_patterns) - - file_list.to_a.each do |file| - require File.expand_path(file) - end - end - - private - def expand_patterns(patterns) - patterns.map do |arg| - arg = arg.gsub(/(:\d+)+?$/, "") - if Dir.exist?(arg) - "#{arg}/**/*_test.rb" - else - arg - end - end - end - end - end -end diff --git a/railties/lib/rails/test_unit/testing.rake b/railties/lib/rails/test_unit/testing.rake index 486446b463..97dd5cef10 100644 --- a/railties/lib/rails/test_unit/testing.rake +++ b/railties/lib/rails/test_unit/testing.rake @@ -1,6 +1,6 @@ gem "minitest" require "minitest" -require_relative "minitest_plugin" +require_relative "runner" task default: :test @@ -9,9 +9,9 @@ task :test do $: << "test" if ENV.key?("TEST") - Minitest.rake_run([ENV["TEST"]]) + Rails::TestUnit::Runner.rake_run([ENV["TEST"]]) else - Minitest.rake_run(["test"], ["test/system/**/*"]) + Rails::TestUnit::Runner.rake_run end end @@ -29,28 +29,28 @@ namespace :test do ["models", "helpers", "controllers", "mailers", "integration", "jobs"].each do |name| task name => "test:prepare" do $: << "test" - Minitest.rake_run(["test/#{name}"]) + Rails::TestUnit::Runner.rake_run(["test/#{name}"]) end end task generators: "test:prepare" do $: << "test" - Minitest.rake_run(["test/lib/generators"]) + Rails::TestUnit::Runner.rake_run(["test/lib/generators"]) end task units: "test:prepare" do $: << "test" - Minitest.rake_run(["test/models", "test/helpers", "test/unit"]) + Rails::TestUnit::Runner.rake_run(["test/models", "test/helpers", "test/unit"]) end task functionals: "test:prepare" do $: << "test" - Minitest.rake_run(["test/controllers", "test/mailers", "test/functional"]) + Rails::TestUnit::Runner.rake_run(["test/controllers", "test/mailers", "test/functional"]) end desc "Run system tests only" task system: "test:prepare" do $: << "test" - Minitest.rake_run(["test/system"]) + Rails::TestUnit::Runner.rake_run(["test/system"]) end end diff --git a/railties/test/application/configuration_test.rb b/railties/test/application/configuration_test.rb index 983ea5c3e6..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,11 +1204,18 @@ 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 + test "config.action_controller.default_protect_from_forgery is true by default" do + app "development" + + assert_equal true, ActionController::Base.default_protect_from_forgery + assert_includes ActionController::Base.__callbacks[:process_action].map(&:filter), :verify_authenticity_token + end + test "config.action_controller.permit_all_parameters can be configured in an initializer" do app_file "config/initializers/permit_all_parameters.rb", <<-RUBY Rails.application.config.action_controller.permit_all_parameters = true @@ -1216,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 @@ -1227,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 @@ -1238,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 @@ -1578,6 +1585,52 @@ module ApplicationTests assert_equal({}, Rails.application.config.my_custom_config) end + test "default SQLite3Adapter.represent_boolean_as_integer for 5.1 is false" do + remove_from_config '.*config\.load_defaults.*\n' + add_to_top_of_config <<-RUBY + config.load_defaults 5.1 + RUBY + app_file "app/models/post.rb", <<-RUBY + class Post < ActiveRecord::Base + end + RUBY + + app "development" + force_lazy_load_hooks { Post } + + assert_not ActiveRecord::ConnectionAdapters::SQLite3Adapter.represent_boolean_as_integer + end + + test "default SQLite3Adapter.represent_boolean_as_integer for new installs is true" do + 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 + + 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 + test "config_for containing ERB tags should evaluate" do app_file "config/custom.yml", <<-RUBY development: @@ -1684,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/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/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 5ae6ea925f..134106812d 100644 --- a/railties/test/application/rake_test.rb +++ b/railties/test/application/rake_test.rb @@ -117,7 +117,7 @@ module ApplicationTests end def test_code_statistics_sanity - assert_match "Code LOC: 26 Test LOC: 0 Code to Test Ratio: 1:0.0", + assert_match "Code LOC: 25 Test LOC: 0 Code to Test Ratio: 1:0.0", Dir.chdir(app_path) { `bin/rails stats` } end 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/test_runner_test.rb b/railties/test/application/test_runner_test.rb index 82dcfe5200..be50fdb49b 100644 --- a/railties/test/application/test_runner_test.rb +++ b/railties/test/application/test_runner_test.rb @@ -16,13 +16,13 @@ module ApplicationTests end def test_run_via_backwardscompatibility - require "rails/test_unit/minitest_plugin" + require "minitest/rails_plugin" assert_nothing_raised do Minitest.run_via[:ruby] = true end - assert_predicate Minitest.run_via, :ruby? + assert Minitest.run_via[:ruby] end def test_run_single_file @@ -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' 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..f243da5815 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 @@ -278,6 +279,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 +522,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 +808,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/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..1fa40f74b9 100644 --- a/railties/test/generators/plugin_generator_test.rb +++ b/railties/test/generators/plugin_generator_test.rb @@ -491,7 +491,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 +678,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_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..6f762d2d3f 100644 --- a/railties/test/railties/engine_test.rb +++ b/railties/test/railties/engine_test.rb @@ -1427,6 +1427,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 diff --git a/railties/test/test_unit/reporter_test.rb b/railties/test/test_unit/reporter_test.rb index 98201394cd..42f973357b 100644 --- a/railties/test/test_unit/reporter_test.rb +++ b/railties/test/test_unit/reporter_test.rb @@ -70,7 +70,7 @@ class TestUnitReporterTest < ActiveSupport::TestCase @reporter.record(errored_test) @reporter.report - expect = %r{\AE\n\nError:\nTestUnitReporterTest::ExampleTest#woot:\nArgumentError: wups\n No backtrace\n\nbin/rails test .*test/test_unit/reporter_test\.rb:\d+\n\n\z} + expect = %r{\AE\n\nError:\nTestUnitReporterTest::ExampleTest#woot:\nArgumentError: wups\n \n\nbin/rails test .*test/test_unit/reporter_test\.rb:\d+\n\n\z} assert_match expect, @output.string end @@ -150,7 +150,7 @@ class TestUnitReporterTest < ActiveSupport::TestCase colored = Rails::TestUnitReporter.new @output, color: true, output_inline: true colored.record(errored_test) - expected = %r{\e\[31mE\e\[0m\n\n\e\[31mError:\nTestUnitReporterTest::ExampleTest#woot:\nArgumentError: wups\n No backtrace\n\e\[0m} + expected = %r{\e\[31mE\e\[0m\n\n\e\[31mError:\nTestUnitReporterTest::ExampleTest#woot:\nArgumentError: wups\n \n\e\[0m} assert_match expected, @output.string end end @@ -171,8 +171,11 @@ class TestUnitReporterTest < ActiveSupport::TestCase end def errored_test + error = ArgumentError.new("wups") + error.set_backtrace([ "some_test.rb:4" ]) + et = ExampleTest.new(:woot) - et.failures << Minitest::UnexpectedError.new(ArgumentError.new("wups")) + et.failures << Minitest::UnexpectedError.new(error) et end |