Skip to content

Commit

Permalink
[Change #9744] Auto disable parallel when combined with cache false
Browse files Browse the repository at this point in the history
  • Loading branch information
rrosenblum authored and bbatsov committed May 17, 2021
1 parent 40d0970 commit dca0471
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 25 deletions.
1 change: 1 addition & 0 deletions changelog/change_disable_parallel_with_cache_false.md
@@ -0,0 +1 @@
* [#9744](https://github.com/rubocop/rubocop/pull/9744): The parallel flag will now be automatically ignored when combined with `--cache false`. Previously, an error was raised and execution stopped. ([@rrosenblum][])
34 changes: 14 additions & 20 deletions lib/rubocop/options.rb
Expand Up @@ -297,7 +297,7 @@ def validate_compatibility # rubocop:disable Metrics/MethodLength
validate_auto_gen_config
validate_auto_correct
validate_display_only_failed
validate_parallel
disable_parallel_when_invalid_option_combo

return if incompatible_options.size <= 1

Expand Down Expand Up @@ -334,33 +334,27 @@ def validate_auto_correct
format('--disable-uncorrectable can only be used together with --auto-correct.')
end

def validate_parallel
def disable_parallel_when_invalid_option_combo
return unless @options.key?(:parallel)

if @options[:cache] == 'false'
raise OptionArgumentError, '-P/--parallel uses caching to speed up ' \
'execution, so combining with --cache ' \
'false is not allowed.'
end

disable_parallel_when_invalid_combo
end
invalid_options = [
{ name: :auto_gen_config, value: true, flag: '--auto-gen-config' },
{ name: :fail_fast, value: true, flag: '-F/--fail-fast.' },
{ name: :auto_correct, value: true, flag: '--auto-correct.' },
{ name: :cache, value: 'false', flag: '--cache false' }
]

def disable_parallel_when_invalid_combo
combos = {
auto_gen_config: '--auto-gen-config',
fail_fast: '-F/--fail-fast.',
auto_correct: '--auto-correct.'
}

invalid_combos = combos.select { |key, _flag| @options.key?(key) }
invalid_flags = invalid_options.each_with_object([]) do |option, flags|
# `>` rather than `>=` because `@options` will also contain `parallel: true`
flags << option[:flag] if @options > { option[:name] => option[:value] }
end

return if invalid_combos.empty?
return if invalid_flags.empty?

@options.delete(:parallel)

puts '-P/--parallel is being ignored because ' \
"it is not compatible with #{invalid_combos.values.join(', ')}"
"it is not compatible with #{invalid_flags.join(', ')}"
end

def only_includes_redundant_disable?
Expand Down
10 changes: 5 additions & 5 deletions spec/rubocop/options_spec.rb
Expand Up @@ -199,11 +199,11 @@ def abs(path)

describe '--parallel' do
context 'combined with --cache false' do
it 'fails with an error message' do
msg = ['-P/--parallel uses caching to speed up execution, so ',
'combining with --cache false is not allowed.'].join
expect { options.parse %w[--parallel --cache false] }
.to raise_error(RuboCop::OptionArgumentError, msg)
it 'ignores parallel' do
msg = '-P/--parallel is being ignored because it is not compatible with --cache false'
options.parse %w[--parallel --cache false]
expect($stdout.string).to include(msg)
expect(options.instance_variable_get('@options').keys).not_to include(:parallel)
end
end

Expand Down

0 comments on commit dca0471

Please sign in to comment.