Skip to content

Commit

Permalink
Handle URLs in inherit_from
Browse files Browse the repository at this point in the history
Previously, when adding the `.rubocop_todo.yml` entry to
`.rubocop.yml`, URLs were not removed correctly,
leaving remnants as follows:

```diff
 inherit_from:
+  - .rubocop_todo.yml
   - https://example.com/foo/bar
+
+://example.com/foo/bar
```

In order to reduce the complexity of the regex matches,
the approach is changed:
instead of replacing only specific characters, all text up the end of line,
independently of what characters it contains, is replaced.
  • Loading branch information
joeyates authored and bbatsov committed Jun 25, 2019
1 parent 7e5c2b2 commit de29e43
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/rubocop/config_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,8 @@ def expand_path(path)

def existing_configuration(config_file)
IO.read(config_file, encoding: Encoding::UTF_8)
.sub(%r{^inherit_from: *[.\/\w]+}, '')
.sub(%r{^inherit_from: *(\n *- *[.\/\w]+)+}, '')
.sub(/^inherit_from: *[^\n]+/, '')
.sub(/^inherit_from: *(\n *- *[^\n]+)+/, '')
end

def write_config_file(file_name, file_string, rubocop_yml_contents)
Expand Down
54 changes: 54 additions & 0 deletions spec/rubocop/cli/cli_auto_gen_config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,60 @@ def f
end
end

context 'when inheriting from a URL' do
let(:remote_config_url) { 'https://example.com/foo/bar' }

before do
stub_request(:get, remote_config_url)
.to_return(status: 200, body: "Style/Encoding:\n Enabled: true")
end

context 'when there is a single entry' do
it 'can generate a todo list' do
create_file('dir/example1.rb', ['$x = 0 ',
'#' * 90,
'y ',
'puts x'])
create_file('.rubocop.yml', <<~YAML)
inherit_from: #{remote_config_url}
YAML
expect(cli.run(%w[--auto-gen-config])).to eq(0)
expect($stderr.string).to eq('')
expect($stdout.string).to include(<<~YAML)
Added inheritance from `.rubocop_todo.yml` in `.rubocop.yml`.
YAML
expect(IO.read('.rubocop.yml')).to eq(<<~YAML)
inherit_from:
- .rubocop_todo.yml
- #{remote_config_url}
YAML
end
end

context 'when there are multiple entries' do
it 'can generate a todo list' do
create_file('dir/example1.rb', ['$x = 0 ',
'#' * 90,
'y ',
'puts x'])
create_file('.rubocop.yml', <<~YAML)
inherit_from:
- #{remote_config_url}
YAML
expect(cli.run(%w[--auto-gen-config])).to eq(0)
expect($stderr.string).to eq('')
expect($stdout.string).to include(<<~YAML)
Added inheritance from `.rubocop_todo.yml` in `.rubocop.yml`.
YAML
expect(IO.read('.rubocop.yml')).to eq(<<~YAML)
inherit_from:
- .rubocop_todo.yml
- #{remote_config_url}
YAML
end
end
end

it 'can generate a todo list' do
create_file('example1.rb', ['# frozen_string_literal: true',
'',
Expand Down

0 comments on commit de29e43

Please sign in to comment.