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

Fix false positive for RSpec/EmptyExampleGroup cop with iterator and simple conditional #2022

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Master (Unreleased)

- Fix `RSpec/SortMetadata` cop to limit sorting to trailing metadata arguments. ([@cbliard])
- Fix `Rspec/EmptyExampleGroup` cop false positive when a simple conditional is used inside an iterator. ([@lovro-bikic])

## 3.3.0 (2024-12-12)

Expand Down Expand Up @@ -991,6 +992,7 @@ Compatibility release so users can upgrade RuboCop to 0.51.0. No new features.
[@leoarnold]: https://github.com/leoarnold
[@liberatys]: https://github.com/Liberatys
[@lokhi]: https://github.com/lokhi
[@lovro-bikic]: https://github.com/lovro-bikic
[@luke-hill]: https://github.com/luke-hill
[@m-yamashita01]: https://github.com/M-Yamashita01
[@marocchino]: https://github.com/marocchino
Expand Down
2 changes: 2 additions & 0 deletions lib/rubocop/cop/rspec/empty_example_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ class EmptyExampleGroup < Base
def_node_matcher :examples?, <<~PATTERN
{
#examples_directly_or_in_block?
#examples_in_branches?
(begin <#examples_directly_or_in_block? ...>)
(begin <#examples_in_branches? ...>)
}
Expand Down Expand Up @@ -170,6 +171,7 @@ def conditionals_with_examples?(body)
end

def examples_in_branches?(condition_node)
return false unless condition_node
return false if !condition_node.if_type? && !condition_node.case_type?

condition_node.branches.any? { |branch| examples?(branch) }
Expand Down
19 changes: 17 additions & 2 deletions spec/rubocop/cop/rspec/empty_example_group_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -225,14 +225,29 @@
end

it 'ignores example group with examples defined in `if` branch ' \
'inside iterator' do
'inside iterator with begin block' do
expect_no_offenses(<<~RUBY)
describe 'RuboCop monthly' do
[1, 2, 3].each do |page|
version = 2.3

if RUBY_VERSION >= version
it { expect(use_safe_navigation_operator?(code)).to be(true) }
it { expect(newspaper(page)).to have_ads }
else
warn 'Ruby < 2.3 is barely supported, please use a newer version for development.'
end
end
end
RUBY
end

it 'ignores example group with examples defined in `if` branch ' \
'inside iterator without begin block' do
expect_no_offenses(<<~RUBY)
describe 'RuboCop monthly' do
[1, 2, 3].each do |page|
if RUBY_VERSION >= 2.3
Copy link
Member

Choose a reason for hiding this comment

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

Just for clarity, does it make sense to use page in the condition? Otherwise, why it's inside the loop, and it's not the loop inside the conditional block?

Copy link
Author

Choose a reason for hiding this comment

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

I guess so, I didn't think about it much because I copied this from another example. I've updated it now to use page in the expectation, as in some other examples.

it { expect(newspaper(page)).to have_ads }
else
warn 'Ruby < 2.3 is barely supported, please use a newer version for development.'
end
Expand Down
Loading