| 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.83'; | ||
| 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__ | ||
| 86 | |||
| 87 | =pod | ||
| 88 | |||
| 89 | =encoding UTF-8 | ||
| 90 | |||
| 91 | =head1 NAME | ||
| 92 | |||
| 93 | Test::Code::TidyAll - Check that all your files are tidy and valid according to tidyall | ||
| 94 | |||
| 95 | =head1 VERSION | ||
| 96 | |||
| 97 | version 0.83 | ||
| 98 | |||
| 99 | =head1 SYNOPSIS | ||
| 100 | |||
| 101 | In a file like 't/tidyall.t': | ||
| 102 | |||
| 103 | #!/usr/bin/perl | ||
| 104 | use Test::Code::TidyAll; | ||
| 105 | tidyall_ok(); | ||
| 106 | |||
| 107 | =head1 DESCRIPTION | ||
| 108 | |||
| 109 | Uses L<Code::TidyAll>'s C<check_only> mode to check that all the files in your | ||
| 110 | project are in a tidied and valid state, i.e. that no plugins throw errors or | ||
| 111 | would change the contents of the file. Does not actually modify any files. | ||
| 112 | |||
| 113 | By default, we look for the config file C<tidyall.ini> or C<.tidyallrc> in the | ||
| 114 | current directory and parent directories, which is generally the right place if | ||
| 115 | you are running L<prove>. | ||
| 116 | |||
| 117 | When invoking L<Code::TidyAll>, we pass C<< mode => 'test' >> by default; see | ||
| 118 | L<modes|tidyall/MODES>. | ||
| 119 | |||
| 120 | =head1 EXPORTS | ||
| 121 | |||
| 122 | This module exports one subroutine, which is exported by default: | ||
| 123 | |||
| 124 | =head2 tidyall_ok(...) | ||
| 125 | |||
| 126 | Most options given to this subroutine will be passed along to the | ||
| 127 | L<Code::TidyAll> constructor. For example, if you don't want to use the tidyall | ||
| 128 | cache and instead check all files every time: | ||
| 129 | |||
| 130 | tidyall_ok( no_cache => 1 ); | ||
| 131 | |||
| 132 | or if you need to specify the config file: | ||
| 133 | |||
| 134 | tidyall_ok( conf_file => '/path/to/conf/file' ); | ||
| 135 | |||
| 136 | By default, this subroutine will test every file that matches the config you | ||
| 137 | specify. However, you can pass a C<files> parameter as an array reference to | ||
| 138 | override this, in which case only the files you specify will be tested. These | ||
| 139 | files are still filtered based on the C<select> and C<exclude> rules defined in | ||
| 140 | your config. | ||
| 141 | |||
| 142 | =head1 SEE ALSO | ||
| 143 | |||
| 144 | L<tidyall> | ||
| 145 | |||
| 146 | =head1 SUPPORT | ||
| 147 | |||
| 148 | Bugs may be submitted at L<https://github.com/houseabsolute/perl-code-tidyall/issues>. | ||
| 149 | |||
| 150 | =head1 SOURCE | ||
| 151 | |||
| 152 | The source code repository for Code-TidyAll can be found at L<https://github.com/houseabsolute/perl-code-tidyall>. | ||
| 153 | |||
| 154 | =head1 AUTHORS | ||
| 155 | |||
| 156 | =over 4 | ||
| 157 | |||
| 158 | =item * | ||
| 159 | |||
| 160 | Jonathan Swartz <swartz@pobox.com> | ||
| 161 | |||
| 162 | =item * | ||
| 163 | |||
| 164 | Dave Rolsky <autarch@urth.org> | ||
| 165 | |||
| 166 | =back | ||
| 167 | |||
| 168 | =head1 COPYRIGHT AND LICENSE | ||
| 169 | |||
| 170 | This software is copyright (c) 2011 - 2022 by Jonathan Swartz. | ||
| 171 | |||
| 172 | This is free software; you can redistribute it and/or modify it under | ||
| 173 | the same terms as the Perl 5 programming language system itself. | ||
| 174 | |||
| 175 | The full text of the license can be found in the | ||
| 176 | F<LICENSE> file included with this distribution. | ||
| 177 | |||
| 178 | =cut |