Skip to content

Commit

Permalink
Split a custom rake task into rubocop:autocorrect and `rubocop:auto…
Browse files Browse the repository at this point in the history
…correct_all`

Follow up rubocop#10709 (comment).

This PR splits deprecated `rubocop:auto_correct` custom rake task
into `rubocop:autocorrect` and `rubocop:autocorrect_all`.
  • Loading branch information
koic committed Jun 24, 2022
1 parent 8eb4f1b commit e60d08f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 11 deletions.
@@ -1 +1 @@
* [#10709](https://github.com/rubocop/rubocop/pull/10709): Rename custom rake task from `rubocop:auto_correct` to `rubocop:autocorrect`. ([@koic][])
* [#10709](https://github.com/rubocop/rubocop/pull/10709): Deprecate `rubocop:auto_correct` custom rake task and newly split `rubocop:autocorrect` and `rubocop:autocorrect-all` custom rake tasks. ([@koic][])
29 changes: 21 additions & 8 deletions lib/rubocop/rake_task.rb
Expand Up @@ -32,6 +32,15 @@ def initialize(name = :rubocop, *args, &task_block)

private

def perform(option)
options = full_options.unshift(option)
# `parallel` will automatically be removed from the options internally.
# This is a nice to have to suppress the warning message
# about --parallel and --autocorrect not being compatible.
options.delete('--parallel')
run_cli(verbose, options)
end

def run_cli(verbose, options)
# We lazy-load RuboCop so that the task doesn't dramatically impact the
# load time of your Rakefile.
Expand Down Expand Up @@ -65,22 +74,26 @@ def setup_subtasks(name, *args, &task_block) # rubocop:disable Metrics/AbcSize,
# rubocop:todo Naming/InclusiveLanguage
task(:auto_correct, *args) do
warn Rainbow(
'rubocop:auto_correct task is deprecated; use rubocop:autocorrect task instead.'
'rubocop:auto_correct task is deprecated; ' \
'use rubocop:autocorrect task or rubocop:autocorrect_all task instead.'
).yellow
::Rake::Task['rubocop:autocorrect'].invoke
end
# rubocop:enable Naming/InclusiveLanguage

desc 'Autocorrect RuboCop offenses'
desc "Autocorrect RuboCop offenses (only when it's safe)."
task(:autocorrect, *args) do |_, task_args|
RakeFileUtils.verbose(verbose) do
yield(*[self, task_args].slice(0, task_block.arity)) if task_block
options = full_options.unshift('--autocorrect-all')
# `parallel` will automatically be removed from the options internally.
# This is a nice to have to suppress the warning message
# about --parallel and --autocorrect not being compatible.
options.delete('--parallel')
run_cli(verbose, options)
perform('--autocorrect')
end
end

desc 'Autocorrect RuboCop offenses (safe and unsafe).'
task(:autocorrect_all, *args) do |_, task_args|
RakeFileUtils.verbose(verbose) do
yield(*[self, task_args].slice(0, task_block.arity)) if task_block
perform('--autocorrect-all')
end
end
end
Expand Down
16 changes: 14 additions & 2 deletions spec/rubocop/rake_task_spec.rb
Expand Up @@ -141,6 +141,18 @@
end

context 'autocorrect' do
it 'runs with --autocorrect' do
described_class.new

cli = instance_double(RuboCop::CLI, run: 0)
allow(RuboCop::CLI).to receive(:new).and_return(cli)
options = ['--autocorrect']

expect(cli).to receive(:run).with(options)

Rake::Task['rubocop:autocorrect'].execute
end

it 'runs with --autocorrect-all' do
described_class.new

Expand All @@ -150,7 +162,7 @@

expect(cli).to receive(:run).with(options)

Rake::Task['rubocop:autocorrect'].execute
Rake::Task['rubocop:autocorrect_all'].execute
end

it 'runs with with the options that were passed to its parent task' do
Expand All @@ -168,7 +180,7 @@

expect(cli).to receive(:run).with(options)

Rake::Task['rubocop:autocorrect'].execute
Rake::Task['rubocop:autocorrect_all'].execute
end
end
end
Expand Down

0 comments on commit e60d08f

Please sign in to comment.