Skip to content

Instantly share code, notes, and snippets.

@nevans
Created December 1, 2008 05:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nevans/30642 to your computer and use it in GitHub Desktop.
Save nevans/30642 to your computer and use it in GitHub Desktop.
compact progressbar for rspec
require 'spec/runner/formatter/base_text_formatter'
require 'progressbar'
module Spec
module Runner
module Formatter
class CompactProgressBarFormatter < BaseTextFormatter
# Threshold for slow specs, in seconds.
# Anything that takes longer than this will be printed out
THRESHOLD = 0.25
def start(example_count)
@error_state = :all_passing
@pbar = ProgressBar.new("#{example_count} examples", example_count, output)
end
def example_started(example)
super
@start_time = Time.now
end
def add_example_group(example_group)
super
@current_group = example_group.description
end
def example_passed(example)
print_warning_if_slow(example_group.description,
example.description,
Time.now - @start_time)
increment
end
def example_pending(example, message, pending_caller)
immediately_dump_pending(example.__full_description, message, pending_caller)
mark_error_state(:pending)
increment
end
def example_failed(example, counter, failure)
immediately_dump_failure(counter, failure)
mark_error_state(:failed)
increment
end
def start_dump
color_start
@pbar.finish
color_end
output.flush
end
def dump_failure(*args)
# no-op; we summarized failures as we were running
end
def method_missing(sym, *args)
# ignore
end
# stolen and slightly modified from BaseTextFormatter#dump_failure
def immediately_dump_failure(counter, failure)
erase_current_line
output.print "#{counter.to_s}) "
output.puts colourise("#{failure.header}\n#{failure.exception.message}", failure)
output.puts format_backtrace(failure.exception.backtrace)
output.puts
output.flush
end
# stolen and modified from BaseTextFormatter#dump_pending
def immediately_dump_pending(desc, msg, called_from)
output.puts yellow("PENDING SPEC: #{desc} (#{msg})")
output.puts " Called from #{called_from}"
output.flush
end
def increment
# TODO: colourise progress bar
color_start
@pbar.inc
color_end
output.flush
end
def color_start
return unless colour? && output_to_tty?
color_code = case @error_state
when :all_passing then "\e[32m"
when :some_pending then "\e[33m"
when :some_failed then "\e[31m"
end
output.print color_code
end
def mark_error_state(type)
@error_state = if type == :failed
:some_failed
elsif type == :pending &&
@error_state != :some_failed
:some_pending
else
@error_state
end
end
def color_end
return unless colour? && output_to_tty?
output.print "\e[0m"
end
def erase_current_line
output.print "\e[K"
end
def print_warning_if_slow(group, example, elapsed)
if elapsed > THRESHOLD
mark_error_state(:pending)
erase_current_line
output.print yellow("SLOW SPEC: #{sprintf("%.4f", elapsed)} ")
output.print " #{group} #{example}"
output.puts
end
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment