CPAN/Maker/Bootstrapper/Role/PerlCritic.pm untested

filter: Statementstmt 0.0% 0 / 67 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 31 MC/DCmcdc 0.0% 0 / 20 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 158 CC 15 SCAR 39.3
CC
15
Cov
0%
cmd_perlcritic
90.0
_perlcritic_files
56.0
BEGIN
2.0
CRAP 50.8
1 package CPAN::Maker::Bootstrapper::Role::PerlCritic;
2  
3 use strict;
4 use warnings;
5  
6 use CLI::Simple::Constants qw(:booleans);
7 use CLI::Simple::Utils qw(slurp);
8 use English qw(-no_match_vars);
9 use Role::Tiny;
10  
11 use Readonly;
12 Readonly::Scalar our $DEFAULT_SEVERITY => 5;
13  
14 ########################################################################
15 sub cmd_perlcritic {
16 ########################################################################
17 my ($self) = @_;
18  
19 my $has_perl_critic = eval {
20 require Perl::Critic;
21 1;
22 };
23  
24 die "ERROR: perlcritic command requires Perl::Critic\n"
25 if !$has_perl_critic || $EVAL_ERROR;
26  
27 my ( $source, $arg_severity ) = $self->get_args;
28  
29 my $file_list = $self->get_file_list;
30 my $severity = $self->get_severity;
31 my $profile = $self->get_profile;
32 my $theme = $self->get_theme;
33  
34 die "ERROR: source-file and --file-list are mutually exclusive\n"
35 if $source && $file_list;
36  
37 die
38 "ERROR: usage: cmb perlcritic [--file-list file-list] [--severity 1-5] [--profile file] [--theme theme] [source-file [severity]]\n"
39 if !$source && !$file_list;
40  
41 $severity //= $arg_severity;
42 $severity //= $DEFAULT_SEVERITY;
43  
44 die "ERROR: severity must be an integer between 1 and 5\n"
45 if $severity !~ /\A[1-5]\z/xsm;
46  
47 die "ERROR: $profile not found or not readable\n"
48 if $profile && ( !-f $profile || !-r $profile );
49  
50 my @files = $self->_perlcritic_files( $source, $file_list );
51  
52 my %critic_args = ( -severity => $severity, );
53  
54 $critic_args{-profile} = $profile
55 if $profile;
56  
57 $critic_args{-theme} = $theme
58 if $theme;
59  
60 my $critic = Perl::Critic->new(%critic_args);
61  
62 for my $file (@files) {
63 my @violations = $critic->critique($file);
64 my $report = "$file.crit";
65  
66 open my $fh, '>', $report
67 or die "ERROR: cannot write $report: $ERRNO\n";
68  
69 for my $violation (@violations) {
70 print {$fh} $violation->to_string();
71 }
72  
73 close $fh
74 or die "ERROR: cannot close $report: $ERRNO\n";
75  
76 die "ERROR: $file fails perlcritic at severity $severity (see $file.crit)\n"
77 if @violations;
78 }
79  
80 return $SUCCESS;
81 }
82  
83 ########################################################################
84 sub _perlcritic_files {
85 ########################################################################
86 my ( $self, $source, $file_list ) = @_;
87  
88 my @files;
89  
90 if ($file_list) {
91 die "ERROR: $file_list not found or not readable\n"
92 if !-f $file_list || !-r $file_list;
93  
94 for my $file ( split /\n/xsm, slurp($file_list) ) {
95 next if !$file;
96 next if $file =~ /^[#]/xsm;
97  
98 push @files, $file;
99 }
100 }
101 else {
102 @files = ($source);
103 }
104  
105 die "ERROR: no files to critique\n"
106 if !@files;
107  
108 for my $file (@files) {
109 die "ERROR: $file not found or not readable\n"
110 if !-f $file || !-r $file;
111 }
112  
113 return @files;
114 }
115  
116 1;