Skip to content

Commit

Permalink
Make RuboCop RSpecRails work as a RuboCop plugin
Browse files Browse the repository at this point in the history
This pull request adds support for RuboCop's plugin feature, added in
rubocop/rubocop#13792, released in v1.72.

It replaces the ad-hoc `inject_defaults!` with RuboCop plugins.

Some Rake tasks may still need to use `inject_defaults!`.
  • Loading branch information
bquorning committed Feb 17, 2025
1 parent 2d257f2 commit e734b08
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 20 deletions.
4 changes: 2 additions & 2 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
inherit_from: .rubocop_todo.yml

require:
plugins:
- rubocop-performance
- rubocop-rake
- rubocop-rspec
- rubocop/cop/internal_affairs
- rubocop-internal_affairs

AllCops:
DisplayCopNames: true
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Handle unknown HTTP status codes for `RSpecRails/HttpStatus` cop. ([@viralpraxis])
- Fix a false negative for `RSpecRails/TravelAround` cop when passed as a proc to a travel method. ([@ydah])
- Make RuboCop RSpecRails work as a RuboCop plugin. ([@bquorning])

## 2.30.0 (2024-06-12)

Expand Down
4 changes: 2 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ gem 'bump'
gem 'rack'
gem 'rake'
gem 'rspec', '~> 3.11'
gem 'rubocop-performance', '~> 1.7'
gem 'rubocop-rake', '~> 0.6'
gem 'rubocop-performance', '~> 1.24'
gem 'rubocop-rake', '~> 0.7'
gem 'simplecov', '>= 0.19'
gem 'yard'

Expand Down
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,31 +33,34 @@ ways to do this:
Put this into your `.rubocop.yml`.

```yaml
require: rubocop-rspec_rails
plugins: rubocop-rspec_rails
```
Alternatively, use the following array notation when specifying multiple extensions.
```yaml
require:
plugins:
- rubocop-rspec
- rubocop-rspec_rails
```
Now you can run `rubocop` and it will automatically load the RuboCop RSpec Rails
cops together with the standard cops.

> [!NOTE]
> The plugin system is supported in RuboCop 1.72+. In earlier versions, use `require` instead of `plugins`.

### Command line

```bash
rubocop --require rubocop-rspec_rails
rubocop --plugin rubocop-rspec_rails
```

### Rake task

```ruby
RuboCop::RakeTask.new do |task|
task.requires << 'rubocop-rspec_rails'
task.plugins << 'rubocop-rspec_rails'
end
```

Expand Down
10 changes: 6 additions & 4 deletions docs/modules/ROOT/pages/usage.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,35 @@ There are three ways to do this:
Put this into your `.rubocop.yml`:

----
require: rubocop-rspec_rails
plugins: rubocop-rspec_rails
----

or, if you are using several extensions:

----
require:
plugins:
- rubocop-rspec
- rubocop-rspec_rails
----

Now you can run `rubocop` and it will automatically load the RuboCop RSpec Rails
cops together with the standard cops.

NOTE: The plugin system is supported in RuboCop 1.72+. In earlier versions, use `require` instead of `plugins`.

== Command line

[source,bash]
----
$ rubocop --require rubocop-rspec_rails
$ rubocop --plugin rubocop-rspec_rails
----

== Rake task

[source,ruby]
----
RuboCop::RakeTask.new do |task|
task.requires << 'rubocop-rspec_rails'
task.plugins << 'rubocop-rspec_rails'
end
----

Expand Down
4 changes: 1 addition & 3 deletions lib/rubocop-rspec_rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@
require 'rubocop'
require 'rubocop/rspec/language'

require_relative 'rubocop/rspec_rails/plugin'
require_relative 'rubocop/rspec_rails/version'

require 'rubocop/cop/rspec/base'
require_relative 'rubocop/cop/rspec_rails_cops'

project_root = File.join(__dir__, '..')
RuboCop::ConfigLoader.inject_defaults!(project_root)
35 changes: 35 additions & 0 deletions lib/rubocop/rspec_rails/plugin.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# frozen_string_literal: true

require 'lint_roller'

module RuboCop
module RSpecRails
# A plugin that integrates RuboCop RSpecRails with RuboCop's plugin system.
class Plugin < LintRoller::Plugin
# :nocov:
def about
LintRoller::About.new(
name: 'rubocop-rspec_rails',
version: Version::STRING,
homepage: 'https://github.com/rubocop/rubocop-rspec_rails',
description: 'Code style checking for RSpec Rails files.'
)
end
# :nocov:

def supported?(context)
context.engine == :rubocop
end

def rules(_context)
project_root = Pathname.new(__dir__).join('../../..')

LintRoller::Rules.new(
type: :path,
config_format: :rubocop,
value: project_root.join('config/default.yml')
)
end
end
end
end
8 changes: 5 additions & 3 deletions rubocop-rspec_rails.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ Gem::Specification.new do |spec|
spec.metadata = {
'changelog_uri' => 'https://github.com/rubocop/rubocop-rspec_rails/blob/master/CHANGELOG.md',
'documentation_uri' => 'https://docs.rubocop.org/rubocop-rspec_rails/',
'rubygems_mfa_required' => 'true'
'rubygems_mfa_required' => 'true',
'default_lint_roller_plugin' => 'RuboCop::RSpecRails::Plugin'
}

spec.add_runtime_dependency 'rubocop', '~> 1.61'
spec.add_runtime_dependency 'rubocop-rspec', '~> 3', '>= 3.0.1'
spec.add_dependency 'lint_roller', '~> 1.1'
spec.add_dependency 'rubocop', '~> 1.72', '>= 1.72.1'
spec.add_runtime_dependency 'rubocop-rspec', '~> 3.5'
end
2 changes: 0 additions & 2 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ module SpecHelper
# We should take their advice!
config.raise_on_warning = true

config.include(ExpectOffense)

config.include_context 'with default RSpec/Language config', :config
config.include_context 'smoke test', type: :cop_spec
end
Expand Down
2 changes: 2 additions & 0 deletions tasks/cops_documentation.rake
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ end

desc 'Generate docs of all cops departments'
task generate_cops_documentation: :yard_for_generate_documentation do
RuboCop::ConfigLoader.inject_defaults!("#{__dir__}/../config/default.yml")

generator = CopsDocumentationGenerator.new(
departments: %w[RSpecRails]
)
Expand Down

0 comments on commit e734b08

Please sign in to comment.