From e60d08f42d5bab1e65d197e8c3b67f9c85f003de Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Fri, 24 Jun 2022 01:46:37 +0900 Subject: [PATCH] Split a custom rake task into `rubocop:autocorrect` and `rubocop:autocorrect_all` Follow up https://github.com/rubocop/rubocop/pull/10709#discussion_r905164287. This PR splits deprecated `rubocop:auto_correct` custom rake task into `rubocop:autocorrect` and `rubocop:autocorrect_all`. --- ...custom_rake_task_to_rubocop_autocorrect.md | 2 +- lib/rubocop/rake_task.rb | 29 ++++++++++++++----- spec/rubocop/rake_task_spec.rb | 16 ++++++++-- 3 files changed, 36 insertions(+), 11 deletions(-) diff --git a/changelog/change_rename_custom_rake_task_to_rubocop_autocorrect.md b/changelog/change_rename_custom_rake_task_to_rubocop_autocorrect.md index 8ac6c4ea7f5..8cc0e152dad 100644 --- a/changelog/change_rename_custom_rake_task_to_rubocop_autocorrect.md +++ b/changelog/change_rename_custom_rake_task_to_rubocop_autocorrect.md @@ -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][]) diff --git a/lib/rubocop/rake_task.rb b/lib/rubocop/rake_task.rb index 6a5c163ca74..60ed7f6d4de 100644 --- a/lib/rubocop/rake_task.rb +++ b/lib/rubocop/rake_task.rb @@ -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. @@ -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 diff --git a/spec/rubocop/rake_task_spec.rb b/spec/rubocop/rake_task_spec.rb index 94ca94f9098..667eaca2bf2 100644 --- a/spec/rubocop/rake_task_spec.rb +++ b/spec/rubocop/rake_task_spec.rb @@ -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 @@ -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 @@ -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