Skip to content

Commit

Permalink
Merge pull request #505 from jonas054/enable_rails_cops_in_config
Browse files Browse the repository at this point in the history
[Fix #456] New configuration parameter AllCops/RunExtraRailsCops.
  • Loading branch information
bbatsov committed Sep 22, 2013
2 parents 7cbee2a + b587f3a commit db9d159
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 4 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 3 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 7 additions & 3 deletions lib/rubocop/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,15 @@ 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

if @options[:only]
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]
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
37 changes: 37 additions & 0 deletions spec/rubocop/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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', [
Expand Down

0 comments on commit db9d159

Please sign in to comment.