/usr/share/perl5/Test/EOL.pm is in libtest-eol-perl 1.6-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 | package Test::EOL;
our $AUTHORITY = 'cpan:FLORA';
# ABSTRACT: Check the correct line endings in your project
$Test::EOL::VERSION = '1.6';
use strict;
use warnings;
use Test::Builder;
use File::Spec;
use File::Find;
use Cwd qw/ cwd /;
use vars qw( $PERL $UNTAINT_PATTERN $PERL_PATTERN);
$PERL = $^X || 'perl';
$UNTAINT_PATTERN = qr|^([-+@\w./:\\]+)$|;
$PERL_PATTERN = qr/^#!.*perl/;
my %file_find_arg = ($] <= 5.006) ? () : (
untaint => 1,
untaint_pattern => $UNTAINT_PATTERN,
untaint_skip => 1,
);
my $Test = Test::Builder->new;
my $updir = File::Spec->updir();
my $no_plan;
sub import {
my $self = shift;
my $caller = caller;
{
no strict 'refs';
*{$caller.'::eol_unix_ok'} = \&eol_unix_ok;
*{$caller.'::all_perl_files_ok'} = \&all_perl_files_ok;
}
$Test->exported_to($caller);
if ($_[0] && $_[0] eq 'no_plan') {
shift;
$no_plan = 1;
}
$Test->plan(@_);
}
sub _all_perl_files {
my @all_files = _all_files(@_);
return grep { _is_perl_module($_) || _is_perl_script($_) } @all_files;
}
sub _all_files {
my @base_dirs = @_ ? @_ : cwd();
my $options = pop(@base_dirs) if ref $base_dirs[-1] eq 'HASH';
my @found;
my $want_sub = sub {
return if ($File::Find::dir =~ m![\\/]?CVS[\\/]|[\\/]?\.svn[\\/]!); # Filter out cvs or subversion dirs/
return if ($File::Find::dir =~ m![\\/]?blib[\\/]libdoc$!); # Filter out pod doc in dist
return if ($File::Find::dir =~ m![\\/]?blib[\\/]man\d$!); # Filter out pod doc in dist
return if ($File::Find::dir =~ m![\\/]?inc!); # Filter out Module::Install stuff
return if ($File::Find::name =~ m!Build$!i); # Filter out autogenerated Build script
return unless (-f $File::Find::name && -r _);
push @found, File::Spec->no_upwards( $File::Find::name );
};
my $find_arg = {
%file_find_arg,
wanted => $want_sub,
no_chdir => 1,
};
find( $find_arg, @base_dirs);
return @found;
}
# Formats various human invisible symbols
# to similar visible ones.
# Perhaps ^M or something like that
# would be more appropriate?
sub _show_whitespace {
my $string = shift;
$string =~ s/\r/[\\r]/g;
$string =~ s/\t/[\\t]/g;
$string =~ s/ /[\\s]/g;
return $string;
}
# Format a line record for diagnostics.
sub _debug_line {
my ( $options, $line ) = @_;
$line->[2] =~ s/\n\z//g;
return "line $line->[1]: $line->[0] " . (
$options->{show_lines} ? qq{: } . _show_whitespace( $line->[2] ) : q{}
);
}
sub eol_unix_ok {
my $file = shift;
my $test_txt;
$test_txt = shift if !ref $_[0];
$test_txt ||= "No incorrect line endings in '$file'";
my $options = shift if ref $_[0] eq 'HASH';
$options ||= {
trailing_whitespace => 0,
all_reasons => 0,
};
$file = _module_to_path($file);
open my $fh, $file or do { $Test->ok(0, $test_txt); $Test->diag("Could not open $file: $!"); return; };
# Windows-- , default is :crlf, which hides \r\n -_-
binmode( $fh, ':raw' );
my $line = 0;
my @fails;
while (<$fh>) {
$line++;
if ( !$options->{trailing_whitespace} && /(\r+)$/ ) {
my $match = $1;
push @fails, [ _show_whitespace( $match ) , $line , $_ ];
}
if ( $options->{trailing_whitespace} && /([ \t]*\r+|[ \t]+)$/ ) {
my $match = $1;
push @fails, [ _show_whitespace($match), $line , $_ ];
}
# Minor short-circuit for people who don't need the whole file scanned
# once there's an err.
last if( @fails > 0 && !$options->{all_reasons} );
}
if( @fails ){
$Test->ok( 0, $test_txt . " on " . _debug_line({ show_lines => 0 } , $fails[0] ) );
if ( $options->{all_reasons} || 1 ){
$Test->diag( " Problem Lines: ");
for ( @fails ){
$Test->diag(_debug_line({ show_lines => 1 } , $_ ) );
}
}
return 0;
}
$Test->ok(1, $test_txt);
return 1;
}
sub all_perl_files_ok {
my $options = shift if ref $_[0] eq 'HASH';
my @files = _all_perl_files( @_ );
_make_plan();
foreach my $file ( @files ) {
eol_unix_ok($file, $options);
}
}
sub _is_perl_module {
$_[0] =~ /\.pm$/i || $_[0] =~ /::/;
}
sub _is_perl_script {
my $file = shift;
return 1 if $file =~ /\.pl$/i;
return 1 if $file =~ /\.t$/;
open (my $fh, $file) or return;
my $first = <$fh>;
return 1 if defined $first && ($first =~ $PERL_PATTERN);
return;
}
sub _module_to_path {
my $file = shift;
return $file unless ($file =~ /::/);
my @parts = split /::/, $file;
my $module = File::Spec->catfile(@parts) . '.pm';
foreach my $dir (@INC) {
my $candidate = File::Spec->catfile($dir, $module);
next unless (-e $candidate && -f _ && -r _);
return $candidate;
}
return $file;
}
sub _make_plan {
return if $no_plan;
unless ($Test->has_plan) {
$Test->plan( 'no_plan' );
}
$Test->expected_tests;
}
sub _untaint {
my @untainted = map { ($_ =~ $UNTAINT_PATTERN) } @_;
return wantarray ? @untainted : $untainted[0];
}
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
Test::EOL - Check the correct line endings in your project
=head1 SYNOPSIS
C<Test::EOL> lets you check for the presence of trailing whitespace and/or
windows line endings in your perl code. It reports its results in standard
C<Test::Simple> fashion:
use Test::EOL tests => 1;
eol_unix_ok( 'lib/Module.pm', 'Module is ^M free');
and to add checks for trailing whitespace:
use Test::EOL tests => 1;
eol_unix_ok( 'lib/Module.pm', 'Module is ^M and trailing whitespace free', { trailing_whitespace => 1 });
Module authors can include the following in a t/eol.t and have C<Test::EOL>
automatically find and check all perl files in a module distribution:
use Test::EOL;
all_perl_files_ok();
or
use Test::EOL;
all_perl_files_ok( @mydirs );
and if authors would like to check for trailing whitespace:
use Test::EOL;
all_perl_files_ok({ trailing_whitespace => 1 });
or
use Test::EOL;
all_perl_files_ok({ trailing_whitespace => 1 }, @mydirs );
or
use Test::More;
use Test::EOL 'no_test';
all_perl_files_ok();
done_testing;
=head1 DESCRIPTION
This module scans your project/distribution for any perl files (scripts,
modules, etc) for the presence of windows line endings.
=head1 FUNCTIONS
=head2 all_perl_files_ok
all_perl_files_ok( [ \%options ], [ @directories ] )
Applies C<eol_unix_ok()> to all perl files found in C<@directories> (and sub
directories). If no <@directories> is given, the starting point is one level
above the current running script, that should cover all the files of a typical
CPAN distribution. A perl file is *.pl or *.pm or *.t or a file starting
with C<#!...perl>
Valid C<\%options> currently are:
=over
=item * trailing_whitespace
By default Test::EOL only looks for Windows (CR/LF) line-endings. Set this
to true to raise errors if any kind of trailing whitespace is present in
the file.
=item * all_reasons
Normally Test::EOL reports only the first error in every file (given that
a text file originated on Windows will fail every single line). Set this
a true value to register a test failure for every line with an error.
=back
If the test plan is defined:
use Test::EOL tests => 3;
all_perl_files_ok();
the total number of files tested must be specified.
=head2 eol_unix_ok
eol_unix_ok ( $file [, $text] [, \%options ] )
Run a unix EOL check on C<$file>. For a module, the path (lib/My/Module.pm) or the
name (My::Module) can be both used. C<$text> is the diagnostic label emited after
the C<ok>/C<not ok> TAP output. C<\%options> takes the same values as described in
L</all_perl_files_ok>.
=head1 EXPORT
A list of functions that can be exported. You can delete this section
if you don't export anything, such as for a purely object-oriented module.
=head1 ACKNOWLEDGEMENTS
Shamelessly ripped off from L<Test::NoTabs>.
=head1 SEE ALSO
L<Test::More>, L<Test::Pod>. L<Test::Distribution>, L<Test:NoWarnings>,
L<Test::NoTabs>, L<Module::Install::AuthorTests>.
=head1 AUTHORS
=over 4
=item *
Arthur Axel 'fREW' Schmidt <frioux@gmail.com>
=item *
Florian Ragwitz <rafl@debian.org>
=item *
Kent Fredric <kentfredric@gmail.com>
=item *
Peter Rabbitson <ribasushi@cpan.org>
=item *
Tomas Doran <bobtfish@bobtfish.net>
=item *
Olivier Mengue <dolmen@cpan.org>
=back
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2015 by Tomas Doran.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut
|