How should I promote Perl warnings to fatal errors during development?

半腔热情 提交于 2020-01-01 04:19:09

问题


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?


回答1:


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




回答2:


The reason use warnings FATAL => qw( all ); isn't working for you is because use warnings is lexically scoped. So any warnings produced inside TestHelper.pm would be fatal, but warnings produced elsewhere will work as normal.

If you want to enable fatal warnings globally, I think a $SIG{__WARN__} handler is probably the only way to do it. If you don't want it to blow up on the first warning, you could let your handler store them in an array, then check it in an END block.

my @WARNINGS;
$SIG{__WARN__} = sub { push @WARNINGS, shift };

END { 
    if ( @WARNINGS )  {       
        print STDERR "There were warnings!\n";
        print "$_\n" for @WARNINGS;
        exit 1;
    }
}


来源:https://stackoverflow.com/questions/1353179/how-should-i-promote-perl-warnings-to-fatal-errors-during-development

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!