mirror of
https://github.com/pry/pry.git
synced 2022-11-09 12:35:05 -05:00
Refactor test colorization
This commit is contained in:
parent
6a9d59c6a1
commit
f4b7ddf870
1 changed files with 32 additions and 19 deletions
|
@ -8,34 +8,47 @@ puts "Ruby v#{RUBY_VERSION} (#{defined?(RUBY_ENGINE) ? RUBY_ENGINE : "ruby"}), P
|
||||||
require 'bacon'
|
require 'bacon'
|
||||||
require 'open4'
|
require 'open4'
|
||||||
|
|
||||||
# Colorize output (greeneggs (c) 2009 Michael Fleet)
|
# Colorize output (based on greeneggs (c) 2009 Michael Fleet)
|
||||||
|
# TODO: Make own gem (assigned to rking)
|
||||||
module Bacon
|
module Bacon
|
||||||
class << self
|
COLORS = {'F' => 31, 'E' => 35, 'M' => 33, '.' => 32}
|
||||||
# ANSI terminal colors
|
|
||||||
COLORS = {'F' => 31, 'E' => 35, 'M' => 33, '.' => 32}
|
|
||||||
|
|
||||||
def colorize_string(text, color)
|
module TestUnitOutput
|
||||||
"\e[1m\e[#{COLORS[color]}m#{text}\e[0m"
|
def handle_specification(name)
|
||||||
|
@use_color = Pry::Helpers::BaseHelpers.use_ansi_codes?
|
||||||
|
yield
|
||||||
end
|
end
|
||||||
|
|
||||||
def colorize_result(out)
|
def handle_requirement(description)
|
||||||
return out unless Pry::Helpers::BaseHelpers.use_ansi_codes?
|
error = yield
|
||||||
|
|
||||||
summary_color = (out =~ /0 failures, 0 errors/) ? '.' : 'F'
|
if error.empty?
|
||||||
|
print colorize_string('.')
|
||||||
out.
|
else
|
||||||
sub(/^.$/, colorize_string('\0', out)).
|
print colorize_string(error[0..0])
|
||||||
sub(/^.+\d+ failures, \d+ errors$/, colorize_string('\0', summary_color))
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def puts(*args)
|
def handle_summary
|
||||||
args.map! { |arg| arg.is_a?(String) ? colorize_result(arg) : arg }
|
puts
|
||||||
super
|
puts ErrorLog if Backtraces
|
||||||
|
|
||||||
|
out = "%d tests, %d assertions, %d failures, %d errors" %
|
||||||
|
Counter.values_at(:specifications, :requirements, :failed, :errors)
|
||||||
|
|
||||||
|
if Counter.values_at(:failed, :errors).inject(:+) > 0
|
||||||
|
puts colorize_string(out, 'F')
|
||||||
|
else
|
||||||
|
puts colorize_string(out, '.')
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def print(*args)
|
def colorize_string(text, color = nil)
|
||||||
args.map! { |arg| arg.is_a?(String) ? colorize_result(arg) : arg }
|
if @use_color
|
||||||
super
|
"\e[#{ COLORS[color || text] }m#{ text }\e[0m"
|
||||||
|
else
|
||||||
|
text
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue