1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/railties/lib/minitest/rails_plugin.rb
Kasper Timm Hansen 0d72489b2a * Don't eagerly require Rails' minitest plugin.
By making the Rails minitest behave like a standard minitest plugin
we're much more likely to not break when people use other minitest
plugins. Like minitest-focus and pride.

To do this, we need to behave like minitest: require files up front
and then perform the plugin behavior via the at_exit hook.
This also saves us a fair bit of wrangling with test file loading.

Finally, since the environment and warnings options have to be applied
as early as possible, and since minitest loads plugins at_exit, they
have to be moved to the test command.

* Don't expect the root method.

It's likely this worked because we eagerly loaded the Rails minitest plugin
and that somehow defined a root method on `Rails`.

* Assign a backtrace to failed exceptions.

Otherwise Minitest pukes when attempting to filter the backtrace (which
Rails' backtrace cleaner then removes).

Means the exception message test has to be revised too.

This is likely caused by the rails minitest plugin now being loaded for
these tests and assigning a default backtrace cleaner.
2017-07-10 20:40:16 +02:00

49 lines
1.7 KiB
Ruby

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