Skip to content

Commit

Permalink
Merge pull request #1515 from ydah/fix/1514
Browse files Browse the repository at this point in the history
Fix a false positive for `RSpec/PendingWithoutReason` when not inside example
  • Loading branch information
pirj authored Dec 15, 2022
2 parents b723efb + 0e7d995 commit 495e175
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Add new `RSpec/Capybara/MatchStyle` cop. ([@ydah])
- Add new `RSpec/Rails/MinitestAssertions` cop. ([@ydah])
- Fix a false positive for `RSpec/PendingWithoutReason` when not inside example. ([@ydah])

## 2.16.0 (2022-12-13)

Expand Down
26 changes: 14 additions & 12 deletions lib/rubocop/cop/rspec/pending_without_reason.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,35 +87,37 @@ class PendingWithoutReason < Base
(send #rspec? {#ExampleGroups.all #Examples.all} ... {<(sym :skip) ...> (hash <(pair (sym :skip) true) ...>)})
PATTERN

# @!method without_reason?(node)
def_node_matcher :without_reason?, <<~PATTERN
(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) && inside_example?(node)
add_offense(node,
message: "Give the reason for #{node.method_name}.")
end
end

private

def pending_by_pending_step_without_reason?(node)
node.method?(:pending) && node.first_argument.nil?
end

def pending_without_reason?(node)
pending_by_example_method?(node.block_node) ||
pending_by_metadata_without_reason?(node) ||
pending_by_pending_step_without_reason?(node)
end

def skipped_by_skip_step_without_reason?(node)
node.method?(:skip) && node.first_argument.nil?
pending_by_metadata_without_reason?(node)
end

def skipped_without_reason?(node)
skipped_by_example_group_method?(node.block_node) ||
skipped_by_example_method?(node.block_node) ||
skipped_by_metadata_without_reason?(node) ||
skipped_by_skip_step_without_reason?(node)
skipped_by_metadata_without_reason?(node)
end

def inside_example?(node)
node.ancestors.find { |parent| example?(parent) }
end
end
end
Expand Down
46 changes: 46 additions & 0 deletions spec/rubocop/cop/rspec/pending_without_reason_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,52 @@
end
end

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

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

context 'when pending with receiver' do
it 'registers no offense' do
expect_no_offenses(<<~RUBY)
it 'does something' do
Foo.pending
end
RUBY
end
end

context 'when skip with receiver' do
it 'registers no offense' do
expect_no_offenses(<<~RUBY)
it 'does something' do
Foo.skip
end
RUBY
end
end

context 'when pending by example method' do
it 'registers offense' do
expect_offense(<<~RUBY)
Expand Down

0 comments on commit 495e175

Please sign in to comment.