11 use vars qw( $VERSION $PERL $UNTAINT_PATTERN $PERL_PATTERN);
15 $PERL = $^X || 'perl';
16 $UNTAINT_PATTERN = qr|^([-+@\w./:\\]+)$|;
17 $PERL_PATTERN = qr/^#!.*perl/;
19 my %file_find_arg = ($] <= 5.006) ? () : (
21 untaint_pattern => $UNTAINT_PATTERN,
25 my $Test = Test::Builder->new;
26 my $updir = File::Spec->updir();
33 *{$caller.'::eol_unix_ok'} = \&eol_unix_ok;
34 *{$caller.'::all_perl_files_ok'} = \&all_perl_files_ok;
36 $Test->exported_to($caller);
41 my @all_files = _all_files(@_);
42 return grep { _is_perl_module($_) || _is_perl_script($_) } @all_files;
46 my @base_dirs = @_ ? @_ : File::Spec->catdir($Bin, $updir);
49 return if ($File::Find::dir =~ m![\\/]?CVS[\\/]|[\\/]?.svn[\\/]!); # Filter out cvs or subversion dirs/
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 );
61 find( $find_arg, @base_dirs);
68 $test_txt = shift if !ref $_[0];
69 $test_txt ||= "No windows line endings in '$file'";
70 my $options = shift if ref $_[0] eq 'HASH';
72 trailing_whitespace => 0,
74 $file = _module_to_path($file);
75 open my $fh, $file or do { $Test->ok(0, $test_txt); $Test->diag("Could not open $file: $!"); return; };
80 (!$options->{trailing_whitespace} && /\r$/) ||
81 ( $options->{trailing_whitespace} && /(\r|[ \t]+)$/)
83 $Test->ok(0, $test_txt . " on line $line");
87 $Test->ok(1, $test_txt);
90 sub all_perl_files_ok {
91 my $options = shift if ref $_[0] eq 'HASH';
92 my @files = _all_perl_files( @_ );
94 foreach my $file ( @files ) {
95 eol_unix_ok($file, $options);
100 $_[0] =~ /\.pm$/i || $_[0] =~ /::/;
103 sub _is_perl_script {
105 return 1 if $file =~ /\.pl$/i;
106 return 1 if $file =~ /\.t$/;
107 open my $fh, $file or return;
109 return 1 if defined $first && ($first =~ $PERL_PATTERN);
113 sub _module_to_path {
115 return $file unless ($file =~ /::/);
116 my @parts = split /::/, $file;
117 my $module = File::Spec->catfile(@parts) . '.pm';
118 foreach my $dir (@INC) {
119 my $candidate = File::Spec->catfile($dir, $module);
120 next unless (-e $candidate && -f _ && -r _);
127 unless ($Test->has_plan) {
128 $Test->plan( 'no_plan' );
130 $Test->expected_tests;
134 my @untainted = map { ($_ =~ $UNTAINT_PATTERN) } @_;
135 return wantarray ? @untainted : $untainted[0];
143 Test::EOL - Check the correct line endings in your project
147 C<Test::EOL> lets you check the presence of windows line endings in your
149 report its results in standard C<Test::Simple> fashion:
151 use Test::EOL tests => 1;
152 eol_unix_ok( 'lib/Module.pm', 'Module is ^M free');
154 and to add checks for trailing whitespace:
156 use Test::EOL tests => 1;
157 eol_unix_ok( 'lib/Module.pm', 'Module is ^M and trailing whitespace free', { trailing_whitespace => 1 });
159 Module authors can include the following in a t/eol.t and have C<Test::EOL>
160 automatically find and check all perl files in a module distribution:
168 all_perl_files_ok( @mydirs );
170 and if authors would like to check for trailing whitespace:
173 all_perl_files_ok({ trailing_whitespace => 1 });
178 all_perl_files_ok({ trailing_whitespace => 1 }, @mydirs );
182 This module scans your project/distribution for any perl files (scripts,
183 modules, etc) for the presence of windows line endings.
187 A list of functions that can be exported. You can delete this section
188 if you don't export anything, such as for a purely object-oriented module.
192 =head2 all_perl_files_ok( [ \%options ], [ @directories ] )
194 Applies C<eol_unix_ok()> to all perl files found in C<@directories> (and sub
195 directories). If no <@directories> is given, the starting point is one level
196 above the current running script, that should cover all the files of a typical
197 CPAN distribution. A perl file is *.pl or *.pm or *.t or a file starting
200 If the test plan is defined:
202 use Test::EOL tests => 3;
205 the total number of files tested must be specified.
207 =head2 eol_unix_ok( $file [, $text] [, \%options ] )
209 Run a unix EOL check on C<$file>. For a module, the path (lib/My/Module.pm) or the
210 name (My::Module) can be both used.
214 Tomas Doran (t0m) C<< <bobtfish@bobtfish.net> >>
218 Testing for EOL styles other than unix (\n) currently unsupported.
220 The source code can be found on github, as listed in C< META.yml >,
223 Otherwise please report any bugs or feature requests to
224 C<bug-test-eol at rt.cpan.org>, or through the web interface at
225 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Test-EOL>.
226 I will be notified, and then you'll automatically be notified of progress on
227 your bug as I make changes.
229 =head1 ACKNOWLEDGEMENTS
231 Shamelessly ripped off from L<Test::NoTabs>.
235 L<Test::More>, L<Test::Pod>. L<Test::Distribution>, L<Test:NoWarnings>,
236 L<Test::NoTabs>, L<Module::Install::AuthorTests>.
238 =head1 COPYRIGHT & LICENSE
240 Copyright 2009 Tomas Doran, some rights reserved.
242 This program is free software; you can redistribute it and/or modify it
243 under the same terms as Perl itself.