N.N. N.N. - 7 months ago 44
Ruby Question

How to integrate rubocop with Rake?

rubocop is a code style checker for Ruby. A similar tool to rubocop, Cane, can be integrated with Rake. I prefer rubocop to Cane since rubocop makes checks based on the Ruby Style Guide and it seems to spot more problems. To automate the process of style checking I would like to integrate rubocop with Rake so that the build fails if code quality is lacking.

Gem already supports adding tests to packages via Rake. I would like to do the same with style checks so that style checks are run along with the tests. How can I do this?

If it helps to start with a Rakefile here is one:

# -*- coding: utf-8; mode: ruby -*-

require 'bundler/gem_tasks'
require 'rake/testtask'

Rake::TestTask.new do |t|
t.libs << 'test'
t.test_files = FileList['test/unit/test*.rb']

desc 'Run tests'
task default: :test


I would recommend shelling out to the rubocop program. It's the simplest solution. Just add this to your Rakefile:

task test: :rubocop

task :rubocop do
  sh 'rubocop'