How to Integrate Rubocop with Rake

Rails - Rake test and rubocop in one task

You could easily define your own rake task which first invokes Rails' test rake task and then the code snippet you mentioned for rubocop.

For example, in a .rake file you could have something like that:

require 'rubocop/rake_task'

desc 'Run tests and rubocop'
task :my_test do
Rake::Task['test'].invoke
RuboCop::RakeTask.new
end

If you feel the need to customize the call to Rubocop and that involves more code, you could create another custom task, say :rubocop, which you then invoke from :my_test as well.

Finally, an alternative to creating your own rake task and sticking with rake test would be to modify your test_helper to invoke whatever you need invoked after testing is completed.

With Rails using Minitest, how can I set up RuboCop to run automatically each time tests are run with rake?

I added the following task to lib/tasks/test.rake:

require 'rubocop/rake_task'

# Add additional test suite definitions to the default test task here
namespace :test do
desc 'Runs RuboCop on specified directories'
RuboCop::RakeTask.new(:rubocop) do |task|
# Dirs: app, lib, test
task.patterns = ['app/**/*.rb', 'lib/**/*.rb', 'test/**/*.rb']

# Make it easier to disable cops.
task.options << "--display-cop-names"

# Abort on failures (fix your code first)
task.fail_on_error = false
end
end

Rake::Task[:test].enhance ['test:rubocop']

The result:

$ bundle exec rake test
Running RuboCop...
Inspecting 9 files
.........

9 files inspected, no offenses detected
Run options: --seed 55148

# Running:

...................

Finished in 1.843280s, 10.3077 runs/s, 34.7207 assertions/s.

19 runs, 64 assertions, 0 failures, 0 errors, 0 skips

Keep getting an Error while running rubocop with Travis CI

Look a little closer at the error:

Error: obsolete parameter AlignWith (for Lint/EndAlignment) found in /home/travis/build/firehosefirechess/chess_app/vendor/bundle/ruby/2.4.0/gems/rake-12.0.0/.rubocop.yml AlignWith has been renamed to EnforcedStyleAlignWith

It's not your app's .rubocop.yml where the error is showing up, it's actually in chess_app/vendor/bundle/ruby/2.4.0/gems/rake-12.0.0/.rubocop.yml!

As you probably don't want to be running Rubocop on vendor code, you can resolve this by adding a line to the Exclude list in your own config:

- 'vendor/**/*'

RSpec & Rubocop / Ruby Style Guide

I would recommend making a pull request to RSpec changing their templates to be Rubocop-compliant.



Related Topics



Leave a reply



Submit