Skip to content

Commit

Permalink
[Fix rubocop#5970] Handle paths in inherit_from
Browse files Browse the repository at this point in the history
We couldn't deal with slashes in inherit_from file paths.

There was also a weird checking of falsiness of the parameter
rubocop_yml_contents, even though it's always truthy. Checking if it contains
any non-whitespace achieves the desired result.
  • Loading branch information
jonas054 authored and bbatsov committed Oct 26, 2018
1 parent fcd56cb commit 1ab8170
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Bug fixes

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

## 0.60.0 (2018-10-26)

Expand Down
7 changes: 4 additions & 3 deletions lib/rubocop/config_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,9 @@ def add_inheritance_from_auto_generated_file
files.unshift(AUTO_GENERATED_FILE)
file_string = "\n - " + files.join("\n - ") if files.size > 1
rubocop_yml_contents = IO.read(config_file, encoding: Encoding::UTF_8)
.sub(/^inherit_from: *[.\w]+/, '')
.sub(/^inherit_from: *(\n *- *[.\w]+)+/, '')
.sub(%r{^inherit_from: *[.\/\w]+}, '')
.sub(%r{^inherit_from: *(\n *- *[.\/\w]+)+},
'')
end
write_config_file(config_file, file_string, rubocop_yml_contents)
puts "Added inheritance from `#{AUTO_GENERATED_FILE}` in `#{DOTFILE}`."
Expand All @@ -148,7 +149,7 @@ def add_inheritance_from_auto_generated_file
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
f.write "\n#{rubocop_yml_contents}" if rubocop_yml_contents =~ /\S/
end
end

Expand Down
35 changes: 35 additions & 0 deletions spec/rubocop/cli/cli_auto_gen_config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,41 @@ def f
end
end

context 'when working in a subdirectory' do
it 'can generate a todo list' do
create_file('dir/example1.rb', ['$x = 0 ',
'#' * 90,
'y ',
'puts x'])
create_file('dir/.rubocop.yml', <<-YAML.strip_indent)
inherit_from: ../.rubocop.yml
YAML
create_file('.rubocop.yml', <<-YAML.strip_indent)
Layout/TrailingWhitespace:
Enabled: false
Metrics/LineLength:
Max: 95
YAML
Dir.chdir('dir') { 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['dir/.*']).to include('dir/.rubocop_todo.yml')
todo_contents = IO.read('dir/.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/.rubocop.yml')).to eq(<<-YAML.strip_indent)
inherit_from:
- .rubocop_todo.yml
- ../.rubocop.yml
YAML
end
end

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

0 comments on commit 1ab8170

Please sign in to comment.