在开发过程中,我应该如何将 Perl 警告升级为致命错误? [英] How should I promote Perl warnings to fatal errors during development?

查看:16
本文介绍了在开发过程中,我应该如何将 Perl 警告升级为致命错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在运行应用程序测试套件时,我想将所有 Perl 编译和运行时警告(例如未初始化变量"警告)升级为致命错误,以便我和其他开发人员调查并修复生成警告的代码.但我只想在开发和 CI 测试期间这样做.在生产中,警告应该只是作为警告.

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.

我尝试了以下方法:在t/lib"中,我创建了一个模块 TestHelper.pm:

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

但这并没有将所有警告提升为致命错误的预期效果.我收到了正常的警告,但警告似乎没有被视为致命.请注意,我所有的 test.t 脚本都有使用警告;"这一行.在它们中-也许这会覆盖 TestHelper.pm 中的那个,因为使用警告"具有本地范围?

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?

相反,我这样做了:

# 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?

推荐答案

我想你正在寻找 测试::NoWarnings.

这篇关于在开发过程中,我应该如何将 Perl 警告升级为致命错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆