|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +return unless defined?(Prosopite) |
| 4 | + |
| 5 | +# Test configuration |
| 6 | +Prosopite.enabled = true |
| 7 | +Prosopite.raise = true # Fail specs on N+1 detection |
| 8 | +Prosopite.rails_logger = true |
| 9 | +Prosopite.prosopite_logger = true |
| 10 | + |
| 11 | +# Allowlist for known acceptable N+1 patterns (e.g., test matchers) |
| 12 | +Prosopite.allow_stack_paths = [ |
| 13 | + "shoulda/matchers/active_record/validate_uniqueness_of_matcher.rb", |
| 14 | + "shoulda/matchers/active_model/validate_presence_of_matcher.rb", |
| 15 | + "shoulda/matchers/active_model/validate_inclusion_of_matcher.rb", |
| 16 | + "shoulda/matchers/active_model/allow_value_matcher.rb" |
| 17 | +] |
| 18 | + |
| 19 | +# Optional: Load ignore list from file for gradual rollout |
| 20 | +PROSOPITE_IGNORE = if File.exist?("spec/.prosopite_ignore") |
| 21 | + File.read("spec/.prosopite_ignore").lines.map(&:chomp).reject(&:empty?) |
| 22 | +else |
| 23 | + [] |
| 24 | +end |
| 25 | + |
| 26 | +# Monkey-patch FactoryBot to pause during factory creation |
| 27 | +# Prevents false positives from factory callbacks |
| 28 | +if defined?(FactoryBot) |
| 29 | + module FactoryBot |
| 30 | + module Strategy |
| 31 | + class Create |
| 32 | + alias_method :original_result, :result |
| 33 | + |
| 34 | + def result(evaluation) |
| 35 | + if defined?(Prosopite) && Prosopite.enabled? |
| 36 | + Prosopite.pause { original_result(evaluation) } |
| 37 | + else |
| 38 | + original_result(evaluation) |
| 39 | + end |
| 40 | + end |
| 41 | + end |
| 42 | + end |
| 43 | + end |
| 44 | +end |
| 45 | + |
| 46 | +# RSpec integration |
| 47 | +RSpec.configure do |config| |
| 48 | + config.around do |example| |
| 49 | + if use_prosopite?(example) |
| 50 | + Prosopite.scan { example.run } |
| 51 | + else |
| 52 | + original_enabled = Prosopite.enabled? |
| 53 | + Prosopite.enabled = false |
| 54 | + example.run |
| 55 | + Prosopite.enabled = original_enabled |
| 56 | + end |
| 57 | + end |
| 58 | +end |
| 59 | + |
| 60 | +def use_prosopite?(example) |
| 61 | + # Explicit metadata takes precedence |
| 62 | + return false if example.metadata[:disable_prosopite] |
| 63 | + return true if example.metadata[:enable_prosopite] |
| 64 | + |
| 65 | + # Check against ignore list |
| 66 | + PROSOPITE_IGNORE.none? do |path| |
| 67 | + File.fnmatch?("./#{path}/*", example.metadata[:rerun_file_path].to_s) |
| 68 | + end |
| 69 | +end |
0 commit comments