Skip to content

Commit

Permalink
Fix a false positive for RSpec/PendingWithoutReason when not inside…
Browse files Browse the repository at this point in the history
… example and pending/skip with block

Fix: rubocop#1565
  • Loading branch information
ydah committed Jan 17, 2023
1 parent 68252e8 commit 9a417a9
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 8 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Master (Unreleased)

- Fix a false positive for `RSpec/PendingWithoutReason` when not inside example and pending/skip with block. ([@ydah])

## 2.18.0 (2023-01-16)

- Extract Capybara cops to a separate repository. ([@pirj])
Expand Down
21 changes: 13 additions & 8 deletions lib/rubocop/cop/rspec/pending_without_reason.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ class PendingWithoutReason < Base
MSG = 'Give the reason for pending or skip.'

# @!method pending_by_example_method?(node)
def_node_matcher :pending_by_example_method?, block_pattern(<<~PATTERN)
#Examples.pending
def_node_matcher :pending_by_example_method?, <<~PATTERN
(block (send nil? #Examples.pending (...)) args ...)
PATTERN

# @!method pending_by_metadata_without_reason?(node)
Expand All @@ -70,8 +70,8 @@ class PendingWithoutReason < Base
PATTERN

# @!method skipped_by_example_method?(node)
def_node_matcher :skipped_by_example_method?, block_pattern(<<~PATTERN)
#Examples.skipped
def_node_matcher :skipped_by_example_method?, <<~PATTERN
(block (send nil? #Examples.skipped (...)) args ...)
PATTERN

# @!method skipped_by_example_group_method?(node)
Expand All @@ -89,17 +89,22 @@ class PendingWithoutReason < Base

# @!method without_reason?(node)
def_node_matcher :without_reason?, <<~PATTERN
(send nil? ${:pending :skip})
(block ... $(send nil? {:pending :skip}))
PATTERN

def on_send(node)
if pending_without_reason?(node)
add_offense(node, message: 'Give the reason for pending.')
elsif skipped_without_reason?(node)
add_offense(node, message: 'Give the reason for skip.')
elsif without_reason?(node) && example?(node.parent)
add_offense(node,
message: "Give the reason for #{node.method_name}.")
end
end

def on_block(node)
return unless Examples.all(node.send_node.method_name)

without_reason?(node) do |pending_node|
add_offense(pending_node, message: "Give the reason for #{pending_node.method_name}.")
end
end

Expand Down
26 changes: 26 additions & 0 deletions spec/rubocop/cop/rspec/pending_without_reason_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,32 @@
end
end

context 'when pending by pending step without reason ' \
'and not inside example with block' do
it 'registers no offense' do
expect_no_offenses(<<~RUBY)
FactoryBot.define do
factory :task do
pending { true }
end
end
RUBY
end
end

context 'when skipped by skip step without reason ' \
'and not inside example with block' do
it 'registers no offense' do
expect_no_offenses(<<~RUBY)
FactoryBot.define do
factory :task do
skip { true }
end
end
RUBY
end
end

context 'when pending with receiver' do
it 'registers no offense' do
expect_no_offenses(<<~RUBY)
Expand Down

0 comments on commit 9a417a9

Please sign in to comment.