[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Rake post test task

Daniel Berger

2/18/2009 3:15:00 PM

Hi,

How do I run a task after the fact with Rake?

For example, I have a test task for a C extension. It looks something
like this:

Rake::TestTask.new('test') do |test|
task :test => [:build]
test.libs << 'ext'
test.warning = true
test.verbose = true
end

That works fine, but I'd like it to run the "clean" task after it's
finished.

And no, simply sticking "task :test => [:clean]" at the bottom of the
test task doesn't work.

Regards,

Dan

9 Answers

Tim Pease

2/18/2009 4:10:00 PM

0


On Feb 18, 2009, at 8:15 AM, Daniel Berger wrote:

> Hi,
>
> How do I run a task after the fact with Rake?
>
> For example, I have a test task for a C extension. It looks something
> like this:
>
> Rake::TestTask.new('test') do |test|
> task :test => [:build]
> test.libs << 'ext'
> test.warning = true
> test.verbose = true
> end
>
> That works fine, but I'd like it to run the "clean" task after it's
> finished.
>
> And no, simply sticking "task :test => [:clean]" at the bottom of the
> test task doesn't work.
>
> Regards,
>
> Dan
>


task :test do
Rake.application[:clean].execute
end


You can append as many blocks of code to a task as you want. The
blocks will be run in the order they were added. This block looks up
the "clean" task in the application and then invokes the execute method.

Blessings,
TwP

Daniel Berger

2/18/2009 6:59:00 PM

0



On Feb 18, 9:10=A0am, Tim Pease <tim.pe...@gmail.com> wrote:
> On Feb 18, 2009, at 8:15 AM, Daniel Berger wrote:
>
>
>
>
>
> > Hi,
>
> > How do I run a task after the fact with Rake?
>
> > For example, I have a test task for a C extension. It looks something
> > like this:
>
> > Rake::TestTask.new('test') do |test|
> > =A0 task :test =3D> [:build]
> > =A0 test.libs << 'ext'
> > =A0 test.warning =3D true
> > =A0 test.verbose =3D true
> > end
>
> > That works fine, but I'd like it to run the "clean" task after it's
> > finished.
>
> > And no, simply sticking "task :test =3D> [:clean]" at the bottom of the
> > test task doesn't work.
>
> > Regards,
>
> > Dan
>
> task :test do
> =A0 =A0Rake.application[:clean].execute
> end
>
> You can append as many blocks of code to a task as you want. The =A0
> blocks will be run in the order they were added. This block looks up =A0
> the "clean" task in the application and then invokes the execute method.

Thanks Tim, that will work.

Regards,

Dan

Ryan Davis

2/19/2009 5:06:00 AM

0


On Feb 18, 2009, at 07:15 , Daniel Berger wrote:

> How do I run a task after the fact with Rake?
>
> For example, I have a test task for a C extension. It looks something
> like this:
>
> Rake::TestTask.new('test') do |test|
> task :test => [:build]
> test.libs << 'ext'
> test.warning = true
> test.verbose = true
> end
>
> That works fine, but I'd like it to run the "clean" task after it's
> finished.
>
> And no, simply sticking "task :test => [:clean]" at the bottom of the
> test task doesn't work.

task :test => :clean DOES work, just _before_ the fact, not after.

to do after you attach another task to the same name:

task :test do
after
end

One problem with Tim's suggestion:

> task :test do
> Rake.application[:clean].execute
> end

is that it won't run if that dependency has already been met, so:

% rake clean test

won't run it after your test. You might prefer to do:

> task :test do
> sh "rake clean"
> end

instead.

----

that said... why are you cleaning afterwards? I can imagine a number
of situations where that will screw you up. You probably DO want to
put :clean (pre) dependencies on a number of your tasks like packaging
and stuff, but having it after your test could introduce some hiccups.

if it is a lengthy build/link, it'll also slow down code/test cycles.


Rick DeNatale

2/19/2009 5:08:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

On Thu, Feb 19, 2009 at 12:06 AM, Ryan Davis <ryand-ruby@zenspider.com>wrote:

>
> On Feb 18, 2009, at 07:15 , Daniel Berger wrote:
>
> How do I run a task after the fact with Rake?
>>
>> For example, I have a test task for a C extension. It looks something
>> like this:
>>
>> Rake::TestTask.new('test') do |test|
>> task :test => [:build]
>> test.libs << 'ext'
>> test.warning = true
>> test.verbose = true
>> end
>>
>> That works fine, but I'd like it to run the "clean" task after it's
>> finished.
>>
>> And no, simply sticking "task :test => [:clean]" at the bottom of the
>> test task doesn't work.
>>
>
> task :test => :clean DOES work, just _before_ the fact, not after.
>
> to do after you attach another task to the same name:
>
> task :test do
> after
> end


The after here is a placeholder for code, right? It can't be a task name
since a task does not define a method which can be invoked like that, at
least not with the Rake I've got installed on my maching.

Unless I'm missing something this is effectively the same thing as one :test
task with the code from both inside the block.

It works but...


>
> that said... why are you cleaning afterwards? I can imagine a number of
> situations where that will screw you up. You probably DO want to put :clean
> (pre) dependencies on a number of your tasks like packaging and stuff, but
> having it after your test could introduce some hiccups.
>


The way I'd approach this would be to have tasks for whatever sequences of
subtasks make sense, so something like:

desc "clean up"
task :clean do |t|
puts "cleaning up"
end

desc "test"
task :test do |t|
puts "testing"
end

desc "test and clean up"
task :test_and_cleanup => [:test, :clean]

desc "clean up then test"
task :cleanup_and_test => [:clean, :test]

Task names could be adjusted, so if you wanted to normally clean up and then
test, or test and then clean up you could rename
the 'normal' task, .e.g :test becomes something like :test_only and
:cleanup_and_test becomes :test


--
Rick DeNatale

Blog: http://talklikeaduck.denh...
Twitter: http://twitter.com/Ri...
WWR: http://www.workingwithrails.com/person/9021-ric...
LinkedIn: http://www.linkedin.com/in/ri...

Daniel Berger

2/19/2009 6:09:00 PM

0



On Feb 18, 10:06=A0pm, Ryan Davis <ryand-r...@zenspider.com> wrote:
> On Feb 18, 2009, at 07:15 , Daniel Berger wrote:
>
>
>
> > How do I run a task after the fact with Rake?
>
> > For example, I have a test task for a C extension. It looks something
> > like this:
>
> > Rake::TestTask.new('test') do |test|
> > =A0 task :test =3D> [:build]
> > =A0 test.libs << 'ext'
> > =A0 test.warning =3D true
> > =A0 test.verbose =3D true
> > end
>
> > That works fine, but I'd like it to run the "clean" task after it's
> > finished.
>
> > And no, simply sticking "task :test =3D> [:clean]" at the bottom of the
> > test task doesn't work.
>
> task :test =3D> :clean DOES work, just _before_ the fact, not after.
>
> to do after you attach another task to the same name:
>
> task :test do
> =A0 =A0after
> end
>
> One problem with Tim's suggestion:
>
> > task :test do
> > =A0Rake.application[:clean].execute
> > end
>
> is that it won't run if that dependency has already been met, so:
>
> % rake clean test
>
> won't run it after your test. You might prefer to do:
>
> > task :test do
> > =A0 sh "rake clean"
> > end
>
> instead.

Interesting, thanks.

> that said... why are you cleaning afterwards? I can imagine a number =A0
> of situations where that will screw you up. You probably DO want to =A0
> put :clean (pre) dependencies on a number of your tasks like packaging =
=A0
> and stuff, but having it after your test could introduce some hiccups.
>
> if it is a lengthy build/link, it'll also slow down code/test cycles.

It's a small C extension, meaning I have to :build before I run the
tests. That's fine, but it leaves the .so, .obj, etc, files laying
around afterward. So rather than running 'rake clean' (or running
'make distclean' manually) after every 'rake test', I just want the
build files cleaned up automatically because I have no use for them.

Regards,

Dan

Daniel Berger

2/19/2009 6:11:00 PM

0

On Feb 19, 10:07=A0am, Rick DeNatale <rick.denat...@gmail.com> wrote:

<snip>

> The way I'd approach this would be to have tasks for whatever sequences o=
f
> subtasks make sense, so something like:
>
> =A0 desc "clean up"
> =A0 task :clean do |t|
> =A0 =A0 puts "cleaning up"
> =A0 end
>
> =A0 desc "test"
> =A0 task :test do |t|
> =A0 =A0 puts "testing"
> =A0 end
>
> =A0 desc "test and clean up"
> =A0 task :test_and_cleanup =3D> [:test, :clean]
>
> =A0 desc "clean up then test"
> =A0 task :cleanup_and_test =3D> [:clean, :test]
>
> Task names could be adjusted, so if you wanted to normally clean up and t=
hen
> test, or test and then clean up you could rename
> the 'normal' task, .e.g :test becomes something like :test_only and
> :cleanup_and_test becomes :test

Hm, maybe I should just create a separate task as you say.

Thanks,

Dan

Daniel Berger

2/19/2009 6:16:00 PM

0



On Feb 19, 11:10=A0am, Daniel Berger <djber...@gmail.com> wrote:
> On Feb 19, 10:07=A0am, Rick DeNatale <rick.denat...@gmail.com> wrote:
>
> <snip>
>
>
>
> > The way I'd approach this would be to have tasks for whatever sequences=
of
> > subtasks make sense, so something like:
>
> > =A0 desc "clean up"
> > =A0 task :clean do |t|
> > =A0 =A0 puts "cleaning up"
> > =A0 end
>
> > =A0 desc "test"
> > =A0 task :test do |t|
> > =A0 =A0 puts "testing"
> > =A0 end
>
> > =A0 desc "test and clean up"
> > =A0 task :test_and_cleanup =3D> [:test, :clean]
>
> > =A0 desc "clean up then test"
> > =A0 task :cleanup_and_test =3D> [:clean, :test]
>
> > Task names could be adjusted, so if you wanted to normally clean up and=
then
> > test, or test and then clean up you could rename
> > the 'normal' task, .e.g :test becomes something like :test_only and
> > :cleanup_and_test becomes :test
>
> Hm, maybe I should just create a separate task as you say.

Upon further review that won't work, because the :clean task is
already a prerequisite for the :build task. So, what I'm really doing
is "clean, build, test, clean".

It may seem redundant, but sometimes I do the ol' "ruby extconf.rb;
make" routine by hand first for various reasons, then run a rake task
later, and I won't the "old" build files cleaned up first.

Regards,

Dan

Tim Pease

2/19/2009 8:35:00 PM

0


On Feb 18, 2009, at 10:06 PM, Ryan Davis wrote:

>
> On Feb 18, 2009, at 07:15 , Daniel Berger wrote:
>
>> How do I run a task after the fact with Rake?
>>
>> For example, I have a test task for a C extension. It looks something
>> like this:
>>
>> Rake::TestTask.new('test') do |test|
>> task :test => [:build]
>> test.libs << 'ext'
>> test.warning = true
>> test.verbose = true
>> end
>>
>> That works fine, but I'd like it to run the "clean" task after it's
>> finished.
>>
>> And no, simply sticking "task :test => [:clean]" at the bottom of the
>> test task doesn't work.
>
> task :test => :clean DOES work, just _before_ the fact, not after.
>
> to do after you attach another task to the same name:
>
> task :test do
> after
> end
>
> One problem with Tim's suggestion:
>
>> task :test do
>> Rake.application[:clean].execute
>> end
>
> is that it won't run if that dependency has already been met, so:
>
> % rake clean test
>
> won't run it after your test. You might prefer to do:
>

The Rake::Task#execute method will always run the task regardless of
whether it has been run previously. You are thinking of the
Rake::Task#invoke method -- this method ensures the task is only
executed once.

Blessings,
TwP

Daniel Berger

2/26/2009 4:38:00 PM

0

On Thu, Feb 19, 2009 at 1:34 PM, Tim Pease <tim.pease@gmail.com> wrote:
>
> On Feb 18, 2009, at 10:06 PM, Ryan Davis wrote:
>
>>
>> On Feb 18, 2009, at 07:15 , Daniel Berger wrote:
>>
>>> How do I run a task after the fact with Rake?
>>>
>>> For example, I have a test task for a C extension. It looks something
>>> like this:
>>>
>>> Rake::TestTask.new('test') do |test|
>>> =A0task :test =3D> [:build]
>>> =A0test.libs << 'ext'
>>> =A0test.warning =3D true
>>> =A0test.verbose =3D true
>>> end
>>>
>>> That works fine, but I'd like it to run the "clean" task after it's
>>> finished.
>>>
>>> And no, simply sticking "task :test =3D> [:clean]" at the bottom of the
>>> test task doesn't work.
>>
>> task :test =3D> :clean DOES work, just _before_ the fact, not after.
>>
>> to do after you attach another task to the same name:
>>
>> task :test do
>> =A0after
>> end
>>
>> One problem with Tim's suggestion:
>>
>>> task :test do
>>> Rake.application[:clean].execute
>>> end
>>
>> is that it won't run if that dependency has already been met, so:
>>
>> % rake clean test
>>
>> won't run it after your test. You might prefer to do:
>>
>
> The Rake::Task#execute method will always run the task regardless of whet=
her
> it has been run previously. You are thinking of the Rake::Task#invoke met=
hod
> -- this method ensures the task is only executed once.

Interestingly, I discovered that the Rake::Task.execute approach you
suggested broke with Rake 0.8.1. It worked fine after I upgraded to
0.8.3.

Don't ask me how I forgot to upgrade Rake on this particular system,
but there you go.

Regards,

Dan