Massive Raft full of chnages. Hopefully, this is the exact right fix.
[catagits/Test-EOL.git] / lib / Test / EOL.pm
CommitLineData
a83ae797 1package Test::EOL;
2
3use strict;
4use warnings;
5
6use Test::Builder;
7use File::Spec;
8use FindBin qw($Bin);
9use File::Find;
10
11use vars qw( $VERSION $PERL $UNTAINT_PATTERN $PERL_PATTERN);
12
fb935bc1 13$VERSION = '0.7';
a83ae797 14
15$PERL = $^X || 'perl';
16$UNTAINT_PATTERN = qr|^([-+@\w./:\\]+)$|;
17$PERL_PATTERN = qr/^#!.*perl/;
18
19my %file_find_arg = ($] <= 5.006) ? () : (
20 untaint => 1,
21 untaint_pattern => $UNTAINT_PATTERN,
22 untaint_skip => 1,
23);
24
25my $Test = Test::Builder->new;
26my $updir = File::Spec->updir();
27
28sub import {
29 my $self = shift;
30 my $caller = caller;
31 {
32 no strict 'refs';
33 *{$caller.'::eol_unix_ok'} = \&eol_unix_ok;
34 *{$caller.'::all_perl_files_ok'} = \&all_perl_files_ok;
35 }
36 $Test->exported_to($caller);
37 $Test->plan(@_);
38}
39
40sub _all_perl_files {
41 my @all_files = _all_files(@_);
42 return grep { _is_perl_module($_) || _is_perl_script($_) } @all_files;
43}
44
45sub _all_files {
46 my @base_dirs = @_ ? @_ : File::Spec->catdir($Bin, $updir);
47 my @found;
48 my $want_sub = sub {
5bcec1c9 49 return if ($File::Find::dir =~ m![\\/]?CVS[\\/]|[\\/]?.svn[\\/]!); # Filter out cvs or subversion dirs/
a83ae797 50 return if ($File::Find::dir =~ m![\\/]?blib[\\/]libdoc$!); # Filter out pod doc in dist
51 return if ($File::Find::dir =~ m![\\/]?blib[\\/]man\d$!); # Filter out pod doc in dist
52 return if ($File::Find::name =~ m!Build$!i); # Filter out autogenerated Build script
53 return unless (-f $File::Find::name && -r _);
54 push @found, File::Spec->no_upwards( $File::Find::name );
55 };
56 my $find_arg = {
57 %file_find_arg,
58 wanted => $want_sub,
59 no_chdir => 1,
60 };
61 find( $find_arg, @base_dirs);
62 return @found;
63}
64
f17f4176 65# Formats various human invisible symbols
66# to similar visible ones.
67# Perhaps ^M or something like that
68# would be more appropriate?
69
70sub _show_whitespace {
71 my $string = shift;
72 $string =~ s/\r/[\\r]/g;
73 $string =~ s/\t/[\\t]/g;
74 $string =~ s/ /[\\s]/g;
75 return $string;
76}
77
78# Format a line record for diagnostics.
79
80sub _debug_line {
81 my ( $options, $line ) = @_;
82 $line->[2] =~ s/\n\z//g;
83 return "line $line->[1] : $line->[0] " . (
84 $options->{show_lines} ? qq{: } . _show_whitespace( $line->[2] ) : q{}
85 );
86}
87
a83ae797 88sub eol_unix_ok {
89 my $file = shift;
91613276 90 my $test_txt;
91 $test_txt = shift if !ref $_[0];
92 $test_txt ||= "No windows line endings in '$file'";
93 my $options = shift if ref $_[0] eq 'HASH';
94 $options ||= {
95 trailing_whitespace => 0,
f17f4176 96 all_reasons => 0,
91613276 97 };
a83ae797 98 $file = _module_to_path($file);
f17f4176 99
a83ae797 100 open my $fh, $file or do { $Test->ok(0, $test_txt); $Test->diag("Could not open $file: $!"); return; };
f17f4176 101 # Windows-- , default is :crlf, which hides \r\n -_-
102 binmode( $fh, ':raw:utf8' );
a83ae797 103 my $line = 0;
f17f4176 104 my @fails;
a83ae797 105 while (<$fh>) {
106 $line++;
f17f4176 107 if ( !$options->{trailing_whitespace} && /(\r+)$/ ) {
108 my $match = $1;
109 push @fails, [ _show_whitespace( $match ) , $line , $_ ];
a83ae797 110 }
f17f4176 111 if ( $options->{trailing_whitespace} && /([ \t]*\r+|[ \t]+)$/ ) {
112 my $match = $1;
113 push @fails, [ _show_whitespace($match), $line , $_ ];
114 }
115 # Minor short-circuit for people who don't need the whole file scanned
116 # once there's an err.
117 last if( @fails > 0 && !$options->{all_reasons} );
118 }
119 if( @fails ){
120 $Test->ok( 0, $test_txt . " on " . _debug_line({ show_lines => 0 } , $fails[0] ) );
121 if ( $options->{all_reasons} || 1 ){
122 $Test->diag( " Problem Lines: ");
123 for ( @fails ){
124 $Test->diag(_debug_line({ show_lines => 1 } , $_ ) );
125 }
126 }
127 return 0;
a83ae797 128 }
129 $Test->ok(1, $test_txt);
130 return 1;
131}
a83ae797 132sub all_perl_files_ok {
91613276 133 my $options = shift if ref $_[0] eq 'HASH';
a83ae797 134 my @files = _all_perl_files( @_ );
135 _make_plan();
136 foreach my $file ( @files ) {
91613276 137 eol_unix_ok($file, $options);
a83ae797 138 }
139}
140
141sub _is_perl_module {
142 $_[0] =~ /\.pm$/i || $_[0] =~ /::/;
143}
144
145sub _is_perl_script {
146 my $file = shift;
147 return 1 if $file =~ /\.pl$/i;
148 return 1 if $file =~ /\.t$/;
149 open my $fh, $file or return;
150 my $first = <$fh>;
151 return 1 if defined $first && ($first =~ $PERL_PATTERN);
152 return;
153}
154
155sub _module_to_path {
156 my $file = shift;
157 return $file unless ($file =~ /::/);
158 my @parts = split /::/, $file;
159 my $module = File::Spec->catfile(@parts) . '.pm';
160 foreach my $dir (@INC) {
161 my $candidate = File::Spec->catfile($dir, $module);
162 next unless (-e $candidate && -f _ && -r _);
163 return $candidate;
164 }
165 return $file;
166}
167
168sub _make_plan {
169 unless ($Test->has_plan) {
170 $Test->plan( 'no_plan' );
171 }
172 $Test->expected_tests;
173}
174
175sub _untaint {
176 my @untainted = map { ($_ =~ $UNTAINT_PATTERN) } @_;
177 return wantarray ? @untainted : $untainted[0];
178}
179
1801;
181__END__
182
183=head1 NAME
184
185Test::EOL - Check the correct line endings in your project
186
187=head1 SYNOPSIS
188
29330dd1 189C<Test::EOL> lets you check the presence of windows line endings in your
190perl code. It
a83ae797 191report its results in standard C<Test::Simple> fashion:
192
193 use Test::EOL tests => 1;
29330dd1 194 eol_unix_ok( 'lib/Module.pm', 'Module is ^M free');
a83ae797 195
91613276 196and to add checks for trailing whitespace:
197
198 use Test::EOL tests => 1;
199 eol_unix_ok( 'lib/Module.pm', 'Module is ^M and trailing whitespace free', { trailing_whitespace => 1 });
200
a83ae797 201Module authors can include the following in a t/eol.t and have C<Test::EOL>
202automatically find and check all perl files in a module distribution:
203
204 use Test::EOL;
205 all_perl_files_ok();
206
207or
208
209 use Test::EOL;
210 all_perl_files_ok( @mydirs );
211
91613276 212and if authors would like to check for trailing whitespace:
213
214 use Test::EOL;
215 all_perl_files_ok({ trailing_whitespace => 1 });
216
217or
218
219 use Test::EOL;
220 all_perl_files_ok({ trailing_whitespace => 1 }, @mydirs );
221
a83ae797 222=head1 DESCRIPTION
223
224This module scans your project/distribution for any perl files (scripts,
29330dd1 225modules, etc) for the presence of windows line endings.
a83ae797 226
227=head1 EXPORT
228
229A list of functions that can be exported. You can delete this section
230if you don't export anything, such as for a purely object-oriented module.
231
232=head1 FUNCTIONS
233
91613276 234=head2 all_perl_files_ok( [ \%options ], [ @directories ] )
a83ae797 235
236Applies C<eol_unix_ok()> to all perl files found in C<@directories> (and sub
237directories). If no <@directories> is given, the starting point is one level
238above the current running script, that should cover all the files of a typical
239CPAN distribution. A perl file is *.pl or *.pm or *.t or a file starting
240with C<#!...perl>
241
242If the test plan is defined:
243
244 use Test::EOL tests => 3;
245 all_perl_files_ok();
246
247the total number of files tested must be specified.
248
91613276 249=head2 eol_unix_ok( $file [, $text] [, \%options ] )
a83ae797 250
251Run a unix EOL check on C<$file>. For a module, the path (lib/My/Module.pm) or the
252name (My::Module) can be both used.
253
254=head1 AUTHOR
255
256Tomas Doran (t0m) C<< <bobtfish@bobtfish.net> >>
257
258=head1 BUGS
259
260Testing for EOL styles other than unix (\n) currently unsupported.
261
262The source code can be found on github, as listed in C< META.yml >,
263patches are welcome.
264
265Otherwise please report any bugs or feature requests to
266C<bug-test-eol at rt.cpan.org>, or through the web interface at
267L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Test-EOL>.
268I will be notified, and then you'll automatically be notified of progress on
269your bug as I make changes.
270
271=head1 ACKNOWLEDGEMENTS
272
273Shamelessly ripped off from L<Test::NoTabs>.
274
275=head1 SEE ALSO
276
277L<Test::More>, L<Test::Pod>. L<Test::Distribution>, L<Test:NoWarnings>,
278L<Test::NoTabs>, L<Module::Install::AuthorTests>.
279
280=head1 COPYRIGHT & LICENSE
281
282Copyright 2009 Tomas Doran, some rights reserved.
283
284This program is free software; you can redistribute it and/or modify it
285under the same terms as Perl itself.
286
287=cut
288