Debora/Util.pm

filter: Statementstmt 76.6% 125 / 163 Branchbran 46.9% 31 / 66 Conditioncond 55.0% 11 / 20 MC/DCmcdc 46.1% 6 / 13 Subroutinesub 92.8% 26 / 28 Podpod 100% 11 / 11 totaltotal 69.7% 210 / 301 CC 42 SCAR 31.1
CC
42
Cov
67%
run
110.0
filetype
24.9
__ANON__
9.1
CRAP 22.3
« Debora.pm ↑ Summary Debora/Package.pm »
1 package CPANPLUS::Dist::Debora::Util;
2  
3 # SPDX-License-Identifier: Artistic-1.0-Perl OR GPL-1.0-or-later
4  
5 9 ▶use 5.016;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 9 our $VERSION = '0.019';
10  
11 9 ▶use parent qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 9 our @EXPORT_OK = qw(
14 parse_version
15 module_is_distributed_with_perl
16 decode_utf8
17 slurp_utf8
18 spew_utf8
19 can_run
20 run
21 unix_path
22 filetype
23 find_most_recent_mtime
24 find_shared_objects
25 is_testing
26 );
27  
28 9 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
29 9 ▶use Cwd qw(cwd);
Subroutine BEGIN: called CC=1 SCAR=0.0
30 9 ▶use Encode qw(decode);
Subroutine BEGIN: called CC=1 SCAR=0.0
31 9 ▶use English qw(-no_match_vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
32 9 ▶use File::Spec::Functions qw(catfile splitdir splitpath);
Subroutine BEGIN: called CC=1 SCAR=0.0
33 9 ▶use File::Spec::Unix qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
34 9 ▶use IPC::Cmd qw(can_run);
Subroutine BEGIN: called CC=1 SCAR=0.0
35 9 ▶use Module::CoreList 2.32;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 9 ▶use version 0.77;
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 9 ▶use CPANPLUS::Error qw(error);
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 # Avoid warnings from IO::Select by using IPC::Run.
41 9 $IPC::Cmd::USE_IPC_RUN = IPC::Cmd->can_use_ipc_run;
42  
43 9 my $perl_version = parse_version($PERL_VERSION);
44  
45 29 ▶sub parse_version {
Subroutine parse_version: called CC=1 SCAR=0.0
Pod documented
46 29 my $string = shift;
47  
48 29 return version->parse($string);
49 }
50  
51 13 ▶sub module_is_distributed_with_perl {
Subroutine module_is_distributed_with_perl: called CC=5 SCAR=16.2
Pod documented
52 13 my ($module_name, $version) = @_;
53  
54 13 my $ok = 0;
55  
56 # cpan2dist is run with -w, which triggers a warning in Module::CoreList.
57 13 local $WARNING = 0;
58  
59 13 my $upper = Module::CoreList->removed_from($module_name);
60 13 ▶ if (!defined $upper || $perl_version < parse_version($upper)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $upper or $perl_version < parse_version($upper)) 7 6 13
Condition66%2/3
not defined $upper or $perl_version < parse_version($upper)
l!l&&r!l&&!r
706
MC/DC50%1/2
not defined $upper or $perl_version < parse_version($upper)
not defined $upper $perl_version < parse_version($upper)
Truth table66%2/3
not(A) or B
A= not defined $upperB= $perl_version < parse_version($upper)
ABresult
1X1
011
000
61 7 my $lower = Module::CoreList->first_release($module_name, $version);
62 7 ▶ if (defined $lower && $perl_version >= parse_version($lower)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $lower and $perl_version >= parse_version($lower)) 1 6 7
Condition66%2/3
defined $lower and $perl_version >= parse_version($lower)
!ll&&!rl&&r
601
MC/DC50%1/2
defined $lower and $perl_version >= parse_version($lower)
defined $lower $perl_version >= parse_version($lower)
Truth table66%2/3
A and B
A= defined $lowerB= $perl_version >= parse_version($lower)
ABresult
0X0
100
111
63 1 $ok = 1;
64 }
65 }
66  
67 13 return $ok;
68 }
69  
70 7 ▶sub decode_utf8 {
Subroutine decode_utf8: called CC=1 SCAR=0.0
Pod documented
71 7 my $bytes = shift;
72  
73 7 return decode('UTF-8', $bytes);
74 }
75  
76 3 ▶sub slurp_utf8 {
Subroutine slurp_utf8: called CC=3 SCAR=11.1
Pod documented
77 3 my $filename = shift;
78  
79 3 my $data;
80  
81 3 my $ok = open my $fh, '<:encoding(UTF-8)', $filename;
82 3 ▶ if ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) 3 0 3
83 3 local $RS = undef;
84 3 $data = <$fh>;
85 3 ▶ close $fh or $ok = 0;
Branch: 1 branch
BranchTrueFalseTotal
close $fh or $ok = 0 3 0 3
86 }
87  
88 3 return $data;
89 }
90  
91 1 ▶sub spew_utf8 {
Subroutine spew_utf8: called CC=3 SCAR=11.2
Pod documented
92 1 my ($filename, $string) = @_;
93  
94 1 my $ok = open my $fh, '>:encoding(UTF-8)', $filename;
95 1 ▶ if ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) 1 0 1
96 1 $ok = print {$fh} $string;
97 1 ▶ close $fh or $ok = 0;
Branch: 1 branch
BranchTrueFalseTotal
close $fh or $ok = 0 1 0 1
98 }
99  
100 1 return $ok;
101 }
102  
103 0 ▶sub run {
Subroutine run: not called CC=10 SCAR=47.0
Pod documented
104 0 my (%options) = @_;
105  
106 0 my $ok = 0;
107  
108 0 my $command = $options{command};
109 0 ▶ if (!$command) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($command) 0 0 0
110 0 error('No command');
111 0 return $ok;
112 }
113  
114 0 my $dir = $options{dir};
115 0 delete $options{dir};
116  
117 0 ▶ if (!exists $options{buffer}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $options{"buffer"}) 0 0 0
118 0 my $buf = q{};
119 0 $options{buffer} = \$buf;
120 }
121  
122 my $on_error = $options{on_error}
123 0 ▶ // sub { error("Could not run '$_[0]': $_[1]") };
Subroutine __ANON__: not called CC=1 SCAR=6.9
Condition0%0/3
$options{"on_error"} // sub { error("Could not run '$_[0]': $_[1]"); }
l!l&&r!l&&!r
000
MC/DC0%0/2
$options{"on_error"} // sub { error("Could not run '$_[0]': $_[1]"); }
$options{"on_error"} sub { error("Could not run '$_[0]': $_[1]"); }
Truth table0%0/3
A // B
A= $options{"on_error"}B= sub { error("Could not run '$_[0]': $_[1]"); }
ABresult
1X1
011
000
124 0 delete $options{on_error};
125  
126 0 my $origdir;
127 0 ▶ if ($dir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir) 0 0 0
128 0 $origdir = cwd;
129 0 ▶ if (!chdir $dir) {
Branch: 1 branch
BranchTrueFalseTotal
unless (chdir $dir) 0 0 0
130 0 return $ok;
131 }
132 }
133  
134 0 $ok = IPC::Cmd::run(%options);
135 0 ▶ if (!$ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 0 0
136 0 my $cmdline = join q{ }, @{$command};
137 0 ▶ my $output = ${$options{buffer}} // q{};
Condition0%0/2
${$options{"buffer"};} // ""
l!l
00
MC/DC0%0/1
${$options{"buffer"};} // ""
${$options{"buffer"};}
Truth table0%0/2
A
A= ${$options{"buffer"};}
Aresult
11
00
138 0 $on_error->($cmdline, $output);
139 }
140  
141 0 ▶ if ($origdir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($origdir) 0 0 0
142 0 ▶ if (!chdir $origdir) {
Branch: 1 branch
BranchTrueFalseTotal
unless (chdir $origdir) 0 0 0
143 0 $ok = 0;
144 }
145 }
146  
147 0 return $ok;
148 }
149  
150 6 ▶sub unix_path {
Subroutine unix_path: called CC=1 SCAR=0.0
Pod documented
151 6 my $path = shift;
152  
153 6 (undef, $path) = splitpath($path, 1);
154 6 $path = File::Spec::Unix->catfile(splitdir($path));
155  
156 6 return $path;
157 }
158  
159 7 ▶sub filetype {
Subroutine filetype: called CC=9 SCAR=32.1
Pod documented
160 7 my $filename = shift;
161  
162 7 my %type_for = (
163 '1' => 'text',
164 '1p' => 'text',
165 '3' => 'text',
166 '3perl' => 'text',
167 '3pm' => 'text',
168 'bat' => 'script',
169 'dll' => 'executable',
170 'dylib' => 'executable',
171 'exe' => 'executable',
172 'pl' => 'script',
173 'pm' => 'text',
174 'pod' => 'text',
175 'so' => 'executable',
176 );
177  
178 7 my @magic = (
179 [0, 4, '7F454C46', 'executable'], # ELF
180 [0, 4, 'FEEDFACE', 'executable'], # Mach-O
181 [0, 4, 'CEFAEDFE', 'executable'], # Mach-O
182 [0, 4, 'FEEDFACF', 'executable'], # Mach-O
183 [0, 4, 'CFFAEDFE', 'executable'], # Mach-O
184 [0, 2, '4D5A', 'executable'], # PE
185 [0, 2, '2321', 'script'], # Shebang
186 );
187  
188 7 my $type = 'data';
189  
190 7 ▶ if ($filename =~ m{[.]([^.]+) \z}xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filename =~ /[.]([^.]+) \z/msux) 7 0 7
191 7 my $suffix = lc $1;
192 7 ▶ if (exists $type_for{$suffix}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $type_for{$suffix}) 7 0 7
193 7 $type = $type_for{$suffix};
194 }
195 }
196  
197 7 ▶ if ($type eq 'data') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "data") 0 7 7
198 0 ▶ if (open my $fh, '<:raw', $filename) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, "<:raw", $filename) 0 0 0
199 0 ▶ if (read $fh, my $data, 16) {
Branch: 1 branch
BranchTrueFalseTotal
if (read $fh, my $data, 16) 0 0 0
200 TYPE:
201 0 for (@magic) {
202 0 ▶ if (substr($data, $_->[0], $_->[1]) eq pack 'H*', $_->[2]) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($data, $_->[0], $_->[1]) eq pack("H*", $_->[2])) 0 0 0
203 0 $type = $_->[3];
204 0 last TYPE;
205 }
206 }
207 }
208 0 ▶ close $fh or undef;
Branch: 1 branch
BranchTrueFalseTotal
close $fh or undef 0 0 0
209 }
210 }
211  
212 7 return $type;
213 }
214  
215 6 ▶sub find_most_recent_mtime {
Subroutine find_most_recent_mtime: called CC=1 SCAR=0.0
Pod documented
216 6 my $sourcedir = shift;
217  
218 6 my $most_recent_mtime = 0;
219  
220 236 ▶ my $find = sub {
Subroutine __ANON__: called CC=9 SCAR=22.0
221 236 my $dir = shift;
222  
223 236 ▶ opendir my $dh, $dir or croak "Could not traverse '$dir': $OS_ERROR";
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $dir or croak("Could not traverse '${dir}': $CPANPLUS::Dist::Debora::Util::OS_ERROR") 236 0 236
224 ENTRY:
225 236 ▶ while (defined(my $entry = readdir $dh)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $entry = readdir $dh)) 1270 236 1506
226 1270 ▶ next ENTRY if $entry eq q{.} || $entry eq q{..};
Branch: 1 branch
BranchTrueFalseTotal
if $entry eq "." or $entry eq ".." 472 798 1270
Condition100%3/3
$entry eq "." or $entry eq ".."
l!l&&r!l&&!r
236236798
MC/DC100%2/2
$entry eq "." or $entry eq ".."
$entry eq "." $entry eq ".."
Truth table100%3/3
A or B
A= $entry eq "."B= $entry eq ".."
ABresult
1X1
011
000
227  
228 798 my $path = catfile($dir, $entry);
229  
230 # Skip symbolic links.
231 798 ▶ next ENTRY if -l $path;
Branch: 1 branch
BranchTrueFalseTotal
if -l $path 0 798 798
232  
233 798 ▶ if (-d $path) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $path) { } 230 568 798
234 230 __SUB__->($path);
235 }
236 else {
237 568 my @stat = stat $path;
238 568 ▶ if (@stat) {
Branch: 1 branch
BranchTrueFalseTotal
if (@stat) 568 0 568
239 568 my $mtime = $stat[9];
240 568 ▶ if ($most_recent_mtime < $mtime) {
Branch: 1 branch
BranchTrueFalseTotal
if ($most_recent_mtime < $mtime) 62 506 568
241 62 $most_recent_mtime = $mtime;
242 }
243 }
244 }
245 }
246 236 closedir $dh;
247  
248 236 return;
249 6 };
250 6 $find->($sourcedir);
251  
252 6 return $most_recent_mtime;
253 }
254  
255 3 ▶sub find_shared_objects {
Subroutine find_shared_objects: called CC=1 SCAR=0.0
Pod documented
256 3 my $stagingdir = shift;
257  
258 3 my @shared_objects;
259  
260 18 ▶ my $find = sub {
Subroutine __ANON__: called CC=8 SCAR=21.1
261 18 my $dir = shift;
262  
263 18 ▶ opendir my $dh, $dir
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $dir or croak("Could not traverse '${dir}': $CPANPLUS::Dist::Debora::Util::OS_ERROR") 18 0 18
264 or croak "Could not traverse '$dir': $OS_ERROR";
265 ENTRY:
266 18 ▶ while (defined(my $entry = readdir $dh)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $entry = readdir $dh)) 57 18 75
267 57 ▶ next ENTRY if $entry eq q{.} || $entry eq q{..};
Branch: 1 branch
BranchTrueFalseTotal
if $entry eq "." or $entry eq ".." 36 21 57
Condition100%3/3
$entry eq "." or $entry eq ".."
l!l&&r!l&&!r
181821
MC/DC100%2/2
$entry eq "." or $entry eq ".."
$entry eq "." $entry eq ".."
Truth table100%3/3
A or B
A= $entry eq "."B= $entry eq ".."
ABresult
1X1
011
000
268  
269 21 my $path = catfile($dir, $entry);
270  
271 # Skip symbolic links.
272 21 ▶ next ENTRY if -l $path;
Branch: 1 branch
BranchTrueFalseTotal
if -l $path 0 21 21
273  
274 21 ▶ if (-d $path) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $path) { } 15 6 21
275 15 __SUB__->($path);
276 }
277 else {
278 6 ▶ if (filetype($path) eq 'executable') {
Branch: 1 branch
BranchTrueFalseTotal
if (filetype($path) eq "executable") 0 6 6
279 0 push @shared_objects, $path;
280 }
281 }
282 }
283 18 closedir $dh;
284  
285 18 return;
286 3 };
287 3 $find->($stagingdir);
288  
289 3 return \@shared_objects;
290 }
291  
292 2 ▶sub is_testing {
Subroutine is_testing: called CC=2 SCAR=9.2
Pod documented
293 2 ▶ return $ENV{AUTOMATED_TESTING} || $ENV{RELEASE_TESTING};
Condition33%1/3
$ENV{'AUTOMATED_TESTING'} || $ENV{'RELEASE_TESTING'}
l!l&&r!l&&!r
200
MC/DC0%0/2
$ENV{'AUTOMATED_TESTING'} || $ENV{'RELEASE_TESTING'}
$ENV{'AUTOMATED_TESTING'} $ENV{'RELEASE_TESTING'}
Truth table33%1/3
A || B
A= $ENV{'AUTOMATED_TESTING'}B= $ENV{'RELEASE_TESTING'}
ABresult
1X1
011
000
294 }
295  
296 9 1;
297 __END__
« Debora.pm ↑ Summary Debora/Package.pm »