From 0d09c1bc9f4099d25db4f3f7ff870c9bd4ff6b37 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Tue, 7 Jul 2020 14:15:17 +0900 Subject: [PATCH] [Fix #8256] Fix an error for `--auto-gen-config` when running a cop without autocorrect Fixes #8256. This PR fixes an error for `--auto-gen-config` when running a cop who do not support auto-correction. --- CHANGELOG.md | 1 + .../formatter/disabled_config_formatter.rb | 2 +- spec/rubocop/cli/cli_auto_gen_config_spec.rb | 41 +++++++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3576749bba4..c1e3055a63d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ * [#8252](https://github.com/rubocop-hq/rubocop/issues/8252): Fix a command line option name from `--safe-autocorrect` to `--safe-auto-correct`, which is compatible with RuboCop 0.86 and lower. ([@koic][]) * [#8239](https://github.com/rubocop-hq/rubocop/pull/8239): Don't load `.rubocop.yml` from personal folders to check for exclusions if given a custom configuration file. ([@deivid-rodriguez][]) +* [#8256](https://github.com/rubocop-hq/rubocop/issues/8256): Fix an error for `--auto-gen-config` when running a cop who do not support auto-correction. ([@koic][]) ## 0.87.0 (2020-07-06) diff --git a/lib/rubocop/formatter/disabled_config_formatter.rb b/lib/rubocop/formatter/disabled_config_formatter.rb index 81315e3b3b7..b10f24dc40f 100644 --- a/lib/rubocop/formatter/disabled_config_formatter.rb +++ b/lib/rubocop/formatter/disabled_config_formatter.rb @@ -115,7 +115,7 @@ def output_cop_comments(output_buffer, cfg, cop_name, offense_count) output_buffer.puts "# Offense count: #{offense_count}" if @show_offense_counts cop_class = Cop::Cop.registry.find_by_cop_name(cop_name) - output_buffer.puts '# Cop supports --auto-correct.' if cop_class&.new&.support_autocorrect? + output_buffer.puts '# Cop supports --auto-correct.' if cop_class&.support_autocorrect? default_cfg = default_config(cop_name) return unless default_cfg diff --git a/spec/rubocop/cli/cli_auto_gen_config_spec.rb b/spec/rubocop/cli/cli_auto_gen_config_spec.rb index b98858daf95..5f794a483cc 100644 --- a/spec/rubocop/cli/cli_auto_gen_config_spec.rb +++ b/spec/rubocop/cli/cli_auto_gen_config_spec.rb @@ -441,6 +441,47 @@ def f end end + context 'when working with a cop who do not support auto-correction' do + it 'can generate a todo list' do + create_file('example1.rb', <<~RUBY) + def fooBar; end + RUBY + create_file('.rubocop.yml', <<~YAML) + # The following cop does not support auto-correction. + Naming/MethodName: + Enabled: true + YAML + expect(cli.run(%w[--auto-gen-config])).to eq(0) + expect($stderr.string).to eq('') + # expect($stdout.string).to include('Created .rubocop_todo.yml.') + expect(Dir['.*']).to include('.rubocop_todo.yml') + todo_contents = IO.read('.rubocop_todo.yml').lines[8..-1].join + expect(todo_contents).to eq(<<~YAML) + # Offense count: 1 + # Configuration parameters: EnforcedStyle, IgnoredPatterns. + # SupportedStyles: snake_case, camelCase + Naming/MethodName: + Exclude: + - 'example1.rb' + + # Offense count: 1 + # Cop supports --auto-correct. + # Configuration parameters: EnforcedStyle. + # SupportedStyles: always, always_true, never + Style/FrozenStringLiteralComment: + Exclude: + - 'example1.rb' + YAML + expect(IO.read('.rubocop.yml')).to eq(<<~YAML) + inherit_from: .rubocop_todo.yml + + # The following cop does not support auto-correction. + Naming/MethodName: + Enabled: true + YAML + end + end + context 'when working in a subdirectory' do it 'can generate a todo list' do create_file('dir/example1.rb', ['$x = 0 ',