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

Conversation

javierjulio
Copy link
Contributor

@javierjulio javierjulio commented Apr 6, 2024

We need to include the ActiveSupport::CurrentAttributes::TestHelper in the rails example group, otherwise rspec does not reset ActiveSupport::CurrentAttributes around each example unless being reset manually. We need to mimic what Rails v7 does for test setup for the "else" case until #2712 can be revisited. Related to #2713. This should address #2503 properly.

In the meantime, I've created #2753 which recreates #2712 with some new changes since the GHA logs for the original are no longer available, we can't address without a new attempt. Ideally, #2753 would replace this with the suggestion from #2753 (comment) if it would work.

This needs to be included otherwise RSpec does not reset ActiveSupport::CurrentAttributes around each example. We need to mimic what Rails does in: https://github.com/rails/rails/blob/v7.0.0/activesupport/lib/active_support/railtie.rb#L50-L61 for the "else" case until rspec#2712 can be revisited.
javierjulio added a commit to javierjulio/rspec-rails that referenced this pull request Apr 6, 2024
This recreates rspec#2712 since the GHA logs are no longer available which we need to address this issue. We should be wrapping the Rails examples with the executor to mimic what Rails v7 does (as noted in rspec#2713) to properly reset state. This has been tested in a test suite for an app but we need to address what issues there is within rspec-rails. This change request originated from rspec#2503 and rspec#2752 which the latter is meant as a temporary fix since this is the expected way to reset state.
Copied this from Rails source but I forgot to update before committing originally.
javierjulio and others added 6 commits April 8, 2024 16:23
Now that we've confirmed the failing test, by including RailsExampleGroup, it should reset CurrentAttributes as expected.
The Rubocop failure I was unsure about but it seems from other specs here that a class can be defined in the top level block so we'll try that.

The spec now only runs in Rails 7+ and only asserts against the defined attribute as that should suffice.
CI reported on line 2.
@javierjulio
Copy link
Contributor Author

javierjulio commented Apr 9, 2024

@pirj this is ready for review. Considering the current state of rspec-rails, I figure its best to start here since its basically missing this CurrentAttributes test helper inclusion to ensure parity with Rails, pre-executor. I've added tests here that I confirmed failed first and now pass with the updated module.

@@ -1,6 +1,10 @@
module RSpec::Rails
RSpec.describe RailsExampleGroup do
if ::Rails::VERSION::MAJOR >= 7
class CurrentSample < ActiveSupport::CurrentAttributes
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What fails exactly without this?
At a glance it may looks that it’s the following example that check for tagged logger, but I guess it’s not, is it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its just setup...

@@ -32,5 +36,18 @@ module RSpec::Rails

expect(results).to all be true
end

describe 'CurrentAttributes', order: :defined, if: ::Rails::VERSION::MAJOR >= 7 do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The concept is hard to absorb at first, but here there are two types of groups, the first that is part of our spec suite, that is first evaluated when you run rspec over rspec-rails specs, but before running any specs. The second is defined during specs runtime, like RSpec.describe you can see above. I wouldn’t mix in RailsExampleGroup into the former, but it can be freely done with the latter. See example above for the reference.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pirj I didn't get to this in time. Seeing Jon's update, I'm just not familiar with how specs are written here and struggled a bit. I'll know how to better approach it in the future. Thanks!

@JonRowe
Copy link
Member

JonRowe commented Apr 10, 2024

Thanks for tackling this, I took the liberty of refactoring the spec to what we'd usually do I hope you don't mind.

@JonRowe JonRowe merged commit 24ca7fc into rspec:main Apr 10, 2024
17 checks passed
JonRowe added a commit that referenced this pull request Apr 10, 2024
JonRowe added a commit that referenced this pull request Apr 10, 2024
Reset CurrentAttributes on each rails example
JonRowe added a commit that referenced this pull request Apr 10, 2024
JonRowe pushed a commit to javierjulio/rspec-rails that referenced this pull request Apr 10, 2024
This recreates rspec#2712 since the GHA logs are no longer available which we need to address this issue. We should be wrapping the Rails examples with the executor to mimic what Rails v7 does (as noted in rspec#2713) to properly reset state. This has been tested in a test suite for an app but we need to address what issues there is within rspec-rails. This change request originated from rspec#2503 and rspec#2752 which the latter is meant as a temporary fix since this is the expected way to reset state.
@javierjulio
Copy link
Contributor Author

@JonRowe I don't mind, I was just trying to figure it out and contribute as best I could. Thank you again!

@JonRowe
Copy link
Member

JonRowe commented Jun 19, 2024

Released in 6.1.3

@pond
Copy link

pond commented Jul 2, 2024

This was a great idea and well intentioned but regrettably it has introduced a serious bug.

Pleas see #2773.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants