From 84ebac984066bdeb2d714c6e6520dc4a623bc556 Mon Sep 17 00:00:00 2001 From: Jon Rowe Date: Thu, 16 Nov 2023 08:56:39 +0000 Subject: [PATCH] Fix leak of execution context between specs --- .../rails/example/rails_example_group.rb | 9 ++++++- .../rails/example/rails_example_group_spec.rb | 27 +++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/lib/rspec/rails/example/rails_example_group.rb b/lib/rspec/rails/example/rails_example_group.rb index c3d42a527..0643b9ae1 100644 --- a/lib/rspec/rails/example/rails_example_group.rb +++ b/lib/rspec/rails/example/rails_example_group.rb @@ -2,6 +2,10 @@ # suite and ammeter. require 'rspec/rails/matchers' +if ::Rails::VERSION::MAJOR >= 7 + require 'active_support/execution_context/test_helper' +end + module RSpec module Rails # @api public @@ -12,7 +16,10 @@ module RailsExampleGroup include RSpec::Rails::MinitestLifecycleAdapter include RSpec::Rails::MinitestAssertionAdapter include RSpec::Rails::FixtureSupport - include RSpec::Rails::TaggedLoggingAdapter if ::Rails::VERSION::MAJOR >= 7 + if ::Rails::VERSION::MAJOR >= 7 + include RSpec::Rails::TaggedLoggingAdapter + include ActiveSupport::ExecutionContext::TestHelper + end end end end diff --git a/spec/rspec/rails/example/rails_example_group_spec.rb b/spec/rspec/rails/example/rails_example_group_spec.rb index 2ec78353f..7faac1646 100644 --- a/spec/rspec/rails/example/rails_example_group_spec.rb +++ b/spec/rspec/rails/example/rails_example_group_spec.rb @@ -5,5 +5,32 @@ module RSpec::Rails expect(described_class.private_instance_methods).to include(:tagged_logger) end end + + it 'does not leak context between example groups', if: ::Rails::VERSION::MAJOR >= 7 do + groups = + [ + RSpec::Core::ExampleGroup.describe("A group") do + include RSpec::Rails::RailsExampleGroup + specify { expect(ActiveSupport::ExecutionContext.to_h).to eq({}) } + end, + RSpec::Core::ExampleGroup.describe("A controller group", type: :controller) do + specify do + Rails.error.set_context(foo: "bar") + expect(ActiveSupport::ExecutionContext.to_h).to eq(foo: "bar") + end + end, + RSpec::Core::ExampleGroup.describe("Another group") do + include RSpec::Rails::RailsExampleGroup + specify { expect(ActiveSupport::ExecutionContext.to_h).to eq({}) } + end + ] + + results = + groups.map do |group| + group.run(failure_reporter) ? true : failure_reporter.exceptions + end + + expect(results).to all be true + end end end