提交 552e4e6b 编写于 作者: K Kasper Timm Hansen

Merge pull request #22756 from lucasmazza/lm-test-reporter-colored-output

Add colored output to the new test reporter.
......@@ -49,6 +49,12 @@ def self.plugin_rails_options(opts, options)
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
options[:patterns] = opts.order!
end
......@@ -80,6 +86,8 @@ def self.plugin_rails_init(options)
# Disable the extra failure output after a run, unless output is deferred.
self.hide_aggregated_results = options[:output_inline]
self.reporter.reporters.clear
self.reporter << SummaryReporter.new(options[:io], options)
self.reporter << ::Rails::TestUnitReporter.new(options[:io], options)
end
......
......@@ -6,13 +6,34 @@ class TestUnitReporter < Minitest::StatisticsReporter
class_attribute :executable
self.executable = "bin/rails test"
COLOR_CODES_FOR_RESULTS = {
"." => :green,
"E" => :red,
"F" => :red,
"S" => :yellow
}
COLOR_CODES = {
red: 31,
green: 32,
yellow: 33,
blue: 34
}
def record(result)
super
color = COLOR_CODES_FOR_RESULTS[result.result_code]
if options[:verbose]
io.puts color(format_line(result), color)
else
io.print color(result.result_code, color)
end
if output_inline? && result.failure && (!result.skipped? || options[:verbose])
io.puts
io.puts
io.puts format_failures(result)
io.puts format_failures(result).map { |line| color(line, :red) }
io.puts
io.puts format_rerun_snippet(result)
io.puts
......@@ -56,6 +77,10 @@ def fail_fast?
options[:fail_fast]
end
def format_line(result)
"%s#%s = %.2f s = %s" % [result.class, result.name, result.time, result.result_code]
end
def format_failures(result)
result.failures.map do |failure|
"#{failure.result_label}:\n#{result.class}##{result.name}:\n#{failure.message}\n"
......@@ -76,5 +101,18 @@ def format_rerun_snippet(result)
def app_root
@app_root ||= defined?(ENGINE_ROOT) ? ENGINE_ROOT : Rails.root
end
def colored_output?
options[:color] && io.respond_to?(:tty?) && io.tty?
end
def color(string, color)
if colored_output?
color = COLOR_CODES[color]
"\e[#{color}m#{string}\e[0m"
else
string
end
end
end
end
require 'abstract_unit'
require 'rails/test_unit/reporter'
require 'minitest/mock'
class TestUnitReporterTest < ActiveSupport::TestCase
class ExampleTest < Minitest::Test
......@@ -61,7 +62,7 @@ def woot; end
@reporter.record(failed_test)
@reporter.report
expect = %r{\A\n\nFailure:\nTestUnitReporterTest::ExampleTest#woot:\nboo\n\nbin/rails test test/test_unit/reporter_test.rb:\d+\n\n\z}
expect = %r{\AF\n\nFailure:\nTestUnitReporterTest::ExampleTest#woot:\nboo\n\nbin/rails test test/test_unit/reporter_test.rb:\d+\n\n\z}
assert_match expect, @output.string
end
......@@ -69,7 +70,7 @@ def woot; end
@reporter.record(errored_test)
@reporter.report
expect = %r{\A\n\nError:\nTestUnitReporterTest::ExampleTest#woot:\nArgumentError: wups\n No backtrace\n\nbin/rails test .*test/test_unit/reporter_test.rb:6\n\n\z}
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}
assert_match expect, @output.string
end
......@@ -78,7 +79,7 @@ def woot; end
verbose.record(skipped_test)
verbose.report
expect = %r{\A\n\nSkipped:\nTestUnitReporterTest::ExampleTest#woot:\nskipchurches, misstemples\n\nbin/rails test test/test_unit/reporter_test.rb:\d+\n\n\z}
expect = %r{\ATestUnitReporterTest::ExampleTest#woot = 10\.00 s = S\n\n\nSkipped:\nTestUnitReporterTest::ExampleTest#woot:\nskipchurches, misstemples\n\nbin/rails test test/test_unit/reporter_test.rb:\d+\n\n\z}
assert_match expect, @output.string
end
......@@ -113,6 +114,36 @@ def woot; end
assert_no_match 'Failed tests:', @output.string
end
test "outputs colored passing results" do
@output.stub(:tty?, true) do
colored = Rails::TestUnitReporter.new @output, color: true, output_inline: true
colored.record(passing_test)
expect = %r{\e\[32m\.\e\[0m}
assert_match expect, @output.string
end
end
test "outputs colored skipped results" do
@output.stub(:tty?, true) do
colored = Rails::TestUnitReporter.new @output, color: true, output_inline: true
colored.record(skipped_test)
expect = %r{\e\[33mS\e\[0m}
assert_match expect, @output.string
end
end
test "outputs colored failed results" do
@output.stub(:tty?, true) do
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}
assert_match expected, @output.string
end
end
private
def assert_rerun_snippet_count(snippet_count)
assert_equal snippet_count, @output.string.scan(%r{^bin/rails test }).size
......@@ -145,6 +176,7 @@ def skipped_test
rescue Minitest::Assertion => e
e
end
st.time = 10
st
end
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册