diff options
author | Lucas Mazza <lucastmazza@gmail.com> | 2015-12-21 22:13:00 -0200 |
---|---|---|
committer | Lucas Mazza <lucastmazza@gmail.com> | 2015-12-22 10:58:05 -0200 |
commit | 7fa3a0c90a02d198d6058fb29507564539abdd6a (patch) | |
tree | 7f9d000f6b07ff994efde70f7553d7473f5136f1 /railties | |
parent | 2de2263118d6763c11a02db05079a5b7a97a0a34 (diff) | |
download | rails-7fa3a0c90a02d198d6058fb29507564539abdd6a.tar.gz rails-7fa3a0c90a02d198d6058fb29507564539abdd6a.tar.bz2 rails-7fa3a0c90a02d198d6058fb29507564539abdd6a.zip |
Add colored output to the new test reporter.
Diffstat (limited to 'railties')
-rw-r--r-- | railties/lib/rails/test_unit/minitest_plugin.rb | 8 | ||||
-rw-r--r-- | railties/lib/rails/test_unit/reporter.rb | 40 | ||||
-rw-r--r-- | railties/test/test_unit/reporter_test.rb | 38 |
3 files changed, 82 insertions, 4 deletions
diff --git a/railties/lib/rails/test_unit/minitest_plugin.rb b/railties/lib/rails/test_unit/minitest_plugin.rb index d39d2f32bf..c1ea307f93 100644 --- a/railties/lib/rails/test_unit/minitest_plugin.rb +++ b/railties/lib/rails/test_unit/minitest_plugin.rb @@ -49,6 +49,12 @@ module Minitest 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 @@ module Minitest # 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 diff --git a/railties/lib/rails/test_unit/reporter.rb b/railties/lib/rails/test_unit/reporter.rb index 695c67756b..ba80dad2e3 100644 --- a/railties/lib/rails/test_unit/reporter.rb +++ b/railties/lib/rails/test_unit/reporter.rb @@ -6,13 +6,34 @@ module Rails 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 @@ module Rails 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 @@ module Rails 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 diff --git a/railties/test/test_unit/reporter_test.rb b/railties/test/test_unit/reporter_test.rb index 3ff7cd18cd..2d08d4ec30 100644 --- a/railties/test/test_unit/reporter_test.rb +++ b/railties/test/test_unit/reporter_test.rb @@ -1,5 +1,6 @@ require 'abstract_unit' require 'rails/test_unit/reporter' +require 'minitest/mock' class TestUnitReporterTest < ActiveSupport::TestCase class ExampleTest < Minitest::Test @@ -61,7 +62,7 @@ class TestUnitReporterTest < ActiveSupport::TestCase @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 @@ class TestUnitReporterTest < ActiveSupport::TestCase @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 @@ class TestUnitReporterTest < ActiveSupport::TestCase 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 @@ class TestUnitReporterTest < ActiveSupport::TestCase 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 @@ class TestUnitReporterTest < ActiveSupport::TestCase rescue Minitest::Assertion => e e end + st.time = 10 st end end |