How to integrate rubocop with Rake?

前端 未结 5 1407
一整个雨季
一整个雨季 2021-02-06 22:46

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 Sty

相关标签:
5条回答
  • 2021-02-06 23:19

    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
    
    0 讨论(0)
  • 2021-02-06 23:21

    I highly recommend,

    require 'rubocop/rake_task'
    
    RuboCop::RakeTask.new(:rubocop) do |t|
      t.options = ['--display-cop-names']
    end
    

    This uses the rubocop's own rake tasks and allows you to pass options if you like.

    0 讨论(0)
  • 2021-02-06 23:35

    You will probably find https://github.com/yujinakayama/guard-rubocop useful if you use Guard for your RSpec tests. It enables Rubocop to give you instant feedback as soon as you save the file, along with your test results.

    0 讨论(0)
  • 2021-02-06 23:36

    You can shell out via Rake with the options you prefer:

      desc 'Run Rubocop with options'
      task rubocop: :environment do
        sh 'bundle exec rubocop -D --format offenses --format progress || true'
      end
    

    I then recommend modifying the default task to include the output. The trick is to clear the task and then add back what you want. Note the need to end with || true so that an error from Rubocop will not prevent the next task from running. Here's what I do, which also uses parallel tests:

    task(:default).clear.enhance ['parallel:parallel_prepare', 'parallel:spec',
                                  'parallel:features', 'lint:rubocop',
                                  'lint:rails_best_practices']
    
    0 讨论(0)
  • 2021-02-06 23:39

    As of version 0.10.0 rubocop contain a custom rake task that you can use. Just put the following in your Rakefile

    require 'rubocop/rake_task'
    
    RuboCop::RakeTask.new
    

    Make sure to use upper-case 'R' and 'C' or you will get a NameError.

    0 讨论(0)
提交回复
热议问题