Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix #6262] Optimise --auto-gen-config when Metrics/LineLength cop is disabled #6343

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Bug fixes

* [#6554](https://github.com/rubocop-hq/rubocop/issues/6554): Prevent Layout/RescueEnsureAlignment cop from breaking on block assignment when assignment is on a separate line. ([@timmcanty][])
* [#6343](https://github.com/rubocop-hq/rubocop/pull/6343): Optimise `--auto-gen-config` when `Metrics/LineLength` cop is disabled. ([@tom-lord][])
* [#6389](https://github.com/rubocop-hq/rubocop/pull/6389): Fix false negative for `Style/TrailingCommaInHashLitera`/`Style/TrailingCommaInArrayLiteral` when there is a comment in the last line. ([@bayandin][])

## 0.61.1 (2018-12-06)
Expand Down Expand Up @@ -3687,4 +3688,5 @@
[@dduugg]: https://github.com/dduugg
[@mmedal]: https://github.com/mmedal
[@timmcanty]: https://github.com/timmcanty
[@tom-lord]: https://github.com/tom-lord
[@bayandin]: https://github.com/bayandin
52 changes: 38 additions & 14 deletions lib/rubocop/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@ module RuboCop
class CLI
include Formatter::TextUtil

SKIPPED_PHASE_1 = 'Phase 1 of 2: run Metrics/LineLength cop (skipped ' \
'because the default Metrics/LineLength:Max is ' \
'overridden)'.freeze
PHASE_1 = 'Phase 1 of 2: run Metrics/LineLength cop'.freeze
PHASE_2 = 'Phase 2 of 2: run all cops'.freeze

PHASE_1_OVERRIDDEN = '(skipped because the default Metrics/LineLength:Max' \
' is overridden)'.freeze
PHASE_1_DISABLED = '(skipped because Metrics/LineLength is ' \
'disabled)'.freeze

STATUS_SUCCESS = 0
STATUS_OFFENSES = 1
STATUS_ERROR = 2
Expand Down Expand Up @@ -64,28 +69,47 @@ def run(args = ARGV)
def execute_runners(paths)
if @options[:auto_gen_config]
reset_config_and_auto_gen_file
line_length_contents =
if max_line_length(@config_store.for(Dir.pwd)) ==
max_line_length(ConfigLoader.default_configuration)
run_line_length_cop_auto_gen_config(paths)
else
puts Rainbow(SKIPPED_PHASE_1).yellow
''
end
line_length_contents = maybe_run_line_length_cop(paths)
run_all_cops_auto_gen_config(line_length_contents, paths)
else
execute_runner(paths)
end
end

def maybe_run_line_length_cop(paths)
if !line_length_enabled?(@config_store.for(Dir.pwd))
puts Rainbow("#{PHASE_1} #{PHASE_1_DISABLED}").yellow
''
elsif !same_max_line_length?(
@config_store.for(Dir.pwd), ConfigLoader.default_configuration
)
puts Rainbow("#{PHASE_1} #{PHASE_1_OVERRIDDEN}").yellow
''
else
run_line_length_cop_auto_gen_config(paths)
end
end

def line_length_enabled?(config)
line_length_cop(config)['Enabled']
end

def same_max_line_length?(config1, config2)
max_line_length(config1) == max_line_length(config2)
end

def max_line_length(config)
config.for_cop('Metrics/LineLength')['Max']
line_length_cop(config)['Max']
end

def line_length_cop(config)
config.for_cop('Metrics/LineLength')
end

# Do an initial run with only Metrics/LineLength so that cops that depend
# on Metrics/LineLength:Max get the correct value for that parameter.
def run_line_length_cop_auto_gen_config(paths)
puts Rainbow('Phase 1 of 2: run Metrics/LineLength cop').yellow
puts Rainbow(PHASE_1).yellow
@options[:only] = ['Metrics/LineLength']
execute_runner(paths)
@options.delete(:only)
Expand All @@ -98,7 +122,7 @@ def run_line_length_cop_auto_gen_config(paths)
end

def run_all_cops_auto_gen_config(line_length_contents, paths)
puts Rainbow('Phase 2 of 2: run all cops').yellow
puts Rainbow(PHASE_2).yellow
result = execute_runner(paths)
# This run was made with the current maximum length allowed, so append
# the saved setting for LineLength.
Expand Down
49 changes: 48 additions & 1 deletion spec/rubocop/cli/cli_auto_gen_config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ def f
context 'with Metrics/LineLength:Max overridden' do
before do
create_file('.rubocop.yml', ['Metrics/LineLength:',
" Max: #{line_length_max}"])
" Max: #{line_length_max}",
" Enabled: #{line_length_enabled}"])
create_file('.rubocop_todo.yml', [''])
create_file('example.rb', <<-RUBY.strip_indent)
def f
Expand All @@ -146,6 +147,7 @@ def f
context 'when .rubocop.yml has Metrics/LineLength:Max less than code ' \
'base max' do
let(:line_length_max) { 90 }
let(:line_length_enabled) { true }

it "bases other cops' configuration on the overridden LineLength:Max" do
expect(cli.run(['--auto-gen-config'])).to eq(0)
Expand Down Expand Up @@ -174,6 +176,7 @@ def f

Metrics/LineLength:
Max: 90
Enabled: true
YAML
$stdout = StringIO.new
expect(described_class.new.run(%w[--format simple --debug])).to eq(1)
Expand All @@ -189,9 +192,52 @@ def f
end
end

context 'when .rubocop.yml has Metrics/LineLength disabled ' do
let(:line_length_max) { 90 }
let(:line_length_enabled) { false }

it 'skips the cop from both phases of the run' do
expect(cli.run(['--auto-gen-config'])).to eq(0)
expect($stdout.string).to include(<<-YAML.strip_indent)
Added inheritance from `.rubocop_todo.yml` in `.rubocop.yml`.
Phase 1 of 2: run Metrics/LineLength cop (skipped because Metrics/LineLength is disabled)
Phase 2 of 2: run all cops
YAML

# The code base max line length is 99, but the setting Enabled: false
# overrides that so no Metrics/LineLength:Max setting is generated in
# .rubocop_todo.yml.
expect(IO.readlines('.rubocop_todo.yml')
.drop_while { |line| line.start_with?('#') }.join)
.to eq(<<-YAML.strip_indent)

# Offense count: 1
# Cop supports --auto-correct.
Style/IfUnlessModifier:
Exclude:
- 'example.rb'
YAML
expect(IO.read('.rubocop.yml')).to eq(<<-YAML.strip_indent)
inherit_from: .rubocop_todo.yml

Metrics/LineLength:
Max: 90
Enabled: false
YAML
$stdout = StringIO.new
expect(described_class.new.run(%w[--format simple])).to eq(0)
expect($stderr.string).to eq('')
expect($stdout.string).to eq(<<-OUTPUT.strip_indent)

1 file inspected, no offenses detected
OUTPUT
end
end

context 'when .rubocop.yml has Metrics/LineLength:Max more than code ' \
'base max' do
let(:line_length_max) { 150 }
let(:line_length_enabled) { true }

it "bases other cops' configuration on the overridden LineLength:Max" do
expect(cli.run(['--auto-gen-config'])).to eq(0)
Expand All @@ -218,6 +264,7 @@ def f

Metrics/LineLength:
Max: 150
Enabled: true
YAML
$stdout = StringIO.new
expect(described_class.new.run(%w[--format simple])).to eq(0)
Expand Down