in reply to Re^2: BAIL_OUT with older Test::More
in thread BAIL_OUT with older Test::More

Well, I was thinking of this as well, but with the red tape and all I still don't know if I can introduce foreign code like this. It depends on whether the management will OK this or not, and whether the IT people feel paranoid that day...

In the meantime I'm doing without BAIL_OUT. In the eventuality that I can't use a more modern Test::More at all, what do you suggest?

Replies are listed 'Best First'.
Re^4: BAIL_OUT with older Test::More
by ELISHEVA (Prior) on Feb 15, 2011 at 17:20 UTC

    Looking at the oldest source code on CPAN (Test-Harness 2.52) it might be as simple as die "Bail out!" when you want to bail.

    The older versions of prove are backed by Test::Harness. prove calls Test::Harness::runtests which does nothing more than stuff each .t file into a strap (see Test::Harness::Straps), execute it, and add the results to its cumulative total.

    Digging down into the source code, I notice that Test::Harness::Straps has a flag $self->{saw_bailout} that is set to 1 if the .t file's output stream (STDERR, I believe) contains a line matching: /^Bail out!\s*(.*)/i. Test::Harness checks for this flag and, if found, calls a bail out handler subroutine which... aborts the testing process.

    If that doesn't work and you are very determined, I'd consider studying the source code of those two modules closely and writing a test runner script of your own that does what you want.

      Back at home now, I've just tried die('Bail out! Something happened'), but that just does the same as dying with any random string.

      print "Bail out! Something happened\n", however, works, with a caveat.

      This:

      use Test::More tests => 2; ok(1, 'duh'); print "Bail out! Something happened.\n"; ok(2, 'buh');

      Results in:

      blah.t .. 1/2 Bailout called. Further testing stopped: Something hap +pened. FAILED--Further testing stopped: Something happened.

      Which is exactly what I want, but removing the \n in the print 'Bail out! Something happened' statement gives:

      blah.t .. 1/2 Bailout called. Further testing stopped: Something hap +pened.ok 2 - buh FAILED--Further testing stopped: Something happened.ok 2 - buh

      Which is apparently the harness choking on the bailout message, note the ok 2 - buh there at the end.

      I'm trying all this on a recent Perl and Test::More at home, so I'm not sure it'll work on the older Perl at work, but it's worth a try. I'll have to remember to document print 'Bail out! blah' everywhere I use it though, especially since the rest of the code is in French and someday some guy might decide to translate it "properly". Oh well.

      Anyway, thanks a lot for taking the time to source dive for some random Anonymous Monk, I appreciate it!

        Thanks for letting us know how it seems to be working.

        Why not wrap it in a sub and document the sub? That way everything will be in one place and you'll have less to type each time you want to bail.

        BTW, If you did want to abort, you could probably do print ....; die - the print to force the bail. The die to abort that particular .t file on the spot. Maybe you could even pass a flag to your sub indicating whether you want to die now or not?

Re^4: BAIL_OUT with older Test::More
by JavaFan (Canon) on Feb 15, 2011 at 17:01 UTC
    One trick is to have each .t file check whether a file .BAILOUT exists. If it does, don't run any tests. If you want to BAILOUT, create this file, then die. Use a first (and a last) test file that cleans up the .BAILOUT file.