Skip to content

Commit

Permalink
Improve RSpec/NoExpectationExample cop to ignore examples skipped o…
Browse files Browse the repository at this point in the history
…r pending via metadata
  • Loading branch information
pirj committed Oct 18, 2022
1 parent b394b77 commit 322986b
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* Update `RSpec/ExampleWording` cop to raise error for insufficient descriptions. ([@akrox58][])
* Add new `RSpec/Capybara/NegationMatcher` cop. ([@ydah][])
* Add `AllowedPatterns` configuration option to `RSpec/NoExpectationExample`. ([@ydah][])
* Improve `RSpec/NoExpectationExample` cop to ignore examples skipped or pending via metatada. ([@pirj][])

## 2.13.2 (2022-09-23)

Expand Down
1 change: 1 addition & 0 deletions lib/rubocop-rspec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
require_relative 'rubocop/cop/rspec/mixin/inside_example_group'
require_relative 'rubocop/cop/rspec/mixin/namespace'
require_relative 'rubocop/cop/rspec/mixin/css_selector'
require_relative 'rubocop/cop/rspec/mixin/skip_or_pending'

require_relative 'rubocop/rspec/concept'
require_relative 'rubocop/rspec/example_group'
Expand Down
23 changes: 23 additions & 0 deletions lib/rubocop/cop/rspec/mixin/skip_or_pending.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

module RuboCop
module Cop
module RSpec
# Helps check offenses with variable definitions
module SkipOrPending
extend RuboCop::NodePattern::Macros

# @!method skipped_in_metadata?(node)
def_node_matcher :skipped_in_metadata?, <<-PATTERN
{
(send _ _ <#skip_or_pending? ...>)
(send _ _ ... (hash <(pair #skip_or_pending? { true str }) ...>))
}
PATTERN

# @!method skip_or_pending?(node)
def_node_matcher :skip_or_pending?, '{(sym :skip) (sym :pending)}'
end
end
end
end
3 changes: 3 additions & 0 deletions lib/rubocop/cop/rspec/no_expectation_example.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ module RSpec
#
class NoExpectationExample < Base
include AllowedPattern
include SkipOrPending

MSG = 'No expectation found in this example.'

# @!method regular_or_focused_example?(node)
Expand Down Expand Up @@ -91,6 +93,7 @@ def on_block(node)
return unless regular_or_focused_example?(node)
return if includes_expectation?(node)
return if includes_skip_example?(node)
return if skipped_in_metadata?(node.send_node)

add_offense(node)
end
Expand Down
13 changes: 2 additions & 11 deletions lib/rubocop/cop/rspec/pending.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ module RSpec
# end
#
class Pending < Base
include SkipOrPending

MSG = 'Pending spec found.'

# @!method skippable?(node)
Expand All @@ -41,17 +43,6 @@ class Pending < Base
{#ExampleGroups.regular #Examples.regular}
PATTERN

# @!method skipped_in_metadata?(node)
def_node_matcher :skipped_in_metadata?, <<-PATTERN
{
(send _ _ <#skip_or_pending? ...>)
(send _ _ ... (hash <(pair #skip_or_pending? { true str }) ...>))
}
PATTERN

# @!method skip_or_pending?(node)
def_node_matcher :skip_or_pending?, '{(sym :skip) (sym :pending)}'

# @!method pending_block?(node)
def_node_matcher :pending_block?,
send_pattern(<<~PATTERN)
Expand Down
17 changes: 17 additions & 0 deletions spec/rubocop/cop/rspec/no_expectation_example_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,23 @@
end
end

it 'registers no offense for skipped/pending with metadata' do
expect_no_offenses(<<~RUBY)
it 'is skipped', :skip do
foo
end
it 'is skipped', skip: true do
foo
end
it 'is pending', :pending do
foo
end
it 'is pending', pending: true do
foo
end
RUBY
end

context 'when `AllowedPatterns: [^expect_]`' do
let(:cop_config) { { 'AllowedPatterns' => ['^expect_'] } }

Expand Down

0 comments on commit 322986b

Please sign in to comment.