I grovelled through more of the Perl standard library than I ever wanted to to come up with this as my best option so far:
# Install a filter to prevent BAIL_OUT() from aborting the whole
# run.
Test2::API::test2_add_callback_context_init(sub {
my $context = shift;
$context->hub->filter(sub {
my ($hub, $event) = @_;
#
# If you want to see details on the event contents use:
#
#print("EVENT FILTER: " . Dumper($event) . "\n");
# Turn BAIL_OUT() into die(), or maybe $ctx->throw(). We can't easily
# get the context.
if ($event->isa('Test2::Event::Bail')) {
# Ideally we'd produce a Test2::Event with a Test2::EventFacet::Control
# with terminate true, and a Test2::EventFacet::Trace . For for
# now we'll just log the BAIL_OUT() event and exit.
#
# If we ->throw it instead, we get a big stack for little benefit.
# And END { } blocks will still get run when we exit(...) here so
# appropriate cleanup still happens.
#
$event->trace->alert($event->reason);
exit(1);
}
return $event;
});
});
This will cause a test script that calls BAIL_OUT("FOOBAR");
to bail with something like
FOOBAR at t/foo.pl line 4.
# Looks like your test exited with 1 just after 5.
then prove
stderr will show:
t/foo.pl ......... 1/38
# Looks like your test exited with 1 just after 5.
and the summary will show
t/foo.pl (Wstat: 256 Tests: 5 Failed: 0)
Non-zero exit status: 1
Parse errors: Bad plan. You planned 38 tests but ran 5.
This is far from ideal, as the stderr and summary doesn't tell us what went wrong.
But it prevents the BAIL_OUT()
from nuking the rest of the run.
One downside is that this won't help provide more info for tests that die()
. So I'm still looking for better options.