"Michael G Schwern via RT" <bug-Test-NoWarnings@rt.cpan.org> writes:
Show quoted text>
> Tracking down what warned when and why is
> difficult when they're all smashed together at the end.
My main trouble was turning on Smart::Comments in modules for
development to see traces for a failing .t file. It's vital those
traces come out in sequence with the test output. They can be big too,
so don't really want to sit on them all in memory.
I found myself disabling Test::NoWarnings for that because it got in the
way, and then having to remember to re-enable it -- because I do want an
error for Smart::Comments or other warns leftover in the modules -- and
so having to dec and inc the test count etc ...
Show quoted text> The version of Test::NoWarnings I wrote as a test for Test::Builder2
> lets the warnings pass through and I find it far more pleasant to work with.
I use the blob below, initiated by calling nowarnings(). It grabs info
to show at the end but lets the warning otherwise go out unmolested.
I put it at the very start of a test script so no need to chain to a
previously installed $SIG{__WARN__}.
I ended up agreeing with the stack trace of Test::NoWarnings as a way of
diagnosing cpantesters reports, and keeping that trace until the end,
but when there's a lot of warnings I don't really want a trace for all
of them. I've found 3 enough to start on.
I preferred a non-zero exit to a test count too, so as to keep out of
the hair of the main .t code. With a little care it could probably be
made agnostic about which if any Test, Test::Simple, Test::More etc was
in use, but I've been happy with Test::More::diag().
{
my $warning_count;
my $stacktraces;
my $stacktraces_count = 0;
sub nowarnings_handler {
$warning_count++;
if ($stacktraces_count < 3 && eval { require Devel::StackTrace }) {
$stacktraces_count++;
$stacktraces .= "\n" . Devel::StackTrace->new->as_string() . "\n";
}
warn @_;
}
sub nowarnings {
$SIG{'__WARN__'} = \&nowarnings_handler;
}
END {
if ($warning_count) {
require Test::More;
Test::More::diag("Saw $warning_count warning(s):");
Test::More::diag($stacktraces);
Test::More::diag("Exit code 1 for warnings");
$? = 1;
}
}
}