From 774b40b41e4288d77e689bee6ca18ea695acedff Mon Sep 17 00:00:00 2001 From: Lovro Bikic Date: Sun, 12 Jan 2025 22:55:24 +0100 Subject: [PATCH] Fix false positive for RSpec/EmptyExampleGroup with iterator and simple conditional --- CHANGELOG.md | 2 ++ lib/rubocop/cop/rspec/empty_example_group.rb | 2 ++ .../cop/rspec/empty_example_group_spec.rb | 19 +++++++++++++++++-- 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b0ce06ab2..4111601e8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) @@ -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 diff --git a/lib/rubocop/cop/rspec/empty_example_group.rb b/lib/rubocop/cop/rspec/empty_example_group.rb index 44293a2c6..b1e788b3c 100644 --- a/lib/rubocop/cop/rspec/empty_example_group.rb +++ b/lib/rubocop/cop/rspec/empty_example_group.rb @@ -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? ...>) } @@ -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) } diff --git a/spec/rubocop/cop/rspec/empty_example_group_spec.rb b/spec/rubocop/cop/rspec/empty_example_group_spec.rb index 1501ef817..8fd1134cb 100644 --- a/spec/rubocop/cop/rspec/empty_example_group_spec.rb +++ b/spec/rubocop/cop/rspec/empty_example_group_spec.rb @@ -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