jeudi 28 mai 2015

Rake running all my invokes after task block

As outlined in a previous question, I'm trying to get my testing database to reset properly between runs of each of our three suites: Units, functionals, and integration. These suites run without a problem individually, as the database is cleared beforehand, but for some reason this doesn't happen when running all three in succession using rake test.

So what does a complete newbie to Rails-flavored Rake do? He tries to futz with it by making his own version of rake test where the reset-between-suites does happen, guided somewhat by this substantially more clear-headed post by Ken Collins. The result is this crude chunk of code stuffed into my Rakefile:

Rake::Task['test:run'].clear # Intent: Kill default "rake test" task. Seems to work.
namespace :test do
    task :run do
        if Rails.env == "test" # Try not to obliterate other DBs.
            Rake::Task["db:test:prepare"].reenable
            Rake::Task["db:test:prepare"].invoke
            Rake::Task["test:units"].reenable
            Rake::Task["test:units"].invoke

            Rake::Task["db:test:prepare"].reenable
            Rake::Task["db:test:prepare"].invoke
            Rake::Task["test:functionals"].reenable
            Rake::Task["test:functionals"].invoke

            Rake::Task["db:test:prepare"].reenable
            Rake::Task["db:test:prepare"].invoke
            Rake::Task["test:integration"].reenable
            Rake::Task["test:integration"].invoke
        end
    end
end

What results from the above seems to be exactly the same as if I were to run the vanilla rake test - The suites run in order but the database does not reset as I intended, resulting in the same weird breakages each time.

But I know that it's my custom task running, because having puts statements in the block results in the statements printing to my screen BEFORE any of the test suites are run.

In other words, it looks like rake is running all my invoked tasks AFTER the chunk of code defined in my :run task AND not running those invoked tasks more than once, which is a serious problem when the success of my suites relies on some of those tasks being able to run more than once and in the order I specify.

To be perfectly honest, I don't even know what I don't know about Rake to make it do what I want to do in this situation. If you have any guidance to offer I'd be major grateful.

Aucun commentaire:

Enregistrer un commentaire