Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add RSpecRails/PendingOnlyExampleGroup cop #50

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)

- Add `RSpecRails/PendingOnlyExampleGroup` cop. ([@r7kamura])

## 2.30.0 (2024-06-12)

- Fix an runtime error for rubocop-rspec +3.0. ([@bquorning])
Expand Down
6 changes: 6 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ RSpecRails/NegationBeValid:
VersionChanged: '2.29'
Reference: https://www.rubydoc.info/gems/rubocop-rspec_rails/RuboCop/Cop/RSpecRails/NegationBeValid

RSpecRails/PendingOnlyExampleGroup:
Description: Remove pending-only test files.
Enabled: pending
VersionAdded: "<<next>>"
Reference: https://www.rubydoc.info/gems/rubocop-rspec_rails/RuboCop/Cop/RSpecRails/PendingOnlyExampleGroup

RSpecRails/TravelAround:
Description: Prefer to travel in `before` rather than `around`.
Enabled: pending
Expand Down
1 change: 1 addition & 0 deletions docs/modules/ROOT/pages/cops.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* xref:cops_rspecrails.adoc#rspecrailsinferredspectype[RSpecRails/InferredSpecType]
* xref:cops_rspecrails.adoc#rspecrailsminitestassertions[RSpecRails/MinitestAssertions]
* xref:cops_rspecrails.adoc#rspecrailsnegationbevalid[RSpecRails/NegationBeValid]
* xref:cops_rspecrails.adoc#rspecrailspendingonlyexamplegroup[RSpecRails/PendingOnlyExampleGroup]
* xref:cops_rspecrails.adoc#rspecrailstravelaround[RSpecRails/TravelAround]

// END_COP_LIST
28 changes: 28 additions & 0 deletions docs/modules/ROOT/pages/cops_rspecrails.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,34 @@ expect(foo).to be_invalid.or be_even

* https://www.rubydoc.info/gems/rubocop-rspec_rails/RuboCop/Cop/RSpecRails/NegationBeValid

== RSpecRails/PendingOnlyExampleGroup

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Pending
| Yes
| No
| <<next>>
| -
|===

Remove pending-only test files.

=== Examples

[source,ruby]
----
# bad
RSpec.describe Post do
pending "add some examples to (or delete) #{__FILE__}"
end
----

=== References

* https://www.rubydoc.info/gems/rubocop-rspec_rails/RuboCop/Cop/RSpecRails/PendingOnlyExampleGroup

== RSpecRails/TravelAround

|===
Expand Down
38 changes: 38 additions & 0 deletions lib/rubocop/cop/rspec_rails/pending_only_example_group.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# frozen_string_literal: true

module RuboCop
module Cop
module RSpecRails
# Remove pending-only test files.
#
# @example
# # bad
# RSpec.describe Post do
# pending "add some examples to (or delete) #{__FILE__}"
# end
class PendingOnlyExampleGroup < ::RuboCop::Cop::Base
MSG = 'Remove pending-only test files.'

RESTRICT_ON_SEND = %i[
describe
].freeze

# @!method pending_only_example_group?(node)
def_node_matcher :pending_only_example_group?, <<~PATTERN
(block
(send (const nil? :RSpec) :describe ...)
(args)
(send nil? :pending ...)
)
PATTERN

def on_send(node)
block_node = node.parent
return unless pending_only_example_group?(block_node)

add_offense(block_node)
end
end
end
end
end
1 change: 1 addition & 0 deletions lib/rubocop/cop/rspec_rails_cops.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
require_relative 'rspec_rails/inferred_spec_type'
require_relative 'rspec_rails/minitest_assertions'
require_relative 'rspec_rails/negation_be_valid'
require_relative 'rspec_rails/pending_only_example_group'
require_relative 'rspec_rails/travel_around'
28 changes: 28 additions & 0 deletions spec/rubocop/cop/rspec_rails/pending_only_example_group_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::RSpecRails::PendingOnlyExampleGroup do
context 'with pending only example group' do
it 'registers offense' do
expect_offense(<<~RUBY)
RSpec.describe Post do
^^^^^^^^^^^^^^^^^^^^^^ Remove pending-only test files.
pending "add some examples to (or delete) \#{__FILE__}"
end
RUBY
end
end

context 'with some examples' do
it 'registers no offense' do
expect_no_offenses(<<~RUBY)
RSpec.describe Post do
pending "TODO"

it 'does something' do
expect(foo).to eq(1)
end
end
RUBY
end
end
end