Rake: Making "cleanup" task

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? :slight_smile:

TIA;
Pistos

Pistos C. wrote:

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? :slight_smile:

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.


– Jim W.

Jim W. wrote:

Is this possible? If so, how? :slight_smile:

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

rake [args, if any]
rake cleanup_task

It’s too bad Rake can’t do this [yet]. :\

Pistos

Eric A. wrote:

Jim W. wrote:

Is this possible? If so, how? :slight_smile:

Rake is just executing a ruby script, right?

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.

Pistos

Jim W. wrote:

Is this possible? If so, how? :slight_smile:

Rake is just executing a ruby script, right?

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)

Pistos C. wrote:

I thought END in Perl made the interpreter stop and ignore
everything below that line.

It does, and ruby does the same thing with END

Zach

On 4/7/06, Pistos C. [email protected] wrote:

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
}

-austin

Austin Z. wrote:

On 4/7/06, Pistos C. [email protected] wrote:

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


– Jim W.

Austin Z. wrote:

wondering why it wouldn’t work)
END {

# put your cleanup code here

}

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

Zach

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.
:_)

On Apr 7, 2006, at 9:10 PM, Eric A. wrote:

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

On Apr 10, 2006, at 7:47 PM, Eric A. wrote:

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.

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!)

I’ve got the original version of Pickaxe,
along with 2 others I got several years
ago (Nutshell and The Way).

I’m looking forward to getting updated
versions. Pickaxe has been updated in
print yet, has it?

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.

Guillaume.

Le 10 avr. 06, à 19:47, Eric A. a écrit :

On Apr 11, 2006, at 12:01 AM, Eric A. wrote:

I’m looking forward to getting updated
versions. Pickaxe has been updated in
print yet, has it?

Yeah there is a second edition now

Thanks. It must be standard on unix systems. All those
years wasted mastering Data General technologies…
Sigh.
:_)

Le 14 avr. 06, à 00:50, Eric A. a écrit :

Thanks. It must be standard on unix systems. All those
years wasted mastering Data General technologies…
Sigh.
:_)

That’s why I put quotes around standard. To exaggerate a bit: standard
is in the eyes of the beholder :slight_smile:

Guillaume.