Skip to content

Commit

Permalink
[Fix #7745] Suppress pending cop warnings when department is disabled
Browse files Browse the repository at this point in the history
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
```
  • Loading branch information
koic authored and bbatsov committed Feb 26, 2020
1 parent 9074c6b commit 7cce1fe
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 43 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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)

Expand Down
31 changes: 22 additions & 9 deletions lib/rubocop/config.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
7 changes: 2 additions & 5 deletions lib/rubocop/config_loader.rb
Expand Up @@ -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 ' \
Expand Down
92 changes: 63 additions & 29 deletions spec/rubocop/cli/cli_options_spec.rb
Expand Up @@ -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
Expand Down

0 comments on commit 7cce1fe

Please sign in to comment.