简体   繁体   English

测试:: File :: Find :: Rule检查我们所有的perl脚本都使用严格的

[英]Test::File::Find::Rule check that all our perl scripts have use strict

use warnings;
use Test::More;
use File::Find::Rule;
use Test::File::Find::Rule;

my $rule = File::Find::Rule->file->name('*.pl')->not_grep(qr/^\s*use\s+strict;/m, sub { 1 });
match_rule_no_result($rule, ".", 'use strict usage');
done_testing();

and the output was : 输出为:

out put :
ok 1 - use strict usage
1..1

it is always passing the test even when my script doesn't use strict, just like this script which is located inside "." 即使我的脚本未使用严格脚本,它始终可以通过测试,就像位于“”中的脚本一样。 directory. 目录。 the same code is available as an example at http://metacpan.org/pod/Test::File::Find::Rule http://metacpan.org/pod/Test::File::Find::Rule中可以使用相同的代码作为示例

any clue ? 有什么线索吗?

F. F。

Consider using Perl::Critic instead, which can do this more reliably, is configurable, and do a whole lot more. 考虑改用Perl :: Critic ,它可以更可靠地完成此操作,并且是可配置的,并且可以执行更多操作。 There is even Test::Perl::Critic to enforce it. 甚至还有Test :: Perl :: Critic来强制执行它。

Perl::Critic also has the advantage of being aware of things like Moose which turn on strictures. Perl :: Critic还具有了解Moose之类的东西的优势,这些东西会打开狭窄部位。

To check for use strict just do this: 要检查use strict只需执行以下操作:

my $rule = File::Find::Rule
    ->file->name('*.pl')
    ->not_grep( qr/^\s*use\s+strict;/ )
;

Update 更新资料

I agree with Schwern and jrockway: there's a better module for enforcing use strict . 我同意Schwern和jrockway的观点:有一个更好的模块来强制use strict

That said, here's what I was able to figure out about the particulars of your question. 就是说,这就是我能够弄清楚您问题的细节的内容。

The use strict example provided by Test::File::Find::Rule is misguided. Test :: File :: Find :: Rule提供的use strict示例被误导了。

As I understand it, the grep method in File::Find::Rule will evaluate each line of a file, using each specifier provided, stopping (ie, retaining the file in its result set) on the first true evaluation. 据我了解, File :: Find :: Rule中grep方法将使用提供的每个说明符来评估文件的每一行,并在第一个真实评估时停止(即,将文件保留在其结果集中)。 File::Find::Rule provides an example of making sure every file begins with a shebang line. File::Find::Rule提供了一个确保每个文件 shebang行开头的示例。 If the first line fails the regex, the next specifier (the anonymous subroutine) will always return true and the non-conforming file will be found. 如果第一行的正则表达式失败,则下一个说明符(匿名子例程)将始终返回true,并且将找到不符合要求的文件。

$rule->grep( qr/^#!.*\bperl/, [ sub { 1 } ] );

For a use strict test, however, you don't want to restrict yourself to the first line. 但是,对于use strict测试,您不想将自己限制在第一行。 Also, the not_grep method makes the additional specifiers unnecessary: we will retain the file if none of the lines in the file match the regex. 另外, not_grep方法not_grep其他说明符:如果文件中没有任何行与正则表达式匹配,我们将保留该文件。 Hope that helps. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM