diff --git a/lib/rubocop/cop/internal_affairs/undefined_config.rb b/lib/rubocop/cop/internal_affairs/undefined_config.rb index bb248c80c4bb1..1e44dd49c8d0e 100644 --- a/lib/rubocop/cop/internal_affairs/undefined_config.rb +++ b/lib/rubocop/cop/internal_affairs/undefined_config.rb @@ -5,12 +5,21 @@ module Cop module InternalAffairs # Looks for references to a cop configuration key that isn't defined in config/default.yml. class UndefinedConfig < Base + # FileFinder is private API not expected to be used by cops but its fine for InternalAffairs + extend FileFinder + ALLOWED_CONFIGURATIONS = %w[ Safe SafeAutoCorrect AutoCorrect Severity StyleGuide Details Reference Include Exclude ].freeze RESTRICT_ON_SEND = %i[[] fetch].freeze MSG = '`%s` is not defined in the configuration for `%s` ' \ 'in `config/default.yml`.' + CONFIG_PATH = find_file_upwards('config/default.yml', Dir.pwd) + CONFIG = if CONFIG_PATH + ConfigLoader.load_yaml_configuration(CONFIG_PATH) + else + {} + end # @!method cop_class_def(node) def_node_search :cop_class_def, <<~PATTERN @@ -31,7 +40,7 @@ def on_new_investigation cop_class = cop_class_def(processed_source.ast).first return unless (@cop_class_name = extract_cop_name(cop_class)) - @config_for_cop = RuboCop::ConfigLoader.default_configuration.for_cop(@cop_class_name) + @config_for_cop = CONFIG[@cop_class_name] || {} end def on_send(node) diff --git a/spec/rubocop/cop/internal_affairs/undefined_config_spec.rb b/spec/rubocop/cop/internal_affairs/undefined_config_spec.rb index 210b6a0a85a69..6caf43842e603 100644 --- a/spec/rubocop/cop/internal_affairs/undefined_config_spec.rb +++ b/spec/rubocop/cop/internal_affairs/undefined_config_spec.rb @@ -12,9 +12,8 @@ Defined: true YAML - allow(RuboCop::ConfigLoader).to receive(:default_configuration).and_return( - RuboCop::ConfigLoader.load_file('config/default.yml', check: false) - ) + stub_const('RuboCop::Cop::InternalAffairs::UndefinedConfig::CONFIG', + RuboCop::ConfigLoader.load_yaml_configuration('config/default.yml')) end it 'does not register an offense for implicit configuration keys' do diff --git a/spec/rubocop/file_finder_spec.rb b/spec/rubocop/file_finder_spec.rb index 465d29f9a1228..42c4de5ebcb0a 100644 --- a/spec/rubocop/file_finder_spec.rb +++ b/spec/rubocop/file_finder_spec.rb @@ -19,6 +19,16 @@ it 'returns nil when file is not found' do expect(finder.find_file_upwards('file2', 'dir')).to be_nil end + + context 'when searching for a file inside a directory' do + it 'returns a file to be found upwards' do + expect(finder.find_file_upwards('dir/file', Dir.pwd)).to eq(File.expand_path('file', 'dir')) + end + + it 'returns nil when file is not found' do + expect(finder.find_file_upwards('dir2/file', Dir.pwd)).to be_nil + end + end end describe '#find_last_file_upwards' do