Class: TestProf::EventProf::RSpecListener

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/test_prof/event_prof/rspec.rb

Overview

:nodoc:

Constant Summary collapse

NOTIFICATIONS =
%i[
  example_group_started
  example_group_finished
  example_started
  example_finished
].freeze

Constants included from Logging

Logging::COLORS

Instance Method Summary collapse

Methods included from Logging

#log

Constructor Details

#initialize ⇒ RSpecListener

Returns a new instance of RSpecListener.



15
16
17
18
19
# File 'lib/test_prof/event_prof/rspec.rb', line 15

def initialize
  @profiler = EventProf.build

  log :info, "EventProf enabled (#{@profiler.events.join(", ")})"
end

Instance Method Details

#example_finished(notification) ⇒ Object



35
36
37
# File 'lib/test_prof/event_prof/rspec.rb', line 35

def example_finished(notification)
  @profiler.example_finished notification.example
end

#example_group_finished(notification) ⇒ Object



26
27
28
29
# File 'lib/test_prof/event_prof/rspec.rb', line 26

def example_group_finished(notification)
  return unless notification.group.top_level?
  @profiler.group_finished notification.group
end

#example_group_started(notification) ⇒ Object



21
22
23
24
# File 'lib/test_prof/event_prof/rspec.rb', line 21

def example_group_started(notification)
  return unless notification.group.top_level?
  @profiler.group_started notification.group
end

#example_started(notification) ⇒ Object



31
32
33
# File 'lib/test_prof/event_prof/rspec.rb', line 31

def example_started(notification)
  @profiler.example_started notification.example
end


39
40
41
42
43
44
45
# File 'lib/test_prof/event_prof/rspec.rb', line 39

def print
  EventProf.config.printer.dump(@profiler)

  return unless EventProf.config.stamp?

  @profiler.each { |profiler| stamp!(profiler) }
end

#stamp!(profiler) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/test_prof/event_prof/rspec.rb', line 47

def stamp!(profiler)
  result = profiler.results

  stamper = RSpecStamp::Stamper.new

  examples = Hash.new { |h, k| h[k] = [] }

  (result[:groups].to_a + result.fetch(:examples, []).to_a)
    .map { |obj| obj[:id].[:location] }.each do |location|
    file, line = location.split(":")
    examples[file] << line.to_i
  end

  examples.each do |file, lines|
    stamper.stamp_file(file, lines.uniq)
  end

  msgs = []

  msgs <<
    <<~MSG
      RSpec Stamp results

      Total patches: #{stamper.total}
      Total files: #{examples.keys.size}

      Failed patches: #{stamper.failed}
      Ignored files: #{stamper.ignored}
    MSG

  log :info, msgs.join
end