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   15909 use strict;
  2         4  
  2         105  
3 2     2   9 use warnings;
  2         4  
  2         97  
4 2     2   8 use utf8;
  2         1  
  2         14  
5 2     2   885 use parent qw/Test::Builder::Module/;
  2         547  
  2         10  
6              
7             my @test_more_exports;
8 2     2   1033 BEGIN { @test_more_exports = (qw/done_testing/) }
9 2     2   573 use Test::More import => \@test_more_exports;
  2         4591  
  2         16  
10 2     2   488 use Carp ();
  2         5  
  2         40  
11 2     2   1764 use Path::Tiny 0.068 qw/path/;
  2         26160  
  2         155  
12 2     2   689 use Perl::Lint;
  2         8  
  2         1085  
13              
14             our $VERSION = "0.23";
15              
16             our @EXPORT = (@test_more_exports, qw/all_policies_ok/);
17              
18             sub all_policies_ok {
19 1     1 1 218 local $Test::Builder::Level = $Test::Builder::Level + 2;
20              
21 1         2 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     11 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     6 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 1         42 my $linter = Perl::Lint->new({
35             ignore => $args->{ignore_policies},
36             filter => $args->{filter},
37             });
38              
39 1         4 my @paths;
40 1         4 for my $target (@$targets) {
41 1 50       58 if (-d $target) {
    0          
42             path($target)->visit(sub {
43 3     3   470 my ($path) = @_;
44 3 50       10 if ($path->is_file) {
45 3         38 my $path_string = $path->stringify;
46 3 100       9 if (!grep {$_ eq $path_string} @$ignore_files) {
  3         14  
47 2         9 push @paths, $path_string;
48             }
49             }
50 1         9 }, {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         119 @paths = sort {$a cmp $b} @paths;
  1         4  
62              
63 1         3 for my $path_string (@paths) {
64 2         671 my $violations = $linter->lint($path_string);
65 2 100       10 if (scalar @$violations == 0) {
66 1         10 Test::More::pass(__PACKAGE__ . ' for ' . $path_string);
67             }
68             else {
69 1         2 my $package = __PACKAGE__;
70 1         5 my $error_msg = <<"...";
71              
72             $package found these violations in "$path_string":
73             ...
74              
75 1         3 for my $violation (@$violations) {
76 1         3 my $explanation = $violation->{explanation};
77 1 50       4 if (ref $explanation eq 'ARRAY') {
78 1         5 $explanation = 'See page ' . join(', ', @$explanation) . ' of PBP';
79             }
80 1         7 $error_msg .= <<"...";
81             $violation->{description} at line $violation->{line}. $explanation.
82             ...
83             }
84              
85 1 50       9 Test::More::ok(0, "$package for $path_string") or Test::More::diag($error_msg);
86             }
87             }
88              
89 1         428 return;
90             }
91              
92             1;
93              
94             __END__