Skip to content

Commit 318df37

Browse files
authored
Merge pull request #1020 from rubocop-hq/fix-false-positives-in-empty_example_group
Fix false positives in RSpec/EmptyExampleGroup
2 parents 0d5e03d + 7711c76 commit 318df37

File tree

4 files changed

+84
-0
lines changed

4 files changed

+84
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* Add `RSpec/RepeatedIncludeExample` cop. ([@biinari][])
77
* Fix `RSpec/FilePath` when checking a file with a shared example. ([@pirj][])
88
* Fix subject nesting detection in `RSpec/LeadingSubject`. ([@pirj][])
9+
* Fix false positives in `RSpec/EmptyExampleGroup`. ([@pirj][])
910

1011
## 1.43.1 (2020-08-17)
1112

docs/modules/ROOT/pages/cops_rspec.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,11 @@ describe Bacon do
739739
expect(bacon.chunky?).to be_truthy
740740
end
741741
end
742+
743+
# good
744+
describe Bacon do
745+
pending 'will add tests later'
746+
end
742747
----
743748

744749
==== configuration

lib/rubocop/cop/rspec/empty_example_group.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ module RSpec
3333
# end
3434
# end
3535
#
36+
# # good
37+
# describe Bacon do
38+
# pending 'will add tests later'
39+
# end
40+
#
3641
# @example configuration
3742
#
3843
# # .rubocop.yml
@@ -83,11 +88,14 @@ class EmptyExampleGroup < Base
8388
# it_behaves_like 'an animal'
8489
# it_behaves_like('a cat') { let(:food) { 'milk' } }
8590
# it_has_root_access
91+
# skip
92+
# it 'will be implemented later'
8693
#
8794
# @param node [RuboCop::AST::Node]
8895
# @return [Array<RuboCop::AST::Node>] matching nodes
8996
def_node_matcher :example_or_group_or_include?, <<~PATTERN
9097
{
98+
#{Examples::ALL.send_pattern}
9199
#{Examples::ALL.block_pattern}
92100
#{ExampleGroups::ALL.block_pattern}
93101
#{Includes::ALL.send_pattern}
@@ -152,6 +160,9 @@ class EmptyExampleGroup < Base
152160
PATTERN
153161

154162
def on_block(node)
163+
return if node.each_ancestor(:def, :defs).any?
164+
return if node.each_ancestor(:block).any? { |block| example?(block) }
165+
155166
example_group_body(node) do |body|
156167
add_offense(node.send_node) unless examples?(body)
157168
end

spec/rubocop/cop/rspec/empty_example_group_spec.rb

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,4 +226,71 @@
226226
RUBY
227227
end
228228
end
229+
230+
it 'ignores example groups with pending examples' do
231+
expect_no_offenses(<<~RUBY)
232+
describe Foo do
233+
it 'will be implemented later'
234+
end
235+
236+
describe Foo do
237+
it 'will be implemented later', year: 2030
238+
end
239+
240+
describe Foo do
241+
pending
242+
end
243+
244+
describe Foo do
245+
pending 'too hard to specify'
246+
end
247+
248+
describe Foo do
249+
skip
250+
end
251+
252+
describe Foo do
253+
skip 'undefined behaviour'
254+
end
255+
256+
xdescribe Foo
257+
258+
describe Foo
259+
RUBY
260+
end
261+
262+
it 'ignores example groups defined inside methods' do
263+
expect_no_offenses(<<~RUBY)
264+
RSpec.describe Foo do
265+
def self.with_yaml_loaded(&block)
266+
context 'with YAML loaded' do
267+
module_exec(&block)
268+
end
269+
end
270+
271+
class << self
272+
def without_yaml_loaded(&block)
273+
context 'without YAML loaded' do
274+
module_exec(&block)
275+
end
276+
end
277+
end
278+
279+
with_yaml_loaded do
280+
it_behaves_like 'normal YAML serialization'
281+
end
282+
end
283+
RUBY
284+
end
285+
286+
it 'ignores example groups inside examples' do
287+
expect_no_offenses(<<~RUBY)
288+
RSpec.describe 'rspec-core' do
289+
it 'runs an example group' do
290+
group = RSpec.describe { }
291+
group.run
292+
end
293+
end
294+
RUBY
295+
end
229296
end

0 commit comments

Comments
 (0)