diff options
author | Kasper Timm Hansen <kaspth@gmail.com> | 2015-12-23 23:07:14 +0100 |
---|---|---|
committer | Kasper Timm Hansen <kaspth@gmail.com> | 2015-12-23 23:07:14 +0100 |
commit | 552e4e6b9e3d6ae7a911aa496422c6c598813d5d (patch) | |
tree | a439dd6a97fdeb8a2c1d1b792aaa920a5be48e9d /railties/lib | |
parent | 1e72a9865c9ec1516ce477bb0c1756b3b0a94b96 (diff) | |
parent | 7fa3a0c90a02d198d6058fb29507564539abdd6a (diff) | |
download | rails-552e4e6b9e3d6ae7a911aa496422c6c598813d5d.tar.gz rails-552e4e6b9e3d6ae7a911aa496422c6c598813d5d.tar.bz2 rails-552e4e6b9e3d6ae7a911aa496422c6c598813d5d.zip |
Merge pull request #22756 from lucasmazza/lm-test-reporter-colored-output
Add colored output to the new test reporter.
Diffstat (limited to 'railties/lib')
-rw-r--r-- | railties/lib/rails/test_unit/minitest_plugin.rb | 8 | ||||
-rw-r--r-- | railties/lib/rails/test_unit/reporter.rb | 40 |
2 files changed, 47 insertions, 1 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 |