Skip to content

Commit

Permalink
Merge pull request #7920 from jonas054/595_erb_processing_of_configur…
Browse files Browse the repository at this point in the history
…ation

[Fix #595] Add ERB pre-processing for configuration files
  • Loading branch information
koic committed May 1, 2020
2 parents 218f476 + 663abb9 commit 1548f83
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -7,6 +7,7 @@
* [#7895](https://github.com/rubocop-hq/rubocop/pull/7895): Include `.simplecov` file by default. ([@robotdana][])
* [#7916](https://github.com/rubocop-hq/rubocop/pull/7916): Support autocorrection for `Lint/AmbiguousRegexpLiteral`. ([@koic][])
* [#7917](https://github.com/rubocop-hq/rubocop/pull/7917): Support autocorrection for `Lint/UselessAccessModifier`. ([@koic][])
* [#595](https://github.com/rubocop-hq/rubocop/issues/595): Add ERB pre-processing for configuration files. ([@jonas054][])

### Bug fixes

Expand Down
20 changes: 10 additions & 10 deletions lib/rubocop/config_loader.rb
Expand Up @@ -217,7 +217,10 @@ def resolver
end

def load_yaml_configuration(absolute_path)
yaml_code = read_file(absolute_path)
file_contents = read_file(absolute_path)
yaml_code = Dir.chdir(File.dirname(absolute_path)) do
ERB.new(file_contents).result
end
check_duplication(yaml_code, absolute_path)
hash = yaml_safe_load(yaml_code, absolute_path) || {}

Expand All @@ -241,8 +244,7 @@ def check_duplication(yaml_code, absolute_path)
"#{smart_path}:#{line1}: " \
"`#{value}` is concealed by line #{line2}"
else
"#{smart_path}: " \
"`#{value}` is concealed by duplicate"
"#{smart_path}: `#{value}` is concealed by duplicate"
end
warn Rainbow(message).yellow
end
Expand All @@ -263,13 +265,11 @@ def yaml_safe_load(yaml_code, filename)
SafeYAML.load(yaml_code, filename, whitelisted_tags: %w[!ruby/regexp])
# Ruby 2.6+
elsif Gem::Version.new(Psych::VERSION) >= Gem::Version.new('3.1.0')
YAML.safe_load(
yaml_code,
permitted_classes: [Regexp, Symbol],
permitted_symbols: [],
aliases: true,
filename: filename
)
YAML.safe_load(yaml_code,
permitted_classes: [Regexp, Symbol],
permitted_symbols: [],
aliases: true,
filename: filename)
else
YAML.safe_load(yaml_code, [Regexp, Symbol], [], true, filename)
end
Expand Down
15 changes: 15 additions & 0 deletions manual/configuration.md
Expand Up @@ -237,6 +237,21 @@ Style/For:
In this example the `Exclude` would only include `bar.rb`.


## Pre-processing

Configuration files are pre-processed using the ERB templating mechanism. This
makes it possible to add dynamic content that will be evaluated when the
configuation file is read. For example, you could let RuboCop ignore all files
ignored by Git.

```yaml
AllCops:
Exclude:
<% `git status --ignored --porcelain`.lines.grep(/^!! /).each do |path| %>
- <%= path.sub(/^!! /, '') %>
<% end %>
```

## Defaults

The file [config/default.yml][1] under the RuboCop home directory contains the
Expand Down
30 changes: 30 additions & 0 deletions spec/rubocop/config_loader_spec.rb
Expand Up @@ -1007,6 +1007,36 @@ def cop_enabled?(cop_class)
)
end

it 'does ERB pre-processing of the configuration file' do
%w[a.rb b.rb].each { |file| create_file(file, 'puts 1') }
create_file(configuration_path, <<~YAML)
Style/Encoding:
Enabled: <%= 1 == 1 %>
Exclude:
<% Dir['*.rb'].sort.each do |name| %>
- <%= name %>
<% end %>
YAML
configuration = load_file
expect(configuration['Style/Encoding'])
.to eq('Enabled' => true,
'Exclude' => [abs('a.rb'), abs('b.rb')])
end

it 'does ERB pre-processing of a configuration file in a subdirectory' do
create_file('dir/c.rb', 'puts 1')
create_file('dir/.rubocop.yml', <<~YAML)
Style/Encoding:
Exclude:
<% Dir['*.rb'].each do |name| %>
- <%= name %>
<% end %>
YAML
configuration = described_class.load_file('dir/.rubocop.yml')
expect(configuration['Style/Encoding'])
.to eq('Exclude' => [abs('dir/c.rb')])
end

it 'fails with a TypeError when loading a malformed configuration file' do
create_file(configuration_path, 'This string is not a YAML hash')
expect { load_file }.to raise_error(
Expand Down

0 comments on commit 1548f83

Please sign in to comment.