简体   繁体   English

Rails - 在一项任务中进行 Rake 测试和 rubocop

[英]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:我正在尝试设置我的 rails 项目,以便贡献者所需的所有验证都在一个命令中,目前我们一直在运行:

rake test

But now we also want to use rubocop for static analysis:但是现在我们也想使用 rubocop 进行静态分析:

rubocop -R -a

I want this to be executable in one simple rake task.我希望它可以在一个简单的 rake 任务中执行。 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.覆盖'rake test'以运行rubocop然后是rails项目的标准rake测试内容会很好,因为这样没有人将不得不记住更改命令。 But if I have to create a separate rake task, that's probably fine too.但如果我必须创建一个单独的 rake 任务,那也可能没问题。

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?我已经在底部看到了 rubocop rake 集成,但我不确定如何将它与“rake 测试”捆绑到一项任务中......有什么想法吗?

I prefer to set my default task to run rubocop then the tests.我更喜欢将我的默认任务设置为运行 rubocop 然后是测试。 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

This is the .rake file that I ended up with in the end.这是我最终得到的 .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

You could easily define your own rake task which first invokes Rails' test rake task and then the code snippet you mentioned for rubocop.您可以轻松定义自己的 rake 任务,该任务首先调用 Rails 的test rake 任务,然后是您提到的 rubocop 代码片段。

For example, in a .rake file you could have something like that:例如,在一个 .rake 文件中,你可以有这样的东西:

require 'rubocop/rake_task'

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

If you feel the need to customize the call to Rubocop and that involves more code, you could create another custom task, say :rubocop, which you then invoke from :my_test as well.如果您觉得需要自定义对 Rubocop 的调用并且涉及更多代码,您可以创建另一个自定义任务,比如 :rubocop,然后您也可以从 :my_test 调用它。

Finally, an alternative to creating your own rake task and sticking with rake test would be to modify your test_helper to invoke whatever you need invoked after testing is completed.最后,创建您自己的 rake 任务并坚持使用rake test的替代方法是修改您的 test_helper 以调用您在测试完成后需要调用的任何内容。

It appears to have changed from:它似乎已经从:

Rubocop::RakeTask.new Rubocop::RakeTask.new

to:到:

Rubo C op::RakeTask.new Rubo C op::RakeTask.new

See ma?看到吗? I know how to CamelCase!我知道如何使用 CamelCase!

I used following .rake file to run the test and rubocop tasks at once:我使用以下 .rake 文件同时运行测试和 rubocop 任务:

task default: %w[rubocop test]

RuboCop::RakeTask.new(:rubocop) do |task|
  task.patterns = ['**/*.rb']
  task.fail_on_error = false
  task.options = ["--auto-correct-all"]
end

task :test do
  ruby 'test/program_test.rb'
end

The first line allows both tasks to be run by calling rake .第一行允许通过调用rake来运行这两个任务。

Command line arguments can also be added in the options array.命令行参数也可以添加到options数组中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM