diff --git a/changelog/fix_no_exclude_limit_max.md b/changelog/fix_no_exclude_limit_max.md new file mode 100644 index 000000000000..d68cf197aa54 --- /dev/null +++ b/changelog/fix_no_exclude_limit_max.md @@ -0,0 +1 @@ +* [#12888](https://github.com/rubocop/rubocop/issues/12888): Fix `--no-exclude-limit` generating a todo with `Max` config instead of listing everything out with `Exclude`. ([@earlopain][]) diff --git a/lib/rubocop/formatter/disabled_config_formatter.rb b/lib/rubocop/formatter/disabled_config_formatter.rb index 90056e00c1ad..2ad83b23719a 100644 --- a/lib/rubocop/formatter/disabled_config_formatter.rb +++ b/lib/rubocop/formatter/disabled_config_formatter.rb @@ -116,20 +116,24 @@ def output_cop(cop_name, offense_count) def set_max(cfg, cop_name) return unless cfg[:exclude_limit] - max_set_in_user_config = - @config_for_pwd.for_cop(cop_name)['Max'] != default_config(cop_name)['Max'] - if !max_set_in_user_config && - # In case auto_gen_only_exclude is set, only modify the maximum if the files are not - # excluded one by one. - (!@options[:auto_gen_only_exclude] || - @files_with_offenses[cop_name].size > @exclude_limit) - cfg.merge!(cfg[:exclude_limit]) - end + cfg.merge!(cfg[:exclude_limit]) if should_set_max?(cop_name) # Remove already used exclude_limit. cfg.reject! { |key| key == :exclude_limit } end + def should_set_max?(cop_name) + max_set_in_user_config = + @config_for_pwd.for_cop(cop_name)['Max'] != default_config(cop_name)['Max'] + + max_allowed = !max_set_in_user_config && !no_exclude_limit? + return false unless max_allowed + + # In case auto_gen_only_exclude is set, only modify the maximum if the files are not + # excluded one by one. + !@options[:auto_gen_only_exclude] || @files_with_offenses[cop_name].size > @exclude_limit + end + def output_cop_comments(output_buffer, cfg, cop_name, offense_count) output_buffer.puts "# Offense count: #{offense_count}" if show_offense_counts? diff --git a/spec/rubocop/cli/auto_gen_config_spec.rb b/spec/rubocop/cli/auto_gen_config_spec.rb index e9e96c55cf5a..67658e7d7c3e 100644 --- a/spec/rubocop/cli/auto_gen_config_spec.rb +++ b/spec/rubocop/cli/auto_gen_config_spec.rb @@ -1354,33 +1354,68 @@ def function(arg1, arg2, arg3, arg4, arg5, arg6, arg7) end describe 'when --no-exclude-limit is given' do - before do - offending_files_count.times do |i| - create_file("example#{i}.rb", [' ']) - end - end - let(:offending_files_count) do RuboCop::Options::DEFAULT_MAXIMUM_EXCLUSION_ITEMS + 1 end - it 'always prefers Exclude to Enabled' do - expect(cli.run(['--auto-gen-config', '--no-exclude-limit'])).to eq(0) - lines = File.readlines('.rubocop_todo.yml') - expect(lines[1]).to eq("# `rubocop --auto-gen-config --no-exclude-limit`\n") - expect(lines[9..12].join).to eq( - <<~YAML - # This cop supports safe autocorrection (--autocorrect). - # Configuration parameters: AllowInHeredoc. - Layout/TrailingWhitespace: - Exclude: + context 'for a cop without Max configuration' do + before do + offending_files_count.times do |i| + create_file("example#{i}.rb", [' ']) + end + end + + it 'always prefers Exclude to Enabled' do + expect(cli.run(['--auto-gen-config', '--no-exclude-limit'])).to eq(0) + lines = File.readlines('.rubocop_todo.yml') + expect(lines[1]).to eq("# `rubocop --auto-gen-config --no-exclude-limit`\n") + expect(lines[9..12].join).to eq( + <<~YAML + # This cop supports safe autocorrection (--autocorrect). + # Configuration parameters: AllowInHeredoc. + Layout/TrailingWhitespace: + Exclude: + YAML + ) + expect(lines[13..]).to eq( + Array.new(offending_files_count) do |i| + " - 'example#{i}.rb'\n" + end.sort + ) + end + end + + context 'for a cop with Max config' do + before do + create_file('.rubocop.yml', <<~YAML) + AllCops: + DisabledByDefault: true + Metrics/ClassLength: + Enabled: true YAML - ) - expect(lines[13..]).to eq( - Array.new(offending_files_count) do |i| - " - 'example#{i}.rb'\n" - end.sort - ) + offending_files_count.times do |i| + max_lines = RuboCop::ConfigLoader.default_configuration['Metrics/ClassLength']['Max'] + create_file("example#{i}.rb", ['class Foo', *Array.new(max_lines + 1, ' bar'), 'end']) + end + end + + it 'always prefers Exclude to Enabled' do + expect(cli.run(['--auto-gen-config', '--auto-gen-only-exclude', + '--no-exclude-limit'])).to eq(0) + lines = File.readlines('.rubocop_todo.yml') + expect(lines[9..11].join).to eq( + <<~YAML + # Configuration parameters: CountComments, Max, CountAsOne. + Metrics/ClassLength: + Exclude: + YAML + ) + expect(lines[12..]).to eq( + Array.new(offending_files_count) do |i| + " - 'example#{i}.rb'\n" + end.sort + ) + end end end