How can I run features within a rake task in parallel? - ruby

Normally I run my tests with parallel_cucumber which runs different features in parallel using parallel_test gem. I want to setup a rake task that will run using different profiles and run the features within each task in parallel.
I have setup my Rakefile this way:
namespace :features do
Cucumber::Rake::Task.new(:basket) do |t|
t.profile = "basket"
end
Cucumber::Rake::Task.new(:fruits) do |t|
t.profile = "fruits"
end
Cucumber::Rake::Task.new(:veggies) do |t|
t.profile = "veggies"
end
task :all => [:basket, :fruits, :veggies]
end
When I run "rake features:all" it will run each task in sequence(as expected/desired) but will run the features within each task one at a time(not desired). I would like to keep each task running in sequence but would like the features within each task to run in parallel. Is this possible? If not is there a way this can be done?
As always your help is much appreciated.

Rake offers multitask. You could either
Changing task to multitask in the rakefile:
multitask :all => [:basket, :fruits, :veggies]
use -m option in the command line:
rake -m features:all

Have a look at the parallel_tests gem. It allows you to run your features in parallel. While the typical use case is to run all of your features, you are able to run specific rake tasks in parallel. See the documentation for more details.

Related

Order of Rake Test Task

I have a rake file with three tasks, which I need to execute in order.
require 'rake/testtask'
file 'some_binary_file.elf' do
puts 'fetching file from server ...'
# this task connects to a server and downloads some binaries
# it takes a few seconds to run
end
task flash_application: 'some_binary_file.elf' do
puts 'flashing the file to the hardware ...'
# this task copies a binary file to the flash memory
# of some external hardware, also takes a few seconds
end
Rake::TestTask(:hardware) do |t|
puts 'running tests ...'
f.test_files = FileList['test/**/*_test.rb']
end
rake default: [:flash_application, :hardware]
when I run $ rake in a terminal, it produces the following output.
running tests ... < ---- (not actually running)
fetching file from server ...
flashing the file to the hardware ...
I would expect rake to run the tasks in the order I specified, but It seems to always execute the test task first. It is remarkable that the tests do not run - but the output of the task creation is produced anyway.
When you want to run the tasks in a specific order, you must depend them on each other. In your case, :flash_application should then depend on :hardware
Found the Bug - This problem was not ruby / rake specific. The flash_application task changes the working directory. Because of that, there is no Rakefile with a task 'hardware' in the current working directory. But researching for this bug yielded some interesting insights.
Ruby arrays are ordered, if one want to execute task in an order it is sufficient to define them in execution order in an array i.e.
task some_task: [:first, :second, :third]
Rake::TestTask.new defines a plain old rake task when called. That means, when rake is called, ruby creates an instance of a Rake::TestTask. All code passed into the constructor is executed / yielded during this phase. This yields the described behavior from the original question.

How can you pass in arguments to rake tasks called using Heroku Scheduler?

Rake tasks scheduled in Heroku found online are in the "rake task-name" format. Heroku passes in an :environment argument to the rake task, but there is no documentation about extra args being passed to a task listed in scheduler.rake
Heroku Scheduler Doc
You may try this:
task :my_task, [:arg1, :arg2] => :environment do |t, args|
puts "Args were: #{args}"
end
and then run manually to see the output:
heroku run rake my_task
UPDATE:
this answer can be an old one already
Instead of rake task_name input rake task_name[arg1, arg2] to the string input accepted by the Task column in the Heroku Scheduler main screen.
As mentioned by CodeGroover, your rake task defined in lib/scheduler.rake should define the parameters it is expecting.

Access default_url_options from rake task

In my application.rb file I have a few options to modify my apps URLs like so:
config.action_controller.default_url_options = { :trailing_slash => true }
But these do not seem to take effect in my Rake tasks, despite the fact that I am running them in the Rails environment via the :environment dependency.
I know that I can get this to work simply by calling the following in my rake task:
default_url_options[:trailing_slash] = true
... but I'd like to DRY this up. Is there a clean way to make a rake task use Rails' default_url_options from application.rb?
You can use the following in a rake task (and in Rails console):
Rails.application.config.action_controller.default_url_options

How to add a Rake task to the default Rake task?

Somehow Rspec and Cucumber are making it into my default rake task (which is fine because I want them there). But I have tried adding additional tasks to the default task and it has no effect.
What is the proper way to add tasks to the default rake task?
Typically your Rakefile will have something like this:
task :default => [:spec]
You just need to add more tasks into this list.
The answer from davogenes is correct for non-namespaced rake tasks.
If you want to have a default task in namespaces you need to do the following
namespace :mynamespace do
desc "This should be the default"
task :mydefault do
Do.something_by_default
end
desc "Another task in this namespace"
task :other do
Do.something
end
end
desc "This is the default task of mynamespace"
task mynamespace: 'mynamespace:mydefault'
Then you can run rake mynamespace as well as rake mynamespace:other or rake mynamespace:mydefault.

How to integrate rubocop with Rake?

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 Style Guide and it seems to spot more problems. To automate the process of style checking I would like to integrate rubocop with Rake so that the build fails if code quality is lacking.
Gem already supports adding tests to packages via Rake. I would like to do the same with style checks so that style checks are run along with the tests. How can I do this?
If it helps to start with a Rakefile here is one:
# -*- coding: utf-8; mode: ruby -*-
require 'bundler/gem_tasks'
require 'rake/testtask'
Rake::TestTask.new do |t|
t.libs << 'test'
t.test_files = FileList['test/unit/test*.rb']
end
desc 'Run tests'
task default: :test
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.
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.
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.
I needed to do something similar myself, and ended up looking in the internal source code of the RuboCop::RakeTask here:
https://github.com/rubocop/rubocop/blob/a34a1c2c2dd1fa6d90ffd06c183421a495a0717c/lib/rubocop/rake_task.rb#L40-L43
require 'rubocop'
cli = CLI.new
puts 'Running RuboCop...' if verbose
result = cli.run(options)
abort('RuboCop failed!') if result.nonzero? && fail_on_error
You can actually invoke similar code directly in your own codebase / rake task.
I ended up writing a little wrapper module I can call to, with some default flags that I always want to be applied:
module RubocopCli
def self.run!(*args)
require "rubocop"
cli = RuboCop::CLI.new
result = cli.run(["--display-cop-names", "--force-exclusion", "--fail-level", "autocorrect", *args])
raise "RubocopCli.run! Linting failed." if result.nonzero?
end
end
Then you can call it with additional args from any task, or app code, like:
files_to_lint = %w[lib/whatever.rb spec/lib/whatever_spec.rb]
RubocopCli.run!("--auto-correct", *files_to_lint)
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']
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

Resources