4

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

Daryn
  • 3,394
  • 5
  • 30
  • 41

1 Answers1

3

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
Talgat Medetbekov
  • 3,741
  • 1
  • 19
  • 26