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

#build_log_msg, #colorize, #log

Constructor Details

#initializeListener

Returns a new instance of Listener.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/test_prof/rspec_dissect/rspec.rb', line 16

def initialize
  @collectors = []

  if RSpecDissect.config.let?
    collectors << Collectors::Let.new(top_count: RSpecDissect.config.top_count)
  end

  if RSpecDissect.config.before?
    collectors << Collectors::Before.new(top_count: RSpecDissect.config.top_count)
  end

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

Instance Method Details

#example_finished(notification) ⇒ Object



32
33
34
35
# File 'lib/test_prof/rspec_dissect/rspec.rb', line 32

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

#example_group_finished(notification) ⇒ Object



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

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]

  collectors.each { |c| c.populate!(data) }
  collectors.each { |c| c << data }

  @total_examples_time += @examples_time
  @examples_count = 0
  @examples_time = 0.0

  RSpecDissect.reset!
end


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/test_prof/rspec_dissect/rspec.rb', line 56

def print
  msgs = []

  msgs <<
    <<~MSG
      RSpecDissect report

      Total time: #{@total_examples_time.duration}
    MSG

  collectors.each do |c|
    msgs << c.total_time_message
  end

  msgs << "\n"

  collectors.each do |c|
    msgs << c.print_results
  end

  log :info, msgs.join

  stamp! if RSpecDissect.config.stamp?
end

#stamp!Object



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
106
107
108
109
110
111
112
# File 'lib/test_prof/rspec_dissect/rspec.rb', line 81

def stamp!
  stamper = RSpecStamp::Stamper.new

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

  all_results = collectors.inject([]) { |acc, c| acc + c.results.to_a }

  all_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