lib/CPAN/Maker/Role/Provides.pm

filter: Statementstmt 25.9% 35 / 135 Branchbran 0.0% 0 / 56 Conditioncond 0.0% 0 / 37 MC/DCmcdc 0.0% 0 / 22 Subroutinesub 61.1% 11 / 18 Podpod 0.0% 0 / 3 totaltotal 16.9% 46 / 271 CC 46 SCAR 44.9
CC
46
Cov
15%
get_provides
210.0
__ANON__
132.0
_write_provides
72.0
CRAP 89.5
« lib/CPAN/Maker.pm ↑ Summary lib/CPAN/Maker/Role/FileUtils.pm »
1 package CPAN::Maker::Role::Provides;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use CLI::Simple::Utils qw(slurp);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use CLI::Simple::Constants qw(:booleans);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use English qw(-no_match_vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use ExtUtils::MM;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use File::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use File::Process qw(process_file);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Scalar::Util qw(reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 ▶use Role::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 1 requires qw(get_module_version);
18  
19 ########################################################################
20 0 ▶sub create_provides {
Subroutine create_provides: not called CC=4 SCAR=30.0
Pod undocumented
21 ########################################################################
22 0 my ( $self, %args ) = @_;
23  
24 0 ▶ my $path = $args{path} // 'lib';
Condition0%0/2
$args{"path"} // "lib"
l!l
00
MC/DC0%0/1
$args{"path"} // "lib"
$args{"path"}
Truth table0%0/2
A
A= $args{"path"}
Aresult
11
00
25 0 ▶ my $file = $args{file} // 'provides';
Condition0%0/2
$args{"file"} // "provides"
l!l
00
MC/DC0%0/1
$args{"file"} // "provides"
$args{"file"}
Truth table0%0/2
A
A= $args{"file"}
Aresult
11
00
26  
27 0 my @provides;
28  
29 0 ▶ if ( -d $path ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $path) 0 0 0
30 find(
31 { follow => $TRUE,
32 no_chdir => $TRUE,
33 0 ▶ wanted => sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
34 0 my $module_file = $File::Find::name;
35  
36 0 ▶ return if !-f $module_file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $module_file 0 0 0
37 0 ▶ return if $module_file !~ /[.]pm$/xsm;
Branch: 1 branch
BranchTrueFalseTotal
unless $module_file =~ /[.]pm$/msx 0 0 0
38  
39 0 my $module = $module_file;
40 0 $module =~ s{^\Q$path\E/?}{}xsm;
41 0 $module =~ s{/}{::}gxsm;
42 0 $module =~ s{[.]pm$}{}xsm;
43  
44 0 my $module_version = $self->get_module_version( $module, $path );
45  
46 0 ▶ my $provided_module = $module_version->{module} // $module;
Condition0%0/3
$module_version->{"module"} // $module
l!l&&r!l&&!r
000
MC/DC0%0/2
$module_version->{"module"} // $module
$module_version->{"module"} $module
Truth table0%0/3
A // B
A= $module_version->{"module"}B= $module
ABresult
1X1
011
000
47  
48 0 ▶ my $version = $module_version->{version} // 'undef';
Condition0%0/2
$module_version->{"version"} // "undef"
l!l
00
MC/DC0%0/1
$module_version->{"version"} // "undef"
$module_version->{"version"}
Truth table0%0/2
A
A= $module_version->{"version"}
Aresult
11
00
49  
50 0 push @provides, sprintf '%s %s', $provided_module, $version;
51  
52 0 return;
53 },
54 },
55 0 $path
56 );
57 }
58  
59 $self->write_provides(
60 0 provides => \@provides,
61 file => $file,
62 );
63  
64 0 return $file;
65 }
66  
67 ########################################################################
68 0 ▶sub get_provides {
Subroutine get_provides: not called CC=14 SCAR=53.5
Pod undocumented
69 ########################################################################
70 0 my ( $self, %args ) = @_;
71  
72 0 my $file = $args{file};
73 0 my $work_dir = $args{work_dir};
74  
75 0 my %provides;
76 my @missing;
77  
78 0 ▶ return %provides
Branch: 1 branch
BranchTrueFalseTotal
unless $file and -e $file 0 0 0
Condition0%0/3
$file and -e $file
!ll&&!rl&&r
000
MC/DC0%0/2
$file and -e $file
$file -e $file
Truth table0%0/3
A and B
A= $fileB= -e $file
ABresult
0X0
100
111
79 if !$file || !-e $file;
80  
81 process_file(
82 $file,
83 chomp => $TRUE,
84 skip_blank_lines => $TRUE,
85 prefix => 'lib',
86 0 ▶ process => sub {
Subroutine __ANON__: not called CC=11 SCAR=48.8
87 0 my $line = pop @_;
88 0 my $args = pop @_;
89  
90 return ()
91 0 ▶ if !$line;
Branch: 1 branch
BranchTrueFalseTotal
unless $line 0 0 0
92  
93 0 my ( $module, $provided_version ) = split /\s+/xsm, $line, 2;
94  
95 return ()
96 0 ▶ if !$module;
Branch: 1 branch
BranchTrueFalseTotal
unless $module 0 0 0
97  
98 0 ▶ $provided_version //= 'undef';
Condition0%0/2
$provided_version //= "undef"
l!l
00
MC/DC0%0/1
$provided_version //= "undef"
$provided_version
Truth table0%0/2
A
A= $provided_version
Aresult
11
00
99  
100 0 my $prefix = $args->{prefix};
101 0 my $include_path = $prefix;
102  
103 0 ▶ if ($work_dir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($work_dir) 0 0 0
104 0 $include_path = sprintf '%s/%s', $work_dir, $prefix;
105 }
106  
107 0 my $module_version = $self->get_module_version( $module, $include_path );
108  
109 0 ▶ my $provided_module = $module_version->{module} // $module;
Condition0%0/3
$module_version->{"module"} // $module
l!l&&r!l&&!r
000
MC/DC0%0/2
$module_version->{"module"} // $module
$module_version->{"module"} $module
Truth table0%0/3
A // B
A= $module_version->{"module"}B= $module
ABresult
1X1
011
000
110  
111 # get_module_version() returns the *expected* filename even when
112 # that file does not exist. Moreover, a file with that name can
113 # declare a different package (e.g. Text::URLEncode in
114 # lib/Bedrock/Text/URLEncode.pm).
115 0 my $module_file = $module_version->{file};
116 0 ▶ my $path = $module_file ? "$include_path/$module_file" : q{};
Branch: 1 branch
BranchTrueFalseTotal
$module_file ? : 0 0 0
117 0 my $matches_package = $FALSE;
118  
119 0 ▶ if ( $path && -f $path ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path and -f $path) 0 0 0
Condition0%0/3
$path and -f $path
!ll&&!rl&&r
000
MC/DC0%0/2
$path and -f $path
$path -f $path
Truth table0%0/3
A and B
A= $pathB= -f $path
ABresult
0X0
100
111
120 0 my $text = slurp($path);
121 0 $text =~ s/^=pod(.*?)=cut//xsmg;
122 0 ▶ $matches_package = $TRUE
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /^package\s+\Q$module\E\s*(?:;|\{)/msx 0 0 0
123 if $text =~ /^package\s+\Q$module\E\s*(?:;|\{)/xsm;
124 }
125  
126 0 ▶ if ( !$matches_package ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($matches_package) 0 0 0
127 0 push @missing,
128 {
129 module => $module,
130 version => $provided_version,
131 };
132  
133 0 return $provided_module;
134 }
135  
136 $provides{$provided_module} = {
137 0 file => sprintf( '%s/%s', $prefix, $module_version->{file}, ),
138 version => $provided_version,
139 };
140  
141 0 return $provided_module;
142 }
143 0 );
144  
145 0 ▶ if (@missing) {
Branch: 1 branch
BranchTrueFalseTotal
if (@missing) 0 0 0
146 0 ▶ my $dir = $work_dir ? sprintf( '%s/lib', $work_dir ) : 'lib';
Branch: 1 branch
BranchTrueFalseTotal
$work_dir ? : 0 0 0
147  
148 0 my @file_list;
149  
150 0 ▶ if ( -d $dir ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $dir) 0 0 0
151 find(
152 { no_chdir => $TRUE,
153 0 ▶ wanted => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
154 0 my $module_file = $File::Find::name;
155 0 ▶ return if !-f $module_file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $module_file 0 0 0
156 0 ▶ return if $module_file !~ /[.]pm$/xsm;
Branch: 1 branch
BranchTrueFalseTotal
unless $module_file =~ /[.]pm$/msx 0 0 0
157 0 push @file_list, $module_file;
158  
159 0 return;
160 },
161 },
162 0 $dir
163 );
164 }
165  
166 0 local @INC = ( $dir, @INC );
167  
168 0 foreach my $missing (@missing) {
169 0 my $module = $missing->{module};
170 0 my $provided_version = $missing->{version};
171  
172 0 foreach my $module_file (@file_list) {
173 0 my $text = slurp($module_file);
174  
175 next
176 0 ▶ if $text !~ /^package\s+\Q$module\E\s*(?:;|\{)/xsm;
Branch: 1 branch
BranchTrueFalseTotal
unless $text =~ /^package\s+\Q$module\E\s*(?:;|\{)/msx 0 0 0
177  
178 # Remove POD before confirming the package declaration so a
179 # documented package name does not produce a false positive.
180 0 $text =~ s/^=pod(.*?)=cut//xsmg;
181  
182 next
183 0 ▶ if $text !~ /^package\s+\Q$module\E\s*(?:;|\{)/xsm;
Branch: 1 branch
BranchTrueFalseTotal
unless $text =~ /^package\s+\Q$module\E\s*(?:;|\{)/msx 0 0 0
184  
185 0 my $version = $provided_version;
186  
187 0 ▶ if ( !defined $version || $version eq 'undef' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $version or $version eq "undef") 0 0 0
Condition0%0/3
not defined $version or $version eq "undef"
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $version or $version eq "undef"
not defined $version $version eq "undef"
Truth table0%0/3
not(A) or B
A= not defined $versionB= $version eq "undef"
ABresult
1X1
011
000
188 0 $version = eval { return ExtUtils::MM->parse_version($module_file); };
189  
190 0 ▶ $version = 'undef'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $version 0 0 0
191 if !defined $version;
192 }
193  
194 0 my $rel_path = $module_file;
195  
196 0 ▶ if ($work_dir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($work_dir) 0 0 0
197 0 $rel_path =~ s{^\Q$work_dir\E/}{}xsm;
198 }
199  
200 0 $provides{$module} = {
201 file => $rel_path,
202 version => $version,
203 };
204  
205 0 last;
206 }
207 }
208 }
209  
210 0 return %provides;
211 }
212  
213 ########################################################################
214 0 ▶sub _write_provides {
Subroutine _write_provides: not called CC=8 SCAR=42.8
215 ########################################################################
216 0 my ( $self, $fh, $provides ) = @_;
217  
218 0 ▶ croak "provides must be an array\n"
Branch: 1 branch
BranchTrueFalseTotal
if not defined $provides && ref $provides or builtin::reftype($provides) ne "ARRAY" 0 0 0
Condition0%0/6
not defined $provides && ref $provides or builtin::reftype($provides) ne "ARRAY"
l!l&&r!l&&!r
000
defined $provides && ref $provides
!ll&&!rl&&r
000
MC/DC0%0/3
not defined $provides && ref $provides or builtin::reftype($provides) ne "ARRAY"
defined $provides ref $provides builtin::reftype($provides) ne "ARRAY"
Truth table0%0/4
not(A && B) or C
A= defined $providesB= ref $providesC= builtin::reftype($provides) ne "ARRAY"
ABCresult
0XX1
10X1
1111
1100
219 if !defined $provides || !ref $provides || reftype($provides) ne 'ARRAY';
220  
221 0 foreach my $provide ( sort @{$provides} ) {
222 0 ▶ next if !$provide;
Branch: 1 branch
BranchTrueFalseTotal
unless $provide 0 0 0
223  
224 0 my ( $module, $version ) = split /\s+/xsm, $provide, 2;
225  
226 0 ▶ if ( !defined $version || $version eq q{} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $version or $version eq "") 0 0 0
Condition0%0/3
not defined $version or $version eq ""
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $version or $version eq ""
not defined $version $version eq ""
Truth table0%0/3
not(A) or B
A= not defined $versionB= $version eq ""
ABresult
1X1
011
000
227 0 $version = 'undef';
228 }
229  
230 0 print {$fh} sprintf "%s %s\n", $module, $version;
231 }
232  
233 0 return;
234 }
235  
236 ########################################################################
237 0 ▶sub write_provides {
Subroutine write_provides: not called CC=7 SCAR=40.3
Pod undocumented
238 ########################################################################
239 0 my ( $self, %args ) = @_;
240  
241 0 my $provides = $args{provides};
242 0 ▶ my $file = $args{file} // 'provides';
Condition0%0/2
$args{"file"} // "provides"
l!l
00
MC/DC0%0/1
$args{"file"} // "provides"
$args{"file"}
Truth table0%0/2
A
A= $args{"file"}
Aresult
11
00
243  
244 return
245 0 ▶ if !$provides;
Branch: 1 branch
BranchTrueFalseTotal
unless $provides 0 0 0
246  
247 0 ▶ croak "provides must be an array\n"
Branch: 1 branch
BranchTrueFalseTotal
if not ref $provides or builtin::reftype($provides) ne "ARRAY" 0 0 0
Condition0%0/3
not ref $provides or builtin::reftype($provides) ne "ARRAY"
l!l&&r!l&&!r
000
MC/DC0%0/2
not ref $provides or builtin::reftype($provides) ne "ARRAY"
not ref $provides builtin::reftype($provides) ne "ARRAY"
Truth table0%0/3
not(A) or B
A= not ref $providesB= builtin::reftype($provides) ne "ARRAY"
ABresult
1X1
011
000
248 if !ref $provides || reftype($provides) ne 'ARRAY';
249  
250 0 ▶ open my $fh, '>', $file
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $file or croak("could not open '${file}' for writing: $CPAN::Maker::Role::Provides::OS_ERROR\n") 0 0 0
251 or croak "could not open '$file' for writing: $OS_ERROR\n";
252  
253 0 $self->_write_provides( $fh, $provides );
254  
255 0 ▶ close $fh
Branch: 1 branch
BranchTrueFalseTotal
close $fh or croak("could not close '${file}': $CPAN::Maker::Role::Provides::OS_ERROR\n") 0 0 0
256 or croak "could not close '$file': $OS_ERROR\n";
257  
258 0 return;
259 }
260  
261 1 1;
« lib/CPAN/Maker.pm ↑ Summary lib/CPAN/Maker/Role/FileUtils.pm »