Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add colored output to the new test reporter. #22756

Merged
merged 1 commit into from
Dec 23, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions railties/lib/rails/test_unit/minitest_plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
40 changes: 39 additions & 1 deletion railties/lib/rails/test_unit/reporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand All @@ -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
38 changes: 35 additions & 3 deletions railties/test/test_unit/reporter_test.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'abstract_unit'
require 'rails/test_unit/reporter'
require 'minitest/mock'

class TestUnitReporterTest < ActiveSupport::TestCase
class ExampleTest < Minitest::Test
Expand Down Expand Up @@ -61,15 +62,15 @@ 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

test "outputs errors inline" do
@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

Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -145,6 +176,7 @@ def skipped_test
rescue Minitest::Assertion => e
e
end
st.time = 10
st
end
end