I’m just getting my feet wet with Rake, at least as far as running unit
tests is concerned.
I have some initializer and cleanup stuff that I’d like run before and
after ANY rake task (all of which are TestTasks in my case). I’m using
the dependency method to setup the initializer, which is fine, but I
don’t know how to get my cleanup task to run, since rake aborts at the
first test failure.
I basically need the rake-task equivalent of Ruby’s “ensure”.
I’m just getting my feet wet with Rake, at least as far as running unit
tests is concerned.
I have some initializer and cleanup stuff that I’d like run before and
after ANY rake task (all of which are TestTasks in my case). I’m using
the dependency method to setup the initializer, which is fine, but I
don’t know how to get my cleanup task to run, since rake aborts at the
first test failure.
I basically need the rake-task equivalent of Ruby’s “ensure”.
Is this possible? If so, how?
There’s no direct way to do this in Rake task, but you can always wrap
your task in begin/ensure/end yourself.
For example:
task :test do
begin
ruby ‘test_example.rb’
ensure
puts “OK”
end
end
will always print “OK”, even if the tests fail. Unfortunately, the
built-in test task does not have any hooks for this, but it its not hard
to roll your own in this case.
There’s no direct way to do this in Rake task, but you can always wrap
your task in begin/ensure/end yourself.
For example:
task :test do
begin
ruby ‘test_example.rb’
ensure
puts “OK”
end
end
will always print “OK”, even if the tests fail. Unfortunately, the
built-in test task does not have any hooks for this, but it its not hard
to roll your own in this case.
/me nods.
At the moment, I have a wrapper script which calls
So what’s wrong with coding an END block
at the end of the script?
(he asked, not yet having tried it, but
wondering why it wouldn’t work)
Could you show some example code? I’ve never heard of this “END block”
thing. I thought END in Perl made the interpreter stop and ignore
everything below that line.
Could you show some example code? I’ve never heard of this “END block”
thing. I thought END in Perl made the interpreter stop and ignore
everything below that line.
Could you show some example code? I’ve never heard of this “END block”
thing. I thought END in Perl made the interpreter stop and ignore
everything below that line.
It does.
However, that’s not what Eric is talking about.
He’s talking about:
END {
# put your cleanup code here
}
The problem with this is that it will run every time, no matter what
tasks were invoked from the rake command line. It might work better to
do something like this:
task :cleanup do
at_exit {
# cleanup code here
}
end
task :main_task => [:other_prerequisites, :cleanup] do
# Task code that needs cleaning up
end
Which I think you implicitly confirm this Austin, but don’t say so…
ruby does support the END { … } that Austin posted above… just to be
a little bit more explicit
That’s looking like a cool solution. What is that
at_exit thing, btw? Is that a standard part of rake
I should know about?
Note:
We’ve gotten as far as building Ruby on solaris,
but seem to be missing the libraries. I’ll looking
to find a way to give the installation instructions
back to the community, when I’ve finished figuring
out the details.
Since the libraries are missing, the rake install
failed…but I look forward to getting the issues
resolved soon. Rake is the reason I’ve begun taking
a very serious look at Ruby.
:_)
That’s looking like a cool solution. What is that
at_exit thing, btw? Is that a standard part of rake
I should know about?
at_exit is a standard part of ruby
% ri at_exit
--------------------------------------------------------- Kernel#at_exit
at_exit { block } -> proc
Converts block to a Proc object (and therefore binds it at the
point of call) and registers it for execution when the program
exits. If multiple handlers are registered, they are executed in
reverse order of registration.
def do_at_exit(str1)
at_exit { print str1 }
end
at_exit { puts "cruel world" }
do_at_exit("goodbye ")
exit
produces:
goodbye cruel world
Most excellent. How on earth did you find that?
How did you know to look for it, orginally?
Or did you just happen to stumble across it while
persuing API docs using ri?? (In which case, that
would seem to be the thing to do!)
Have you read the PickAxe? (1st edition is available for free
online). Sometimes its easier to learn this minutia in the course of
reading a book instead of trying to hunt for stuff in reference
material.
Note that this is fairly “standard” and exists also in C. From the man
page of exit() on Linux:
DESCRIPTION
The exit() function causes normal program termination and the
the value of
status & 0377 is returned to the parent (see wait(2)). All
functions regis-
tered with atexit() and on_exit() are called in the reverse
order of their
registration, and all open streams are flushed and closed.
Files created by
tmpfile() are removed.