Class: TestProf::RSpecDissect::Listener

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

Overview

:nodoc:

Constant Summary collapse

NOTIFICATIONS =
%i[
  example_group_finished
  example_finished
].freeze

Constants included from Logging

Logging::COLORS

Instance Method Summary collapse

Methods included from Logging

#log

Constructor Details

#initializeListener

Returns a new instance of Listener.



19
20
21
22
23
24
25
26
# File 'lib/test_prof/rspec_dissect/rspec.rb', line 19

def initialize
  @collector = Collector.new(top_count: top_count)

  @examples_count = 0
  @examples_time = 0.0
  @total_examples_time = 0.0
  @total_setup_time = 0.0
end

Instance Method Details

#example_finished(notification) ⇒ Object



28
29
30
31
# File 'lib/test_prof/rspec_dissect/rspec.rb', line 28

def example_finished(notification)
  @examples_count += 1
  @examples_time += notification.example.execution_result.run_time
end

#example_group_finished(notification) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/test_prof/rspec_dissect/rspec.rb', line 33

def example_group_finished(notification)
  return unless notification.group.top_level?

  data = {}
  data[:total] = @examples_time
  data[:count] = @examples_count
  data[:desc] = notification.group.top_level_description
  data[:loc] = notification.group.[:location]

  RSpecDissect.populate_from_spans!(data)

  collector << data

  @total_setup_time += data[:total_setup]
  @total_examples_time += @examples_time
  @examples_count = 0
  @examples_time = 0.0

  RSpecDissect.reset!
end


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/test_prof/rspec_dissect/rspec.rb', line 54

def print
  msgs = []

  msgs <<
    <<~MSG
      RSpecDissect report

      Total time: #{@total_examples_time.duration}
      Total setup time: #{@total_setup_time.duration}
    MSG

  msgs << "\n"

  msgs << collector.print_results

  log :info, msgs.join

  stamp! if RSpecDissect.config.stamp?
end

#stamp!Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/test_prof/rspec_dissect/rspec.rb', line 74

def stamp!
  stamper = RSpecStamp::Stamper.new

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

  results = collector.results.to_a

  results
    .map { |obj| obj[:loc] }.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