From 05bccb67040490e2562d77bc960b291bd33c717e Mon Sep 17 00:00:00 2001 From: Luke Hill <20105237+luke-hill@users.noreply.github.com> Date: Tue, 29 Mar 2022 10:58:26 +0100 Subject: [PATCH] Exclude all spec blocks from Metrics/BlockLength This was something that was originally discussed in https://github.com/rubocop/rubocop-rspec/issues/988 --- .rubocop.yml | 1 - CHANGELOG.md | 2 ++ config/default.yml | 8 ++++++++ lib/rubocop/rspec/config_formatter.rb | 3 +++ spec/project/default_config_spec.rb | 5 +++-- 5 files changed, 16 insertions(+), 3 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 042adaea7..27f95813f 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -51,7 +51,6 @@ Metrics/BlockLength: - rubocop-rspec.gemspec - Rakefile - '**/*.rake' - - spec/**/*.rb Naming/FileName: Exclude: diff --git a/CHANGELOG.md b/CHANGELOG.md index ba553587a..d6566f1ee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ * Drop Ruby 2.5 support. ([@ydah][]) * Add new `RSpec/ChangeByZero` cop. ([@ydah][]) * Improve `RSpec/ExpectChange` to detect namespaced and top-level constants. ([@M-Yamashita01][]) +* Introduce an amendment to `Metrics/BlockLength` to exclude spec files. ([@luke-hill][]) ## 2.10.0 (2022-04-19) @@ -686,3 +687,4 @@ Compatibility release so users can upgrade RuboCop to 0.51.0. No new features. [@ydah]: https://github.com/ydah [@t3h2mas]: https://github.com/t3h2mas [@M-Yamashita01]: https://github.com/M-Yamashita01 +[@luke-hill]: https://github.com/luke-hill diff --git a/config/default.yml b/config/default.yml index 670b2abc2..e668cf321 100644 --- a/config/default.yml +++ b/config/default.yml @@ -110,6 +110,14 @@ RSpec: - subject - subject! +Metrics/BlockLength: + inherit_mode: + merge: + - Exclude + Exclude: + - "**/*_spec.rb" + - "**/spec/**/*" + RSpec/AlignLeftLetBrace: Description: Checks that left braces for adjacent single line lets are aligned. Enabled: false diff --git a/lib/rubocop/rspec/config_formatter.rb b/lib/rubocop/rspec/config_formatter.rb index d722023a1..f9c16352e 100644 --- a/lib/rubocop/rspec/config_formatter.rb +++ b/lib/rubocop/rspec/config_formatter.rb @@ -8,6 +8,7 @@ module RSpec class ConfigFormatter EXTENSION_ROOT_DEPARTMENT = %r{^(RSpec/)}.freeze SUBDEPARTMENTS = %(RSpec/Capybara RSpec/FactoryBot RSpec/Rails) + AMENDMENTS = %(Metrics/BlockLength) COP_DOC_BASE_URL = 'https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/' def initialize(config, descriptions) @@ -18,6 +19,7 @@ def initialize(config, descriptions) def dump YAML.dump(unified_config) .gsub(EXTENSION_ROOT_DEPARTMENT, "\n\\1") + .gsub(*AMENDMENTS, "\n\\0") .gsub(/^(\s+)- /, '\1 - ') end @@ -26,6 +28,7 @@ def dump def unified_config cops.each_with_object(config.dup) do |cop, unified| next if SUBDEPARTMENTS.include?(cop) + next if AMENDMENTS.include?(cop) unified[cop].merge!(descriptions.fetch(cop)) unified[cop]['Reference'] = COP_DOC_BASE_URL + cop.sub('RSpec/', '') diff --git a/spec/project/default_config_spec.rb b/spec/project/default_config_spec.rb index e5414e0f2..a04501bfd 100644 --- a/spec/project/default_config_spec.rb +++ b/spec/project/default_config_spec.rb @@ -42,8 +42,9 @@ def cop_configuration(config_key) end end - it 'has configuration for all cops' do - expect(default_config.keys).to match_array(config_keys) + it 'has configuration for all cops and amendments' do + expect(default_config.keys) + .to match_array([*config_keys, 'Metrics/BlockLength']) end it 'sorts configuration keys alphabetically', :pending do