| 1 | package Test::Code::TidyAll; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Code::TidyAll; | ||
| 7 | use List::Compare; | ||
| 8 | use Test::Builder; | ||
| 9 | |||
| 10 | # Text::Diff has to be loaded before ::Table | ||
| 11 | use Text::Diff; | ||
| 12 | use Text::Diff::Table; | ||
| 13 | |||
| 14 | use Exporter qw(import); | ||
| 15 | |||
| 16 | our $VERSION = '0.85'; | ||
| 17 | |||
| 18 | my $test = Test::Builder->new; | ||
| 19 | |||
| 20 | our @EXPORT_OK = qw(tidyall_ok); | ||
| 21 | our @EXPORT = @EXPORT_OK; | ||
| 22 | |||
| 23 | sub tidyall_ok { | ||
| 24 | my %options = @_; | ||
| 25 | my $conf_file = delete( $options{conf_file} ); | ||
| 26 | if ( !$conf_file ) { | ||
| 27 | my @conf_names = Code::TidyAll->default_conf_names; | ||
| 28 | $conf_file = Code::TidyAll->find_conf_file( \@conf_names, "." ); | ||
| 29 | } | ||
| 30 | $options{quiet} = 1 unless $options{verbose}; | ||
| 31 | $test->diag("Using $conf_file for config") | ||
| 32 | if $options{verbose}; | ||
| 33 | |||
| 34 | my $files = delete $options{files}; | ||
| 35 | my $ct = Code::TidyAll->new_from_conf_file( | ||
| 36 | $conf_file, | ||
| 37 | check_only => 1, | ||
| 38 | mode => 'test', | ||
| 39 | msg_outputter => \&_msg_outputter, | ||
| 40 | %options, | ||
| 41 | ); | ||
| 42 | |||
| 43 | my @files; | ||
| 44 | if ($files) { | ||
| 45 | @files = List::Compare->new( $files, [ $ct->find_matched_files ] )->get_intersection; | ||
| 46 | } | ||
| 47 | else { | ||
| 48 | @files = $ct->find_matched_files; | ||
| 49 | } | ||
| 50 | |||
| 51 | unless (@files) { | ||
| 52 | $test->plan( tests => 1 ); | ||
| 53 | $test->ok( 1, 'found no matching files for tidyall_ok' ); | ||
| 54 | return; | ||
| 55 | } | ||
| 56 | |||
| 57 | $test->plan( tests => scalar(@files) ); | ||
| 58 | foreach my $file (@files) { | ||
| 59 | my $desc = $ct->_small_path($file); | ||
| 60 | my $result = $ct->process_file($file); | ||
| 61 | $test->ok( $result->ok, "$desc is tidy" ); | ||
| 62 | unless ( $result->ok ) { | ||
| 63 | $test->diag( $result->error ); | ||
| 64 | |||
| 65 | if ( $options{verbose} ) { | ||
| 66 | my $orig = $result->orig_contents; | ||
| 67 | my $new = $result->new_contents; | ||
| 68 | if ( defined $orig && defined $new ) { | ||
| 69 | $test->diag( diff( \$orig, \$new, { STYLE => 'Table' } ) ); | ||
| 70 | } | ||
| 71 | } | ||
| 72 | } | ||
| 73 | } | ||
| 74 | } | ||
| 75 | |||
| 76 | sub _msg_outputter { | ||
| 77 | my $format = shift; | ||
| 78 | $test->diag( sprintf $format, @_ ); | ||
| 79 | } | ||
| 80 | |||
| 81 | 1; | ||
| 82 | |||
| 83 | # ABSTRACT: Check that all your files are tidy and valid according to tidyall | ||
| 84 | |||
| 85 | __END__ |