diff options
Diffstat (limited to 'railties')
45 files changed, 553 insertions, 179 deletions
diff --git a/railties/CHANGELOG.md b/railties/CHANGELOG.md index 8b10144413..a181823576 100644 --- a/railties/CHANGELOG.md +++ b/railties/CHANGELOG.md @@ -1,22 +1,36 @@ -* Created rake restart task. Restarts your Rails app by touching the - `tmp/restart.txt`. +* Respect `pluralize_table_names` when generating fixture file. - Fixes #18876. + Fixes #19519. - *Hyonjee Joo* + *Yuji Yaginuma* + +* Add a new-line to the end of route method generated code. + + We need to add a `\n`, because we cannot have two routes + in the same line. + + *arthurnn* -* Set Rails console to use log formatter and log level as specified for the - given environment. +* Add `rake initializers` - Fixes #15470. + This task prints out all defined initializers in the order they are invoked + by Rails. This is helpful for debugging issues related to the initialization + process. - *Jacob Evelyn* + *Naoto Kaneko* + +* Created rake restart task. Restarts your Rails app by touching the + `tmp/restart.txt`. + + Fixes #18876. + + *Hyonjee Joo* * Add `config/initializers/active_record_belongs_to_required_by_default.rb` Newly generated Rails apps have a new initializer called `active_record_belongs_to_required_by_default.rb` which sets the value of - the configuration option `config.active_record.belongs_to_requred_by_default` + the configuration option `config.active_record.belongs_to_required_by_default` to `true` when ActiveRecord is not skipped. As a result, new Rails apps require `belongs_to` association on model diff --git a/railties/lib/rails/app_rails_loader.rb b/railties/lib/rails/app_rails_loader.rb index 39d8007333..9a7c6c5f2d 100644 --- a/railties/lib/rails/app_rails_loader.rb +++ b/railties/lib/rails/app_rails_loader.rb @@ -1,4 +1,5 @@ require 'pathname' +require 'rails/version' module Rails module AppRailsLoader @@ -9,7 +10,7 @@ module Rails BUNDLER_WARNING = <<EOS Looks like your app's ./bin/rails is a stub that was generated by Bundler. -In Rails 4, your app's bin/ directory contains executables that are versioned +In Rails #{Rails::VERSION::MAJOR}, your app's bin/ directory contains executables that are versioned like any other source code, rather than stubs that are generated on demand. Here's how to upgrade: diff --git a/railties/lib/rails/application.rb b/railties/lib/rails/application.rb index b11815e013..e9683d4a95 100644 --- a/railties/lib/rails/application.rb +++ b/railties/lib/rails/application.rb @@ -159,8 +159,9 @@ module Rails # Implements call according to the Rack API. It simply # dispatches the request to the underlying middleware stack. def call(env) - env["ORIGINAL_FULLPATH"] = build_original_fullpath(env) - env["ORIGINAL_SCRIPT_NAME"] = env["SCRIPT_NAME"] + req = ActionDispatch::Request.new env + env["ORIGINAL_FULLPATH"] = req.fullpath + env["ORIGINAL_SCRIPT_NAME"] = req.script_name super(env) end @@ -427,9 +428,9 @@ module Rails # Return an array of railties respecting the order they're loaded # and the order specified by the +railties_order+ config. # - # While when running initializers we need engines in reverse - # order here when copying migrations from railties we need them in the same - # order as given by +railties_order+. + # While running initializers we need engines in reverse order here when + # copying migrations from railties ; we need them in the order given by + # +railties_order+. def migration_railties # :nodoc: ordered_railties.flatten - [self] end @@ -504,18 +505,6 @@ module Rails default_stack.build_stack end - def build_original_fullpath(env) #:nodoc: - path_info = env["PATH_INFO"] - query_string = env["QUERY_STRING"] - script_name = env["SCRIPT_NAME"] - - if query_string.present? - "#{script_name}#{path_info}?#{query_string}" - else - "#{script_name}#{path_info}" - end - end - def validate_secret_key_config! #:nodoc: if secrets.secret_key_base.blank? ActiveSupport::Deprecation.warn "You didn't set `secret_key_base`. " + diff --git a/railties/lib/rails/commands.rb b/railties/lib/rails/commands.rb index f32bf772a5..12bd73db24 100644 --- a/railties/lib/rails/commands.rb +++ b/railties/lib/rails/commands.rb @@ -6,7 +6,8 @@ aliases = { "c" => "console", "s" => "server", "db" => "dbconsole", - "r" => "runner" + "r" => "runner", + "t" => "test", } command = ARGV.shift diff --git a/railties/lib/rails/commands/commands_tasks.rb b/railties/lib/rails/commands/commands_tasks.rb index 8bae08e44e..d8d4080c3e 100644 --- a/railties/lib/rails/commands/commands_tasks.rb +++ b/railties/lib/rails/commands/commands_tasks.rb @@ -14,6 +14,7 @@ The most common rails commands are: generate Generate new code (short-cut alias: "g") console Start the Rails console (short-cut alias: "c") server Start the Rails server (short-cut alias: "s") + test Run tests (short-cut alias: "t") dbconsole Start a console for the database specified in config/database.yml (short-cut alias: "db") new Create a new Rails application. "rails new my_app" creates a @@ -27,7 +28,7 @@ In addition to those, there are: All commands can be run with -h (or --help) for more information. EOT - COMMAND_WHITELIST = %w(plugin generate destroy console server dbconsole runner new version help) + COMMAND_WHITELIST = %w(plugin generate destroy console server dbconsole runner new version help test) def initialize(argv) @argv = argv @@ -81,6 +82,10 @@ EOT end end + def test + require_command!("test") + end + def dbconsole require_command!("dbconsole") Rails::DBConsole.start diff --git a/railties/lib/rails/commands/test.rb b/railties/lib/rails/commands/test.rb new file mode 100644 index 0000000000..598e224a6f --- /dev/null +++ b/railties/lib/rails/commands/test.rb @@ -0,0 +1,5 @@ +require "rails/test_unit/runner" + +$: << File.expand_path("../../test", APP_PATH) + +Rails::TestRunner.run(ARGV) diff --git a/railties/lib/rails/engine.rb b/railties/lib/rails/engine.rb index fd37fd0457..83cee28fa3 100644 --- a/railties/lib/rails/engine.rb +++ b/railties/lib/rails/engine.rb @@ -528,7 +528,7 @@ module Rails # Defines the routes for this engine. If a block is given to # routes, it is appended to the engine. def routes - @routes ||= ActionDispatch::Routing::RouteSet.new + @routes ||= ActionDispatch::Routing::RouteSet.new_with_config(config) @routes.append(&Proc.new) if block_given? @routes end diff --git a/railties/lib/rails/generators/actions.rb b/railties/lib/rails/generators/actions.rb index c1bc646c65..70a20801a0 100644 --- a/railties/lib/rails/generators/actions.rb +++ b/railties/lib/rails/generators/actions.rb @@ -221,7 +221,7 @@ module Rails sentinel = /\.routes\.draw do\s*\n/m in_root do - inject_into_file 'config/routes.rb', " #{routing_code}", { after: sentinel, verbose: false, force: true } + inject_into_file 'config/routes.rb', " #{routing_code}\n", { after: sentinel, verbose: false, force: true } end end diff --git a/railties/lib/rails/generators/app_base.rb b/railties/lib/rails/generators/app_base.rb index 253272c7dd..813f8b21fd 100644 --- a/railties/lib/rails/generators/app_base.rb +++ b/railties/lib/rails/generators/app_base.rb @@ -291,7 +291,7 @@ module Rails end def javascript_runtime_gemfile_entry - comment = 'See https://github.com/sstephenson/execjs#readme for more supported runtimes' + comment = 'See https://github.com/rails/execjs#readme for more supported runtimes' if defined?(JRUBY_VERSION) GemfileEntry.version 'therubyrhino', nil, comment else diff --git a/railties/lib/rails/generators/named_base.rb b/railties/lib/rails/generators/named_base.rb index 36456e64f5..01a8e2e9b4 100644 --- a/railties/lib/rails/generators/named_base.rb +++ b/railties/lib/rails/generators/named_base.rb @@ -141,6 +141,10 @@ module Rails @plural_file_name ||= file_name.pluralize end + def fixture_file_name + @fixture_file_name ||= (pluralize_table_names? ? plural_file_name : file_name) + end + def route_url @route_url ||= class_path.collect {|dname| "/" + dname }.join + "/" + plural_file_name end diff --git a/railties/lib/rails/generators/rails/app/templates/Gemfile b/railties/lib/rails/generators/rails/app/templates/Gemfile index 82a0315379..c11bb58bfa 100644 --- a/railties/lib/rails/generators/rails/app/templates/Gemfile +++ b/railties/lib/rails/generators/rails/app/templates/Gemfile @@ -36,10 +36,6 @@ group :development, :test do # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring gem 'spring' <% end -%> - - # Adds `Method#source` and `Method#comment` to get the source code of a - # method from the console - gem 'method_source' <% end -%> end <% if RUBY_PLATFORM.match(/bccwin|cygwin|emx|mingw|mswin|wince|java/) -%> diff --git a/railties/lib/rails/generators/rails/app/templates/app/assets/javascripts/application.js.tt b/railties/lib/rails/generators/rails/app/templates/app/assets/javascripts/application.js.tt index c1a77944e8..cb86978d4c 100644 --- a/railties/lib/rails/generators/rails/app/templates/app/assets/javascripts/application.js.tt +++ b/railties/lib/rails/generators/rails/app/templates/app/assets/javascripts/application.js.tt @@ -7,7 +7,7 @@ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the // compiled file. // -// Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details +// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details // about supported directives. // <% unless options[:skip_javascript] -%> diff --git a/railties/lib/rails/generators/rails/app/templates/app/jobs/application_job.rb b/railties/lib/rails/generators/rails/app/templates/app/jobs/application_job.rb new file mode 100644 index 0000000000..bddb5bcd0d --- /dev/null +++ b/railties/lib/rails/generators/rails/app/templates/app/jobs/application_job.rb @@ -0,0 +1,3 @@ +class ApplicationJob < ActiveJob::Base + +end diff --git a/railties/lib/rails/generators/rails/app/templates/config/databases/jdbcmysql.yml b/railties/lib/rails/generators/rails/app/templates/config/databases/jdbcmysql.yml index acb93939e1..f5b62e8fb3 100644 --- a/railties/lib/rails/generators/rails/app/templates/config/databases/jdbcmysql.yml +++ b/railties/lib/rails/generators/rails/app/templates/config/databases/jdbcmysql.yml @@ -7,7 +7,7 @@ # gem 'activerecord-jdbcmysql-adapter' # # And be sure to use new-style password hashing: -# http://dev.mysql.com/doc/refman/5.0/en/old-client.html +# http://dev.mysql.com/doc/refman/5.6/en/old-client.html # default: &default adapter: mysql diff --git a/railties/lib/rails/generators/rails/app/templates/config/databases/mysql.yml b/railties/lib/rails/generators/rails/app/templates/config/databases/mysql.yml index 596c916573..b0767bd93a 100644 --- a/railties/lib/rails/generators/rails/app/templates/config/databases/mysql.yml +++ b/railties/lib/rails/generators/rails/app/templates/config/databases/mysql.yml @@ -7,7 +7,7 @@ # gem 'mysql2' # # And be sure to use new-style password hashing: -# http://dev.mysql.com/doc/refman/5.0/en/old-client.html +# http://dev.mysql.com/doc/refman/5.6/en/old-client.html # default: &default adapter: mysql2 diff --git a/railties/lib/rails/generators/rails/plugin/templates/rails/javascripts.js b/railties/lib/rails/generators/rails/plugin/templates/rails/javascripts.js index c28e5badc6..8913b40f69 100644 --- a/railties/lib/rails/generators/rails/plugin/templates/rails/javascripts.js +++ b/railties/lib/rails/generators/rails/plugin/templates/rails/javascripts.js @@ -7,7 +7,7 @@ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the // compiled file. // -// Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details +// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details // about supported directives. // //= require_tree . diff --git a/railties/lib/rails/generators/rails/resource_route/resource_route_generator.rb b/railties/lib/rails/generators/rails/resource_route/resource_route_generator.rb index e4a2bc2b0f..c986f95e67 100644 --- a/railties/lib/rails/generators/rails/resource_route/resource_route_generator.rb +++ b/railties/lib/rails/generators/rails/resource_route/resource_route_generator.rb @@ -29,8 +29,10 @@ module Rails write("end", route_length - index) end - # route prepends two spaces onto the front of the string that is passed, this corrects that - route route_string[2..-1] + # route prepends two spaces onto the front of the string that is passed, this corrects that. + # Also it adds a \n to the end of each line, as route already adds that + # we need to correct that too. + route route_string[2..-2] end private diff --git a/railties/lib/rails/generators/test_unit/mailer/templates/preview.rb b/railties/lib/rails/generators/test_unit/mailer/templates/preview.rb index 6b85764a66..b063cbc47b 100644 --- a/railties/lib/rails/generators/test_unit/mailer/templates/preview.rb +++ b/railties/lib/rails/generators/test_unit/mailer/templates/preview.rb @@ -1,9 +1,9 @@ <% module_namespacing do -%> -# Preview all emails at http://localhost:3000/rails/mailers/<%= file_path %> +# Preview all emails at http://localhost:3000/rails/mailers/<%= file_path %>_mailer class <%= class_name %>MailerPreview < ActionMailer::Preview <% actions.each do |action| -%> - # Preview this email at http://localhost:3000/rails/mailers/<%= file_path %>/<%= action %> + # Preview this email at http://localhost:3000/rails/mailers/<%= file_path %>_mailer/<%= action %> def <%= action %> <%= class_name %>Mailer.<%= action %> end diff --git a/railties/lib/rails/generators/test_unit/model/model_generator.rb b/railties/lib/rails/generators/test_unit/model/model_generator.rb index 2826a3ffa1..086588750e 100644 --- a/railties/lib/rails/generators/test_unit/model/model_generator.rb +++ b/railties/lib/rails/generators/test_unit/model/model_generator.rb @@ -19,7 +19,7 @@ module TestUnit # :nodoc: def create_fixture_file if options[:fixture] && options[:fixture_replacement].nil? - template 'fixtures.yml', File.join('test/fixtures', class_path, "#{plural_file_name}.yml") + template 'fixtures.yml', File.join('test/fixtures', class_path, "#{fixture_file_name}.yml") end end diff --git a/railties/lib/rails/ruby_version_check.rb b/railties/lib/rails/ruby_version_check.rb index e2a199ec4d..9131c51e91 100644 --- a/railties/lib/rails/ruby_version_check.rb +++ b/railties/lib/rails/ruby_version_check.rb @@ -1,13 +1,13 @@ -if RUBY_VERSION < '2.2.0' && RUBY_ENGINE == 'ruby' +if RUBY_VERSION < '2.2.1' && RUBY_ENGINE == 'ruby' desc = defined?(RUBY_DESCRIPTION) ? RUBY_DESCRIPTION : "ruby #{RUBY_VERSION} (#{RUBY_RELEASE_DATE})" abort <<-end_message - Rails 5 requires to run on Ruby 2.2.0 or newer. + Rails 5 requires to run on Ruby 2.2.1 or newer. You're running #{desc} - Please upgrade to Ruby 2.2.0 or newer to continue. + Please upgrade to Ruby 2.2.1 or newer to continue. end_message end diff --git a/railties/lib/rails/tasks.rb b/railties/lib/rails/tasks.rb index a5e4d2935e..2c3d278eca 100644 --- a/railties/lib/rails/tasks.rb +++ b/railties/lib/rails/tasks.rb @@ -4,6 +4,7 @@ require 'rake' %w( annotations framework + initializers log middleware misc diff --git a/railties/lib/rails/tasks/initializers.rake b/railties/lib/rails/tasks/initializers.rake new file mode 100644 index 0000000000..2968b5cb53 --- /dev/null +++ b/railties/lib/rails/tasks/initializers.rake @@ -0,0 +1,6 @@ +desc "Print out all defined initializers in the order they are invoked by Rails." +task initializers: :environment do + Rails.application.initializers.tsort_each do |initializer| + puts initializer.name + end +end diff --git a/railties/lib/rails/test_help.rb b/railties/lib/rails/test_help.rb index 40a1915b54..5cf44e6331 100644 --- a/railties/lib/rails/test_help.rb +++ b/railties/lib/rails/test_help.rb @@ -2,6 +2,7 @@ # so fixtures aren't loaded into that environment abort("Abort testing: Your Rails environment is running in production mode!") if Rails.env.production? +require "rails/test_unit/minitest_plugin" require 'active_support/testing/autorun' require 'active_support/test_case' require 'action_controller' @@ -9,12 +10,6 @@ require 'action_controller/test_case' require 'action_dispatch/testing/integration' require 'rails/generators/test_case' -# Config Rails backtrace in tests. -require 'rails/backtrace_cleaner' -if ENV["BACKTRACE"].nil? - Minitest.backtrace_filter = Rails.backtrace_cleaner -end - if defined?(ActiveRecord::Base) ActiveRecord::Migration.maintain_test_schema! diff --git a/railties/lib/rails/test_unit/minitest_plugin.rb b/railties/lib/rails/test_unit/minitest_plugin.rb new file mode 100644 index 0000000000..70ce9d3360 --- /dev/null +++ b/railties/lib/rails/test_unit/minitest_plugin.rb @@ -0,0 +1,14 @@ +require "minitest" +require "rails/test_unit/reporter" + +def Minitest.plugin_rails_init(options) + self.reporter << Rails::TestUnitReporter.new(options[:io], options) + if $rails_test_runner && (method = $rails_test_runner.find_method) + options[:filter] = method + end + + if !($rails_test_runner && $rails_test_runner.show_backtrace?) + Minitest.backtrace_filter = Rails.backtrace_cleaner + end +end +Minitest.extensions << 'rails' diff --git a/railties/lib/rails/test_unit/reporter.rb b/railties/lib/rails/test_unit/reporter.rb new file mode 100644 index 0000000000..64e99626eb --- /dev/null +++ b/railties/lib/rails/test_unit/reporter.rb @@ -0,0 +1,22 @@ +require "minitest" + +module Rails + class TestUnitReporter < Minitest::StatisticsReporter + def report + return if results.empty? + io.puts + io.puts "Failed tests:" + io.puts + io.puts aggregated_results + end + + def aggregated_results # :nodoc: + filtered_results = results.dup + filtered_results.reject!(&:skipped?) unless options[:verbose] + filtered_results.map do |result| + location, line = result.method(result.name).source_location + "bin/rails test #{location}:#{line}" + end.join "\n" + end + end +end diff --git a/railties/lib/rails/test_unit/runner.rb b/railties/lib/rails/test_unit/runner.rb new file mode 100644 index 0000000000..5573fa6904 --- /dev/null +++ b/railties/lib/rails/test_unit/runner.rb @@ -0,0 +1,137 @@ +require "optparse" +require "rake/file_list" +require "method_source" + +module Rails + class TestRunner + class Options + def self.parse(args) + options = { backtrace: !ENV["BACKTRACE"].nil?, name: nil, environment: "test" } + + opt_parser = ::OptionParser.new do |opts| + opts.banner = "Usage: bin/rails test [options] [file or directory]" + + opts.separator "" + opts.on("-e", "--environment [ENV]", + "Run tests in the ENV environment") do |env| + options[:environment] = env.strip + end + opts.separator "" + opts.separator "Filter options:" + opts.separator "" + opts.separator <<-DESC + You can run a single test by appending the line number to filename: + + bin/rails test test/models/user_test.rb:27 + + DESC + + opts.on("-n", "--name [NAME]", + "Only run tests matching NAME") do |name| + options[:name] = name + end + opts.on("-p", "--pattern [PATTERN]", + "Only run tests matching PATTERN") do |pattern| + options[:name] = "/#{pattern}/" + end + + opts.separator "" + opts.separator "Output options:" + + opts.on("-b", "--backtrace", + "Show the complete backtrace") do + options[:backtrace] = true + end + + opts.separator "" + opts.separator "Common options:" + + opts.on_tail("-h", "--help", "Show this message") do + puts opts + exit + end + end + + opt_parser.order!(args) + + options[:patterns] = [] + while arg = args.shift + if (file_and_line = arg.split(':')).size > 1 + options[:filename], options[:line] = file_and_line + options[:filename] = File.expand_path options[:filename] + options[:line] &&= options[:line].to_i + else + arg = arg.gsub(':', '') + if Dir.exist?("#{arg}") + options[:patterns] << File.expand_path("#{arg}/**/*_test.rb") + elsif File.file?(arg) + options[:patterns] << File.expand_path(arg) + end + end + end + options + end + end + + def initialize(options = {}) + @options = options + end + + def self.run(arguments) + options = Rails::TestRunner::Options.parse(arguments) + Rails::TestRunner.new(options).run + end + + def run + $rails_test_runner = self + ENV["RAILS_ENV"] = @options[:environment] + run_tests + end + + def find_method + return @options[:name] if @options[:name] + return unless @options[:line] + method = test_methods.find do |location, test_method, start_line, end_line| + location == @options[:filename] && + (start_line..end_line).include?(@options[:line].to_i) + end + method[1] if method + end + + def show_backtrace? + @options[:backtrace] + end + + def test_files + return [@options[:filename]] if @options[:filename] + if @options[:patterns] && @options[:patterns].count > 0 + pattern = @options[:patterns] + else + pattern = "test/**/*_test.rb" + end + Rake::FileList[pattern] + end + + private + def run_tests + test_files.to_a.each do |file| + require File.expand_path file + end + end + + def test_methods + methods_map = [] + suites = Minitest::Runnable.runnables.shuffle + suites.each do |suite_class| + suite_class.runnable_methods.each do |test_method| + method = suite_class.instance_method(test_method) + location = method.source_location + start_line = location.last + end_line = method.source.split("\n").size + start_line - 1 + methods_map << [File.expand_path(location.first), test_method, start_line, end_line] + end + end + methods_map + end + end +end diff --git a/railties/lib/rails/test_unit/testing.rake b/railties/lib/rails/test_unit/testing.rake index d836c0d6d6..0f26621b59 100644 --- a/railties/lib/rails/test_unit/testing.rake +++ b/railties/lib/rails/test_unit/testing.rake @@ -1,11 +1,12 @@ -require 'rake/testtask' -require 'rails/test_unit/sub_test_task' +require "rails/test_unit/runner" task default: :test desc "Runs all tests in test folder" task :test do - Rails::TestTask.test_creator(Rake.application.top_level_tasks).invoke_rake_task + $: << "test" + args = ARGV[0] == "test" ? ARGV[1..-1] : [] + Rails::TestRunner.run(args) end namespace :test do @@ -14,30 +15,30 @@ namespace :test do # If used with Active Record, this task runs before the database schema is synchronized. end - Rails::TestTask.new(:run) do |t| - t.pattern = "test/**/*_test.rb" - end + task :run => %w[test] desc "Run tests quickly, but also reset db" task :db => %w[db:test:prepare test] - Rails::TestTask.new(single: "test:prepare") - ["models", "helpers", "controllers", "mailers", "integration", "jobs"].each do |name| - Rails::TestTask.new(name => "test:prepare") do |t| - t.pattern = "test/#{name}/**/*_test.rb" + task name => "test:prepare" do + $: << "test" + Rails::TestRunner.run(["test/#{name}"]) end end - Rails::TestTask.new(generators: "test:prepare") do |t| - t.pattern = "test/lib/generators/**/*_test.rb" + task :generators => "test:prepare" do + $: << "test" + Rails::TestRunner.run(["test/lib/generators"]) end - Rails::TestTask.new(units: "test:prepare") do |t| - t.pattern = 'test/{models,helpers,unit}/**/*_test.rb' + task :units => "test:prepare" do + $: << "test" + Rails::TestRunner.run(["test/models", "test/helpers", "test/unit"]) end - Rails::TestTask.new(functionals: "test:prepare") do |t| - t.pattern = 'test/{controllers,mailers,functional}/**/*_test.rb' + task :functionals => "test:prepare" do + $: << "test" + Rails::TestRunner.run(["test/controllers", "test/mailers", "test/functional"]) end end diff --git a/railties/railties.gemspec b/railties/railties.gemspec index 7a1c897e3d..001882fdc6 100644 --- a/railties/railties.gemspec +++ b/railties/railties.gemspec @@ -7,7 +7,7 @@ Gem::Specification.new do |s| s.summary = 'Tools for creating, working with, and running Rails applications.' s.description = 'Rails internals: application bootup, plugins, generators, and rake tasks.' - s.required_ruby_version = '>= 2.2.0' + s.required_ruby_version = '>= 2.2.1' s.license = 'MIT' @@ -28,6 +28,7 @@ Gem::Specification.new do |s| s.add_dependency 'rake', '>= 0.8.7' s.add_dependency 'thor', '>= 0.18.1', '< 2.0' + s.add_dependency 'method_source' s.add_development_dependency 'actionview', version end diff --git a/railties/test/abstract_unit.rb b/railties/test/abstract_unit.rb index ab8883e135..794d180e5d 100644 --- a/railties/test/abstract_unit.rb +++ b/railties/test/abstract_unit.rb @@ -30,9 +30,4 @@ end class ActiveSupport::TestCase include ActiveSupport::Testing::Stream - - # FIXME: we have tests that depend on run order, we should fix that and - # remove this method call. - self.test_order = :sorted - end diff --git a/railties/test/application/asset_debugging_test.rb b/railties/test/application/asset_debugging_test.rb index 9a571fac3a..acd387256c 100644 --- a/railties/test/application/asset_debugging_test.rb +++ b/railties/test/application/asset_debugging_test.rb @@ -57,8 +57,8 @@ module ApplicationTests class ::PostsController < ActionController::Base ; end get '/posts?debug_assets=true' - assert_match(/<script src="\/assets\/application-([0-z]+)\.js\?body=1"><\/script>/, last_response.body) - assert_match(/<script src="\/assets\/xmlhr-([0-z]+)\.js\?body=1"><\/script>/, last_response.body) + assert_match(/<script src="\/assets\/application(\.self)?-([0-z]+)\.js\?body=1"><\/script>/, last_response.body) + assert_match(/<script src="\/assets\/xmlhr(\.self)?-([0-z]+)\.js\?body=1"><\/script>/, last_response.body) end end end diff --git a/railties/test/application/assets_test.rb b/railties/test/application/assets_test.rb index 0a2f283cce..f6b7d4c855 100644 --- a/railties/test/application/assets_test.rb +++ b/railties/test/application/assets_test.rb @@ -204,7 +204,7 @@ module ApplicationTests app_file "app/assets/javascripts/application.js", "alert();" precompile! - manifest = Dir["#{app_path}/public/assets/manifest-*.json"].first + manifest = Dir["#{app_path}/public/assets/.sprockets-manifest-*.json"].first assets = ActiveSupport::JSON.decode(File.read(manifest)) assert_match(/application-([0-z]+)\.js/, assets["assets"]["application.js"]) @@ -217,7 +217,7 @@ module ApplicationTests precompile! - manifest = Dir["#{app_path}/public/x/manifest-*.json"].first + manifest = Dir["#{app_path}/public/x/.sprockets-manifest-*.json"].first assets = ActiveSupport::JSON.decode(File.read(manifest)) assert_match(/application-([0-z]+)\.js/, assets["assets"]["application.js"]) end @@ -229,7 +229,7 @@ module ApplicationTests ENV["RAILS_ENV"] = "production" precompile! - manifest = Dir["#{app_path}/public/assets/manifest-*.json"].first + manifest = Dir["#{app_path}/public/assets/.sprockets-manifest-*.json"].first assets = ActiveSupport::JSON.decode(File.read(manifest)) asset_path = assets["assets"]["application.js"] @@ -261,7 +261,7 @@ module ApplicationTests ENV["RAILS_ENV"] = "production" precompile! - manifest = Dir["#{app_path}/public/assets/manifest-*.json"].first + manifest = Dir["#{app_path}/public/assets/.sprockets-manifest-*.json"].first assets = ActiveSupport::JSON.decode(File.read(manifest)) asset_path = assets["assets"]["application.css"] @@ -291,7 +291,7 @@ module ApplicationTests precompile! - manifest = Dir["#{app_path}/public/assets/manifest-*.json"].first + manifest = Dir["#{app_path}/public/assets/.sprockets-manifest-*.json"].first assets = ActiveSupport::JSON.decode(File.read(manifest)) assert asset_path = assets["assets"].find { |(k, _)| k && k =~ /.png/ }[1] @@ -437,9 +437,9 @@ module ApplicationTests class ::PostsController < ActionController::Base; end get '/posts', {}, {'HTTPS'=>'off'} - assert_match('src="http://example.com/assets/application.js', last_response.body) + assert_match('src="http://example.com/assets/application.self.js', last_response.body) get '/posts', {}, {'HTTPS'=>'on'} - assert_match('src="https://example.com/assets/application.js', last_response.body) + assert_match('src="https://example.com/assets/application.self.js', last_response.body) end test "asset urls should be protocol-relative if no request is in scope" do diff --git a/railties/test/application/build_original_fullpath_test.rb b/railties/test/application/build_original_fullpath_test.rb deleted file mode 100644 index 647ffb097a..0000000000 --- a/railties/test/application/build_original_fullpath_test.rb +++ /dev/null @@ -1,27 +0,0 @@ -require "abstract_unit" - -module ApplicationTests - class BuildOriginalPathTest < ActiveSupport::TestCase - def test_include_original_PATH_info_in_ORIGINAL_FULLPATH - env = { 'PATH_INFO' => '/foo/' } - assert_equal "/foo/", Rails.application.send(:build_original_fullpath, env) - end - - def test_include_SCRIPT_NAME - env = { - 'SCRIPT_NAME' => '/foo', - 'PATH_INFO' => '/bar' - } - - assert_equal "/foo/bar", Rails.application.send(:build_original_fullpath, env) - end - - def test_include_QUERY_STRING - env = { - 'PATH_INFO' => '/foo', - 'QUERY_STRING' => 'bar', - } - assert_equal "/foo?bar", Rails.application.send(:build_original_fullpath, env) - end - end -end diff --git a/railties/test/application/rake_test.rb b/railties/test/application/rake_test.rb index 2bff21dae5..0648b11813 100644 --- a/railties/test/application/rake_test.rb +++ b/railties/test/application/rake_test.rb @@ -99,7 +99,7 @@ module ApplicationTests end def test_code_statistics_sanity - assert_match "Code LOC: 5 Test LOC: 0 Code to Test Ratio: 1:0.0", + assert_match "Code LOC: 7 Test LOC: 0 Code to Test Ratio: 1:0.0", Dir.chdir(app_path){ `rake stats` } end diff --git a/railties/test/application/test_runner_test.rb b/railties/test/application/test_runner_test.rb index a12f3cfc24..c122b315c0 100644 --- a/railties/test/application/test_runner_test.rb +++ b/railties/test/application/test_runner_test.rb @@ -7,7 +7,6 @@ module ApplicationTests def setup build_app - ENV['RAILS_ENV'] = nil create_schema end @@ -55,7 +54,7 @@ module ApplicationTests create_test_file :models, 'foo' create_test_file :models, 'bar' create_test_file :controllers, 'foobar_controller' - run_test_models_command.tap do |output| + run_test_command("test/models").tap do |output| assert_match "FooTest", output assert_match "BarTest", output assert_match "2 runs, 2 assertions, 0 failures", output @@ -66,7 +65,7 @@ module ApplicationTests create_test_file :helpers, 'foo_helper' create_test_file :helpers, 'bar_helper' create_test_file :controllers, 'foobar_controller' - run_test_helpers_command.tap do |output| + run_test_command("test/helpers").tap do |output| assert_match "FooHelperTest", output assert_match "BarHelperTest", output assert_match "2 runs, 2 assertions, 0 failures", output @@ -74,6 +73,7 @@ module ApplicationTests end def test_run_units + skip "we no longer have the concept of unit tests. Just different directories..." create_test_file :models, 'foo' create_test_file :helpers, 'bar_helper' create_test_file :unit, 'baz_unit' @@ -90,7 +90,7 @@ module ApplicationTests create_test_file :controllers, 'foo_controller' create_test_file :controllers, 'bar_controller' create_test_file :models, 'foo' - run_test_controllers_command.tap do |output| + run_test_command("test/controllers").tap do |output| assert_match "FooControllerTest", output assert_match "BarControllerTest", output assert_match "2 runs, 2 assertions, 0 failures", output @@ -101,7 +101,7 @@ module ApplicationTests create_test_file :mailers, 'foo_mailer' create_test_file :mailers, 'bar_mailer' create_test_file :models, 'foo' - run_test_mailers_command.tap do |output| + run_test_command("test/mailers").tap do |output| assert_match "FooMailerTest", output assert_match "BarMailerTest", output assert_match "2 runs, 2 assertions, 0 failures", output @@ -112,7 +112,7 @@ module ApplicationTests create_test_file :jobs, 'foo_job' create_test_file :jobs, 'bar_job' create_test_file :models, 'foo' - run_test_jobs_command.tap do |output| + run_test_command("test/jobs").tap do |output| assert_match "FooJobTest", output assert_match "BarJobTest", output assert_match "2 runs, 2 assertions, 0 failures", output @@ -120,6 +120,7 @@ module ApplicationTests end def test_run_functionals + skip "we no longer have the concept of functional tests. Just different directories..." create_test_file :mailers, 'foo_mailer' create_test_file :controllers, 'bar_controller' create_test_file :functional, 'baz_functional' @@ -135,7 +136,7 @@ module ApplicationTests def test_run_integration create_test_file :integration, 'foo_integration' create_test_file :models, 'foo' - run_test_integration_command.tap do |output| + run_test_command("test/integration").tap do |output| assert_match "FooIntegration", output assert_match "1 runs, 1 assertions, 0 failures", output end @@ -165,7 +166,7 @@ module ApplicationTests end RUBY - run_test_command('test/unit/chu_2_koi_test.rb test_rikka').tap do |output| + run_test_command('-n test_rikka test/unit/chu_2_koi_test.rb').tap do |output| assert_match "Rikka", output assert_no_match "Sanae", output end @@ -186,7 +187,7 @@ module ApplicationTests end RUBY - run_test_command('test/unit/chu_2_koi_test.rb /rikka/').tap do |output| + run_test_command('-p rikka test/unit/chu_2_koi_test.rb').tap do |output| assert_match "Rikka", output assert_no_match "Sanae", output end @@ -194,18 +195,18 @@ module ApplicationTests def test_load_fixtures_when_running_test_suites create_model_with_fixture - suites = [:models, :helpers, [:units, :unit], :controllers, :mailers, - [:functionals, :functional], :integration] + suites = [:models, :helpers, :controllers, :mailers, :integration] suites.each do |suite, directory| directory ||= suite create_fixture_test directory - assert_match "3 users", run_task(["test:#{suite}"]) + assert_match "3 users", run_test_command("test/#{suite}") Dir.chdir(app_path) { FileUtils.rm_f "test/#{directory}" } end end def test_run_with_model + skip "These feel a bit odd. Not sure we should keep supporting them." create_model_with_fixture create_fixture_test 'models', 'user' assert_match "3 users", run_task(["test models/user"]) @@ -213,6 +214,7 @@ module ApplicationTests end def test_run_different_environment_using_env_var + skip "no longer possible. Running tests in a different environment should be explicit" app_file 'test/unit/env_test.rb', <<-RUBY require 'test_helper' @@ -227,7 +229,7 @@ module ApplicationTests assert_match "development", run_test_command('test/unit/env_test.rb') end - def test_run_different_environment_using_e_tag + def test_run_different_environment env = "development" app_file 'test/unit/env_test.rb', <<-RUBY require 'test_helper' @@ -239,7 +241,7 @@ module ApplicationTests end RUBY - assert_match env, run_test_command("test/unit/env_test.rb RAILS_ENV=#{env}") + assert_match env, run_test_command("-e #{env} test/unit/env_test.rb") end def test_generated_scaffold_works_with_rails_test @@ -248,17 +250,8 @@ module ApplicationTests end private - def run_task(tasks) - Dir.chdir(app_path) { `bundle exec rake #{tasks.join ' '}` } - end - def run_test_command(arguments = 'test/unit/test_test.rb') - run_task ['test', arguments] - end - %w{ mailers models helpers units controllers functionals integration jobs }.each do |type| - define_method("run_test_#{type}_command") do - run_task ["test:#{type}"] - end + Dir.chdir(app_path) { `bin/rails t #{arguments}` } end def create_model_with_fixture diff --git a/railties/test/application/test_test.rb b/railties/test/application/test_test.rb index c7132837b1..61652e5052 100644 --- a/railties/test/application/test_test.rb +++ b/railties/test/application/test_test.rb @@ -65,6 +65,7 @@ module ApplicationTests output = run_test_file('unit/failing_test.rb', env: { "BACKTRACE" => "1" }) assert_match %r{/app/test/unit/failing_test\.rb}, output + assert_match %r{/app/test/unit/failing_test\.rb:4}, output end test "ruby schema migrations" do @@ -300,23 +301,7 @@ Expected: ["id", "name"] end def run_test_file(name, options = {}) - ruby '-Itest', "#{app_path}/test/#{name}", options.deep_merge(env: {"RAILS_ENV" => "test"}) - end - - def ruby(*args) - options = args.extract_options! - env = options.fetch(:env, {}) - env["RUBYLIB"] = $:.join(':') - - Dir.chdir(app_path) do - `#{env_string(env)} #{Gem.ruby} #{args.join(' ')} 2>&1` - end - end - - def env_string(variables) - variables.map do |key, value| - "#{key}='#{value}'" - end.join " " + Dir.chdir(app_path) { `bin/rails test "#{app_path}/test/#{name}" 2>&1` } end end end diff --git a/railties/test/application/url_generation_test.rb b/railties/test/application/url_generation_test.rb index ef16ab56ed..894e18cb39 100644 --- a/railties/test/application/url_generation_test.rb +++ b/railties/test/application/url_generation_test.rb @@ -42,5 +42,18 @@ module ApplicationTests get "/" assert_equal "/", last_response.body end + + def test_routes_know_the_relative_root + boot_rails + require "rails" + require "action_controller/railtie" + require "action_view/railtie" + + relative_url = '/hello' + ENV["RAILS_RELATIVE_URL_ROOT"] = relative_url + app = Class.new(Rails::Application) + assert_equal relative_url, app.routes.relative_url_root + ENV["RAILS_RELATIVE_URL_ROOT"] = nil + end end end diff --git a/railties/test/generators/actions_test.rb b/railties/test/generators/actions_test.rb index c6de2c1fb9..c0b88089b3 100644 --- a/railties/test/generators/actions_test.rb +++ b/railties/test/generators/actions_test.rb @@ -219,6 +219,30 @@ class ActionsTest < Rails::Generators::TestCase assert_file 'config/routes.rb', /#{Regexp.escape(route_command)}/ end + def test_route_should_add_data_with_an_new_line + run_generator + action :route, "root 'welcome#index'" + route_path = File.expand_path("config/routes.rb", destination_root) + content = File.read(route_path) + + # Remove all of the comments and blank lines from the routes file + content.gsub!(/^ \#.*\n/, '') + content.gsub!(/^\n/, '') + + File.open(route_path, "wb") { |file| file.write(content) } + assert_file "config/routes.rb", /\.routes\.draw do\n root 'welcome#index'\nend\n\z/ + + action :route, "resources :product_lines" + + routes = <<-F +Rails.application.routes.draw do + resources :product_lines + root 'welcome#index' +end +F + assert_file "config/routes.rb", routes + end + def test_readme run_generator Rails::Generators::AppGenerator.expects(:source_root).times(2).returns(destination_root) diff --git a/railties/test/generators/app_generator_test.rb b/railties/test/generators/app_generator_test.rb index 4c5dd70a88..282e8cc4f9 100644 --- a/railties/test/generators/app_generator_test.rb +++ b/railties/test/generators/app_generator_test.rb @@ -18,6 +18,7 @@ DEFAULT_APP_FILES = %w( app/mailers app/models app/models/concerns + app/jobs app/views/layouts bin/bundle bin/rails @@ -67,6 +68,11 @@ class AppGeneratorTest < Rails::Generators::TestCase assert_file("app/assets/javascripts/application.js") end + def test_application_job_file_present + run_generator + assert_file("app/jobs/application_job.rb") + end + def test_invalid_application_name_raises_an_error content = capture(:stderr){ run_generator [File.join(destination_root, "43-things")] } assert_equal "Invalid application name 43-things. Please give a name which does not start with numbers.\n", content @@ -443,13 +449,6 @@ class AppGeneratorTest < Rails::Generators::TestCase end end - def test_inclusion_of_method_source - run_generator - assert_file "Gemfile" do |content| - assert_gem 'method_source' - end - end - def test_template_from_dir_pwd FileUtils.cd(Rails.root) assert_match(/It works from file!/, run_generator([destination_root, "-m", "lib/template.rb"])) diff --git a/railties/test/generators/job_generator_test.rb b/railties/test/generators/job_generator_test.rb index a9e0cea94f..7fd8f2062f 100644 --- a/railties/test/generators/job_generator_test.rb +++ b/railties/test/generators/job_generator_test.rb @@ -7,14 +7,14 @@ class JobGeneratorTest < Rails::Generators::TestCase def test_job_skeleton_is_created run_generator ["refresh_counters"] assert_file "app/jobs/refresh_counters_job.rb" do |job| - assert_match(/class RefreshCountersJob < ActiveJob::Base/, job) + assert_match(/class RefreshCountersJob < ApplicationJob/, job) end end def test_job_queue_param run_generator ["refresh_counters", "--queue", "important"] assert_file "app/jobs/refresh_counters_job.rb" do |job| - assert_match(/class RefreshCountersJob < ActiveJob::Base/, job) + assert_match(/class RefreshCountersJob < ApplicationJob/, job) assert_match(/queue_as :important/, job) end end @@ -22,7 +22,7 @@ class JobGeneratorTest < Rails::Generators::TestCase def test_job_namespace run_generator ["admin/refresh_counters", "--queue", "admin"] assert_file "app/jobs/admin/refresh_counters_job.rb" do |job| - assert_match(/class Admin::RefreshCountersJob < ActiveJob::Base/, job) + assert_match(/class Admin::RefreshCountersJob < ApplicationJob/, job) assert_match(/queue_as :admin/, job) end end diff --git a/railties/test/generators/mailer_generator_test.rb b/railties/test/generators/mailer_generator_test.rb index 584e7a82aa..f01e8cd2d9 100644 --- a/railties/test/generators/mailer_generator_test.rb +++ b/railties/test/generators/mailer_generator_test.rb @@ -47,13 +47,13 @@ class MailerGeneratorTest < Rails::Generators::TestCase assert_match(/test "bar"/, test) end assert_file "test/mailers/previews/notifier_mailer_preview.rb" do |preview| - assert_match(/\# Preview all emails at http:\/\/localhost\:3000\/rails\/mailers\/notifier/, preview) + assert_match(/\# Preview all emails at http:\/\/localhost\:3000\/rails\/mailers\/notifier_mailer/, preview) assert_match(/class NotifierMailerPreview < ActionMailer::Preview/, preview) - assert_match(/\# Preview this email at http:\/\/localhost\:3000\/rails\/mailers\/notifier\/foo/, preview) + assert_match(/\# Preview this email at http:\/\/localhost\:3000\/rails\/mailers\/notifier_mailer\/foo/, preview) assert_instance_method :foo, preview do |foo| assert_match(/NotifierMailer.foo/, foo) end - assert_match(/\# Preview this email at http:\/\/localhost\:3000\/rails\/mailers\/notifier\/bar/, preview) + assert_match(/\# Preview this email at http:\/\/localhost\:3000\/rails\/mailers\/notifier_mailer\/bar/, preview) assert_instance_method :bar, preview do |bar| assert_match(/NotifierMailer.bar/, bar) end @@ -129,9 +129,9 @@ class MailerGeneratorTest < Rails::Generators::TestCase assert_match(/en\.farm\.animal_mailer\.moos\.subject/, mailer) end assert_file "test/mailers/previews/farm/animal_mailer_preview.rb" do |preview| - assert_match(/\# Preview all emails at http:\/\/localhost\:3000\/rails\/mailers\/farm\/animal/, preview) + assert_match(/\# Preview all emails at http:\/\/localhost\:3000\/rails\/mailers\/farm\/animal_mailer/, preview) assert_match(/class Farm::AnimalMailerPreview < ActionMailer::Preview/, preview) - assert_match(/\# Preview this email at http:\/\/localhost\:3000\/rails\/mailers\/farm\/animal\/moos/, preview) + assert_match(/\# Preview this email at http:\/\/localhost\:3000\/rails\/mailers\/farm\/animal_mailer\/moos/, preview) end assert_file "app/views/farm/animal_mailer/moos.text.erb" assert_file "app/views/farm/animal_mailer/moos.html.erb" diff --git a/railties/test/generators/model_generator_test.rb b/railties/test/generators/model_generator_test.rb index 17a13fbf1f..abd3ff50a4 100644 --- a/railties/test/generators/model_generator_test.rb +++ b/railties/test/generators/model_generator_test.rb @@ -319,6 +319,16 @@ class ModelGeneratorTest < Rails::Generators::TestCase assert_no_file "test/fixtures/accounts.yml" end + def test_fixture_without_pluralization + original_pluralize_table_name = ActiveRecord::Base.pluralize_table_names + ActiveRecord::Base.pluralize_table_names = false + run_generator + assert_generated_fixture("test/fixtures/account.yml", + {"one"=>{"name"=>"MyString", "age"=>1}, "two"=>{"name"=>"MyString", "age"=>1}}) + ensure + ActiveRecord::Base.pluralize_table_names = original_pluralize_table_name + end + def test_check_class_collision content = capture(:stderr){ run_generator ["object"] } assert_match(/The name 'Object' is either already used in your application or reserved/, content) diff --git a/railties/test/rails_info_controller_test.rb b/railties/test/rails_info_controller_test.rb index d87b51d852..c51503c2b7 100644 --- a/railties/test/rails_info_controller_test.rb +++ b/railties/test/rails_info_controller_test.rb @@ -56,26 +56,26 @@ class InfoControllerTest < ActionController::TestCase test "info controller returns exact matches" do exact_count = -> { JSON(response.body)['exact'].size } - get :routes, path: 'rails/info/route' + get :routes, params: { path: 'rails/info/route' } assert exact_count.call == 0, 'should not match incomplete routes' - get :routes, path: 'rails/info/routes' + get :routes, params: { path: 'rails/info/routes' } assert exact_count.call == 1, 'should match complete routes' - - get :routes, path: 'rails/info/routes.html' + + get :routes, params: { path: 'rails/info/routes.html' } assert exact_count.call == 1, 'should match complete routes with optional parts' end test "info controller returns fuzzy matches" do fuzzy_count = -> { JSON(response.body)['fuzzy'].size } - get :routes, path: 'rails/info' + get :routes, params: { path: 'rails/info' } assert fuzzy_count.call == 2, 'should match incomplete routes' - get :routes, path: 'rails/info/routes' + get :routes, params: { path: 'rails/info/routes' } assert fuzzy_count.call == 1, 'should match complete routes' - - get :routes, path: 'rails/info/routes.html' + + get :routes, params: { path: 'rails/info/routes.html' } assert fuzzy_count.call == 0, 'should match optional parts of route literally' end end diff --git a/railties/test/railties/engine_test.rb b/railties/test/railties/engine_test.rb index 6185742cc1..79bd7a8241 100644 --- a/railties/test/railties/engine_test.rb +++ b/railties/test/railties/engine_test.rb @@ -1155,10 +1155,10 @@ YAML assert_equal "App's bar partial", last_response.body.strip get("/assets/foo.js") - assert_equal "// Bukkit's foo js\n;", last_response.body.strip + assert_equal "// Bukkit's foo js", last_response.body.strip get("/assets/bar.js") - assert_equal "// App's bar js\n;", last_response.body.strip + assert_equal "// App's bar js", last_response.body.strip # ensure that railties are not added twice railties = Rails.application.send(:ordered_railties).map(&:class) diff --git a/railties/test/test_unit/reporter_test.rb b/railties/test/test_unit/reporter_test.rb new file mode 100644 index 0000000000..77883612f5 --- /dev/null +++ b/railties/test/test_unit/reporter_test.rb @@ -0,0 +1,74 @@ +require 'abstract_unit' +require 'rails/test_unit/reporter' + +class TestUnitReporterTest < ActiveSupport::TestCase + class ExampleTest < Minitest::Test + def woot; end + end + + setup do + @output = StringIO.new + @reporter = Rails::TestUnitReporter.new @output + end + + test "prints rerun snippet to run a single failed test" do + @reporter.record(failed_test) + @reporter.report + + assert_match %r{^bin/rails test .*test/test_unit/reporter_test.rb:6$}, @output.string + assert_rerun_snippet_count 1 + end + + test "prints rerun snippet for every failed test" do + @reporter.record(failed_test) + @reporter.record(failed_test) + @reporter.record(failed_test) + @reporter.report + + assert_rerun_snippet_count 3 + end + + test "does not print snippet for successful and skipped tests" do + @reporter.record(passing_test) + @reporter.record(skipped_test) + @reporter.report + assert_rerun_snippet_count 0 + end + + test "prints rerun snippet for skipped tests if run in verbose mode" do + verbose = Rails::TestUnitReporter.new @output, verbose: true + verbose.record(skipped_test) + verbose.report + + assert_rerun_snippet_count 1 + end + + private + def assert_rerun_snippet_count(snippet_count) + assert_equal snippet_count, @output.string.scan(%r{^bin/rails test }).size + end + + def failed_test + ft = ExampleTest.new(:woot) + ft.failures << begin + raise Minitest::Assertion, "boo" + rescue Minitest::Assertion => e + e + end + ft + end + + def passing_test + ExampleTest.new(:woot) + end + + def skipped_test + st = ExampleTest.new(:woot) + st.failures << begin + raise Minitest::Skip + rescue Minitest::Assertion => e + e + end + st + end +end diff --git a/railties/test/test_unit/runner_test.rb b/railties/test/test_unit/runner_test.rb new file mode 100644 index 0000000000..9ea8b2c114 --- /dev/null +++ b/railties/test/test_unit/runner_test.rb @@ -0,0 +1,111 @@ +require 'abstract_unit' +require 'env_helpers' +require 'rails/test_unit/runner' + +class TestUnitTestRunnerTest < ActiveSupport::TestCase + include EnvHelpers + + setup do + @options = Rails::TestRunner::Options + end + + test "shows the filtered backtrace by default" do + options = @options.parse([]) + assert_not options[:backtrace] + end + + test "has --backtrace (-b) option to show the full backtrace" do + options = @options.parse(["-b"]) + assert options[:backtrace] + + options = @options.parse(["--backtrace"]) + assert options[:backtrace] + end + + test "show full backtrace using BACKTRACE environment variable" do + switch_env "BACKTRACE", "true" do + options = @options.parse([]) + assert options[:backtrace] + end + end + + test "tests run in the test environment by default" do + options = @options.parse([]) + assert_equal "test", options[:environment] + end + + test "can run in a specific environment" do + options = @options.parse(["-e development"]) + assert_equal "development", options[:environment] + end + + test "parse the filename and line" do + file = "test/test_unit/runner_test.rb" + absolute_file = File.expand_path __FILE__ + options = @options.parse(["#{file}:20"]) + assert_equal absolute_file, options[:filename] + assert_equal 20, options[:line] + + options = @options.parse(["#{file}:"]) + assert_equal [absolute_file], options[:patterns] + assert_nil options[:line] + + options = @options.parse([file]) + assert_equal [absolute_file], options[:patterns] + assert_nil options[:line] + end + + test "find_method on same file" do + options = @options.parse(["#{__FILE__}:#{__LINE__}"]) + runner = Rails::TestRunner.new(options) + assert_equal "test_find_method_on_same_file", runner.find_method + end + + test "find_method on a different file" do + options = @options.parse(["foobar.rb:#{__LINE__}"]) + runner = Rails::TestRunner.new(options) + assert_nil runner.find_method + end + + test "run all tests in a directory" do + options = @options.parse([__dir__]) + + assert_equal ["#{__dir__}/**/*_test.rb"], options[:patterns] + assert_nil options[:filename] + assert_nil options[:line] + end + + test "run multiple folders" do + application_dir = File.expand_path("#{__dir__}/../application") + + options = @options.parse([__dir__, application_dir]) + + assert_equal ["#{__dir__}/**/*_test.rb", "#{application_dir}/**/*_test.rb"], options[:patterns] + assert_nil options[:filename] + assert_nil options[:line] + + runner = Rails::TestRunner.new(options) + assert runner.test_files.size > 0 + end + + test "run multiple files and run one file by line" do + line = __LINE__ + absolute_file = File.expand_path(__FILE__) + options = @options.parse([__dir__, "#{__FILE__}:#{line}"]) + + assert_equal ["#{__dir__}/**/*_test.rb"], options[:patterns] + assert_equal absolute_file, options[:filename] + assert_equal line, options[:line] + + runner = Rails::TestRunner.new(options) + assert_equal [absolute_file], runner.test_files, 'Only returns the file that running by line' + end + + test "running multiple files passing line number" do + line = __LINE__ + options = @options.parse(["foobar.rb:8", "#{__FILE__}:#{line}"]) + + assert_equal File.expand_path(__FILE__), options[:filename], 'Returns the last file' + assert_equal line, options[:line] + end +end |