diff --git a/CHANGELOG.md b/CHANGELOG.md index 38561c43907e..0541a7173b2f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,8 @@ ### New features -* [#491](https://github.com/bbatsov/rubocop/issues/491) - New cop `MethodCalledOnDoEndBlock` keep track of methods called on `do`...`end` blocks. +* [#491](https://github.com/bbatsov/rubocop/issues/491) - New cop `MethodCalledOnDoEndBlock` keeps track of methods called on `do`...`end` blocks. +* [#456](https://github.com/bbatsov/rubocop/issues/456) - New configuration parameter `AllCops`/`RunRailsCops` can be set to `true` for a project, removing the need to give the `-R`/`--rails` option with every invocation of `rubocop`. ### Bugs fixed diff --git a/config/default.yml b/config/default.yml index 585360f64d52..bc58dbe87e94 100644 --- a/config/default.yml +++ b/config/default.yml @@ -12,6 +12,9 @@ AllCops: - '**/*.gemspec' - '**/Rakefile' Excludes: [] + # By default, the rails cops are not run. Override in project or home + # directory .rubocop.yml files, or by giving the -R/--rails option. + RunRailsCops: false LineLength: Max: 79 diff --git a/lib/rubocop/cli.rb b/lib/rubocop/cli.rb index 7d44621a8f78..56a87d53a97c 100644 --- a/lib/rubocop/cli.rb +++ b/lib/rubocop/cli.rb @@ -63,7 +63,7 @@ def run(args = ARGV) return 1 end - def mobilized_cop_classes + def mobilized_cop_classes(config) @mobilized_cop_classes ||= begin cop_classes = Cop::Cop.all @@ -71,7 +71,7 @@ def mobilized_cop_classes cop_classes.select! { |c| c.cop_name == @options[:only] } else # filter out Rails cops unless requested - cop_classes.reject!(&:rails?) unless @options[:rails] + cop_classes.reject!(&:rails?) unless run_rails_cops?(config) # filter out style cops when --lint is passed cop_classes.select!(&:lint?) if @options[:lint] @@ -83,7 +83,7 @@ def mobilized_cop_classes def inspect_file(file) config = @config_store.for(file) - team = Cop::Team.new(mobilized_cop_classes, config, @options) + team = Cop::Team.new(mobilized_cop_classes(config), config, @options) offences = team.inspect_file(file) @errors.concat(team.errors) offences @@ -262,6 +262,10 @@ def display_error_summary(errors) private + def run_rails_cops?(config) + @options[:rails] || config['AllCops']['RunRailsCops'] + end + def target_finder @target_finder ||= TargetFinder.new(@config_store, @options[:debug]) end diff --git a/spec/rubocop/cli_spec.rb b/spec/rubocop/cli_spec.rb index 908eafb433f0..9d24fc915fb8 100644 --- a/spec/rubocop/cli_spec.rb +++ b/spec/rubocop/cli_spec.rb @@ -775,6 +775,43 @@ def interrupt .to eq(['', '1 file inspected, no offences detected', ''].join("\n")) end + describe 'enabling/disabling rails cops' do + it 'by default does not run rails cops' do + create_file('example1.rb', ['# encoding: utf-8', + 'read_attribute(:test)']) + expect(cli.run(['--format', 'simple', 'example1.rb'])).to eq(0) + end + + it 'with -R given runs rails cops' do + create_file('example1.rb', ['# encoding: utf-8', + 'read_attribute(:test)']) + expect(cli.run(['--format', 'simple', '-R', 'example1.rb'])).to eq(1) + expect($stdout.string).to include('Prefer self[:attribute]') + end + + it 'with configation option true runs rails cops' do + create_file('example1.rb', ['# encoding: utf-8', + 'read_attribute(:test)']) + create_file('.rubocop.yml', [ + 'AllCops:', + ' RunRailsCops: true', + ]) + expect(cli.run(['--format', 'simple', 'example1.rb'])).to eq(1) + expect($stdout.string).to include('Prefer self[:attribute]') + end + + it 'with configation option false but -R given runs rails cops' do + create_file('example1.rb', ['# encoding: utf-8', + 'read_attribute(:test)']) + create_file('.rubocop.yml', [ + 'AllCops:', + ' RunRailsCops: false', + ]) + expect(cli.run(['--format', 'simple', '-R', 'example1.rb'])).to eq(1) + expect($stdout.string).to include('Prefer self[:attribute]') + end + end + describe 'configuration from file' do it 'finds included files' do create_file('example', [