From e734b08f6231e82b7999b02b1652809ebea3709a Mon Sep 17 00:00:00 2001 From: Benjamin Quorning Date: Mon, 17 Feb 2025 09:46:19 +0100 Subject: [PATCH] Make RuboCop RSpecRails work as a RuboCop plugin This pull request adds support for RuboCop's plugin feature, added in https://github.com/rubocop/rubocop/pull/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!`. --- .rubocop.yml | 4 ++-- CHANGELOG.md | 1 + Gemfile | 4 ++-- README.md | 11 ++++++---- docs/modules/ROOT/pages/usage.adoc | 10 +++++---- lib/rubocop-rspec_rails.rb | 4 +--- lib/rubocop/rspec_rails/plugin.rb | 35 ++++++++++++++++++++++++++++++ rubocop-rspec_rails.gemspec | 8 ++++--- spec/spec_helper.rb | 2 -- tasks/cops_documentation.rake | 2 ++ 10 files changed, 61 insertions(+), 20 deletions(-) create mode 100644 lib/rubocop/rspec_rails/plugin.rb diff --git a/.rubocop.yml b/.rubocop.yml index 143718a1..cefb245b 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -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 diff --git a/CHANGELOG.md b/CHANGELOG.md index 661b9868..1ff10daa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/Gemfile b/Gemfile index f6fddf52..089cdf4e 100644 --- a/Gemfile +++ b/Gemfile @@ -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' diff --git a/README.md b/README.md index a3cc6eb2..fa0cb4a5 100644 --- a/README.md +++ b/README.md @@ -33,13 +33,13 @@ 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 ``` @@ -47,17 +47,20 @@ require: 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 ``` diff --git a/docs/modules/ROOT/pages/usage.adoc b/docs/modules/ROOT/pages/usage.adoc index 26f08c2e..d79a8ddc 100644 --- a/docs/modules/ROOT/pages/usage.adoc +++ b/docs/modules/ROOT/pages/usage.adoc @@ -8,13 +8,13 @@ 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 ---- @@ -22,11 +22,13 @@ require: 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 @@ -34,7 +36,7 @@ $ rubocop --require rubocop-rspec_rails [source,ruby] ---- RuboCop::RakeTask.new do |task| - task.requires << 'rubocop-rspec_rails' + task.plugins << 'rubocop-rspec_rails' end ---- diff --git a/lib/rubocop-rspec_rails.rb b/lib/rubocop-rspec_rails.rb index 04f2b3b9..21bb137a 100644 --- a/lib/rubocop-rspec_rails.rb +++ b/lib/rubocop-rspec_rails.rb @@ -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) diff --git a/lib/rubocop/rspec_rails/plugin.rb b/lib/rubocop/rspec_rails/plugin.rb new file mode 100644 index 00000000..cc0fd34a --- /dev/null +++ b/lib/rubocop/rspec_rails/plugin.rb @@ -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 diff --git a/rubocop-rspec_rails.gemspec b/rubocop-rspec_rails.gemspec index 51ff67c9..d6f654ad 100644 --- a/rubocop-rspec_rails.gemspec +++ b/rubocop-rspec_rails.gemspec @@ -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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 94b0dab5..b71a6647 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -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 diff --git a/tasks/cops_documentation.rake b/tasks/cops_documentation.rake index 9936db9f..df2e08a2 100644 --- a/tasks/cops_documentation.rake +++ b/tasks/cops_documentation.rake @@ -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] )