Skip to content

Commit

Permalink
Merge pull request #1020 from rubocop-hq/fix-false-positives-in-empty…
Browse files Browse the repository at this point in the history
…_example_group

Fix false positives in RSpec/EmptyExampleGroup
  • Loading branch information
pirj committed Aug 24, 2020
2 parents 0d5e03d + 7711c76 commit 318df37
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Add `RSpec/RepeatedIncludeExample` cop. ([@biinari][])
* Fix `RSpec/FilePath` when checking a file with a shared example. ([@pirj][])
* Fix subject nesting detection in `RSpec/LeadingSubject`. ([@pirj][])
* Fix false positives in `RSpec/EmptyExampleGroup`. ([@pirj][])

## 1.43.1 (2020-08-17)

Expand Down
5 changes: 5 additions & 0 deletions docs/modules/ROOT/pages/cops_rspec.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,11 @@ describe Bacon do
expect(bacon.chunky?).to be_truthy
end
end
# good
describe Bacon do
pending 'will add tests later'
end
----

==== configuration
Expand Down
11 changes: 11 additions & 0 deletions lib/rubocop/cop/rspec/empty_example_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ module RSpec
# end
# end
#
# # good
# describe Bacon do
# pending 'will add tests later'
# end
#
# @example configuration
#
# # .rubocop.yml
Expand Down Expand Up @@ -83,11 +88,14 @@ class EmptyExampleGroup < Base
# it_behaves_like 'an animal'
# it_behaves_like('a cat') { let(:food) { 'milk' } }
# it_has_root_access
# skip
# it 'will be implemented later'
#
# @param node [RuboCop::AST::Node]
# @return [Array<RuboCop::AST::Node>] matching nodes
def_node_matcher :example_or_group_or_include?, <<~PATTERN
{
#{Examples::ALL.send_pattern}
#{Examples::ALL.block_pattern}
#{ExampleGroups::ALL.block_pattern}
#{Includes::ALL.send_pattern}
Expand Down Expand Up @@ -152,6 +160,9 @@ class EmptyExampleGroup < Base
PATTERN

def on_block(node)
return if node.each_ancestor(:def, :defs).any?
return if node.each_ancestor(:block).any? { |block| example?(block) }

example_group_body(node) do |body|
add_offense(node.send_node) unless examples?(body)
end
Expand Down
67 changes: 67 additions & 0 deletions spec/rubocop/cop/rspec/empty_example_group_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -226,4 +226,71 @@
RUBY
end
end

it 'ignores example groups with pending examples' do
expect_no_offenses(<<~RUBY)
describe Foo do
it 'will be implemented later'
end
describe Foo do
it 'will be implemented later', year: 2030
end
describe Foo do
pending
end
describe Foo do
pending 'too hard to specify'
end
describe Foo do
skip
end
describe Foo do
skip 'undefined behaviour'
end
xdescribe Foo
describe Foo
RUBY
end

it 'ignores example groups defined inside methods' do
expect_no_offenses(<<~RUBY)
RSpec.describe Foo do
def self.with_yaml_loaded(&block)
context 'with YAML loaded' do
module_exec(&block)
end
end
class << self
def without_yaml_loaded(&block)
context 'without YAML loaded' do
module_exec(&block)
end
end
end
with_yaml_loaded do
it_behaves_like 'normal YAML serialization'
end
end
RUBY
end

it 'ignores example groups inside examples' do
expect_no_offenses(<<~RUBY)
RSpec.describe 'rspec-core' do
it 'runs an example group' do
group = RSpec.describe { }
group.run
end
end
RUBY
end
end

0 comments on commit 318df37

Please sign in to comment.