Skip to content

Commit

Permalink
Fix false positive for RSpec/EmptyExampleGroup with iterator and simp…
Browse files Browse the repository at this point in the history
…le conditional
  • Loading branch information
lovro-bikic committed Jan 13, 2025
1 parent e44a27b commit 774b40b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
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
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

0 comments on commit 774b40b

Please sign in to comment.