Invoking the same rake task twice in RSpec - ruby

I am trying to test a rake task with rspec, and for that purpose I need to invoke it twice but
it is only being invoked once.
it 'first test' do
Rake::Task['my_rake_task'].invoke
# rake task was processed
end
it 'second test' do
Rake::Task['my_rake_task'].invoke
# rake task was NOT processed
end

if a rake task has already been invoked once it won't run again unless you call:
#rake[#task_name].reenable
or invoke it with
#rake[#task_name].execute

To adding to Guy Segev answer I prefer adding this to your spec file
after(:each) do
Rake::Task["task:name"].reenable
end

Related

Can I catch errors and continue to next Rake task?

I invoke Rake tasks in another Rake task like following:
namespace :test do
task :migrate do
Rake::Task['A:migrate'].invoke
Rake::Task['B:migrate'].invoke
end
end
What I want to know is whether I can continue to the next Rake task even if the earlier one fails. For instance, there is an error in A:migrate, I want to catch it and log it but move ahead with B:migrate. How can I do this?
Rake is just Ruby, so you can use Ruby's error handling feature.
namespace :test do
task :migrate do
begin
Rake::Task['A:migrate'].invoke
rescue => e
log(e)
end
Rake::Task['B:migrate'].invoke
end
end

pass command-line arguments to RSpec RakeTask dynamically

The rspec command comes with several options you can use to customize
RSpec's behavior, including output formats, filtering examples, etc.
For example:
$ rspec path/to/spec_file.rb
$ rspec --example 'first example'
$ rspec --tag type:special
$ rspec -P "**/*_test.rb"
How can I do the same thing with rake spec (with full Rspec options).
My Rakefile:
require 'bundler/gem_tasks'
require 'rspec/core/rake_task'
task default: :spec
RSpec::Core::RakeTask.new(:spec)
I have been google but didn't find any complete answer for that. Thanks.
Command line arguments can be passed automatically to the ENV hash.
For example:
From command line: FOO=BAR rspec spec/*spec.rb
Inside RSpec: puts ENV["FOO"] # => "BAR"
In your Rakefile, you can use backticks to call the shell command.
You can do it but it requires some changes.
First of all you need to undefine already defined spec task if its present, then define it again.
Or use other name, like spec_with_opts. Though I went through renaming.
in Rakefile
Rake::Task["spec"].clear
RSpec::Core::RakeTask.new(:spec) do |task, args|
task.rspec_opts = ENV['RSPEC_OPTS'] if ENV['RSPEC_OPTS'].present?
task.pattern = ENV['RSPEC_PATTERN'] if ENV['RSPEC_PATTERN'].present?
task.exclude_pattern = ENV['RSPEC_EXCLUDE_PATTERN'] if ENV['RSPEC_EXCLUDE_PATTERN'].present?
end
task default: :spec
So it now can be run this way:
rake spec RSPEC_PATTERN=path/to/spec_file.rb
rake spec RSPEC_OPTS="--example 'first example'"
rake spec RSPEC_OPTS="--tag type:special"
This one wont work, you would need to use RSPEC_PATTERN
rake spec RSPEC_OPTS="-P '**/*_test.rb'"
You can find other options that can be defined in source file:
https://github.com/rspec/rspec-core/blob/master/lib/rspec/core/rake_task.rb

Confused with the second variable of task : some_random_name1, [:some_random_name2] => :environment in rake?

I am new to ruby and rake, and currently confused with some ruby syntax.
task :some_random_name1, [:some_random_name2] => :environment do |task, args|
end
What does [:some_random_name2] here mean? I know some_random_name1 is a task that depends on another task :environment, and task and args in |task, args| are arguments from command line. Thanks in advance.
[:some_random_name2] is referring to arguments that you can pass into your rake task. May I suggest checking out this article which can explain in more depth passing arguments to a rake task.
When you call rake -T from the command line, you should see:
rake example: some_random_name1[some_random_name2]
(assuming your namespace is 'example')

Namespaces in Ruby Rake Tasks

Are the following equivalent?
namespace :resque do
task setup: :environment do
end
end
task "resque:setup" => :environment do
end
In short: yes. When running rake resque:setup both of these tasks will be invoked.
Rake will merge these tasks. You can test this by doing the following:
p Rake.application.tasks
Which in this case would return something like
[<Rake::Task resque:setup => [environment]>]
Which is simply an Array holding a single Rake::Task object. You can also check the scope or list of namespaces for a task by doing:
p Rake.application.tasks.first.scope
#=> ["resque"]
If you want to learn a little more on how the internals of Rake work, check out Rake::Task and Rake::TaskManager

Run rake tasks in sequence

I have rake tasks which i want to run in proper sequence.
I want to run one rake task which run other rake tasks in proper sequence.
How may i do that?
you should consider defining dependencies between your tasks like this
task :primary => [:secondary]
task :secondary do
puts "Doing Secondary Task"
end
But if you really, really need to call the tasks directly you can use invoke to call another task
task :primary do
Rake::Task[:secondary].invoke
end
task :secondary do
puts "Doing Secondary Task"
end
see also here

Resources