| 1 | package Test2::Tools::PerlTidy; | ||
| 2 | |||
| 3 | 3 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 3 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 3 | ▶ | use 5.020; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 3 | ▶ | use Test2::API qw( context ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 3 | ▶ | use File::Find (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 3 | ▶ | use Path::Tiny qw( path ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 0 | ▶ | use Perl::Tidy (); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 10 | use IO::File; | ||
| 11 | use Exporter qw( import ); | ||
| 12 | use experimental qw( signatures ); | ||
| 13 | |||
| 14 | our @EXPORT = qw( run_tests ); | ||
| 15 | our @EXPORT_OK = qw( run_tests is_file_tidy ); | ||
| 16 | |||
| 17 | # ABSTRACT: Test2 check that all of your Perl files are tidy | ||
| 18 | our $VERSION = '0.01'; # VERSION | ||
| 19 | |||
| 20 | |||
| 21 | sub run_tests (%args) { | ||
| 22 | my $ctx = context(); | ||
| 23 | |||
| 24 | if($args{skip_all}) { | ||
| 25 | $ctx->plan(0, SKIP => 'All tests skipped.'); | ||
| 26 | } | ||
| 27 | |||
| 28 | my @files = list_files(%args); | ||
| 29 | |||
| 30 | $ctx->plan(scalar @files) unless $args{no_plan}; | ||
| 31 | |||
| 32 | foreach my $file (@files) { | ||
| 33 | my @diag; | ||
| 34 | my $name = "'$file'"; | ||
| 35 | $args{diag} = sub { push @diag, @_ }; | ||
| 36 | my $ok = is_file_tidy($file, $args{perltidyrc}, %args); | ||
| 37 | if($ok) { | ||
| 38 | $ctx->pass($name); | ||
| 39 | } else { | ||
| 40 | $ctx->fail($name, @diag); | ||
| 41 | } | ||
| 42 | } | ||
| 43 | |||
| 44 | $ctx->release; | ||
| 45 | |||
| 46 | (); | ||
| 47 | } | ||
| 48 | |||
| 49 | |||
| 50 | package Test2::Tools::PerlTidy::Diff { | ||
| 51 | |||
| 52 | use Text::Diff (); | ||
| 53 | use Class::Tiny qw( file_to_tidy code_to_tidy perltidyrc is_tidy diff tidied_code logfile errorfile stderr ); | ||
| 54 | |||
| 55 | sub BUILD ($self, $) { | ||
| 56 | |||
| 57 | my $code_to_tidy = $self->code_to_tidy; | ||
| 58 | my $tidied_code = ''; | ||
| 59 | my $logfile = ''; | ||
| 60 | my $errorfile = ''; | ||
| 61 | |||
| 62 | my $stderr_fh = IO::File->new_tmpfile or die "Unable to open temp file $!"; | ||
| 63 | $stderr_fh->autoflush(1); | ||
| 64 | |||
| 65 | Perl::Tidy::perltidy( | ||
| 66 | source => \$code_to_tidy, | ||
| 67 | destination => \$tidied_code, | ||
| 68 | stderr => $stderr_fh, | ||
| 69 | logfile => \$logfile, | ||
| 70 | errorfile => \$errorfile, | ||
| 71 | perltidyrc => $self->perltidyrc, | ||
| 72 | ); | ||
| 73 | |||
| 74 | $stderr_fh->seek(0,0); | ||
| 75 | my $stderr = do { | ||
| 76 | local $/; | ||
| 77 | <$stderr_fh>; | ||
| 78 | }; | ||
| 79 | |||
| 80 | $self->is_tidy(0); | ||
| 81 | |||
| 82 | unless($stderr) { | ||
| 83 | $code_to_tidy =~ s/[\r\n]+$//; | ||
| 84 | $tidied_code =~ s/[\r\n]+$//; | ||
| 85 | |||
| 86 | if($code_to_tidy eq $tidied_code) { | ||
| 87 | $self->diff(''); | ||
| 88 | $self->is_tidy(1); | ||
| 89 | } else { | ||
| 90 | $self->diff( Text::Diff::diff( \$code_to_tidy, \$tidied_code, { STYLE => 'Table' }) ); | ||
| 91 | } | ||
| 92 | } | ||
| 93 | |||
| 94 | $self->tidied_code($tidied_code); | ||
| 95 | $self->logfile($logfile); | ||
| 96 | $self->errorfile($errorfile); | ||
| 97 | $self->stderr($stderr); | ||
| 98 | } | ||
| 99 | } | ||
| 100 | |||
| 101 | sub is_file_tidy ($file_to_tidy, $perltidyrc=undef, %args) { | ||
| 102 | my $code_to_tidy = load_file($file_to_tidy); | ||
| 103 | |||
| 104 | unless(defined $code_to_tidy) { | ||
| 105 | if($args{return_diff_object}) { | ||
| 106 | die "Unable to find or read '$file_to_tidy'"; | ||
| 107 | } else { | ||
| 108 | my $ctx = context(); | ||
| 109 | $ctx->diag("Unable to find or read '$file_to_tidy'"); | ||
| 110 | $ctx->release; | ||
| 111 | return 0; | ||
| 112 | } | ||
| 113 | } | ||
| 114 | |||
| 115 | my $diff = Test2::Tools::PerlTidy::Diff->new( | ||
| 116 | file_to_tidy => $file_to_tidy, | ||
| 117 | code_to_tidy => $code_to_tidy, | ||
| 118 | perltidyrc => $perltidyrc, | ||
| 119 | ); | ||
| 120 | |||
| 121 | if($args{return_diff_object}) { | ||
| 122 | return $diff; | ||
| 123 | } | ||
| 124 | |||
| 125 | my $ctx = context(); | ||
| 126 | my $diag = $args{mute} ? sub { } : $args{diag} || sub { $ctx->diag(shift) }; | ||
| 127 | |||
| 128 | my @diag; | ||
| 129 | |||
| 130 | if($diff->stderr) { | ||
| 131 | $diag->("perltidy reported the following errors:"); | ||
| 132 | $diag->($diff->stderr); | ||
| 133 | $ctx->release; | ||
| 134 | return 0; | ||
| 135 | } | ||
| 136 | |||
| 137 | if($diff->is_tidy) { | ||
| 138 | $ctx->release; | ||
| 139 | return 1; | ||
| 140 | } else { | ||
| 141 | $diag->("The file '$file_to_tidy' is not tidy"); | ||
| 142 | $diag->($diff->diff); | ||
| 143 | $ctx->release; | ||
| 144 | return 0; | ||
| 145 | } | ||
| 146 | } | ||
| 147 | |||
| 148 | |||
| 149 | sub list_files { | ||
| 150 | my %args; | ||
| 151 | my $path; | ||
| 152 | |||
| 153 | # path as only argument is for backward compatability with Test::PerlTidy | ||
| 154 | if(@_ > 1) { | ||
| 155 | %args = @_; | ||
| 156 | $path = $args{path}; | ||
| 157 | } else { | ||
| 158 | ($path) = @_; | ||
| 159 | } | ||
| 160 | |||
| 161 | $path ||= '.'; | ||
| 162 | |||
| 163 | my $ctx = context(); | ||
| 164 | |||
| 165 | $ctx->bail("$path does not exist") unless -e $path; | ||
| 166 | $ctx->bail("$path is not a directory") unless -d $path; | ||
| 167 | |||
| 168 | my $excludes = $args{exclude} || [qr/^blib\//]; # exclude blib by default | ||
| 169 | |||
| 170 | $ctx->bail("exclude must be an array") | ||
| 171 | unless ref $excludes eq 'ARRAY'; | ||
| 172 | |||
| 173 | my @files; | ||
| 174 | |||
| 175 | File::Find::find( | ||
| 176 | sub { | ||
| 177 | my $filename = $_; | ||
| 178 | return if -d $filename; | ||
| 179 | my $path = path($File::Find::name); | ||
| 180 | foreach my $exclude (@$excludes) { | ||
| 181 | return if ref $exclude ? $path =~ $exclude : $path =~ /^$exclude/; | ||
| 182 | } | ||
| 183 | push @files, $path if $filename =~ /\.(?:pl|pm|PL|t)$/; | ||
| 184 | }, | ||
| 185 | $path, | ||
| 186 | ); | ||
| 187 | |||
| 188 | $ctx->release; | ||
| 189 | |||
| 190 | map { "$_" } sort @files; | ||
| 191 | } | ||
| 192 | |||
| 193 | |||
| 194 | sub load_file ($filename=undef) { | ||
| 195 | return unless defined $filename && -f $filename; | ||
| 196 | path($filename)->slurp_utf8; | ||
| 197 | } | ||
| 198 | |||
| 199 | 1; | ||
| 200 | |||
| 201 | __END__ | ||