blib/lib/Test2/Util.pm

filter: Statementstmt 75.5% 102 / 135 Branchbran 37.1% 23 / 62 Conditioncond 32.0% 8 / 25 MC/DCmcdc 23.0% 3 / 13 Subroutinesub 82.3% 28 / 34 Podpod 57.1% 4 / 7 totaltotal 60.8% 168 / 276 CC 43 SCAR 29.3
CC
43
Cov
60%
_can_thread
92.1
_can_fork
20.1
__ANON__
20.0
CRAP 18.7
« blib/lib/Test2/API.pm ↑ Summary blib/lib/Test2/API/Instance.pm »
1 package Test2::Util;
2 456 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 456 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 456 our $VERSION = '1.302226';
6  
7 456 ▶use Config qw/%Config/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 456 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 456 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.6
11 456 ▶ *HAVE_PERLIO = defined &PerlIO::get_layers ? sub() { 1 } : sub() { 0 };
Branch: 1 branch
BranchTrueFalseTotal
defined &PerlIO::get_layers ? : 456 0 456
12 }
13  
14 456 our @EXPORT_OK = qw{
15 try
16  
17 pkg_to_file
18  
19 get_tid USE_THREADS
20 CAN_THREAD
21 CAN_REALLY_FORK
22 CAN_FORK
23  
24 CAN_SIGSYS
25  
26 IS_WIN32
27  
28 ipc_separator
29  
30 gen_uid
31  
32 do_rename do_unlink
33  
34 try_sig_mask
35  
36 clone_io
37 };
38 456 ▶BEGIN { require Exporter; our @ISA = qw(Exporter) }
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 456 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.6
41 456 ▶ *IS_WIN32 = ($^O eq 'MSWin32') ? sub() { 1 } : sub() { 0 };
Branch: 1 branch
BranchTrueFalseTotal
$^O eq 'MSWin32' ? : 0 456 456
42 }
43  
44 456 ▶sub _can_thread {
Subroutine _can_thread: called CC=11 SCAR=45.2
45 456 ▶ return 0 unless $] >= 5.008001;
Branch: 1 branch
BranchTrueFalseTotal
unless $] >= "5.008001" 0 456 456
46 456 ▶ return 0 unless $Config{'useithreads'};
Branch: 1 branch
BranchTrueFalseTotal
unless $Test2::Util::Config{"useithreads"} 456 0 456
47  
48 # Threads are broken on perl 5.10.0 built with gcc 4.8+
49 0 ▶ if ($] == 5.010000 && $Config{'ccname'} eq 'gcc' && $Config{'gccversion'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] == "5.01" and $Test2::Util::Config{"ccname"} eq "gcc" and $Test2::Util::Config{"gccversion"}) 0 0 0
Condition0%0/6
$] == "5.01" and $Test2::Util::Config{"ccname"} eq "gcc" and $Test2::Util::Config{"gccversion"}
!ll&&!rl&&r
000
$] == "5.01" and $Test2::Util::Config{"ccname"} eq "gcc"
!ll&&!rl&&r
000
MC/DC0%0/3
$] == "5.01" and $Test2::Util::Config{"ccname"} eq "gcc" and $Test2::Util::Config{"gccversion"}
$] == "5.01" $Test2::Util::Config{"ccname"} eq "gcc" $Test2::Util::Config{"gccversion"}
Truth table0%0/4
A and B and C
A= $] == "5.01"B= $Test2::Util::Config{"ccname"} eq "gcc"C= $Test2::Util::Config{"gccversion"}
ABCresult
0XX0
10X0
1100
1111
50 0 ▶ return 0 unless $Config{'gccversion'} =~ m/^(\d+)\.(\d+)/;
Branch: 1 branch
BranchTrueFalseTotal
unless $Test2::Util::Config{"gccversion"} =~ /^(\d+)\.(\d+)/ 0 0 0
51 0 my @parts = split /[\.\s]+/, $Config{'gccversion'};
52 0 ▶ return 0 if $parts[0] > 4 || ($parts[0] == 4 && $parts[1] >= 8);
Branch: 1 branch
BranchTrueFalseTotal
if $parts[0] > 4 or $parts[0] == 4 and $parts[1] >= 8 0 0 0
Condition0%0/6
$parts[0] > 4 or $parts[0] == 4 and $parts[1] >= 8
l!l&&r!l&&!r
000
$parts[0] == 4 and $parts[1] >= 8
!ll&&!rl&&r
000
MC/DC0%0/3
$parts[0] > 4 or $parts[0] == 4 and $parts[1] >= 8
$parts[0] > 4 $parts[0] == 4 $parts[1] >= 8
Truth table0%0/4
A or B and C
A= $parts[0] > 4B= $parts[0] == 4C= $parts[1] >= 8
ABCresult
1XX1
0111
00X0
0100
53 }
54  
55 # Change to a version check if this ever changes
56 0 ▶ return 0 if $INC{'Devel/Cover.pm'};
Branch: 1 branch
BranchTrueFalseTotal
if $INC{"Devel/Cover.pm"} 0 0 0
57 0 return 1;
58 }
59  
60 95 ▶sub _can_fork {
Subroutine _can_fork: called CC=5 SCAR=30.0
61 95 ▶ return 1 if $Config{d_fork};
Branch: 1 branch
BranchTrueFalseTotal
if $Test2::Util::Config{"d_fork"} 95 0 95
62 0 ▶ return 0 unless IS_WIN32 || $^O eq 'NetWare';
Branch: 1 branch
BranchTrueFalseTotal
unless $^O eq "NetWare" 0 0 0
63 0 ▶ return 0 unless $Config{useithreads};
Branch: 1 branch
BranchTrueFalseTotal
unless $Test2::Util::Config{"useithreads"} 0 0 0
64 0 ▶ return 0 unless $Config{ccflags} =~ /-DPERL_IMPLICIT_SYS/;
Branch: 1 branch
BranchTrueFalseTotal
unless $Test2::Util::Config{"ccflags"} =~ /-DPERL_IMPLICIT_SYS/ 0 0 0
65  
66 0 return _can_thread();
67 }
68  
69 456 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.0
70 456 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
71 456 ▶ *CAN_THREAD = _can_thread() ? sub() { 1 } : sub() { 0 };
Branch: 1 branch
BranchTrueFalseTotal
_can_thread() ? : 0 456 456
72 }
73 456 my $can_fork;
74 356 ▶sub CAN_FORK () {
Subroutine CAN_FORK: called CC=3 SCAR=11.0
Pod documented
75 356 ▶ return $can_fork
Branch: 1 branch
BranchTrueFalseTotal
if defined $can_fork 261 95 356
76 if defined $can_fork;
77 95 $can_fork = !!_can_fork();
78 456 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
79 95 ▶ *CAN_FORK = $can_fork ? sub() { 1 } : sub() { 0 };
Branch: 1 branch
BranchTrueFalseTotal
$can_fork ? : 95 0 95
80 95 $can_fork;
81 }
82 my $can_really_fork;
83 118 ▶sub CAN_REALLY_FORK () {
Subroutine CAN_REALLY_FORK: called CC=3 SCAR=11.0
Pod documented
84 118 ▶ return $can_really_fork
Branch: 1 branch
BranchTrueFalseTotal
if defined $can_really_fork 38 80 118
85 if defined $can_really_fork;
86 80 $can_really_fork = !!$Config{d_fork};
87 456 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
88 80 ▶ *CAN_REALLY_FORK = $can_really_fork ? sub() { 1 } : sub() { 0 };
Branch: 1 branch
BranchTrueFalseTotal
$can_really_fork ? : 80 0 80
89 80 $can_really_fork;
90 }
91  
92 2 ▶sub _manual_try(&;@) {
Subroutine _manual_try: called CC=4 SCAR=14.0
93 2 my $code = shift;
94 2 my $args = \@_;
95 2 my $err;
96  
97 2 my $die = delete $SIG{__DIE__};
98  
99 2 ▶ eval { $code->(@$args); 1 } or $err = $@ || "Error was squashed!\n";
Branch: 1 branch
BranchTrueFalseTotal
eval { do { &$code(@$args); 1 } } or $err = $@ || "Error was squashed!\n" 1 1 2
Condition50%1/2
$@ || "Error was squashed!\n"
l!l
10
MC/DC0%0/1
$@ || "Error was squashed!\n"
$@
Truth table50%1/2
A
A= $@
Aresult
11
00
100  
101 2 ▶ $die ? $SIG{__DIE__} = $die : delete $SIG{__DIE__};
Branch: 1 branch
BranchTrueFalseTotal
$die ? : 0 2 2
102  
103 2 return (!defined($err), $err);
104 }
105  
106 3742 ▶sub _local_try(&;@) {
Subroutine _local_try: called CC=3 SCAR=11.0
107 3742 my $code = shift;
108 3742 my $args = \@_;
109 3742 my $err;
110  
111 456 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
112 3742 local $SIG{__DIE__};
113 3742 ▶ eval { $code->(@$args); 1 } or $err = $@ || "Error was squashed!\n";
Branch: 1 branch
BranchTrueFalseTotal
eval { do { &$code(@$args); 1 } } or $err = $@ || "Error was squashed!\n" 3715 27 3742
Condition50%1/2
$@ || "Error was squashed!\n"
l!l
270
MC/DC0%0/1
$@ || "Error was squashed!\n"
$@
Truth table50%1/2
A
A= $@
Aresult
11
00
114  
115 3742 return (!defined($err), $err);
116 }
117  
118 # Older versions of perl have a nasty bug on win32 when localizing a variable
119 # before forking or starting a new thread. So for those systems we use the
120 # non-local form. When possible though we use the faster 'local' form.
121 456 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
122 456 if (IS_WIN32 && $] < 5.020002) {
123 *try = \&_manual_try;
124 }
125 else {
126 456 *try = \&_local_try;
127 }
128 }
129  
130 456 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=2.9
131 456 if (CAN_THREAD) {
132 if ($INC{'threads.pm'}) {
133 # Threads are already loaded, so we do not need to check if they
134 # are loaded each time
135 *USE_THREADS = sub() { 1 };
136 0 ▶ *get_tid = sub() { threads->tid() };
Subroutine __ANON__: not called CC=1 SCAR=6.9
137 }
138 else {
139 # :-( Need to check each time to see if they have been loaded.
140 0 ▶ *USE_THREADS = sub() { $INC{'threads.pm'} ? 1 : 0 };
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$INC{'threads.pm'} ? : 0 0 0
141 0 ▶ *get_tid = sub() { $INC{'threads.pm'} ? threads->tid() : 0 };
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$INC{'threads.pm'} ? : 0 0 0
142 }
143 }
144 else {
145 # No threads, not now, not ever!
146 456 *USE_THREADS = sub() { 0 };
147 456 *get_tid = sub() { 0 };
148 }
149 }
150  
151 1486 ▶sub pkg_to_file {
Subroutine pkg_to_file: called CC=1 SCAR=0.0
Pod documented
152 1486 my $pkg = shift;
153 1486 my $file = $pkg;
154 1486 $file =~ s{(::|')}{/}g;
155 1486 $file .= '.pm';
156 1486 return $file;
157 }
158  
159 sub ipc_separator() { "~" }
160  
161 456 my $UID = 1;
162 26751 ▶sub gen_uid() { join ipc_separator() => ($$, get_tid(), time, $UID++) }
Subroutine gen_uid: called CC=1 SCAR=0.0
Pod documented
163  
164 6 ▶sub _check_for_sig_sys {
Subroutine _check_for_sig_sys: called CC=1 SCAR=0.0
165 6 my $sig_list = shift;
166 6 return $sig_list =~ m/\bSYS\b/;
167 }
168  
169 456 my $CAN_SIGSYS;
170 1 ▶sub CAN_SIGSYS () {
Subroutine CAN_SIGSYS: called CC=2 SCAR=7.1
Pod undocumented
171 1 ▶ if (!defined $CAN_SIGSYS) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $CAN_SIGSYS) 1 0 1
172 1 $CAN_SIGSYS = _check_for_sig_sys($Config{sig_name});
173 }
174 1 $CAN_SIGSYS;
175 }
176  
177 456 my %PERLIO_SKIP = (
178 unix => 1,
179 via => 1,
180 );
181  
182 2064 ▶sub clone_io {
Subroutine clone_io: called CC=6 SCAR=18.1
Pod undocumented
183 2064 my ($fh) = @_;
184 2064 my $fileno = eval { fileno($fh) };
185  
186 2064 ▶ return $fh if !defined($fileno) || !length($fileno) || $fileno < 0;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $fileno && length $fileno or $fileno < 0 1 2063 2064
Condition50%3/6
not defined $fileno && length $fileno or $fileno < 0
l!l&&r!l&&!r
012063
defined $fileno && length $fileno
!ll&&!rl&&r
002064
MC/DC33%1/3
not defined $fileno && length $fileno or $fileno < 0
defined $fileno length $fileno $fileno < 0
Truth table50%2/4
not(A && B) or C
A= defined $filenoB= length $filenoC= $fileno < 0
ABCresult
0XX1
10X1
1111
1100
187  
188 2063 ▶ open(my $out, '>&' . $fileno) or die "Can't dup fileno $fileno: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $out, '>&' . $fileno or die "Can't dup fileno ${fileno}: $!" 2063 0 2063
189  
190 2063 my %seen;
191 2063 ▶ my @layers = HAVE_PERLIO ? grep { !$PERLIO_SKIP{$_} and !$seen{$_}++ } PerlIO::get_layers($fh) : ();
Condition100%3/3
$PERLIO_SKIP{$_} || $seen{$_}++
l!l&&r!l&&!r
206311522071
MC/DC100%2/2
$PERLIO_SKIP{$_} || $seen{$_}++
$PERLIO_SKIP{$_} $seen{$_}++
Truth table100%3/3
A || B
A= $PERLIO_SKIP{$_}B= $seen{$_}++
ABresult
1X1
011
000
192 2063 binmode($out, join(":", "", "raw", @layers));
193  
194 2063 my $old = select $fh;
195 2063 my $af = $|;
196 2063 select $out;
197 2063 $| = $af;
198 2063 select $old;
199  
200 2063 return $out;
201 }
202  
203 456 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=3.6
204 456 if (IS_WIN32) {
205 my $max_tries = 5;
206  
207 0 ▶ *do_rename = sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
208 0 my ($from, $to) = @_;
209  
210 0 my $err;
211 0 for (1 .. $max_tries) {
212 0 ▶ return (1) if rename($from, $to);
Branch: 1 branch
BranchTrueFalseTotal
if rename $from, $to 0 0 0
213 0 $err = "$!";
214 0 ▶ last if $_ == $max_tries;
Branch: 1 branch
BranchTrueFalseTotal
if $_ == $max_tries 0 0 0
215 0 sleep 1;
216 }
217  
218 0 return (0, $err);
219 };
220 0 ▶ *do_unlink = sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
221 0 my ($file) = @_;
222  
223 0 my $err;
224 0 for (1 .. $max_tries) {
225 0 ▶ return (1) if unlink($file);
Branch: 1 branch
BranchTrueFalseTotal
if unlink $file 0 0 0
226 0 $err = "$!";
227 0 ▶ last if $_ == $max_tries;
Branch: 1 branch
BranchTrueFalseTotal
if $_ == $max_tries 0 0 0
228 0 sleep 1;
229 }
230  
231 0 return (0, "$!");
232 };
233 }
234 else {
235 227 ▶ *do_rename = sub {
Subroutine __ANON__: called CC=2 SCAR=8.1
236 227 my ($from, $to) = @_;
237 227 ▶ return (1) if rename($from, $to);
Branch: 1 branch
BranchTrueFalseTotal
if rename $from, $to 227 0 227
238 0 return (0, "$!");
239 456 };
240 1274 ▶ *do_unlink = sub {
Subroutine __ANON__: called CC=2 SCAR=8.1
241 1274 my ($file) = @_;
242 1274 ▶ return (1) if unlink($file);
Branch: 1 branch
BranchTrueFalseTotal
if unlink $file 1274 0 1274
243 0 return (0, "$!");
244 456 };
245 }
246 }
247  
248 #for backwards compatibility
249 0 ▶sub try_sig_mask(&) {
Subroutine try_sig_mask: not called CC=1 SCAR=6.9
Pod undocumented
250 0 require Test2::Util::Sig;
251 0 goto &Test2::Util::Sig::try_sig_mask;
252 }
253  
254 456 1;
255  
256 __END__
« blib/lib/Test2/API.pm ↑ Summary blib/lib/Test2/API/Instance.pm »