diff --git a/sentry-rails/spec/dummy/test_rails_app/config/application.rb b/sentry-rails/spec/dummy/test_rails_app/config/application.rb index 715a91ac6..85fb6f364 100644 --- a/sentry-rails/spec/dummy/test_rails_app/config/application.rb +++ b/sentry-rails/spec/dummy/test_rails_app/config/application.rb @@ -13,7 +13,25 @@ module Sentry module Rails module Test class Application < ::Rails::Application + LOAD_HOOK_IVARS = %i[@load_hooks @loaded @run_once].freeze + + def self.snapshot_load_hook_state + @load_hook_snapshot ||= LOAD_HOOK_IVARS.select { |name| ActiveSupport.instance_variable_defined?(name) }.to_h do |name| + [name, ActiveSupport.instance_variable_get(name).transform_values(&:dup)] + end + end + + def self.restore_load_hook_state + snapshot_load_hook_state.each do |name, saved| + current = ActiveSupport.instance_variable_get(name) + current.clear + saved.each { |key, entries| current[key] = entries.dup } + end + end + def self.define + snapshot_load_hook_state + klass = Class.new(Sentry::TestRailsApp) klass.define_singleton_method(:name) { @@ -189,9 +207,44 @@ def after_initialize! end def cleanup! + Test::Application.restore_load_hook_state + + release_global_state ::Rails.application = nil end + def release_global_state + if ::ActiveRecord::Base.respond_to?(:filter_attributes=) + ::ActiveRecord::Base.filter_attributes = [] + end + + if defined?(::ActiveRecord::Encryption) + ::ActiveRecord::Encryption.encrypted_attribute_declaration_listeners = nil + end + + if defined?(::ActiveRecord::FilterAttributeHandler) + handler = ::ActiveRecord::FilterAttributeHandler + if handler.instance_variable_defined?(:@sensitive_attribute_declaration_listeners) + handler.remove_instance_variable(:@sensitive_attribute_declaration_listeners) + end + end + + if defined?(::ActionCable) && ::ActionCable.instance_variable_defined?(:@server) + ::ActionCable.remove_instance_variable(:@server) + end + + mounted_helpers = ::ActionDispatch::Routing::RouteSet::MountedHelpers + %i[main_app _main_app].each do |name| + mounted_helpers.send(:remove_method, name) if mounted_helpers.method_defined?(name, false) + end + + ::Rails.cache = nil + + if defined?(::ActionView::PartialRenderer) && ::ActionView::PartialRenderer.respond_to?(:collection_cache=) + ::ActionView::PartialRenderer.collection_cache = ::ActiveSupport::Cache::MemoryStore.new + end + end + require_relative "applications/#{application_file}" ::Rails.module_eval do diff --git a/sentry-rails/spec/sentry/rails/test_application_spec.rb b/sentry-rails/spec/sentry/rails/test_application_spec.rb new file mode 100644 index 000000000..b888d84dd --- /dev/null +++ b/sentry-rails/spec/sentry/rails/test_application_spec.rb @@ -0,0 +1,96 @@ +# frozen_string_literal: true + +require "spec_helper" + +RSpec.describe Sentry::Rails::Test::Application do + include Sentry::Rails::TestHelper + + def load_hook_state + %i[@load_hooks @loaded @run_once].select { |name| ActiveSupport.instance_variable_defined?(name) }.map do |name| + ActiveSupport.instance_variable_get(name).transform_values(&:dup) + end + end + + describe "#cleanup!" do + it "restores the ActiveSupport load hook state to what it was before the boot" do + before_boot = load_hook_state + + app = make_basic_app + expect(load_hook_state).not_to eq(before_boot) + + app.cleanup! + + expect(load_hook_state).to eq(before_boot) + end + + it "releases the app's filter parameters from ActiveRecord" do + skip "ActiveRecord::Base.filter_attributes is not available" unless ActiveRecord::Base.respond_to?(:filter_attributes) + + app = make_basic_app + expect(ActiveRecord::Base.filter_attributes).not_to be_empty + + app.cleanup! + + expect(ActiveRecord::Base.filter_attributes).to be_empty + end + + it "releases the encrypted attribute listeners the app registered" do + skip "ActiveRecord::Encryption is not available" unless defined?(ActiveRecord::Encryption) + + app = make_basic_app + expect(ActiveRecord::Encryption.encrypted_attribute_declaration_listeners).not_to be_empty + + app.cleanup! + + expect(ActiveRecord::Encryption.encrypted_attribute_declaration_listeners).to be_blank + end + + it "releases the sensitive attribute listeners the app registered" do + skip "ActiveRecord::FilterAttributeHandler is not available" unless defined?(ActiveRecord::FilterAttributeHandler) + + app = make_basic_app + listeners = -> { ActiveRecord::FilterAttributeHandler.instance_variable_get(:@sensitive_attribute_declaration_listeners) } + expect(listeners.call).not_to be_empty + + app.cleanup! + + expect(listeners.call).to be_blank + end + + it "releases the Action Cable server built for the app" do + app = make_basic_app + server = ActionCable.server + + app.cleanup! + + expect(ActionCable.server).not_to equal(server) + end + + it "releases the main_app route helper bound to the app" do + app = make_basic_app + expect(ActionDispatch::Routing::RouteSet::MountedHelpers.method_defined?(:main_app)).to be(true) + + app.cleanup! + + expect(ActionDispatch::Routing::RouteSet::MountedHelpers.method_defined?(:main_app)).to be(false) + end + + it "releases the cache store built for the app" do + app = make_basic_app + expect(Rails.cache).not_to be_nil + + app.cleanup! + + expect(Rails.cache).to be_nil + end + + it "releases the partial rendering cache built for the app" do + app = make_basic_app + cache = ActionView::PartialRenderer.collection_cache + + app.cleanup! + + expect(ActionView::PartialRenderer.collection_cache).not_to equal(cache) + end + end +end