Class: RspecSpinner::RspecSpinnerBase

Inherits:
Spec::Runner::Formatter::BaseTextFormatter
  • Object
show all
Defined in:
lib/rspec_spinner/base.rb

Direct Known Subclasses

Bar, Spinner

Constant Summary collapse

THRESHOLD =

Threshold for slow specs, in seconds. Anything that takes longer than this will be printed out THRESHOLD = 0.25

3.0
ERROR_STATE_COLORS =
{
  :all_passing  => "\e[32m", # green
  :some_pending => "\e[33m", # yellow
  :some_failed  => "\e[31m", # red
  :pending_fix  => "\e[34m", # blue
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options, where) ⇒ RspecSpinnerBase

Returns a new instance of RspecSpinnerBase.



14
15
16
17
# File 'lib/rspec_spinner/base.rb', line 14

def initialize(options, where)
  super
  @example_times = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args) ⇒ Object



73
74
75
# File 'lib/rspec_spinner/base.rb', line 73

def method_missing(sym, *args)
  # ignore
end

Instance Attribute Details

#currentObject (readonly)

Returns the value of attribute current.



12
13
14
# File 'lib/rspec_spinner/base.rb', line 12

def current
  @current
end

#totalObject (readonly)

Returns the value of attribute total.



12
13
14
# File 'lib/rspec_spinner/base.rb', line 12

def total
  @total
end

Instance Method Details

#dump_failure(*args) ⇒ Object



69
70
71
# File 'lib/rspec_spinner/base.rb', line 69

def dump_failure(*args)
  # no-op; we summarized failures as we were running
end

#erase_current_lineObject



129
130
131
# File 'lib/rspec_spinner/base.rb', line 129

def erase_current_line
  output.print "\e[K"
end

#example_failed(example, counter, failure) ⇒ Object



46
47
48
49
50
# File 'lib/rspec_spinner/base.rb', line 46

def example_failed(example, counter, failure)
  immediately_dump_failure(counter, failure)
  mark_error_state_failed
  increment
end

#example_passed(example) ⇒ Object



32
33
34
35
36
37
# File 'lib/rspec_spinner/base.rb', line 32

def example_passed(example)
  ex = [example_group.description, example.description, example.location, Time.now - @start_time]
  print_warning_if_slow(*ex)
  @example_times << ex
  increment
end

#example_pending(example, message, deprecated_pending_location = nil) ⇒ Object

third param is optional, because earlier versions of rspec sent only two args



40
41
42
43
44
# File 'lib/rspec_spinner/base.rb', line 40

def example_pending(example, message, deprecated_pending_location=nil)
  immediately_dump_pending(example.description, message, example.location)
  mark_error_state_pending
  increment
end

#example_started(example) ⇒ Object



27
28
29
30
# File 'lib/rspec_spinner/base.rb', line 27

def example_started(example)
  super
  @start_time = Time.now
end

#immediately_dump_failure(counter, failure) ⇒ Object

stolen and slightly modified from BaseTextFormatter#dump_failure



78
79
80
81
82
83
84
85
86
# File 'lib/rspec_spinner/base.rb', line 78

def immediately_dump_failure(counter, failure)
  erase_current_line
  output.puts
  output.print "#{counter.to_s}) "
  # Rspec 1.2.2
  output.puts colorize_failure("#{failure.header}\n#{failure.exception.message}", failure)
  output.puts format_backtrace(failure.exception.backtrace)
  output.puts
end

#immediately_dump_pending(desc, msg, location) ⇒ Object

stolen and modified from BaseTextFormatter#dump_pending



89
90
91
92
93
94
# File 'lib/rspec_spinner/base.rb', line 89

def immediately_dump_pending(desc, msg, location)
  erase_current_line
  output.puts yellow("PENDING SPEC:") + " #{desc} (#{msg})"
  output.puts format_backtrace("  Called from #{location}")
  output.puts
end

#incrementObject



96
97
98
99
100
101
102
103
104
105
# File 'lib/rspec_spinner/base.rb', line 96

def increment
  with_color do
    @current += 1
    # HACK: need to make sure the progress is printed, even when the bar hasn't changed
    @pbar.instance_variable_set("@previous", 0)
    @pbar.instance_variable_set("@title", "#{current}/#{total}")
    @pbar.inc
  end
  output.flush
end

#mark_error_state_failedObject



121
122
123
# File 'lib/rspec_spinner/base.rb', line 121

def mark_error_state_failed
  @error_state = :some_failed
end

#mark_error_state_pendingObject



125
126
127
# File 'lib/rspec_spinner/base.rb', line 125

def mark_error_state_pending
  @error_state = :some_pending unless @error_state == :some_failed
end


133
134
135
136
137
138
139
140
141
# File 'lib/rspec_spinner/base.rb', line 133

def print_warning_if_slow(group, example, location, elapsed)
  if elapsed > THRESHOLD
    #mark_error_state(:pending)
    erase_current_line
    output.print yellow("SLOW SPEC: #{sprintf("%.4f", elapsed)} ")
    output.print "  FROM: #{location} / #{group} #{example}"
    output.puts
  end
end

#start(example_count) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/rspec_spinner/base.rb', line 19

def start(example_count)
  @current     = 0
  @total       = example_count
  @error_state = :all_passing
  @pbar        = RTUI::Progress.new("#{example_count} examples", example_count,
  {:out => output, :components => [:percentage, :spinner, :stat]})
end

#start_dumpObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/rspec_spinner/base.rb', line 52

def start_dump
  output.flush
  super
  @output.puts "\n\nTop 15 slowest examples:\n"

  @example_times = @example_times.sort_by do |description, example, location, time|
    time
  end.reverse

  @example_times[0..14].each do |description, example, location, time|
    _,line = location.split(":")
    @output.print red(sprintf("%.7f", time))
    @output.puts " #{description}:#{line} #{example}"
  end
  @output.flush
end

#with_colorObject



114
115
116
117
118
119
# File 'lib/rspec_spinner/base.rb', line 114

def with_color
  use_color = colour? && output_to_tty?
  output.print ERROR_STATE_COLORS[@error_state] if use_color
  yield
  output.print "\e[0m" if use_color
end