|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +Prosopite.enabled = true |
| 4 | +Prosopite.raise = true # Fail specs on N+1 detection |
| 5 | +Prosopite.rails_logger = true |
| 6 | +Prosopite.prosopite_logger = true |
| 7 | +Prosopite.allow_stack_paths = [ |
| 8 | + "shoulda/matchers/active_record/validate_uniqueness_of_matcher.rb", |
| 9 | + "shoulda/matchers/active_model/validate_presence_of_matcher.rb", |
| 10 | + "shoulda/matchers/active_model/validate_inclusion_of_matcher.rb", |
| 11 | +] |
| 12 | + |
| 13 | +PROSOPITE_PATHS = [ |
| 14 | + "./spec/models/*" |
| 15 | +].freeze |
| 16 | + |
| 17 | +# Monkey-patch FactoryBot to pause Prosopite during factory creation |
| 18 | +# This prevents N+1 detection in factory callbacks, focusing on actual test code |
| 19 | +module FactoryBot |
| 20 | + module Strategy |
| 21 | + class Create |
| 22 | + alias_method :original_result, :result |
| 23 | + |
| 24 | + def result(evaluation) |
| 25 | + if defined?(Prosopite) && Prosopite.enabled? |
| 26 | + Prosopite.pause do |
| 27 | + original_result(evaluation) |
| 28 | + end |
| 29 | + else |
| 30 | + original_result(evaluation) |
| 31 | + end |
| 32 | + end |
| 33 | + end |
| 34 | + end |
| 35 | +end |
| 36 | + |
| 37 | +RSpec.configure do |config| |
| 38 | + config.around do |example| |
| 39 | + should_enable = PROSOPITE_PATHS.any? { |pattern| |
| 40 | + File.fnmatch?(pattern, example.metadata[:rerun_file_path] |
| 41 | + ) |
| 42 | + } |
| 43 | + |
| 44 | + if should_enable && !example.metadata[:disable_prosopite] |
| 45 | + Prosopite.scan do |
| 46 | + example.run |
| 47 | + end |
| 48 | + else |
| 49 | + # Disable prosopite globally for this test (works across threads) |
| 50 | + original_enabled = Prosopite.enabled? |
| 51 | + Prosopite.enabled = false |
| 52 | + example.run |
| 53 | + Prosopite.enabled = original_enabled |
| 54 | + end |
| 55 | + end |
| 56 | +end |
0 commit comments