Skip to content

Commit

Permalink
[Fix #10026] Fix inherit_mode resolution for parent and default config
Browse files Browse the repository at this point in the history
The problem was that we deleted the `inherit_mode` entry in the
configuration hash while processing the resolution of inheritance from
base configurations (`inherit_from`, etc.). When the merging of default
configuration and user configuration was done at a later stage, the
`inherit_mode` parameter was gone.

By not deleting `inherit_mode`, we fix the problem. I don't think it
has any unwanted side effects, but `inherit_mode` does show up in the
output from `--show-cops` now. This should be OK.
  • Loading branch information
jonas054 authored and bbatsov committed Jul 7, 2022
1 parent b8a1cb0 commit 354f88b
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 2 deletions.
1 change: 1 addition & 0 deletions changelog/fix_inherit_mode_resolution.md
@@ -0,0 +1 @@
* [#10026](https://github.com/rubocop/rubocop/issues/10026): Fix merging of array parameters in either parent of default config. ([@jonas054][])
2 changes: 1 addition & 1 deletion lib/rubocop/config_loader_resolver.rb
Expand Up @@ -179,7 +179,7 @@ def warn_on_duplicate_setting(base_hash, derived_hash, key, **opts)

def determine_inherit_mode(hash, key)
cop_cfg = hash[key]
local_inherit = cop_cfg.delete('inherit_mode') if cop_cfg.is_a?(Hash)
local_inherit = cop_cfg['inherit_mode'] if cop_cfg.is_a?(Hash)
local_inherit || hash['inherit_mode'] || {}
end

Expand Down
47 changes: 46 additions & 1 deletion spec/rubocop/cli/options_spec.rb
Expand Up @@ -1203,8 +1203,16 @@ def on_send(node)
create_file('.rubocop.yml', <<~YAML)
Layout/LineLength:
Max: 110
Lint/DeprecatedConstants:
inherit_mode:
merge:
- DeprecatedConstants
DeprecatedConstants:
MY_CONST:
Alternative: 'MyConst'
DeprecatedVersion: '2.7'
YAML
# expect(cli.run(['--show-cops'] + arguments)).to eq(0)

cli.run(['--show-cops'] + arguments)
end

Expand Down Expand Up @@ -1277,6 +1285,43 @@ def full_description_of_cop(cop)
include_examples 'prints config'
end

context 'with one cop given and inherit_mode set in its local configuration' do
let(:arguments) { ['Lint/DeprecatedConstants'] }

it 'prints that cop including inherit_mode' do
expect(stdout).to match(
['# Supports --autocorrect',
'Lint/DeprecatedConstants:',
' Description: Checks for deprecated constants.',
' Enabled: pending',
/^ VersionAdded: '[0-9.]+'$/,
/^ VersionChanged: '[0-9.]+'$/,
' DeprecatedConstants:',
' NIL:',
' Alternative: nil',
" DeprecatedVersion: '2.4'",
" 'TRUE':",
" Alternative: 'true'",
" DeprecatedVersion: '2.4'",
" 'FALSE':",
" Alternative: 'false'",
" DeprecatedVersion: '2.4'",
' Net::HTTPServerException:',
' Alternative: Net::HTTPClientException',
" DeprecatedVersion: '2.6'",
' Random::DEFAULT:',
' Alternative: Random.new',
" DeprecatedVersion: '3.0'",
' MY_CONST:',
' Alternative: MyConst',
" DeprecatedVersion: '2.7'",
' inherit_mode:',
' merge:',
' - DeprecatedConstants'].join("\n")
)
end
end

context 'with two cops given' do
let(:arguments) { ['Layout/IndentationStyle,Layout/LineLength'] }

Expand Down
45 changes: 45 additions & 0 deletions spec/rubocop/config_loader_spec.rb
Expand Up @@ -590,6 +590,51 @@
end
end

context 'when inherit_mode:merge for a cop lists parameters that are either in parent or in ' \
'default configuration' do
let(:file_path) { '.rubocop.yml' }

before do
create_file('hosted_config.yml', <<~YAML)
AllCops:
NewCops: enable
Naming/VariableNumber:
EnforcedStyle: snake_case
Exclude:
- foo.rb
Include:
- bar.rb
YAML
create_file(file_path, <<~YAML)
inherit_from:
- hosted_config.yml
Naming/VariableNumber:
inherit_mode:
merge:
- AllowedIdentifiers
- Exclude
- Include
AllowedIdentifiers:
- iso2
Exclude:
- test.rb
Include:
- another_test.rb
YAML
end

it 'merges array parameters with parent or default configuration' do
examples_configuration = configuration_from_file['Naming/VariableNumber']
expect(examples_configuration['Exclude'].map { |abs_path| File.basename(abs_path) })
.to contain_exactly('foo.rb', 'test.rb')
expect(examples_configuration['Include']).to contain_exactly('bar.rb', 'another_test.rb')
expect(examples_configuration['AllowedIdentifiers'])
.to contain_exactly(*%w[capture3 iso8601 rfc1123_date rfc2822 rfc3339 rfc822 iso2])
end
end

context 'when a department is disabled', :restore_registry do
let(:file_path) { '.rubocop.yml' }

Expand Down

0 comments on commit 354f88b

Please sign in to comment.