distrolint/CheckRole/EachFile.pm

filter: Statementstmt 33.7% 26 / 77 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 35.7% 5 / 14 Podpod 100% 1 / 1 totaltotal 27.3% 32 / 117 CC 14 SCAR 36.0
CC
14
Cov
26%
__ANON__
90.0
glob2regexp
30.0
_run_foreach
6.0
CRAP 36.6
↑ Summary distrolint/Config.pm »
1 # You may distribute under the terms of either the GNU General Public License
2 # or the Artistic License (the same terms as Perl itself)
3 #
4 # (C) Paul Evans, 2023-2026 -- leonerd@leonerd.org.uk
5  
6 5 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use Object::Pad 0.800;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 role App::perl::distrolint::CheckRole::EachFile 0.09;
10  
11 5 ▶use File::Find ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use File::Basename qw( basename );
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 5 ▶use List::Util 1.29 qw( any );
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 =head1 NAME
17  
18 C<App::perl::distrolint::CheckRole::EachFile> - role for checks that iterate over files
19  
20 =head1 DESCRIPTION
21  
22 =for highlighter language=perl
23  
24 This role provides a number of helper trampoline methods for implementing
25 check classes whose logic should iterate over various types of file found in
26 the distribution source.
27  
28 =cut
29  
30 =head1 METHODS
31  
32 The following are trampoline methods. The first argument should either be a
33 code reference or a method name as a plain string. The remaining arguments
34 will be passed to the invoked code, along with the name of each selected file.
35  
36 =cut
37  
38 5 my @SKIP_TOP = qw(
39 blib
40 _build
41 );
42  
43 0 ▶my sub glob2regexp ( $s )
Subroutine glob2regexp: not called CC=5 SCAR=34.0
44 {
45 # Convert glob-like pattern into a regexp. Ish. It's not very good
46 0 $s =~ s{(\*\*)|(\*)|(\?)|(\.)|(.)}
47 0 ▶ {$1 ? ".+" : # ** becomes .+
Branch: 4 branches
BranchTrueFalseTotal
$1 ? : 0 0 0
$2 ? : 0 0 0
$3 ? : 0 0 0
$4 ? : 0 0 0
48 $2 ? "[^/]+" : # * becomes [^/]+
49 $3 ? "." : # ? becomes .
50 $4 ? "\\." : # . becomes \.
51 quotemeta $5}ge;
52 0 return qr(^$s$);
53 }
54  
55 0 ▶method _run_foreach ( $opts, $method, @args )
Subroutine _run_foreach: not called CC=2 SCAR=17.9
56 {
57 0 my $ok = 1;
58  
59 0 my @skip_patterns = split m/\s+/,
60 App::perl::distrolint::Config->check_config( $self, "skip", "" );
61  
62 0 $_ = glob2regexp $_ for @skip_patterns;
63  
64 File::Find::find({
65 no_chdir => 1,
66 0 ▶ preprocess => sub (@names) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
67 0 return sort { $a cmp $b } @names;
68 },
69 0 ▶ wanted => sub () {
Subroutine __ANON__: not called CC=9 SCAR=45.0
70 0 my $path = $_;
71  
72 0 ▶ return if $path eq ".";
Branch: 1 branch
BranchTrueFalseTotal
if $path eq "." 0 0 0
73 0 $path =~ s{^\./}{};
74  
75 0 my $basename = basename $path;
76  
77 # skip hidden
78 0 ▶ $File::Find::prune = 1, return if $basename =~ m{^\..};
Branch: 1 branch
BranchTrueFalseTotal
if $basename =~ /^\../u 0 0 0
79  
80 # skip toplevels
81 0 ▶ $File::Find::prune = 1, return if any { $_ eq $path } @SKIP_TOP;
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if &any(sub { $_ eq $path; } , @SKIP_TOP) 0 0 0
82  
83 0 ▶ -f $path or return;
Branch: 1 branch
BranchTrueFalseTotal
-f $path or return 0 0 0
84  
85 0 ▶ return if $opts->{if_basename} and $basename !~ $opts->{if_basename};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"if_basename"} and not $basename =~ /$opts->{"if_basename"}/u 0 0 0
Condition0%0/3
$opts->{"if_basename"} and not $basename =~ /$opts->{"if_basename"}/u
!ll&&!rl&&r
000
MC/DC0%0/2
$opts->{"if_basename"} and not $basename =~ /$opts->{"if_basename"}/u
$opts->{"if_basename"} not $basename =~ /$opts->{"if_basename"}/u
Truth table0%0/3
A and not(B)
A= $opts->{"if_basename"}B= not $basename =~ /$opts->{"if_basename"}/u
ABresult
0X0
100
111
86  
87 0 ▶ $path =~ $_ and return for @skip_patterns;
Branch: 1 branch
BranchTrueFalseTotal
$path =~ /$_/u and return 0 0 0
88  
89 0 $ok &= $self->$method( $path, @args );
90 },
91 0 }, "." );
92  
93 0 return $ok;
94 5 }
95  
96 =head2 run_for_each_file
97  
98 $check->run_for_each_file( $method, @args );
99  
100 Invokes the code once for every file found in the distribution.
101  
102 =cut
103  
104 0 ▶method run_for_each_file ( $method, @args )
Subroutine run_for_each_file: not called CC=1 SCAR=6.9
105 {
106 0 $self->_run_foreach( {}, $method, @args );
107 5 }
108  
109 5 my %BASENAMES = (
110 perl => qr/\.PL$|\.pl$|\.pm$|\.t$/,
111 xs => qr/\.xs$/,
112 c => qr/\.c$|\.h$/,
113 );
114 5 my $SOURCE_BASENAMES = do {
115 my $re = join "|",
116 5 @BASENAMES{qw( perl xs c )},
117 qr/^typemap$/;
118 5 qr/$re/;
119 };
120  
121 =head2 run_for_each_perl_file
122  
123 $check->run_for_each_perl_file( $method, @args );
124  
125 Invokes the code once for every perl source file found. These will be files
126 whose extension is F<.PL>, F<.pl>, F<.pm> or F<.t>.
127  
128 =cut
129  
130 0 ▶method run_for_each_perl_file ( $method, @args )
Subroutine run_for_each_perl_file: not called CC=1 SCAR=6.9
131 {
132 # TODO: Also check executables in bin/ that start with a perl shebang
133 0 $self->_run_foreach( { if_basename => $BASENAMES{perl} }, $method, @args );
134 5 }
135  
136 =head2 run_for_each_test_file
137  
138 $check->run_for_each_test_file( $method, @args );
139  
140 Invokes the code once for every test source file found. These will be files
141 whose extension is F<.t>.
142  
143 =cut
144  
145 0 ▶method run_for_each_test_file ( $method, @args )
Subroutine run_for_each_test_file: not called CC=1 SCAR=6.9
146 {
147 0 $self->_run_foreach( { if_basename => qr/\.t$/ }, $method, @args );
148 5 }
149  
150 =head2 run_for_each_source_file
151  
152 $check->run_for_each_source_file( $method, @args );
153  
154 Invokes the code once for every source file found. These will be all the Perl
155 source files, plus files whose extension is F<.c>, F<.h>, or F<.xs>, or files
156 named F<typemap>.
157  
158 =cut
159  
160 0 ▶method run_for_each_source_file ( $method, @args )
Subroutine run_for_each_source_file: not called CC=1 SCAR=6.9
Pod documented
161 {
162 0 $self->_run_foreach( { if_basename => $SOURCE_BASENAMES }, $method, @args );
163 5 }
164  
165 =head1 AUTHOR
166  
167 Paul Evans <leonerd@leonerd.org.uk>
168  
169 =cut
170  
171 5 0x55AA;
↑ Summary distrolint/Config.pm »