开发者

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

any clue ?

F.


Consider using Perl::Critic instead, which can do this more reliably, is configurable, and do a whole lot more. There is even Test::Perl::Critic to enforce it.

Perl::Critic also has the advantage of being aware of things like Moose which turn on strictures.


To check for use strict just do this:

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.

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.

As I understand it, the grep method in File::Find::Rule will evaluate each line of a file, using each specifier provided, stopping (i.e., retaining the file in its result set) on the first true evaluation. File::Find::Rule provides an example of making sure every file begins with a shebang line. 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.

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

For a use strict test, however, you don't want to restrict yourself to the first line. 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. Hope that helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜