lib/fuubar.rb in fuubar-2.2.0 vs lib/fuubar.rb in fuubar-2.3.0.beta1

- old
+ new

@@ -1,47 +1,54 @@ # frozen_string_literal: true + require 'rspec/core' require 'rspec/core/formatters/base_text_formatter' require 'ruby-progressbar' require 'fuubar/output' RSpec.configuration.add_setting :fuubar_progress_bar_options, :default => {} class Fuubar < RSpec::Core::Formatters::BaseTextFormatter DEFAULT_PROGRESS_BAR_OPTIONS = { :format => ' %c/%C |%w>%i| %e ' }.freeze - RSpec::Core::Formatters.register self, :start, + RSpec::Core::Formatters.register self, + :start, :message, :example_passed, :example_pending, :example_failed, + :example_started, + :example_finished, :dump_failures - attr_accessor :progress, + attr_accessor :example_tick_thread, + :example_tick_lock, + :progress, :passed_count, :pending_count, :failed_count def initialize(*args) super + self.example_tick_lock = Mutex.new self.progress = ProgressBar.create( - DEFAULT_PROGRESS_BAR_OPTIONS. - merge(:throttle_rate => continuous_integration? ? 1.0 : nil). - merge(:total => 0, - :output => output, - :autostart => false) + DEFAULT_PROGRESS_BAR_OPTIONS + .merge(:throttle_rate => continuous_integration? ? 1.0 : nil) + .merge(:total => 0, + :output => output, + :autostart => false) ) end def start(notification) - progress_bar_options = DEFAULT_PROGRESS_BAR_OPTIONS. - merge(:throttle_rate => continuous_integration? ? 1.0 : nil). - merge(configuration.fuubar_progress_bar_options). - merge(:total => notification.count, - :output => output, - :autostart => false) + progress_bar_options = DEFAULT_PROGRESS_BAR_OPTIONS + .merge(:throttle_rate => continuous_integration? ? 1.0 : nil) + .merge(configuration.fuubar_progress_bar_options) + .merge(:total => notification.count, + :output => output, + :autostart => false) self.progress = ProgressBar.create(progress_bar_options) self.passed_count = 0 self.pending_count = 0 self.failed_count = 0 @@ -49,10 +56,18 @@ super with_current_color { progress.start } end + def example_started(notification) + self.example_tick_thread = start_tick_thread(notification) + end + + def example_finished(_notification) + example_tick_thread.kill + end + def example_passed(_notification) self.passed_count += 1 increment end @@ -72,10 +87,16 @@ output.puts increment end + def example_tick(_notification) + example_tick_lock.synchronize do + refresh + end + end + def message(notification) if progress.respond_to? :log progress.log(notification.message) else super @@ -97,10 +118,14 @@ def increment with_current_color { progress.increment } end + def refresh + with_current_color { progress.refresh } + end + def with_current_color output.print "\e[#{color_code_for(current_color)}m" if color_enabled? yield output.print "\e[0m" if color_enabled? end @@ -127,8 +152,18 @@ RSpec.configuration end def continuous_integration? @continuous_integration ||= \ - ![nil, '', 'false'].include?(ENV['CONTINUOUS_INTEGRATION']) + [nil, '', 'false'].exclude?(ENV['CONTINUOUS_INTEGRATION']) + end + + def start_tick_thread(notification) + Thread.new do + loop do + sleep(1) + + example_tick(notification) + end + end end end