From c4a1c365ab4f263e7464436c9311eb59d8c0c03a Mon Sep 17 00:00:00 2001 From: Earlopain <14981592+Earlopain@users.noreply.github.com> Date: Tue, 20 Aug 2024 11:58:45 +0200 Subject: [PATCH] Load only project config for `InternalAffairs/UndefinedConfig` Going through the default config is fine for RuboCop itself but extensions need to inject their config for RuboCop to pick them up. This makes it necessary to load the extension, even if the extension itself doesn't use it, like `rubocop-rails` or `rubocop-capybara`. Instead just load config/default.yml from `Dir.pwd` in isolation and use just that. This would make https://github.com/rubocop/rubocop-capybara/pull/133 and https://github.com/rubocop/rubocop-rails/commit/288c7ce265d7fd0f5a244c6887224fc975ff2eb9 unnecessary I tested this against a bunch of extensions and the cop seems to continue its job. It also fixes a false positive in `rubocop-factory_bot` --- lib/rubocop/cop/internal_affairs/undefined_config.rb | 11 ++++++++++- .../cop/internal_affairs/undefined_config_spec.rb | 5 ++--- spec/rubocop/file_finder_spec.rb | 10 ++++++++++ 3 files changed, 22 insertions(+), 4 deletions(-) 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