Search code examples
rubycoding-stylerakerubocop

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']
end

desc 'Run tests'
task default: :test

Solution

  • 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'
    end