Skip to content

Commit

Permalink
Generate 'Exclude' list if different styles are used (#3175)
Browse files Browse the repository at this point in the history
Before, if mixed styles of the specific cop are used across project files,
some of the cops set 'Enabled: false' regardless of the count of
files with offense and "--exclude-limit" argument.
  • Loading branch information
flexoid authored and bbatsov committed Jun 22, 2016
1 parent 8bc4c95 commit 35544fe
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 14 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
* [#3223](https://github.com/bbatsov/rubocop/issues/3223): Return can take many arguments. ([@ptarjan][])
* [#3239](https://github.com/bbatsov/rubocop/pull/3239): Fix bug with --auto-gen-config and a file that does not exist. ([@meganemura][])
* [#3138](https://github.com/bbatsov/rubocop/issues/3138): Fix RuboCop crashing when config file contains utf-8 characters and external encoding is not utf-8. ([@deivid-rodriguez][])
* [#3175](https://github.com/bbatsov/rubocop/pull/3175): Generate 'Exclude' list for the cops with configurable enforced style to `.rubocop_todo.yml` if different styles are used. ([@flexoid][])

### Changes

Expand Down Expand Up @@ -2224,3 +2225,4 @@
[@giannileggio]: https://github.com/giannileggio
[@deivid-rodriguez]: https://github.com/deivid-rodriguez
[@pclalv]: https://github.com/pclalv
[@flexoid]: https://github.com/flexoid
9 changes: 7 additions & 2 deletions lib/rubocop/formatter/disabled_config_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,17 @@ def output_cop_comments(output, cfg, cop_name, offense_count)
end

def output_cop_config(output, cfg, cop_name)
# 'Enabled' option will be put into file only if exclude
# limit is exceeded.
cfg_without_enabled = cfg.reject { |key| key == 'Enabled' }

output.puts "#{cop_name}:"
cfg.each do |key, value|
cfg_without_enabled.each do |key, value|
value = value[0] if value.is_a?(Array)
output.puts " #{key}: #{value}"
end
output_offending_files(output, cfg, cop_name)

output_offending_files(output, cfg_without_enabled, cop_name)
end

def output_offending_files(output, cfg, cop_name)
Expand Down
46 changes: 34 additions & 12 deletions spec/rubocop/cli/cli_auto_gen_config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -477,18 +477,40 @@ def abs(path)
end
end

it 'disables cop if different styles appear in different files' do
create_file('example1.rb', ['$!'])
create_file('example2.rb', ['$ERROR_INFO'])
expect(cli.run(['--auto-gen-config'])).to eq(1)
expect(IO.readlines('.rubocop_todo.yml')[8..-1].join)
.to eq(['# Offense count: 1',
'# Cop supports --auto-correct.',
'# Configuration parameters: EnforcedStyle, SupportedStyles.',
'# SupportedStyles: use_perl_names, use_english_names',
'Style/SpecialGlobalVars:',
' Enabled: false',
''].join("\n"))
describe 'when different styles appear in different files' do
before do
create_file('example1.rb', ['$!'])
create_file('example2.rb', ['$!'])
create_file('example3.rb', ['$ERROR_INFO'])
end

it 'disables cop if --exclude-limit is exceeded' do
expect(cli.run(['--auto-gen-config', '--exclude-limit', '1'])).to eq(1)
expect(IO.readlines('.rubocop_todo.yml')[8..-1].join)
.to eq(['# Offense count: 2',
'# Cop supports --auto-correct.',
'# Configuration parameters: EnforcedStyle, SupportedStyles.',
'# SupportedStyles: use_perl_names, use_english_names',
'Style/SpecialGlobalVars:',
' Enabled: false',
''].join("\n"))
end

it 'generates Exclude list if --exclude-limit is not exceeded' do
create_file('example4.rb', ['$!'])
expect(cli.run(['--auto-gen-config', '--exclude-limit', '10'])).to eq(1)
expect(IO.readlines('.rubocop_todo.yml')[8..-1].join)
.to eq(['# Offense count: 3',
'# Cop supports --auto-correct.',
'# Configuration parameters: EnforcedStyle, SupportedStyles.',
'# SupportedStyles: use_perl_names, use_english_names',
'Style/SpecialGlobalVars:',
' Exclude:',
" - 'example1.rb'",
" - 'example2.rb'",
" - 'example4.rb'",
''].join("\n"))
end
end

it 'can be called when there are no files to inspection' do
Expand Down

0 comments on commit 35544fe

Please sign in to comment.