Rails-一项任务中的Rake测试和rubocop

问题描述 投票:4回答:4

我正在尝试设置我的rails项目,以使贡献者所需的所有验证都在一个命令中,当前我们正在运行:

rake test

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

rubocop -R -a

我希望这可以在一个简单的rake任务中执行。最好覆盖“ rake test”以运行rubocop,然后覆盖Rails项目的标准rake测试内容,因为这样就无需记住更改命令了。但是,如果我必须创建一个单独的rake任务,那也可能很好。

我已经看过rubocop耙式集成here, at the bottom,但是我不确定如何将'rake test'捆绑到一个任务中……有什么想法吗?

ruby-on-rails ruby rake static-analysis rubocop
4个回答
1
投票
您可以轻松定义自己的rake任务,该任务首先调用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以调用测试完成后需要调用的任何东西。


8
投票
我希望将默认任务设置为先运行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


4
投票
这是我最后得到的.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


2
投票
似乎从以下位置更改:

Rubocop :: RakeTask.new

至:

Rubo

C op :: RakeTask.new

见妈妈?我知道如何使用CamelCase!
© www.soinside.com 2019 - 2024. All rights reserved.