Skip to content

Commit 26c6d4a

Browse files
authored
Merge pull request #1018 from koic/fix_false_positive_for_rspec_empty_example_group
Fix a false positive for `RSpec/EmptyExampleGroup`
2 parents 890c38f + aac9911 commit 26c6d4a

File tree

3 files changed

+83
-2
lines changed

3 files changed

+83
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44

55
* Move our documentation from rubocop-rspec.readthedocs.io to docs.rubocop.org/rubocop-rspec. ([@bquorning][])
66
* Add `RSpec/RepeatedIncludeExample` cop. ([@biinari][])
7-
* Fix false positives in `RSpec/EmptyExampleGroup`. ([@pirj][])
87
* Add `RSpec/StubbedMock` cop. ([@bquorning][], [@pirj][])
8+
* Fix false positives in `RSpec/EmptyExampleGroup`. ([@pirj][])
9+
* Fix a false positive for `RSpec/EmptyExampleGroup` when example is defined in an `if` branch. ([@koic][])
910

1011
## 1.43.2 (2020-08-25)
1112

@@ -561,3 +562,4 @@ Compatibility release so users can upgrade RuboCop to 0.51.0. No new features.
561562
[@jtannas]: https://github.com/jtannas
562563
[@mockdeep]: https://github.com/mockdeep
563564
[@biinari]: https://github.com/biinari
565+
[@koic]: https://github.com/koic

lib/rubocop/cop/rspec/empty_example_group.rb

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,12 +164,35 @@ def on_block(node)
164164
return if node.each_ancestor(:block).any? { |block| example?(block) }
165165

166166
example_group_body(node) do |body|
167-
add_offense(node.send_node) unless examples?(body)
167+
add_offense(node.send_node) if offensive?(body)
168168
end
169169
end
170170

171171
private
172172

173+
def offensive?(body)
174+
return true unless body
175+
return false if conditionals_with_examples?(body)
176+
177+
if body.if_type?
178+
!examples_in_branches?(body)
179+
else
180+
!examples?(body)
181+
end
182+
end
183+
184+
def conditionals_with_examples?(body)
185+
return unless body.begin_type?
186+
187+
body.each_descendant(:if).any? do |if_node|
188+
examples_in_branches?(if_node)
189+
end
190+
end
191+
192+
def examples_in_branches?(if_node)
193+
if_node.branches.any? { |branch| examples?(branch) }
194+
end
195+
173196
def custom_include?(method_name)
174197
custom_include_methods.include?(method_name)
175198
end

spec/rubocop/cop/rspec/empty_example_group_spec.rb

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,62 @@
5454
RUBY
5555
end
5656

57+
it 'ignores example group with examples defined in `if` branches' do
58+
expect_no_offenses(<<~RUBY)
59+
describe 'Ruby 2.3 syntax' do
60+
version = 2.3
61+
62+
if RUBY_VERSION >= version
63+
it { expect(use_safe_navigation_operator?(code)).to be(true) }
64+
else
65+
warn 'Ruby < 2.3 is barely supported, please use a newer version for development.'
66+
end
67+
end
68+
RUBY
69+
end
70+
71+
it 'ignores example group with examples but no examples in `if` branches' do
72+
expect_no_offenses(<<~RUBY)
73+
describe 'Ruby 2.3 syntax' do
74+
version = 2.3
75+
76+
if RUBY_VERSION < version
77+
warn 'Ruby < 2.3 is barely supported, please use a newer version for development.'
78+
end
79+
80+
it { expect(newspaper(page)).to have_ads }
81+
end
82+
RUBY
83+
end
84+
85+
it 'flags an empty example group with no examples defined in `if` branches' do
86+
expect_offense(<<~RUBY)
87+
describe 'Ruby 2.3 syntax' do
88+
^^^^^^^^^^^^^^^^^^^^^^^^^^ Empty example group detected.
89+
version = 2.3
90+
91+
if RUBY_VERSION >= version
92+
warn 'Ruby > 2.3 is supported'
93+
else
94+
warn 'Ruby < 2.3 is barely supported, please use a newer version for development.'
95+
end
96+
end
97+
98+
describe 'Ruby 2.3 syntax' do
99+
^^^^^^^^^^^^^^^^^^^^^^^^^^ Empty example group detected.
100+
if RUBY_VERSION < 2.3
101+
else
102+
end
103+
end
104+
105+
describe 'Ruby 2.3 syntax' do
106+
^^^^^^^^^^^^^^^^^^^^^^^^^^ Empty example group detected.
107+
if RUBY_VERSION >= 2.3
108+
end
109+
end
110+
RUBY
111+
end
112+
57113
it 'ignores example group with examples defined in iterator' do
58114
expect_no_offenses(<<~RUBY)
59115
describe 'RuboCop monthly' do

0 commit comments

Comments
 (0)