Skip to content

Commit

Permalink
Respect DisabledByDefault for custom cops (#7013)
Browse files Browse the repository at this point in the history
Before this commit, DisabledByDefault has no effect on
custom cops and they are enabled when the config files
contain no mention of them. This is a problematic
behavior as CI servers might load multiple custom cop
and run Rubocop over repos with vastly different
configs. Custom cops end up enabled for repos that
don't care about them, simply because they are loaded.
  • Loading branch information
XrXr authored and bbatsov committed May 15, 2019
1 parent 72402c0 commit 98ad15f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

### Bug fixes

* [#7013](https://github.com/rubocop-hq/rubocop/pull/7013): Respect DisabledByDefault for custom cops. ([@XrXr][])
* [#7043](https://github.com/rubocop-hq/rubocop/issues/7043): Prevent RDoc error when installing RuboCop 0.69.0 on Ubuntu. ([@koic][])

## 0.69.0 (2019-05-13)
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,7 @@ def enable_cop?(qualified_cop_name, cop_options)
end
end

cop_options.fetch('Enabled', true)
cop_options.fetch('Enabled') { !for_all_cops['DisabledByDefault'] }
end

def smart_loaded_path
Expand Down
34 changes: 34 additions & 0 deletions spec/rubocop/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,40 @@ def cop_enabled(cop_class)
expect(cop_enabled(cop_class)).to be true
end
end

context 'when configuration has no mention of a cop' do
let(:hash) do
{}
end

it 'enables the cop that is not mentioned' do
expect(cop_enabled('VeryCustomDepartment/CustomCop')).to be true
end

context 'when all cops are disabled by default' do
let(:hash) do
{
'AllCops' => { 'DisabledByDefault' => true }
}
end

it 'disables the cop that is not mentioned' do
expect(cop_enabled('VeryCustomDepartment/CustomCop')).to be false
end
end

context 'when all cops are explicitly enabled by default' do
let(:hash) do
{
'AllCops' => { 'EnabledByDefault' => true }
}
end

it 'enables the cop that is not mentioned' do
expect(cop_enabled('VeryCustomDepartment/CustomCop')).to be true
end
end
end
end

describe '#target_rails_version' do
Expand Down

0 comments on commit 98ad15f

Please sign in to comment.