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

filter: Statementstmt 0.0% 0 / 66 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 25 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 148 CC 16 SCAR 41.4
CC
16
Cov
0%
cmd_perltidy
110.0
_perltidy_files
56.0
BEGIN
2.0
CRAP 62.8
1 package CPAN::Maker::Bootstrapper::Role::PerlTidy;
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 ########################################################################
12 sub cmd_perltidy {
13 ########################################################################
14 my ($self) = @_;
15  
16 my $has_perl_tidy = eval {
17 require Perl::Tidy;
18 1;
19 };
20  
21 die "ERROR: perltidy command requires Perl::Tidy\n"
22 if !$has_perl_tidy || $EVAL_ERROR;
23  
24 my ($source) = $self->get_args;
25  
26 my $file_list = $self->get_file_list;
27 my $profile = $self->get_profile;
28  
29 die "ERROR: source-file and --file-list are mutually exclusive\n"
30 if $source && $file_list;
31  
32 die "ERROR: usage: cmb perltidy [--file-list file-list] [--profile file] [source-file]\n"
33 if !$source && !$file_list;
34  
35 die "ERROR: $profile not found or not readable\n"
36 if $profile && ( !-f $profile || !-r $profile );
37  
38 my @files = $self->_perltidy_files( $source, $file_list );
39  
40 for my $file (@files) {
41 my $source_text = slurp($file);
42 my $tidied_text = q{};
43 my $stderr = q{};
44 my $errorfile = q{};
45  
46 my %tidy_args = (
47 source => \$source_text,
48 destination => \$tidied_text,
49 stderr => \$stderr,
50 errorfile => \$errorfile,
51 );
52  
53 if ($profile) {
54 $tidy_args{perltidyrc} = $profile;
55 }
56  
57 my $error = Perl::Tidy::perltidy(%tidy_args);
58  
59 if ($error) {
60 if ($stderr) {
61 print {*STDERR} $stderr;
62 }
63  
64 if ($errorfile) {
65 print {*STDERR} $errorfile;
66 }
67  
68 die "ERROR: perltidy failed for $file\n";
69 }
70  
71 die "ERROR: $file is not tidy - run: make tidy\n"
72 if $source_text ne $tidied_text;
73  
74 my $sentinel = "$file.tdy";
75  
76 open my $fh, '>', $sentinel
77 or die "ERROR: cannot write $sentinel: $ERRNO\n";
78  
79 close $fh
80 or die "ERROR: cannot close $sentinel: $ERRNO\n";
81 }
82  
83 return $SUCCESS;
84 }
85  
86 ########################################################################
87 sub _perltidy_files {
88 ########################################################################
89 my ( $self, $source, $file_list ) = @_;
90  
91 my @files;
92  
93 if ($file_list) {
94 die "ERROR: $file_list not found or not readable\n"
95 if !-f $file_list || !-r $file_list;
96  
97 for my $file ( split /\n/xsm, slurp($file_list) ) {
98 next if !$file;
99 next if $file =~ /^[#]/xsm;
100  
101 push @files, $file;
102 }
103 }
104 else {
105 @files = ($source);
106 }
107  
108 die "ERROR: no files to tidy\n"
109 if !@files;
110  
111 for my $file (@files) {
112 die "ERROR: $file not found or not readable\n"
113 if !-f $file || !-r $file;
114 }
115  
116 return @files;
117 }
118  
119 1;