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 #8256] Fix an error for --auto-gen-config when running a cop without autocorrect #8258

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
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/formatter/disabled_config_formatter.rb
Expand Up @@ -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
Expand Down
41 changes: 41 additions & 0 deletions spec/rubocop/cli/cli_auto_gen_config_spec.rb
Expand Up @@ -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 ',
Expand Down