Forum: Ruby Rake: Making "cleanup" task

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Pistos C. (Guest)
on 2006-04-05 23:59
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?  :)

TIA;
Pistos
Jim W. (Guest)
on 2006-04-06 01:09
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?  :)

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.
Pistos C. (Guest)
on 2006-04-06 19:40
Jim W. wrote:
>> 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.

/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. (Guest)
on 2006-04-07 06:19
(Received via mailing list)
> Jim W. wrote:
 >>> Is this possible?  If so, how?  :)
 >>>
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. (Guest)
on 2006-04-07 21:28
Eric A. wrote:
>> Jim W. wrote:
>  >>> Is this possible?  If so, how?  :)
>  >>>
> 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
zdennis (Guest)
on 2006-04-07 21:37
(Received via mailing list)
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
Austin Z. (Guest)
on 2006-04-07 22:01
(Received via mailing list)
On 4/7/06, Pistos C. <removed_email_address@domain.invalid> 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
Jim W. (Guest)
on 2006-04-07 22:06
Austin Z. wrote:
> On 4/7/06, Pistos C. <removed_email_address@domain.invalid> 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.
zdennis (Guest)
on 2006-04-07 22:11
(Received via mailing list)
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
Eric A. (Guest)
on 2006-04-08 05:10
(Received via mailing list)
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.
:_)
Logan C. (Guest)
on 2006-04-08 05:44
(Received via mailing list)
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
Eric A. (Guest)
on 2006-04-11 03:49
(Received via mailing list)
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 C. (Guest)
on 2006-04-11 05:57
(Received via mailing list)
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.
Eric A. (Guest)
on 2006-04-11 08:01
(Received via mailing list)
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 C. (Guest)
on 2006-04-11 08:22
(Received via mailing list)
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
Guillaume M. (Guest)
on 2006-04-12 18:44
(Received via mailing list)
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 :
Eric A. (Guest)
on 2006-04-14 08:54
(Received via mailing list)
Thanks. It must be standard on unix systems. All those
years wasted mastering Data General technologies...
Sigh.
:_)
Guillaume M. (Guest)
on 2006-04-14 19:57
(Received via mailing list)
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 :)

Guillaume.
This topic is locked and can not be replied to.