File: rspec.rb

package info (click to toggle)
ruby-test-prof 0.12.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 508 kB
  • sloc: ruby: 4,075; makefile: 4
file content (140 lines) | stat: -rw-r--r-- 3,155 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# frozen_string_literal: true

require "test_prof/ext/float_duration"

module TestProf
  module RSpecDissect
    class Listener # :nodoc:
      include Logging
      using FloatDuration

      NOTIFICATIONS = %i[
        example_group_finished
        example_finished
      ].freeze

      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

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

      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.metadata[: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

      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

      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

      private

      attr_reader :collectors

      def top_count
        RSpecDissect.config.top_count
      end
    end
  end
end

# Register RSpecDissect listener
TestProf.activate("RD_PROF") do
  RSpec.configure do |config|
    listener = nil

    config.before(:suite) do
      listener = TestProf::RSpecDissect::Listener.new

      config.reporter.register_listener(
        listener, *TestProf::RSpecDissect::Listener::NOTIFICATIONS
      )
    end

    config.after(:suite) { listener&.print }
  end
end