Skip to content

Commit

Permalink
Merge pull request #12598 from jonas054/12179_auto_gen_config_with_max
Browse files Browse the repository at this point in the history
[Fix #12179] Generate Exclude when Max is overridden
  • Loading branch information
koic committed Jan 7, 2024
2 parents 0f2697d + 8844cef commit 3ce4fbf
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 28 deletions.
1 change: 1 addition & 0 deletions changelog/fix_auto_gen_config_exclude_with_max_set.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#12179](https://github.com/rubocop/rubocop/issues/12179): Let `--auto-gen-config` generate `Exclude` when `Max` is overridden. ([@jonas054][])
13 changes: 9 additions & 4 deletions lib/rubocop/formatter/disabled_config_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ def initialize(output, options = {})
@files_with_offenses ||= {}
end

def file_started(_file, _file_info)
def file_started(_file, options)
@config_for_pwd = options[:config_store].for_pwd
@exclude_limit_option = @options[:exclude_limit]
@exclude_limit = Integer(@exclude_limit_option ||
RuboCop::Options::DEFAULT_MAXIMUM_EXCLUSION_ITEMS)
Expand Down Expand Up @@ -115,9 +116,13 @@ def output_cop(cop_name, offense_count)
def set_max(cfg, cop_name)
return unless cfg[:exclude_limit]

# In case auto_gen_only_exclude is set, only modify the maximum if the
# files are not excluded one by one.
if !@options[:auto_gen_only_exclude] || @files_with_offenses[cop_name].size > @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

Expand Down
54 changes: 37 additions & 17 deletions spec/rubocop/cli/auto_gen_config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,21 +155,21 @@ def f
Phase 1 of 2: run Layout/LineLength cop (skipped because the default Layout/LineLength:Max is overridden)
Phase 2 of 2: run all cops
YAML
# We generate a Layout/LineLength:Max even though it's overridden in
# .rubocop.yml. We want to show somewhere what the actual maximum is.
# Layout/LineLength gets an Exclude property because Max is set in .rubocop.yml.
#
# Note that there is no Style/IfUnlessModifier offense registered due
# to the Max:90 setting.
# Note that there is no Style/IfUnlessModifier offense registered due to the Max:90
# setting.
expect(File.readlines('.rubocop_todo.yml')
.drop_while { |line| line.start_with?('#') }.join)
.to eq(<<~YAML)
# Offense count: 1
# This cop supports safe autocorrection (--autocorrect).
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, AllowedPatterns.
# Configuration parameters: Max, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, AllowedPatterns.
# URISchemes: http, https
Layout/LineLength:
Max: 99
Exclude:
- 'example.rb'
# Offense count: 1
# This cop supports unsafe autocorrection (--autocorrect-all).
Expand All @@ -186,17 +186,7 @@ def f
Max: 90
Enabled: true
YAML
$stdout = StringIO.new
expect(RuboCop::CLI.new.run(%w[--format simple --debug])).to eq(1)
expect($stdout.string.include?('.rubocop.yml: Layout/LineLength:Max overrides the ' \
"same parameter in .rubocop_todo.yml\n"))
.to be(true)
expect($stdout.string.include?(<<~OUTPUT)).to be(true)
== example.rb ==
C: 2: 91: Layout/LineLength: Line is too long. [99/90]
1 file inspected, 1 offense detected
OUTPUT
expect(RuboCop::CLI.new.run([])).to eq(0)
end
end

Expand Down Expand Up @@ -1573,5 +1563,35 @@ def function(arg1, arg2, arg3, arg4, arg5, arg6, arg7)
it 'can be called when there are no files to inspection' do
expect(cli.run(['--auto-gen-config'])).to eq(0)
end

context 'when Max configuration is overridden in .rubocop.yml' do
it 'generates Exclude instead of Max' do
create_file('.rubocop.yml', <<~YAML)
Metrics/ClassLength:
Max: 250
Layout/EmptyLinesAroundClassBody:
Enabled: false
YAML
create_file(
'file.rb',
[
'# A long class',
'class TooLong',
*Array.new(100) { |i| [" def method#{i}", " #{i}", ' end', ''] },
'end'
].flatten
)
expect(cli.run(['--auto-gen-config'])).to eq(0)
expect(File.readlines('.rubocop_todo.yml').grep(/^ *[^#\s]/).join).to eq(<<~YAML)
Metrics/ClassLength:
Exclude:
- 'file.rb'
Style/FrozenStringLiteralComment:
Exclude:
- 'file.rb'
YAML
expect(cli.run([])).to eq(0)
end
end
end
end
17 changes: 10 additions & 7 deletions spec/rubocop/formatter/disabled_config_formatter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ def io.path
let(:expected_heading_command) { 'rubocop --auto-gen-config' }

let(:expected_heading_timestamp) { "on #{Time.now} " }
let(:config_store) { instance_double(RuboCop::ConfigStore) }
let(:options) { { config_store: config_store } }

around do |example|
original_stdout = $stdout
Expand All @@ -55,14 +57,15 @@ def io.path
RuboCop::ConfigLoader.clear_options

allow(Time).to receive(:now).and_return(Time.now)
allow(config_store).to receive(:for_pwd).and_return(instance_double(RuboCop::Config))
end

context 'when any offenses are detected' do
before do
formatter.started(['test_a.rb', 'test_b.rb'])
formatter.file_started('test_a.rb', {})
formatter.file_started('test_a.rb', options)
formatter.file_finished('test_a.rb', offenses)
formatter.file_started('test_b.rb', {})
formatter.file_started('test_b.rb', options)
formatter.file_finished('test_b.rb', [offenses.first])
formatter.finished(['test_a.rb', 'test_b.rb'])
end
Expand Down Expand Up @@ -101,9 +104,9 @@ def io.path
YAML

formatter.started(['test_a.rb', 'test_b.rb'])
formatter.file_started('test_a.rb', {})
formatter.file_started('test_a.rb', options)
formatter.file_finished('test_a.rb', offenses)
formatter.file_started('test_b.rb', {})
formatter.file_started('test_b.rb', options)
formatter.file_finished('test_b.rb', [offenses.first])

allow(RuboCop::ConfigLoader.default_configuration).to receive(:[]).and_return({})
Expand Down Expand Up @@ -138,7 +141,7 @@ def io.path
formatter.started(filenames)

filenames.each do |filename|
formatter.file_started(filename, {})
formatter.file_started(filename, options)

if filename == filenames.last
formatter.file_finished(filename, [offenses.first])
Expand Down Expand Up @@ -189,7 +192,7 @@ def io.path
formatter.started(filenames)

filenames.each do |filename|
formatter.file_started(filename, {})
formatter.file_started(filename, options)

if filename == filenames.last
formatter.file_finished(filename, [offenses.first])
Expand Down Expand Up @@ -245,7 +248,7 @@ def io.path
stub_cop_class('Test::Cop3') { extend RuboCop::Cop::AutoCorrector }

formatter.started(['test_autocorrect.rb'])
formatter.file_started('test_autocorrect.rb', {})
formatter.file_started('test_autocorrect.rb', options)
formatter.file_finished('test_autocorrect.rb', offenses)
formatter.finished(['test_autocorrect.rb'])
end
Expand Down

0 comments on commit 3ce4fbf

Please sign in to comment.