-
-
Notifications
You must be signed in to change notification settings - Fork 276
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
Add new RSpec/RepeatedSubjectCall cop #1722
Add new RSpec/RepeatedSubjectCall cop #1722
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you so much! This cop, indeed, is very helpful, especially with the ‘not to change’ case!
Let’s shape it up a bit.
|
||
# @!method subject_calls(node) | ||
def_node_search :subject_calls, <<~PATTERN | ||
(send nil? :subject) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we need to detect named subjects and memoized helpers in general, too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed - wasn't sure on the best way to achieve that especially given the complex searching we already do below with just subject
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should work.
7bca2af
to
2dde408
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good already.
Leaving it up to you if you want to also cover named subjects, or leave it for a follow-up.
Thank you!
Please rebase |
I’m sorry, I broke something. I’m from my mobile. Can you please check? |
It's strange, I've added:
and the cop didn't register that. 🤔 |
ca12e41
to
00345a8
Compare
@pirj Was a little trickier, but updated to support custom subjects! |
00345a8
to
d090985
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good! Thank you great work!
d090985
to
cbe677d
Compare
This cop seems to bring in a lot of false positives, at least for me. Here's the failing case: describe LockManager do
subject(:lock_manager) { described_class }
describe '.lock!' do
it 'requires a lock name and TTL in seconds' do # No offense raised
expect { lock_manager.lock! }.to raise_error(ArgumentError, %r{wrong number of arguments.*required keyword:.*ttl})
end
it 'yields to a block if locking is successful' do # No offense raised
expect { |block| lock_manager.lock!('some_lock', ttl: 1, &block) }.to yield_control
end
it 'raises an exception if locking is not successful', :aggregate_failures do
lock_manager.lock!('some_lock', ttl: 3) do |_lock_held| # No offense raised
expect do
expect { |block| lock_manager.lock!('some_lock', ttl: 1, &block) }.not_to yield_control
# C: RSpec/RepeatedSubjectCall: Calls to subject are memoized, this block is misleading
end.to raise_error(Redlock::LockError, %r{failed to acquire.*some_lock})
end
end
end
end I find the actual error misleading:
|
Doesn’t this PR fix this case for you, @timon ? |
No, dependabot PR with 2.27.0 just failed CI for me No offence was registered for |
You would have to use edge to get the fix from this PR, @timon |
Changelog says it was released today |
Yes, I added |
This adds a cop for catching uses of
subject
whereby you forget that it is memoized, often when trying to test idempotence:Had a hard time coming up with a succinct, descriptive name so open to suggestions.
Before submitting the PR make sure the following are checked:
master
(if not - rebase it).CHANGELOG.md
if the new code introduces user-observable changes.bundle exec rake
) passes (be sure to run this locally, since it may produce updated documentation that you will need to commit).If you have created a new cop:
config/default.yml
.Enabled: pending
inconfig/default.yml
.Enabled: true
in.rubocop.yml
.VersionAdded: "<<next>>"
indefault/config.yml
.If you have modified an existing cop's configuration options:
VersionChanged: "<<next>>"
inconfig/default.yml
.