Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you use multiple tasks using Rake::TestTask?

I am running Minitest with Rake and would like to have two separate Rake tasks.

I have added the following:

require 'rake/testtask'

task :default => [:test]
task :quick => [:unit]

Rake::TestTask.new do |t|
  puts 'within test task'
  t.libs.push 'specs'
  t.pattern = 'specs/*_spec.rb'
  ENV['STACK'] = 'stack1'
  puts "test stack #{ENV['STACK']}"
end


Rake::TestTask.new('unit') do |t|
  puts 'within unit task'
  t.libs.push 'specs'
  t.pattern = 'specs/*_unit.rb'
  ENV['STACK'] = 'stack2'
  puts "test stack #{ENV['STACK']}"
end

When I run bundle exec rake quick then I get this output:

within test task
test stack stack1
within unit task
test stack stack2

I did not expect both the tasks to run. How do I create and run two separate rake tasks? As it is now, the second one always overwrites the environment variable.

Thanks

like image 446
Daryn Avatar asked Oct 23 '25 16:10

Daryn


1 Answers

You solve this issue by using Rake::Task["task_name"].clear like this:

task :test_task do
  Rake::TestTask.new do |t|
    puts 'within test task'
    t.libs.push 'specs'
    t.pattern = 'specs/*_spec.rb'
    ENV['STACK'] = 'stack1'
    puts "test stack #{ENV['STACK']}"
  end
end

task :unit_task do
  Rake::TestTask.new('unit') do |t|
    puts 'within unit task'
    t.libs.push 'specs'
    t.pattern = 'specs/*_unit.rb'
    ENV['STACK'] = 'stack2'
    puts "test stack #{ENV['STACK']}"
  end

end

task :test do
  Rake::Task["unit_task"].clear
  Rake::Task["test_task"].invoke
end

task :unit do
  Rake::Task["test_task"].clear
  Rake::Task["unit_task"].invoke
end
like image 177
Talgat Medetbekov Avatar answered Oct 25 '25 20:10

Talgat Medetbekov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!