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 (58 lines) | stat: -rw-r--r-- 1,427 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
# frozen_string_literal: true

require "test_prof/utils/rspec"

module TestProf
  module StackProf
    # Reporter for RSpec to profile specific examples with StackProf
    class Listener # :nodoc:
      class << self
        attr_accessor :report_name_generator
      end

      self.report_name_generator = Utils::RSpec.method(:example_to_filename)

      NOTIFICATIONS = %i[
        example_started
        example_finished
      ].freeze

      def example_started(notification)
        return unless profile?(notification.example)
        notification.example.metadata[:sprof_report] = TestProf::StackProf.profile
      end

      def example_finished(notification)
        return unless profile?(notification.example)
        return unless notification.example.metadata[:sprof_report] == false

        TestProf::StackProf.dump(
          self.class.report_name_generator.call(notification.example)
        )
      end

      private

      def profile?(example)
        example.metadata.key?(:sprof)
      end
    end
  end
end

RSpec.configure do |config|
  config.before(:suite) do
    listener = TestProf::StackProf::Listener.new

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

# Handle boot profiling
RSpec.configure do |config|
  config.append_before(:suite) do
    TestProf::StackProf.dump("boot") if TestProf::StackProf.config.boot?
  end
end