Skip to content

Commit

Permalink
[Fix #5934] Handle --auto-gen-config --config
Browse files Browse the repository at this point in the history
When a --config option is given with a file name that should be used instead of
.rubocop.yml, we need to use the given file name throughout the somewhat
complicated generation of the todo file.
  • Loading branch information
jonas054 authored and bbatsov committed Oct 26, 2018
1 parent 014639f commit fcd56cb
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## master (unreleased)

### Bug fixes

* [#5934](https://github.com/rubocop-hq/rubocop/issues/5934): Handle the combination of `--auto-gen-config` and `--config FILE` correctly. ([@jonas054][])

## 0.60.0 (2018-10-26)

### New features
Expand Down
2 changes: 2 additions & 0 deletions lib/rubocop/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ def run_all_cops_auto_gen_config(line_length_contents, paths)

def reset_config_and_auto_gen_file
@config_store = ConfigStore.new
@config_store.options_config = @options[:config] if @options[:config]
File.open(ConfigLoader::AUTO_GENERATED_FILE, 'w') {}
ConfigLoader.add_inheritance_from_auto_generated_file
end
Expand All @@ -135,6 +136,7 @@ def act_on_options
ConfigLoader.debug = @options[:debug]
ConfigLoader.auto_gen_config = @options[:auto_gen_config]
ConfigLoader.ignore_parent_exclusion = @options[:ignore_parent_exclusion]
ConfigLoader.options_config = @options[:config]

@config_store.options_config = @options[:config] if @options[:config]
@config_store.force_default_config! if @options[:force_default_config]
Expand Down
18 changes: 10 additions & 8 deletions lib/rubocop/config_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,16 @@ class ConfigLoader
class << self
include FileFinder

attr_accessor :debug, :auto_gen_config, :ignore_parent_exclusion
attr_accessor :debug, :auto_gen_config, :ignore_parent_exclusion,
:options_config
attr_writer :default_configuration

alias debug? debug
alias auto_gen_config? auto_gen_config
alias ignore_parent_exclusion? ignore_parent_exclusion

def clear_options
@debug = @auto_gen_config = nil
@debug = @auto_gen_config = @options_config = nil
FileFinder.root_level = nil
end

Expand Down Expand Up @@ -127,24 +128,25 @@ def target_ruby_version_to_f!(hash)
def add_inheritance_from_auto_generated_file
file_string = " #{AUTO_GENERATED_FILE}"

if File.exist?(DOTFILE)
files = Array(load_yaml_configuration(DOTFILE)['inherit_from'])
config_file = options_config || DOTFILE
if File.exist?(config_file)
files = Array(load_yaml_configuration(config_file)['inherit_from'])
return if files.include?(AUTO_GENERATED_FILE)

files.unshift(AUTO_GENERATED_FILE)
file_string = "\n - " + files.join("\n - ") if files.size > 1
rubocop_yml_contents = IO.read(DOTFILE, encoding: Encoding::UTF_8)
rubocop_yml_contents = IO.read(config_file, encoding: Encoding::UTF_8)
.sub(/^inherit_from: *[.\w]+/, '')
.sub(/^inherit_from: *(\n *- *[.\w]+)+/, '')
end
write_dotfile(file_string, rubocop_yml_contents)
write_config_file(config_file, file_string, rubocop_yml_contents)
puts "Added inheritance from `#{AUTO_GENERATED_FILE}` in `#{DOTFILE}`."
end

private

def write_dotfile(file_string, rubocop_yml_contents)
File.open(DOTFILE, 'w') do |f|
def write_config_file(file_name, file_string, rubocop_yml_contents)
File.open(file_name, 'w') do |f|
f.write "inherit_from:#{file_string}\n"
f.write "\n#{rubocop_yml_contents}" if rubocop_yml_contents
end
Expand Down
34 changes: 34 additions & 0 deletions spec/rubocop/cli/cli_auto_gen_config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,40 @@ def f
''].join("\n"))
end

context 'when --config is used' do
it 'can generate a todo list' do
create_file('example1.rb', ['$x = 0 ',
'#' * 90,
'y ',
'puts x'])
create_file('dir/cop_config.yml', <<-YAML.strip_indent)
Layout/TrailingWhitespace:
Enabled: false
Metrics/LineLength:
Max: 95
YAML
expect(cli.run(%w[--auto-gen-config --config dir/cop_config.yml]))
.to eq(0)
expect(Dir['.*']).to include('.rubocop_todo.yml')
todo_contents = IO.read('.rubocop_todo.yml').lines[8..-1].join
expect(todo_contents).to eq(<<-YAML.strip_indent)
# Offense count: 1
# Configuration parameters: AllowedVariables.
Style/GlobalVars:
Exclude:
- 'example1.rb'
YAML
expect(IO.read('dir/cop_config.yml')).to eq(<<-YAML.strip_indent)
inherit_from: .rubocop_todo.yml
Layout/TrailingWhitespace:
Enabled: false
Metrics/LineLength:
Max: 95
YAML
end
end

it 'can generate a todo list' do
create_file('example1.rb', ['$x= 0 ',
'#' * 90,
Expand Down

0 comments on commit fcd56cb

Please sign in to comment.