From ef2ffa446fc1fccfa36793d2ba0404931dd35ce8 Mon Sep 17 00:00:00 2001 From: Kirk Wang Date: Wed, 3 Apr 2024 18:15:06 -0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=92=84=20Add=20custom=20cop=20for=20`incl?= =?UTF-8?q?ude=20Hyrax::ArResource`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit will add a custom cop to RuboCop that will check for `include Hyrax::ArResrouce` or `include ArRdResource` in the codebase. The `ArResource` module is just for development ease of use and should not be used within the codebase. The custom cop leaves an example for additional custom cops that can be used in the future and where to test them. The key is to make sure the `.rubocop.yml` file is updated to include the new cop. The pattern for it is adding something like Hyrax::RuboCop::CustomCops::SomeCop would be ```yaml Hyrax/SomeCop: Enabled: true ``` From what I gather it is taking the top most namespace and then the class name. --- .rubocop.yml | 5 ++++ lib/hyrax/engine.rb | 1 + lib/hyrax/rubocop/custom_cops.rb | 30 ++++++++++++++++++++++ spec/lib/hyrax/rubocop/custom_cops_spec.rb | 21 +++++++++++++++ spec/spec_helper.rb | 5 ++++ 5 files changed, 62 insertions(+) create mode 100644 lib/hyrax/rubocop/custom_cops.rb create mode 100644 spec/lib/hyrax/rubocop/custom_cops_spec.rb diff --git a/.rubocop.yml b/.rubocop.yml index 7af7717921..04759db2b2 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,3 +1,5 @@ +require: ./lib/hyrax/rubocop/custom_cops.rb + inherit_from: - .rubocop_fixme.yml @@ -125,3 +127,6 @@ RSpec/NestedGroups: RSpec/LeadingSubject: Enabled: false + +Hyrax/ArResource: + Enabled: true diff --git a/lib/hyrax/engine.rb b/lib/hyrax/engine.rb index 71983435b3..d8fcd12f25 100644 --- a/lib/hyrax/engine.rb +++ b/lib/hyrax/engine.rb @@ -34,6 +34,7 @@ class Engine < ::Rails::Engine require 'hyrax/transactions' require 'hyrax/errors' require 'hyrax/valkyrie_simple_path_generator' + require 'hyrax/rubocop/custom_cops' # Force these models to be added to Legato's registry in development mode config.eager_load_paths += %W[ diff --git a/lib/hyrax/rubocop/custom_cops.rb b/lib/hyrax/rubocop/custom_cops.rb new file mode 100644 index 0000000000..fce0b7cf1e --- /dev/null +++ b/lib/hyrax/rubocop/custom_cops.rb @@ -0,0 +1,30 @@ +# frozen_string_literal: true + +require 'rubocop' + +module Hyrax + module RuboCop + module CustomCops + # This custom cop checks for mixins of Hyrax::ArResource + class ArResource < ::RuboCop::Cop::Cop + MSG = 'Do not `include Hyrax::ArResource`.' + + # checks for `include Hyrax::ArResource` + def_node_search :includes_hyrax_ar_resource?, <<-PATTERN + (send nil? {:include :extend} (const (const nil? :Hyrax) :ArResource)) + PATTERN + + # checks for `include ArResource` + def_node_search :includes_ar_resource?, <<-PATTERN + (send nil? {:include :extend} (const nil? :ArResource)) + PATTERN + + def on_send(send_node) + add_offense(send_node, message: MSG) if includes_hyrax_ar_resource?(send_node) || includes_ar_resource?(send_node) + end + end + + # class AdditionalCustomCops < ::RuboCop::Cop::Cop; end + end + end +end diff --git a/spec/lib/hyrax/rubocop/custom_cops_spec.rb b/spec/lib/hyrax/rubocop/custom_cops_spec.rb new file mode 100644 index 0000000000..5c4904dd61 --- /dev/null +++ b/spec/lib/hyrax/rubocop/custom_cops_spec.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +RSpec.describe Hyrax::RuboCop::CustomCops::ArResource do + subject(:cop) { described_class.new } + + it 'is not allowed to include Hyrax::ArResource' do + expect_offense(<<~RUBY) + include Hyrax::ArResource + ^^^^^^^^^^^^^^^^^^^^^^^^^ Do not `include Hyrax::ArResource`. + RUBY + end + + it 'is not allowed to include ArResource' do + expect_offense(<<~RUBY) + include ArResource + ^^^^^^^^^^^^^^^^^^ Do not `include Hyrax::ArResource`. + RUBY + end +end + +# RSpec.describe Hyrax::RuboCop::CustomCops::AdditionalCustomCops do; end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index a023536bab..35c7945fcf 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -50,6 +50,9 @@ def ci_build? require 'hyrax/specs/clamav' require 'hyrax/specs/engine_routes' +require 'rubocop' +require 'rubocop/rspec/support' + # ensure Hyrax::Schema gets loaded is resolvable for `support/` models Hyrax::Schema # rubocop:disable Lint/Void @@ -364,4 +367,6 @@ def clean_active_fedora_repository .to receive(:storage_adapter) .and_return(Valkyrie::StorageAdapter.find(adapter_name)) end + + config.include RuboCop::RSpec::ExpectOffense end