你能要求 ruby 将警告视为错误吗?
ruby 是否允许您将警告视为错误?
我这样做的一个原因是为了确保如果删除一行代码意味着出现警告,我可以选择确保突变体被杀死。
Does ruby allow you to treat warnings as errors?
One reason I'd like to do this is to ensure that if heckle removing a line of code means that a warning occurs, I have the option of ensuring that the mutant get killed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不幸的是,没有真正的方法可以做到这一点,至少在大多数版本的 Ruby 上都没有(可能存在变体),缺乏监视程序输出并在以下情况下中止它:标准错误上会出现警告。 原因如下:
Kernel.warn
,您可以重新定义以执行您想要的任何操作(包括退出),并且您期望(希望)使用它Ruby 一致地报告警告(包括内部,例如解析警告),但是的本机方法rb_warn
来自source/server.c
,完全绕过您对Kernel.warn
的重新定义(例如“字符串文字in condition
”警告,例如,在执行以下操作时发出:do_something if 'string'
,通过来自source/ 的本机
)rb_warn
打印parse.crb_warning
本机方法,Ruby 可以使用它来记录警告,如果-w
或指定了-v
。因此,如果您只需要对应用程序代码调用
Kernel.warn
生成的警告采取行动,那么只需重新定义Kernel.warn
即可。 否则,您只有两个选择:source/error.c
以在rb_warn
和rb_warning
(以及rb_warn_m
?),并重建 Ruby:警告:
”,并在匹配时中止它There is unfortunately no real way of doing this, at least not on most versions of Ruby out there (variations may exist), short of monitoring the program output and aborting it when a warning appears on standard error. Here's why:
Kernel.warn
, which you can redefine to do whatever you wish (including exiting), and which you'd expect (hope) to be used consistently by Ruby to report warnings (including internal e.g. parsing warning), butrb_warn
fromsource/server.c
, completely bypassing your redefinition ofKernel.warn
(e.g. the "string literal in condition
" warning, for example, issued when doing something like:do_something if 'string'
, is printed via the nativerb_warn
fromsource/parse.c
)rb_warning
native method, which can be used by Ruby to log warnings if-w
or-v
is specified.So, if you need to take action solely on warnings generated by your application code's calling
Kernel.warn
then simply redefineKernel.warn
. Otherwise, you have exactly two options:source/error.c
to exit inrb_warn
andrb_warning
(andrb_warn_m
?), and rebuild Ruby: warning:
', and abort it on match您最终可以通过重写
Warning.warn
来做到这一点,如下所示,这会将警告转变为异常。 该解决方案至少从 2.4 分支开始有效。
You can finally do that by overriding
Warning.warn
likeThis will turn the warning into an exception. This solution works at least since 2.4 branch.
您还可以使用 DTrace,并拦截对
rb_warn
和rb_warning
的调用,尽管这不会产生您可以从某处挽救的异常。 相反,它只会将它们放在您可以轻松记录它们的地方。You could also potentially use DTrace, and intercept the calls to
rb_warn
andrb_warning
, though that's not going to produce exceptions you can rescue from somewhere. Rather, it'll just put them somewhere you can easily log them.