From 7cce1fea3e59559cbad0d5b5f780fc6cc945eeac Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Wed, 26 Feb 2020 01:04:06 +0900 Subject: [PATCH] [Fix #7745] Suppress pending cop warnings when department is disabled Fixes #7745. This PR suppresses a pending cop warnings when pending cop's department is disabled. ## Context ```console % rubocop -V 0.80.0 (using Parser 2.7.0.2, running on ruby 2.7.0 x86_64-darwin17) ``` ```yaml % cat .rubocop.yml Style: Enabled: false ``` ## Before The following pending cop warning is displayed for disabled Style department. ```console % bundle exec rubocop The following cops were added to RuboCop, but are not configured. Please set Enabled to either `true` or `false` in your `.rubocop.yml` file: - Style/HashEachMethods - Style/HashTransformKeys - Style/HashTransformValues Inspecting 0 files 0 files inspected, no offenses detected ``` ## After A pending cop warning is not displayed for disabled Style department. ```console % bundle exec rubocop Inspecting 0 files 0 files inspected, no offenses detected ``` --- CHANGELOG.md | 1 + lib/rubocop/config.rb | 31 +++++++--- lib/rubocop/config_loader.rb | 7 +-- spec/rubocop/cli/cli_options_spec.rb | 92 +++++++++++++++++++--------- 4 files changed, 88 insertions(+), 43 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 967288121ab..390288cee9c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ * [#7719](https://github.com/rubocop-hq/rubocop/issues/7719): Fix `Style/NestedParenthesizedCalls` cop for newline. ([@tejasbubane][]) * [#7709](https://github.com/rubocop-hq/rubocop/issues/7709): Fix correction of `Style/RedundantCondition` when the else branch contains a range. ([@rrosenblum][]) * [#7682](https://github.com/rubocop-hq/rubocop/issues/7682): Fix `Style/InverseMethods` autofix leaving parenthesis. ([@tejasbubane][]) +* [#7745](https://github.com/rubocop-hq/rubocop/issues/7745): Suppress a pending cop warnings when pending cop's department is disabled. ([@koic][]) ## 0.80.0 (2020-02-18) diff --git a/lib/rubocop/config.rb b/lib/rubocop/config.rb index b641c931b24..dff3a3a4f8f 100644 --- a/lib/rubocop/config.rb +++ b/lib/rubocop/config.rb @@ -2,6 +2,9 @@ require 'pathname' +# FIXME: Moving Rails department code to RuboCop Rails will remove +# the following rubocop:disable comment. +# rubocop:disable Metrics/ClassLength module RuboCop # This class represents the configuration of the RuboCop application # and all its cops. A Config is associated with a YAML configuration @@ -215,6 +218,15 @@ def bundler_lock_file_path nil end + def pending_cops + keys.select do |qualified_cop_name| + department = department_of(qualified_cop_name) + next if department && department['Enabled'] == false + + self[qualified_cop_name]['Enabled'] == 'pending' + end + end + private def target_rails_version_from_bundler_lock_file @@ -235,17 +247,18 @@ def read_rails_version_from_bundler_lock_file end def enable_cop?(qualified_cop_name, cop_options) - cop_department, cop_name = qualified_cop_name.split('/') - department = cop_name.nil? - - unless department - department_options = self[cop_department] - if department_options && department_options['Enabled'] == false - return false - end - end + department = department_of(qualified_cop_name) + return false if department && department['Enabled'] == false cop_options.fetch('Enabled') { !for_all_cops['DisabledByDefault'] } end + + def department_of(qualified_cop_name) + cop_department, cop_name = qualified_cop_name.split('/') + return nil if cop_name.nil? + + self[cop_department] + end end end +# rubocop:enable Metrics/ClassLength diff --git a/lib/rubocop/config_loader.rb b/lib/rubocop/config_loader.rb index 9f969fdc21b..cbf9d9bdaf9 100644 --- a/lib/rubocop/config_loader.rb +++ b/lib/rubocop/config_loader.rb @@ -117,11 +117,8 @@ def default_configuration end def warn_on_pending_cops(config) - pending_cops = config.keys.select do |key| - config[key]['Enabled'] == 'pending' - end - - return if pending_cops.none? + pending_cops = config.pending_cops + return if pending_cops.empty? warn Rainbow('The following cops were added to RuboCop, but are not ' \ 'configured. Please set Enabled to either `true` or ' \ diff --git a/spec/rubocop/cli/cli_options_spec.rb b/spec/rubocop/cli/cli_options_spec.rb index 88be506c50e..4d243655e57 100644 --- a/spec/rubocop/cli/cli_options_spec.rb +++ b/spec/rubocop/cli/cli_options_spec.rb @@ -220,47 +220,81 @@ class SomeCop < Cop RESULT end - it 'accepts cop names from plugins with a pending cop' do - create_file('.rubocop.yml', <<~YAML) - require: rubocop_ext + context 'when specifying a pending cop' do + let(:rubocop) { "#{RuboCop::ConfigLoader::RUBOCOP_HOME}/exe/rubocop" } - Style/SomeCop: - Description: Something - Enabled: pending - YAML - create_file('rubocop_ext.rb', <<~RUBY) - module RuboCop - module Cop - module Style - class SomeCop < Cop - end - end - end - end - RUBY - create_file('redirect.rb', '$stderr = STDOUT') - rubocop = "#{RuboCop::ConfigLoader::RUBOCOP_HOME}/exe/rubocop" # Since we define a new cop class, we have to do this in a separate # process. Otherwise, the extra cop will affect other specs. - output = + let(:output) do `ruby -I . "#{rubocop}" --require redirect.rb --only Style/SomeCop` + end - expected_prefix = <<~PREFIX + let(:pending_cop_warning) { <<~PENDING_COP_WARNING } The following cops were added to RuboCop, but are not configured. Please set Enabled to either `true` or `false` in your `.rubocop.yml` file: - PREFIX - expected_suffix = <<~SUFFIX + PENDING_COP_WARNING + + let(:inspected_output) { <<~INSPECTED_OUTPUT } Inspecting 2 files .. 2 files inspected, no offenses detected - SUFFIX + INSPECTED_OUTPUT - expect(output).to start_with(expected_prefix) - expect(output).to end_with(expected_suffix) + before do + create_file('rubocop_ext.rb', <<~RUBY) + module RuboCop + module Cop + module Style + class SomeCop < Cop + end + end + end + end + RUBY + + create_file('redirect.rb', '$stderr = STDOUT') + end + + context 'when Style department is enabled' do + before do + create_file('.rubocop.yml', <<~YAML) + require: rubocop_ext + + Style/SomeCop: + Description: Something + Enabled: pending + YAML + end - remaining_range = expected_prefix.length..-(expected_suffix.length + 1) - pending_cops = output[remaining_range].split("\n") - expect(pending_cops).to include(' - Style/SomeCop') + it 'accepts cop names from plugins with a pending cop warning' do + expect(output).to start_with(pending_cop_warning) + expect(output).to end_with(inspected_output) + + remaining_range = + pending_cop_warning.length..-(inspected_output.length + 1) + pending_cops = output[remaining_range].split("\n") + expect(pending_cops).to include(' - Style/SomeCop') + end + end + + context 'when Style department is disabled' do + before do + create_file('.rubocop.yml', <<~YAML) + require: rubocop_ext + + Style: + Enabled: false + + Style/SomeCop: + Description: Something + Enabled: pending + YAML + end + + it 'does not show pending cop warning' do + expect(output).to eq(inspected_output) + end + end end context 'without using namespace' do