到目前为止,我对 Perl 标准库的了解比我想的要多得多:
# 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;
});
});
这将导致一个测试脚本调用BAIL_OUT("FOOBAR");
保释,例如
FOOBAR at t/foo.pl line 4.
# Looks like your test exited with 1 just after 5.
然后prove
stderr 将显示:
t/foo.pl ......... 1/38
# Looks like your test exited with 1 just after 5.
总结会显示
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.
这远非理想,因为标准错误和摘要并没有告诉我们出了什么问题。
但它阻止了BAIL_OUT()
其余的运行。
一个缺点是这无助于为die()
. 所以我还在寻找更好的选择。