R – How should I promote Perl warnings to fatal errors during development

continuous integrationperlunit testingwarnings

When running an applications test suite I want to promote all Perl compile and run-time warnings (eg the "Uninitialized variable" warning) to fatal errors so that I and the other developers investigate and fix the code generating the warning. But I only want to do this during development and CI testing. In production, the warnings should just stay as warnings.

I tried the following: In "t/lib" I created a module TestHelper.pm:

# TestHelper.pm
use warnings FATAL => qw( all );
1;

Then called the test suite like this:

$ PERL5LIB=$PERL5LIB:./t/lib PERL5OPT=-MTestHelper.pm prove -l t/*.t

But this did not have the desired effect of promoting all warnings to fatal errors. I got the warnings as normal but the warnings did not appear to be treated as fatal. Note that all my test.t scripts have the line "use warnings;" in them — perhaps this over-rides the one in TestHelper.pm because "use warnings" has a local scope?

Instead I've done this:

# TestHelper.pm
# Promote all warnings to fatal 
$SIG{__WARN__} = sub { die @_; };
1;

This works but has a code smell about it. I also don't like that it bombs out on the first warning. I'd rather the test suite ran in full, logging all warnings, but that the test status ultimately failed because the code ran with warnings.

Is there a better way to achieve this end result?

Best Answer

I think you're looking for Test::NoWarnings.