-
-
Notifications
You must be signed in to change notification settings - Fork 285
[Close #856] implement repeated example group description/body cops #860
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
Conversation
b15d623 to
22fb458
Compare
|
Hey @pirj thanks for your comment on the issue, it allows me to make better merge and implement all that I wanted with 2 cops. Looking for your review |
pirj
left a comment
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.
Glanced over. Looks good overall, a few nitpicks.
pirj
left a comment
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.
Most of the comments apply to both of the cops.
This is shaping up pretty quickly, good job!
pirj
left a comment
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. Just a couple of edge cases to fix.
|
@pirj Hello, I've resolved all of your issues and made some enhances in node matchers. Hope it will be your last review 😄 |
pirj
left a comment
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 really good. 👍
Some minor comments here and there, nothing that really needs addressing, it's good as is.
I've tested on a large codebase and found several copy-pasted example groups and descriptions, and also where both description and block were identical, yay! 👏 Immediate value.
- (optional) However, there are some false positives, e.g.:
context 'rejected' do
skip
end
context 'processed' do
skip
endI think skip and pending could be skipped, but it's ok to leave this for later.
- Interpolated docstrings are considered equal:
context "when model is in status #{Statuses::PENDING} some postfix" do
...
context "when model is in status #{Statuses::APPROVED} some OTHER postfix" doYou can look for dstr for an example of how to handle it.
- All classes are considered equal:
context A::B do
...
context C::D do- (completely optional) with two above cases, it might make sense to check if a variable or a method call is used as an argument for docstring:
def description
"does that"
end
context(description) do
...
context(description) doI think 2&3 are major.
|
@pirj You have left some comments, but anyway approved PR, why?) And also, I've fixed your last 4 tensions and added test cases for them. Looking for your review |
|
@pirj sorry, I've requested re-review out of habit 😄 |
pirj
left a comment
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.
All good from my point of view, no false positives detected. 👍
Really good job, and impressive node pattern skills.
I didn't tell you in advance, but @bquorning and @Darhazer might chime in to add their notes.
As from my side, I'm happy with the current state of those cops.
spec/rubocop/cop/rspec/repeated_example_group_description_spec.rb
Outdated
Show resolved
Hide resolved
pirj
left a comment
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.
Flawless from my point of view. Good job!
|
@bquorning @Darhazer do you mind to take a look? |
|
Tests-wise, can we check that repeated examples are flagged if there is something else between them? Can we test that context are not flagged for having the same example on different data? |
|
@Darhazer Done |
|
Thank you. This might be hard to catch using static analysis, and I see value in the |
|
It also fails on examples/group without descriptions, that are actually different |
@Darhazer But how is this possible for sibling contexts? We account for metadata differences already. |
@Darhazer how we should handle such cases? Just ignore them and another cop (maybe new) should handle it? |
|
@pirj it's not metadata, it's a @lazycoder9 there's nothing to handle there - it's a false positive - the examples are different and should not be flagged as repeated. |
|
@Darhazer Fixed false positive with example groups without descriptions. I'm going to think about false positive case with |
|
@Darhazer I couldn't come up with a better example: it 'registers an offense for repeated context body' do
expect_offense(<<-RUBY)
context 'when awesome case' do
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Repeated context block body on line(s) [5]
let(:a) { 1 }
it { cool_predicate_method }
end
context 'when another awesome case' do
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Repeated context block body on line(s) [1]
let(:a) { 2 }
it { cool_predicate_method }
end
RUBY
endAnd it fails since cop considers those two examples different. Can you please provide a minimal reproduction case? |
|
|
|
@Darhazer I've tried your example with it 'does not register offense' do
expect_no_offenses(<<-RUBY)
context 'when awesome case' do
let(:a) { 1 }
it { cool_predicate_method(a) }
end
context 'when another awesome case' do
let(:a) { 2 }
it { cool_predicate_method(a) }
end
RUBY
endAnd it works. Maybe another cop was triggered in this code? I think about |
|
Tried on two more pretty large codebases, no signs of false positives. Only traces of copy-pasta, inspiring false confidence in code impeccancy. |
|
@pirj perhaps it was connected with the previous false positives - with the latest code, I don't get any. I'll make a final review early next week. |
Darhazer
left a comment
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.
Fantastic work!
Please squash the commits.
I left a few questions, but nothing that would really block a merge
| RUBY | ||
| end | ||
|
|
||
| it 'registers offense correctly for interpolated docstrings' do |
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.
Should it? if there is a variable and the variable value changes between the two context calls?
Although this is such an edge case that I could live with the false positives. Actually it would be very bad code to start with
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 hope there's no one in the world writing describe "when time is #{Time.now.to_f}" do and repeat that exact description :D It's better to flag this even though technically those two will be two different descriptions.
214d32d to
c31aeaf
Compare
c31aeaf to
32d2e2f
Compare
Darhazer
left a comment
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.
🚀
|
Thanks for those amazing new cops! |
|
And you are always welcome to contribute more cops and come up with ideas for cops/improvements. |
|
Great addition 🎉 ! There was a case in |
There are 2 cops, that ensures that we don't have repeated specs.
RSpec/RepeatedExampleGroupDescriptionChecks for repeated descriptions in example group
Example:
RSpec/RepeatedExampleGroupBodyChecks for repeated body or implementation for example group
Fixes #856
Before submitting the PR make sure the following are checked:
master(if not - rebase it).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.