2014-04-30 30 views
2

我想设置我的Rails项目,使所有的贡献者所需要的验证是一个命令,目前我们已经在运行:导轨 - 耙试验和rubocop在一个任务

rake test 

但现在我们也希望使用rubocop进行静态分析:

rubocop -R -a 

我希望这可以在一个简单的rake任务中执行。如果重写'rake test'来运行rubocop,然后运行rails项目的标准rake测试工具,那么很好,因为没有人会记得更改命令。但是如果我必须创建一个单独的rake任务,那也可能很好。

我见过rubocop rake集成here, at the bottom,但我不确定如何将'rake test'捆绑到一个任务中......任何想法?

回答

0

你可以很容易地定义自己的耙的任务,首先调用Rails的test rake任务,然后将代码片断您rubocop提及。

例如,在一个.rake文件,你可以有这样的事情:

require 'rubocop/rake_task' 

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

如果你觉得有必要定制呼叫Rubocop并涉及更多的代码,你可以创建其他自定义任务,说:rubocop,然后你可以调用:my_test。

最后,创建自己的rake任务和坚持rake test的替代方法是修改test_helper以在测试完成后调用需要调用的任何内容。

+0

谢谢,这真是棒极了,已经把最后的事,作为一个答案 – Imran

3

这是我最终得到的.rake文件。

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 
7

我更喜欢设置我的默认任务来运行rubocop然后测试。无论哪种方式,将这些任务分开是一个好主意,而不是让一个任务做两件事。

注:截至0.24,Rubocop成为你的代码RuboCop

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 

你的任务:

> rake -T 
rake rubocop # Run rubocop 
rake test  # Run tests 
+0

我怎么向RuboCop提供论据? –

2

这似乎已经改变了从:

Rubocop :: RakeTask.new

到:

Rubo Ç OP: :RakeTask.new

查看ma?我知道如何CamelCase!