Class: TestProf::FactoryDoctor::RSpecListener

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

Overview

:nodoc:

Constant Summary collapse

SUCCESS_MESSAGE =
'FactoryDoctor says: "Looks good to me!"'.freeze
NOTIFICATIONS =
%i[
  example_started
  example_passed
  example_failed
  example_pending
].freeze

Constants included from Logging

Logging::COLORS

Instance Method Summary collapse

Methods included from Logging

#build_log_msg, #colorize, #log

Constructor Details

#initializeRSpecListener

Returns a new instance of RSpecListener.



22
23
24
25
26
# File 'lib/test_prof/factory_doctor/rspec.rb', line 22

def initialize
  @count = 0
  @time = 0.0
  @example_groups = Hash.new { |h, k| h[k] = [] }
end

Instance Method Details

#example_finished(notification) ⇒ Object Also known as: example_passed, example_failed, example_pending



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/test_prof/factory_doctor/rspec.rb', line 32

def example_finished(notification)
  FactoryDoctor.stop
  return if notification.example.pending?

  result = FactoryDoctor.result

  return unless result.bad?

  group = notification.example.example_group.parent_groups.last
  notification.example..merge!(
    factories: result.count,
    time: result.time
  )
  @example_groups[group] << notification.example
  @count += 1
  @time += result.time
end

#example_started(_notification) ⇒ Object



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

def example_started(_notification)
  FactoryDoctor.start
end


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

def print
  return log(:info, SUCCESS_MESSAGE) if @example_groups.empty?

  msgs = []

  msgs <<
    <<-MSG.strip_heredoc
      FactoryDoctor report

      Total (potentially) bad examples: #{@count}
      Total wasted time: #{@time.duration}

    MSG

  @example_groups.each do |group, examples|
    msgs << "#{group.description} (#{group.[:location]})\n"
    examples.each do |ex|
      msgs << "  #{ex.description} (#{ex.[:location]}) "\
              "#{pluralize_records(ex.[:factories])} created, "\
              "#{ex.[:time].duration}\n"
    end
    msgs << "\n"
  end

  log :info, msgs.join

  stamp! if FactoryDoctor.stamp?
end

#stamp!Object



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
113
114
# File 'lib/test_prof/factory_doctor/rspec.rb', line 84

def stamp!
  stamper = RSpecStamp::Stamper.new

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

  @example_groups.each_value do |bad_examples|
    bad_examples.each do |example|
      file, line = example.[:location].split(":")
      examples[file] << line.to_i
    end
  end

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

  msgs = []

  msgs <<
    <<-MSG.strip_heredoc
      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