File: minitest.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 (78 lines) | stat: -rw-r--r-- 2,151 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
# frozen_string_literal: true

require "minitest/base_reporter"
require "minitest/event_prof_formatter"

module Minitest
  module TestProf
    class EventProfReporter < BaseReporter # :nodoc:
      def initialize(io = $stdout, options = {})
        super
        @profiler = configure_profiler(options)

        log :info, "EventProf enabled (#{@profiler.events.join(", ")})"

        @formatter = EventProfFormatter.new(@profiler)
        @current_group = nil
        @current_example = nil
      end

      def prerecord(group, example)
        change_current_group(group, example) unless @current_group
        track_current_example(group, example)
      end

      def before_test(test)
        prerecord(test.class, test.name)
      end

      def record(*)
        @profiler.example_finished(@current_example)
      end

      def report
        @profiler.group_finished(@current_group)
        result = @formatter.prepare_results
        log :info, result
      end

      private

      def track_current_example(group, example)
        unless @current_group[:name] == group.name
          @profiler.group_finished(@current_group)
          change_current_group(group, example)
        end

        @current_example = {
          name: example.gsub(/^test_(?:\d+_)?/, ""),
          location: location_with_line_number(group, example)
        }

        @profiler.example_started(@current_example)
      end

      def change_current_group(group, example)
        @current_group = {
          name: group.name,
          location: location_without_line_number(group, example)
        }

        @profiler.group_started(@current_group)
      end

      def configure_profiler(options)
        ::TestProf::EventProf.configure do |config|
          config.event = options[:event]
          config.rank_by = options[:rank_by] if options[:rank_by]
          config.top_count = options[:top_count] if options[:top_count]
          config.per_example = options[:per_example] if options[:per_example]

          ::TestProf::EventProf::CustomEvents.activate_all config.event
        end

        ::TestProf::EventProf.build
      end
    end
  end
end