Skip to content

Commit

Permalink
Merge pull request #2711 from rspec/fix-leak-of-execution-context
Browse files Browse the repository at this point in the history
Fix leak of execution context between specs
  • Loading branch information
JonRowe authored Nov 16, 2023
2 parents 7fdab6a + 84ebac9 commit 55b6947
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/rspec/rails/example/rails_example_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
27 changes: 27 additions & 0 deletions spec/rspec/rails/example/rails_example_group_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 55b6947

Please sign in to comment.