Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reset CurrentAttributes on each rails example #2752

Merged
merged 9 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/rspec/rails/example/rails_example_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require 'rspec/rails/matchers'

if ::Rails::VERSION::MAJOR >= 7
require 'active_support/current_attributes/test_helper'
require 'active_support/execution_context/test_helper'
end

Expand All @@ -18,6 +19,7 @@ module RailsExampleGroup
include RSpec::Rails::FixtureSupport
if ::Rails::VERSION::MAJOR >= 7
include RSpec::Rails::TaggedLoggingAdapter
include ActiveSupport::CurrentAttributes::TestHelper
include ActiveSupport::ExecutionContext::TestHelper
end
end
Expand Down
32 changes: 28 additions & 4 deletions spec/rspec/rails/example/rails_example_group_spec.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
module RSpec::Rails
RSpec.describe RailsExampleGroup do
if ::Rails::VERSION::MAJOR >= 7
it 'supports tagged_logger' do
expect(described_class.private_instance_methods).to include(:tagged_logger)
end
it 'supports tagged_logger', if: ::Rails::VERSION::MAJOR >= 7 do
expect(described_class.private_instance_methods).to include(:tagged_logger)
end

it 'does not leak context between example groups', if: ::Rails::VERSION::MAJOR >= 7 do
Expand Down Expand Up @@ -32,5 +30,31 @@ module RSpec::Rails

expect(results).to all be true
end

it 'will not leak ActiveSupport::CurrentAttributes between examples', if: ::Rails::VERSION::MAJOR >= 7 do
group =
RSpec::Core::ExampleGroup.describe("A group", order: :defined) do
include RSpec::Rails::RailsExampleGroup

# rubocop:disable Lint/ConstantDefinitionInBlock
class CurrentSample < ActiveSupport::CurrentAttributes
attribute :request_id
end
# rubocop:enable Lint/ConstantDefinitionInBlock

it 'sets a current attribute' do
CurrentSample.request_id = '123'
expect(CurrentSample.request_id).to eq('123')
end

it 'does not leak current attributes' do
expect(CurrentSample.request_id).to eq(nil)
end
end

expect(
group.run(failure_reporter) ? true : failure_reporter.exceptions
).to be true
end
end
end