Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails - Rake test and rubocop in one task

I'm trying to setup my rails project so that all the verification required by a contributor is in one command, currently we have been running:

rake test

But now we also want to use rubocop for static analysis:

rubocop -R -a

I want this to be executable in one simple rake task. It would be nice to override 'rake test' to run rubocop then the standard rake test stuff for a rails project, as then no-one will have to remember to change the command. But if I have to create a separate rake task, that's probably fine too.

I've seen the rubocop rake integration here, at the bottom, but I'm not sure how to bundle that with 'rake test' into one task... Any thoughts?

like image 367
Imran Avatar asked Dec 05 '22 06:12

Imran


2 Answers

I prefer to set my default task to run rubocop then the tests. Either way, it is a good idea to have those tasks separate rather than have one task do two things.

require 'rubocop/rake_task'

task :default => [:rubocop, :test]

desc 'Run tests'
task(:test) do
  # run your specs here
end

desc 'Run rubocop'
task :rubocop do
  RuboCop::RakeTask.new
end

Your tasks:

> rake -T
rake rubocop  # Run rubocop
rake test     # Run tests
like image 111
Rimian Avatar answered Dec 26 '22 00:12

Rimian


This is the .rake file that I ended up with in the end.

desc 'Run tests and rubocop'
task :validate do
  Rake::Task['rubocop'].invoke
  Rake::Task['test'].invoke
end

task :rubocop do
  require 'rubocop'
  cli = Rubocop::CLI.new
  cli.run(%w(--rails --auto-correct))
end
like image 24
Imran Avatar answered Dec 25 '22 23:12

Imran