blib/lib/Test/EOF.pm

filter: Statementstmt 75.0% 78 / 104 Branchbran 48.1% 26 / 54 Conditioncond 35.7% 5 / 14 MC/DCmcdc 11.1% 1 / 9 Subroutinesub 93.7% 15 / 16 Podpod 50.0% 1 / 2 totaltotal 63.3% 126 / 199 CC 35 SCAR 25.3
CC
35
Cov
63%
_check_perl_file
24.1
_is_perl
22.9
_module_to_path
11.3
CRAP 12.5
↑ Summary
1 2 ▶use 5.10.1;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package Test::EOF;
6  
7 2 our $VERSION = '0.0804';
8 # ABSTRACT: Check correct end of files in a project.
9 2 our $AUTHORITY = 'cpan:CSSON'; # AUTHORITY
10  
11 2 ▶use Cwd qw/cwd/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use File::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use File::ReadBackwards;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Test::Builder;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 2 my $perlstart = qr/^#!.*perl/;
17 2 my $test = Test::Builder->new;
18  
19 1 ▶sub import {
Subroutine import: called CC=1 SCAR=0.0
20 1 my $self = shift;
21 1 my $caller = caller;
22 {
23 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 *{ $caller.'::all_perl_files_ok'} = \&all_perl_files_ok;
25 }
26 1 $test->exported_to($caller);
27 1 $test->plan(@_);
28 }
29  
30 2 ▶sub all_perl_files_ok {
Subroutine all_perl_files_ok: called CC=6 SCAR=18.8
Pod documented
31 2 ▶ my $options = ref $_[0] eq 'HASH' ? shift : ref $_[-1] eq 'HASH' ? pop : {};
Branch: 2 branches
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 0 2 2
ref $_[-1] eq 'HASH' ? : 1 1 2
32 2 my @files = _all_perl_files(@_);
33  
34 2 $test->expected_tests;
35  
36 # no need to check then...
37 2 ▶ if(exists $options->{'minimum_newlines'} && $options->{'minimum_newlines'} <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $options->{"minimum_newlines"} and $options->{"minimum_newlines"} <= 0) 0 2 2
Condition66%2/3
exists $options->{"minimum_newlines"} and $options->{"minimum_newlines"} <= 0
!ll&&!rl&&r
110
MC/DC0%0/2
exists $options->{"minimum_newlines"} and $options->{"minimum_newlines"} <= 0
exists $options->{"minimum_newlines"} $options->{"minimum_newlines"} <= 0
Truth table66%2/3
A and B
A= exists $options->{"minimum_newlines"}B= $options->{"minimum_newlines"} <= 0
ABresult
0X0
100
111
38 0 return 1;
39 }
40  
41 2 foreach my $file (@files) {
42 2 _check_perl_file($file, $options);
43 }
44 }
45  
46 2 ▶sub _check_perl_file {
Subroutine _check_perl_file: called CC=12 SCAR=31.8
47 2 my $file = shift;
48 2 my $options = shift;
49  
50 2 ▶ $options->{'minimum_newlines'} ||= 1;
Condition100%2/2
$options->{"minimum_newlines"} ||= 1
l!l
11
MC/DC100%1/1
$options->{"minimum_newlines"} ||= 1
$options->{"minimum_newlines"}
Truth table100%2/2
A
A= $options->{"minimum_newlines"}
Aresult
11
00
51 2 ▶ if(!exists $options->{'maximum_newlines'}) {
Branch: 2 branches
BranchTrueFalseTotal
if (not exists $options->{'maximum_newlines'}) { } 2 0 2
elsif (exists $options->{'maximum_newlines'} and $options->{'maximum_newlines'} < $options->{'minimum_newlines'}) { } 0 0 0
52 2 $options->{'maximum_newlines'} = $options->{'minimum_newlines'} + 3;
53 }
54 ▶ elsif(exists $options->{'maximum_newlines'} && $options->{'maximum_newlines'} < $options->{'minimum_newlines'}) {
Condition0%0/3
exists $options->{"maximum_newlines"} and $options->{"maximum_newlines"} < $options->{"minimum_newlines"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $options->{"maximum_newlines"} and $options->{"maximum_newlines"} < $options->{"minimum_newlines"}
exists $options->{"maximum_newlines"} $options->{"maximum_newlines"} < $options->{"minimum_newlines"}
Truth table0%0/3
A and B
A= exists $options->{"maximum_newlines"}B= $options->{"maximum_newlines"} < $options->{"minimum_newlines"}
ABresult
0X0
100
111
55 0 $options->{'maximum_newlines'} = $options->{'minimum_newlines'};
56 }
57  
58 2 ▶ if($options->{'strict'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"strict"}) 0 2 2
59 0 $options->{'minimum_newlines'} = 1;
60 0 $options->{'maximum_newlines'} = 1;
61 }
62  
63 2 $file = _module_to_path($file);
64  
65 2 ▶ my $reader = File::ReadBackwards->new($file) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $reader = "File::ReadBackwards"->new($file) or return 2 0 2
66  
67 2 my $linecount = 0;
68  
69 LINE:
70 2 ▶ while(my $line = $reader->readline) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $line = $reader->readline) 4 0 4
71 4 ▶ ++$linecount if $line =~ m{\v$};
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /\v$/ 4 0 4
72 4 ▶ next LINE if $line =~ m{^\v$};
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\v$/ 2 2 4
73 2 last LINE;
74 }
75  
76 2 ▶ if($linecount < $options->{'minimum_newlines'}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($linecount < $options->{'minimum_newlines'}) { } 0 2 2
elsif ($linecount > $options->{'maximum_newlines'}) { } 0 2 2
77 0 my $wanted = make_wanted($options);
78 0 $test->ok(0, "Not enough line breaks (had $linecount, wanted $wanted) at the end of $file");
79 0 return 0;
80 }
81 elsif($linecount > $options->{'maximum_newlines'}) {
82 0 my $wanted = make_wanted($options);
83 0 $test->ok(0, "Too many line breaks (had $linecount, wanted $wanted) at the end of $file ");
84 0 return 0;
85 }
86 2 $test->ok(1, "Just the right number of line breaks at the end of $file");
87 2 return 1;
88  
89 }
90  
91 0 ▶sub make_wanted {
Subroutine make_wanted: not called CC=2 SCAR=17.9
Pod undocumented
92 0 my $options = shift;
93 0 ▶ return $options->{'minimum_newlines'} if $options->{'minimum_newlines'} == $options->{'maximum_newlines'};
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"minimum_newlines"} == $options->{"maximum_newlines"} 0 0 0
94 0 return sprintf "%d to %d" => $options->{'minimum_newlines'}, $options->{'maximum_newlines'};
95 }
96  
97 2 ▶sub _all_perl_files {
Subroutine _all_perl_files: called CC=3 SCAR=11.4
98 2 ▶ my @base_dirs = @_ ? @_ : cwd();
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 2 0 2
99 2 my @found;
100  
101 4 ▶ my $wants = sub {
Subroutine __ANON__: called CC=6 SCAR=18.8
102 4 ▶ return if $File::Find::dir =~ m{ [\\/]? (?:CVS|\.svn) [\\/] }x;
Branch: 1 branch
BranchTrueFalseTotal
if $File::Find::dir =~ m[ [\\/]? (?:CVS|\.svn) [\\/] ]x 0 4 4
103 4 ▶ return if $File::Find::dir =~ m{ [\\/]? blib [\\/] (?: libdoc | man\d) $ }x;
Branch: 1 branch
BranchTrueFalseTotal
if $File::Find::dir =~ m[ [\\/]? blib [\\/] (?: libdoc | man\d) $ ]x 0 4 4
104 4 ▶ return if $File::Find::dir =~ m{ [\\/]? inc }x;
Branch: 1 branch
BranchTrueFalseTotal
if $File::Find::dir =~ m[ [\\/]? inc ]x 0 4 4
105 4 ▶ return if $File::Find::name =~ m{ Build $ }xi;
Branch: 1 branch
BranchTrueFalseTotal
if $File::Find::name =~ / Build $ /xi 0 4 4
106 4 ▶ return unless -f -r $File::Find::name;
Branch: 1 branch
BranchTrueFalseTotal
unless -f -r $File::Find::name 2 2 4
107 2 push @found => File::Spec->no_upwards($File::Find::name);
108 2 };
109 2 my $find_arg = {
110 wanted => $wants,
111 no_chdir => 1,
112 };
113  
114 2 find($find_arg, @base_dirs);
115  
116 2 ▶ my @perls = grep { _is_perl($_) || _is_perl($_) } @found;
Condition33%1/3
_is_perl($_) || _is_perl($_)
l!l&&r!l&&!r
200
MC/DC0%0/2
_is_perl($_) || _is_perl($_)
_is_perl($_) _is_perl($_)
Truth table33%1/3
A || B
A= _is_perl($_)B= _is_perl($_)
ABresult
1X1
011
000
117  
118 2 return @perls;
119 }
120  
121 2 ▶sub _is_perl {
Subroutine _is_perl: called CC=8 SCAR=31.3
122 2 my $file = shift;
123  
124 # module
125 2 ▶ return 1 if $file =~ m{\.pm$}i;
Branch: 1 branch
BranchTrueFalseTotal
if $file =~ /\.pm$/i 1 1 2
126 1 ▶ return 1 if $file =~ m{::};
Branch: 1 branch
BranchTrueFalseTotal
if $file =~ /::/ 0 1 1
127  
128 # script
129 1 ▶ return 1 if $file =~ m{\.pl}i;
Branch: 1 branch
BranchTrueFalseTotal
if $file =~ /\.pl/i 0 1 1
130 1 ▶ return 1 if $file =~ m{\.t$};
Branch: 1 branch
BranchTrueFalseTotal
if $file =~ /\.t$/ 1 0 1
131  
132 0 ▶ open my $fh, '<', $file or return;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or return 0 0 0
133 0 my $first = <$fh>;
134 0 ▶ if(defined $first && $first =~ $perlstart) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $first and $first =~ /$perlstart/) 0 0 0
Condition0%0/3
defined $first and $first =~ /$perlstart/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $first and $first =~ /$perlstart/
defined $first $first =~ /$perlstart/
Truth table0%0/3
A and B
A= defined $firstB= $first =~ /$perlstart/
ABresult
0X0
100
111
135 0 close $fh;
136 0 return 1;
137 }
138  
139 # nope
140 0 return;
141 }
142  
143 2 ▶sub _module_to_path {
Subroutine _module_to_path: called CC=4 SCAR=24.2
144 2 my $file = shift;
145 2 ▶ return $file unless $file =~ m{::};
Branch: 1 branch
BranchTrueFalseTotal
unless $file =~ /::/ 2 0 2
146 0 my @parts = split /::/ => $file;
147 0 my $module = File::Spec->catfile(@parts) . '.pm';
148  
149 CANDIDATE:
150 0 foreach my $dir (@INC) {
151 0 my $candidate = File::Spec->catfile($dir, $module);
152 0 ▶ next CANDIDATE if !-e -f -r $candidate;
Branch: 1 branch
BranchTrueFalseTotal
unless -e -f -r $candidate 0 0 0
153 0 return $candidate;
154 }
155 0 return $file;
156 }
157  
158 2 1;
159  
160 __END__
↑ Summary