Skip to content

Commit

Permalink
Merge pull request #12902 from Earlopain/max-with-no-exclude-limit
Browse files Browse the repository at this point in the history
[Fix #12888] Fix `--no-exclude-limit` for cops with `Max` config option
  • Loading branch information
koic committed May 16, 2024
2 parents 5e25d8f + 094657f commit 065fb23
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 31 deletions.
1 change: 1 addition & 0 deletions changelog/fix_no_exclude_limit_max.md
Original file line number Diff line number Diff line change
@@ -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][])
22 changes: 13 additions & 9 deletions lib/rubocop/formatter/disabled_config_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?

Expand Down
79 changes: 57 additions & 22 deletions spec/rubocop/cli/auto_gen_config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 065fb23

Please sign in to comment.