Module: Mergify::RSpec::Configuration
- Defined in:
- lib/mergify/rspec/configuration.rb
Overview
Registers RSpec hooks for quarantine and flaky detection, and adds the Mergify Test Insights formatter when running inside CI.
Class Method Summary collapse
-
.setup! ⇒ Object
rubocop:disable Metrics/MethodLength,Metrics/BlockLength,Metrics/AbcSize rubocop:disable Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity.
Class Method Details
.setup! ⇒ Object
rubocop:disable Metrics/MethodLength,Metrics/BlockLength,Metrics/AbcSize rubocop:disable Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
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 |
# File 'lib/mergify/rspec/configuration.rb', line 14 def setup! ::RSpec.configure do |config| # Add formatter when in CI config.add_formatter(Mergify::RSpec::Formatter) if Utils.in_ci? # Flaky detection: prepare session with all example IDs config.before(:suite) do ci = Mergify::RSpec.ci_insights fd = ci&.flaky_detector if fd all_ids = ::RSpec.world.example_groups.flat_map(&:descendants).flat_map(&:examples).map(&:id) fd.prepare_for_session(all_ids) end end # Quarantine: mark tests before execution config.before(:each) do |example| ci = Mergify::RSpec.ci_insights next unless ci&.quarantined_tests&.include?(example.id) ci.quarantined_tests.mark_as_used(example.id) example.[:mergify_quarantined] = true end # Flaky detection: rerun tests within budget config.around(:each) do |example| ci = Mergify::RSpec.ci_insights fd = ci&.flaky_detector example.run # Feed metrics from the initial run so the detector can evaluate if fd run_time = example.execution_result.run_time || 0.0 status = example.execution_result.status fd.fill_metrics_from_report(example.id, 'setup', 0.0, status) fd.fill_metrics_from_report(example.id, 'call', run_time, status) fd.fill_metrics_from_report(example.id, 'teardown', 0.0, status) end next unless fd&.rerunning_test?(example.id) # Mark as flaky detection candidate (even if too slow to rerun) example.[:mergify_flaky_detection] = true example.[:mergify_new_test] = true if fd.mode == 'new' fd.set_test_deadline(example.id) next if fd.test_too_slow?(example.id) distinct_outcomes = Set.new distinct_outcomes.add(example.execution_result.status) if example.execution_result.status rerun_count = 0 # Use a null reporter to prevent reruns from being reported as # individual failures. The final outcome is set after the loop. null_reporter = ::RSpec::Core::NullReporter until example.[:is_last_rerun] example.[:is_last_rerun] = fd.last_rerun_for_test?(example.id) # Reset example state for rerun example.instance_variable_set(:@exception, nil) if example.example_group_instance memoized = example.example_group_instance.instance_variable_get(:@__memoized) if memoized.respond_to?(:clear) memoized.clear elsif memoized # RSpec >= 3.12 uses ThreadsafeMemoized which wraps an internal hash inner = memoized.instance_variable_get(:@memoized) inner&.clear end end example.run(example.example_group_instance, null_reporter) # Feed rerun metrics so budget/deadline tracking stays accurate rerun_time = example.execution_result.run_time || 0.0 rerun_status = example.execution_result.status fd.fill_metrics_from_report(example.id, 'call', rerun_time, rerun_status) distinct_outcomes.add(rerun_status) rerun_count += 1 end is_flaky = distinct_outcomes.include?(:passed) && distinct_outcomes.include?(:failed) example.[:mergify_flaky] = true if is_flaky example.[:mergify_rerun_count] = rerun_count end # Quarantine: override failed quarantined test results config.after(:each) do |example| next unless example.[:mergify_quarantined] && example.exception example.instance_variable_set(:@exception, nil) example.execution_result.status = :pending example.execution_result. = 'Test is quarantined from Mergify Test Insights' end end end |