File Coverage

blib/lib/Test/Perl/Lint.pm
Criterion Covered Total %
statement 57 63 90.4
branch 10 20 50.0
condition 3 9 33.3
subroutine 11 11 100.0
pod 1 1 100.0
total 82 104 78.8


line stmt bran cond sub pod time code
1             package Test::Perl::Lint;
2 2     2   11982 use strict;
  2         3  
  2         62  
3 2     2   8 use warnings;
  2         3  
  2         56  
4 2     2   631 use utf8;
  2         3  
  2         11  
5 2     2   772 use parent qw/Test::Builder::Module/;
  2         465  
  2         7  
6              
7             my @test_more_exports;
8 2     2   827 BEGIN { @test_more_exports = (qw/done_testing/) }
9 2     2   504 use Test::More import => \@test_more_exports;
  2         3320  
  2         12  
10 2     2   382 use Carp ();
  2         3  
  2         44  
11 2     2   1432 use Path::Tiny 0.068 qw/path/;
  2         17512  
  2         120  
12 2     2   728 use Perl::Lint;
  2         6  
  2         819  
13              
14             our $VERSION = "0.24";
15              
16             our @EXPORT = (@test_more_exports, qw/all_policies_ok/);
17              
18             sub all_policies_ok {
19 1     1 1 165 local $Test::Builder::Level = $Test::Builder::Level + 2;
20              
21 1         1 my ($args) = @_;
22              
23 1   33     4 my $targets = $args->{targets} // Carp::croak "Targets must not be empty";
24 1         2 my $ignore_files = $args->{ignore_files};
25              
26 1 50 33     9 if (defined $targets && ref $targets ne 'ARRAY') {
27 0         0 Carp::croak 'Target directories are must be an array reference';
28             }
29              
30 1 50 33     7 if (defined $ignore_files && ref $ignore_files ne 'ARRAY') {
31 0         0 Carp::croak 'Ignore files are must be an array reference';
32             }
33              
34             my $linter = Perl::Lint->new({
35             ignore => $args->{ignore_policies},
36             filter => $args->{filter},
37 1         36 });
38              
39 1         2 my @paths;
40 1         3 for my $target (@$targets) {
41 1 50       38 if (-d $target) {
    0          
42             path($target)->visit(sub {
43 3     3   348 my ($path) = @_;
44 3 50       8 if ($path->is_file) {
45 3         32 my $path_string = $path->stringify;
46 3 100       7 if (!grep {$_ eq $path_string} @$ignore_files) {
  3         12  
47 2         9 push @paths, $path_string;
48             }
49             }
50 1         6 }, {recurse => 1});
51             }
52             elsif (-f $target) {
53 0 0       0 if (!grep {$_ eq $target} @$ignore_files) {
  0         0  
54 0         0 push @paths, $target;
55             }
56             }
57             else {
58 0         0 Carp::carp "'$target' doesn't exist";
59             }
60             }
61 1         59 @paths = sort {$a cmp $b} @paths;
  1         3  
62              
63 1         2 for my $path_string (@paths) {
64 2         508 my $violations = $linter->lint($path_string);
65 2 100       5 if (scalar @$violations == 0) {
66 1         6 Test::More::pass(__PACKAGE__ . ' for ' . $path_string);
67             }
68             else {
69 1         2 my $package = __PACKAGE__;
70 1         3 my $error_msg = <<"...";
71              
72             $package found these violations in "$path_string":
73             ...
74              
75 1         2 for my $violation (@$violations) {
76 1         2 my $explanation = $violation->{explanation};
77 1 50       4 if (ref $explanation eq 'ARRAY') {
78 1         3 $explanation = 'See page ' . join(', ', @$explanation) . ' of PBP';
79             }
80 1         5 $error_msg .= <<"...";
81             $violation->{description} at line $violation->{line}. $explanation.
82             ...
83             }
84              
85 1 50       17 Test::More::ok(0, "$package for $path_string") or Test::More::diag($error_msg);
86             }
87             }
88              
89 1         235 return;
90             }
91              
92             1;
93              
94             __END__