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

···

--
Posted via http://www.ruby-forum.com/.

Pistos Christou 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 Weirich

--
Posted via http://www.ruby-forum.com/\.

Jim Weirich 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

···

--
Posted via http://www.ruby-forum.com/\.

Jim Weirich 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)

Eric Armstrong wrote:

Jim Weirich 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

···

--
Posted via http://www.ruby-forum.com/\.

Pistos Christou 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

It does.

However, that's not what Eric is talking about.

He's talking about:

  END {
    # put your cleanup code here
  }

-austin

···

On 4/7/06, Pistos Christou <jesusrubsyou.5.pistos@geoshell.com> wrote:

Eric Armstrong wrote:
>> Jim Weirich 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.

--
Austin Ziegler * halostatue@gmail.com
               * Alternate: austin@halostatue.ca

Austin Ziegler 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

···

On 4/7/06, Pistos Christou <jesusrubsyou.5.pistos@geoshell.com> wrote:

--
-- Jim Weirich

--
Posted via http://www.ruby-forum.com/\.

Austin Ziegler wrote:

···

On 4/7/06, Pistos Christou <jesusrubsyou.5.pistos@geoshell.com> wrote:

Eric Armstrong wrote:

Jim Weirich 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.

It does.

However, that's not what Eric is talking about.

He's talking about:

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

Jim Weirich wrote:

···

Austin Ziegler wrote:

On 4/7/06, Pistos Christou <jesusrubsyou.5.pistos@geoshell.com> 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 Weirich

at_exit is a standard part of ruby
% ri at_exit
--------------------------------------------------------- Kernel#at_exit
      at_exit { block } -> proc

···

On Apr 7, 2006, at 9:10 PM, Eric Armstrong 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?

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

Logan Capaldo wrote:

···

On Apr 7, 2006, at 9:10 PM, Eric Armstrong 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

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.

···

On Apr 10, 2006, at 7:47 PM, Eric Armstrong 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!)

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 Armstrong a écrit :

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

Logan Capaldo wrote:

On Apr 7, 2006, at 9:10 PM, Eric Armstrong 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

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?

Logan Capaldo wrote:

···

On Apr 10, 2006, at 7:47 PM, Eric Armstrong 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.

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

Guillaume Marcais wrote:

···

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 Armstrong a écrit :

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

Logan Capaldo wrote:

On Apr 7, 2006, at 9:10 PM, Eric Armstrong 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

Yeah there is a second edition now

···

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

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

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.

···

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

Guillaume Marcais wrote:

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 Armstrong a écrit :

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

Logan Capaldo wrote:

On Apr 7, 2006, at 9:10 PM, Eric Armstrong 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