Promises.pm

filter: Statementstmt 65.2% 148 / 227 Branchbran 54.7% 23 / 42 Conditioncond 40.0% 8 / 20 MC/DCmcdc 10.0% 1 / 10 Subroutinesub 46.7% 29 / 62 Podpod 0.0% 0 / 25 totaltotal 54.1% 209 / 386 CC 31 SCAR 12.4
CC
31
Cov
62%
_drop_self
7.9
readline
7.0
__ANON__
6.0
CRAP 3.4
↑ Summary Promises/Utils.pm »
1 package fs::Promises;
2 2 ▶use v5.24;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use experimental qw/signatures lexical_subs/;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶no warnings qw/
Subroutine BEGIN: called CC=1 SCAR=0.0
6 experimental
7 experimental::signatures
8 experimental::lexical_subs
9 2 /;
10  
11 # Core
12 2 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 2 ▶use AnyEvent::XSPromises ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use POSIX::AtFork ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18 0 ▶POSIX::AtFork->add_to_child(sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
19 0 ▶ IO::AIO::reinit() if $INC{'IO/AIO.pm'};
Branch: 1 branch
BranchTrueFalseTotal
if $INC{"IO/AIO.pm"} 0 0 0
20 2 });
21  
22 # Utils
23 2 ▶use Ref::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 ▶use Hash::Util::FieldHash ();
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 2 our $VERSION = 0.04;
27  
28 11 ▶my sub deferred { AnyEvent::XSPromises::deferred() }
Subroutine deferred: called CC=1 SCAR=0.0
29 85 ▶my sub resolved { AnyEvent::XSPromises::resolved(@_) }
Subroutine resolved: called CC=1 SCAR=0.0
30 0 ▶my sub rejected { AnyEvent::XSPromises::rejected(@_) }
Subroutine rejected: not called CC=1 SCAR=6.9
31  
32 3 ▶my sub errno {
Subroutine errno: called CC=1 SCAR=0.0
33 3 my $e_num = 0 + $!;
34 3 my $e_str = "$!";
35 3 return Scalar::Util::dualvar( $e_num, $e_str );
36 }
37  
38 2 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
39 2 our @EXPORT_OK = qw(
40 open
41 close
42 stat
43 lstat
44  
45 seek
46  
47 fcntl
48 ioctl
49  
50  
51 utime
52 chown
53 chmod
54 truncate
55  
56 unlink
57 link
58 symlink
59 rename
60 copy
61 move
62  
63 readlink
64 realpath
65 mkdir
66 rmdir
67 rmtree
68 scandir
69  
70 slurp
71 readline
72 );
73 2 my @promise_versions;
74 2 foreach my $exported ( @EXPORT_OK ) {
75 50 my $promise_version = "${exported}_promise";
76 50 push @promise_versions, $promise_version;
77 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
78 50 *{$promise_version} = \*{$exported};
79 }
80 2 push @EXPORT_OK, @promise_versions;
81  
82 2 ▶use constant DEBUG => $ENV{DEBUG_fs_Promises} // 0;
Subroutine BEGIN: called CC=2 SCAR=7.1
Condition50%1/2
$ENV{"DEBUG_fs_Promises"} // 0
l!l
02
MC/DC0%0/1
$ENV{"DEBUG_fs_Promises"} // 0
$ENV{"DEBUG_fs_Promises"}
Truth table50%1/2
A
A= $ENV{"DEBUG_fs_Promises"}
Aresult
11
00
83 0 ▶sub TELL { say STDERR sprintf(__PACKAGE__ . ': ' . shift, @_) }
Subroutine TELL: not called CC=1 SCAR=6.9
84  
85 2 Hash::Util::FieldHash::fieldhash my %per_fh_buffer_cache;
86  
87 96 ▶my sub _drop_self { shift @_ if @_ > 1 && ($_[0]//'') eq __PACKAGE__; }
Subroutine _drop_self: called CC=4 SCAR=20.7
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 and ($_[0] // "") eq __PACKAGE__ 0 96 96
Condition20%1/5
@_ > 1 and ($_[0] // "") eq __PACKAGE__
!ll&&!rl&&r
9600
$_[0] // ""
l!l
00
MC/DC0%0/2
@_ > 1 and ($_[0] // "") eq __PACKAGE__
@_ > 1 $_[0]
Truth table33%1/3
A and B
A= @_ > 1B= $_[0]
ABresult
0X0
100
111
88  
89 5 ▶my sub lazily_require_aio {
Subroutine lazily_require_aio: called CC=1 SCAR=0.0
90 5 state $loaded = do {
91 2 require IO::AIO;
92 2 require AnyEvent::AIO;
93 2 1;
94 };
95 5 return $loaded;
96 }
97 0 ▶sub scandir {
Subroutine scandir: not called CC=1 SCAR=6.9
Pod undocumented
98 0 &_drop_self;
99 0 my $path = File::Spec->rel2abs(shift);
100 0 my $max_req = shift;
101 0 my $deferred = AnyEvent::XSPromises::deferred();
102 0 ▶ IO::AIO::aio_scandir($path, $max_req, sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
103 0 ▶ if ( !@_ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@_) 0 0 0
104 0 $deferred->reject(errno());
105 0 return;
106 }
107 0 $deferred->resolve(@_);
108 0 });
109 0 return $deferred->promise;
110 }
111  
112 3 ▶sub open {
Subroutine open: called CC=3 SCAR=11.1
Pod undocumented
113 3 &_drop_self;
114 3 my ($maybe_rel_file, $mode) = @_; # TODO: mode!
115 3 lazily_require_aio();
116 3 ▶ $mode ||= IO::AIO::O_RDONLY();
Condition33%1/3
$mode ||= &IO::AIO::O_RDONLY()
l!l&&r!l&&!r
003
MC/DC0%0/2
$mode ||= &IO::AIO::O_RDONLY()
$mode &IO::AIO::O_RDONLY()
Truth table33%1/3
A ||= B
A= $modeB= &IO::AIO::O_RDONLY()
ABresult
1X1
011
000
117  
118 3 my %symbolic_mode_to_numeric = (
119 '>' => IO::AIO::O_WRONLY() | IO::AIO::O_CREAT(),
120 '>>' => IO::AIO::O_WRONLY() | IO::AIO::O_CREAT() | IO::AIO::O_APPEND(),
121 '<' => IO::AIO::O_RDONLY(),
122 );
123 $mode = $symbolic_mode_to_numeric{$mode}
124 3 ▶ if exists $symbolic_mode_to_numeric{$mode};
Branch: 1 branch
BranchTrueFalseTotal
if exists $symbolic_mode_to_numeric{$mode} 0 3 3
125  
126 3 my $abs_file = File::Spec->rel2abs($maybe_rel_file); # AIO api requires absolute paths
127 3 my $deferred = deferred();
128 3 ▶ IO::AIO::aio_open($abs_file, $mode, 0, sub ($fh=undef) {
Subroutine __ANON__: called CC=3 SCAR=11.0
129 3 ▶ if ( !$fh ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh) 1 2 3
130 1 $deferred->reject(errno());
131 1 return;
132 }
133 2 $deferred->resolve($fh);
134 3 });
135 3 return $deferred->promise;
136 }
137  
138 0 ▶my sub _arg_is_fh {
Subroutine _arg_is_fh: not called CC=1 SCAR=6.9
139 0 my $cb = shift;
140 0 &_drop_self;
141 0 my $deferred = deferred();
142 0 ▶ $cb->(@_, sub { $_[0] < 0 ? $deferred->reject(errno()) : $deferred->resolve(@_) });
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0] < 0 ? : 0 0 0
143 0 return $deferred->promise;
144 }
145  
146 0 ▶sub close { lazily_require_aio(); _arg_is_fh(\&IO::AIO::aio_close, @_) } # Don't use unless you know what you are getting into.
Subroutine close: not called CC=1 SCAR=6.9
Pod undocumented
147 0 ▶sub seek { lazily_require_aio(); _arg_is_fh(\&IO::AIO::aio_seek, @_) }
Subroutine seek: not called CC=1 SCAR=6.9
Pod undocumented
148 0 ▶sub fcntl { lazily_require_aio(); _arg_is_fh(\&IO::AIO::aio_fcntl, @_) }
Subroutine fcntl: not called CC=1 SCAR=6.9
Pod undocumented
149 0 ▶sub ioctl { lazily_require_aio(); _arg_is_fh(\&IO::AIO::aio_ioctl, @_) }
Subroutine ioctl: not called CC=1 SCAR=6.9
Pod undocumented
150  
151 2 ▶my sub _ensure_globref_or_absolute_path {
Subroutine _ensure_globref_or_absolute_path: called CC=4 SCAR=17.9
152 2 my ($fh_or_file) = @_;
153 2 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (Ref::Util::XS::is_globref($fh_or_file) or Ref::Util::XS::is_globref(\$fh_or_file) or Ref::Util::XS::is_ioref($fh_or_file)) 0 2 2
Condition33%2/6
Ref::Util::XS::is_globref($fh_or_file) or Ref::Util::XS::is_globref(\$fh_or_file) or Ref::Util::XS::is_ioref($fh_or_file)
l!l&&r!l&&!r
002
Ref::Util::XS::is_globref($fh_or_file) or Ref::Util::XS::is_globref(\$fh_or_file)
l!l&&r!l&&!r
002
MC/DC0%0/3
Ref::Util::XS::is_globref($fh_or_file) or Ref::Util::XS::is_globref(\$fh_or_file) or Ref::Util::XS::is_ioref($fh_or_file)
Ref::Util::XS::is_globref($fh_or_file) Ref::Util::XS::is_globref(\$fh_or_file) Ref::Util::XS::is_ioref($fh_or_file)
Truth table25%1/4
A or B or C
A= Ref::Util::XS::is_globref($fh_or_file)B= Ref::Util::XS::is_globref(\$fh_or_file)C= Ref::Util::XS::is_ioref($fh_or_file)
ABCresult
1XX1
01X1
0011
0000
154 Ref::Util::is_globref($fh_or_file)
155 || Ref::Util::is_globref(\$fh_or_file)
156 || Ref::Util::is_ioref($fh_or_file)
157 ) {
158 # Globref/IO object, just return it
159 0 return $fh_or_file;
160 }
161 # Probably a path -- We need to make it an absolute path per
162 # the AIO API.
163 2 return File::Spec->rel2abs($fh_or_file);
164 }
165  
166 0 ▶my sub _arg_is_fh_or_file {
Subroutine _arg_is_fh_or_file: not called CC=1 SCAR=6.9
167 0 my $cb = shift;
168 0 &_drop_self;
169 0 my $fh_or_maybe_rel_path = shift;
170 0 my $fh_or_abs_path = _ensure_globref_or_absolute_path($fh_or_maybe_rel_path);
171 0 my $deferred = deferred();
172 0 ▶ push @_, sub { $_[0] < 0 ? $deferred->reject(errno()) : $deferred->resolve(@_) };
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0] < 0 ? : 0 0 0
173 0 $cb->($fh_or_abs_path, @_);
174 0 return $deferred->promise;
175 }
176  
177 2 ▶my sub _wrap_stat_and_lstat {
Subroutine _wrap_stat_and_lstat: called CC=1 SCAR=0.0
178 2 my $cb = shift;
179 2 &_drop_self;
180 2 my $fh_or_maybe_rel_path = shift;
181 2 my $fh_or_abs_path = _ensure_globref_or_absolute_path($fh_or_maybe_rel_path);
182 2 my $deferred = deferred();
183 2 ▶ push @_, sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
184 2 my $stat_status = shift;
185 2 ▶ if ( $stat_status ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stat_status) 1 1 2
186 # non-zero status for stat; the call failed, so the pseudo-handle _ will hold nothing
187 # of interest
188 1 $deferred->reject(errno());
189 1 return;
190 }
191  
192 # Get the cached (l)stat results (calling stat or lstat here doesn't matter, since
193 # it just gets whatever is cached in _):
194 1 my $stat_results = [ stat(_) ];
195 1 $deferred->resolve($stat_results);
196 2 };
197 2 $cb->($fh_or_abs_path, @_);
198 2 return $deferred->promise;
199 }
200  
201  
202 2 ▶sub stat { lazily_require_aio(); _wrap_stat_and_lstat(\&IO::AIO::aio_stat, @_) }
Subroutine stat: called CC=1 SCAR=0.0
Pod undocumented
203 0 ▶sub lstat { lazily_require_aio(); _wrap_stat_and_lstat(\&IO::AIO::aio_lstat, @_) }
Subroutine lstat: not called CC=1 SCAR=6.9
Pod undocumented
204 0 ▶sub utime { lazily_require_aio(); _arg_is_fh_or_file(\&IO::AIO::aio_utime, @_) }
Subroutine utime: not called CC=1 SCAR=6.9
Pod undocumented
205 0 ▶sub chown { lazily_require_aio(); _arg_is_fh_or_file(\&IO::AIO::aio_chown, @_) }
Subroutine chown: not called CC=1 SCAR=6.9
Pod undocumented
206 0 ▶sub truncate { lazily_require_aio(); _arg_is_fh_or_file(\&IO::AIO::aio_truncate, @_) }
Subroutine truncate: not called CC=1 SCAR=6.9
Pod undocumented
207 0 ▶sub chmod { lazily_require_aio(); _arg_is_fh_or_file(\&IO::AIO::aio_chmod, @_) }
Subroutine chmod: not called CC=1 SCAR=6.9
Pod undocumented
208 0 ▶sub unlink { lazily_require_aio(); _arg_is_fh_or_file(\&IO::AIO::aio_unlink, @_) }
Subroutine unlink: not called CC=1 SCAR=6.9
Pod undocumented
209  
210 0 ▶my sub _arg_is_two_paths {
Subroutine _arg_is_two_paths: not called CC=1 SCAR=6.9
211 0 my $cb = shift;
212 0 &_drop_self;
213 0 my ($first_path, $second_path) = map File::Spec->rel2abs($_), shift, shift;
214 0 my $deferred = deferred();
215 0 ▶ $cb->($first_path, $second_path, @_, sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
216 0 ▶ $_[0] < 0 ? $deferred->reject(errno()) : $deferred->resolve(@_)
Branch: 1 branch
BranchTrueFalseTotal
$_[0] < 0 ? : 0 0 0
217 0 });
218 0 return $deferred->promise;
219 }
220  
221 0 ▶sub link { lazily_require_aio(); _arg_is_two_paths(\&IO::AIO::aio_link, @_) }
Subroutine link: not called CC=1 SCAR=6.9
Pod undocumented
222 0 ▶sub symlink { lazily_require_aio(); _arg_is_two_paths(\&IO::AIO::aio_symlink, @_) }
Subroutine symlink: not called CC=1 SCAR=6.9
Pod undocumented
223 0 ▶sub rename { lazily_require_aio(); _arg_is_two_paths(\&IO::AIO::aio_rename, @_) }
Subroutine rename: not called CC=1 SCAR=6.9
Pod undocumented
224 0 ▶sub copy { lazily_require_aio(); _arg_is_two_paths(\&IO::AIO::aio_copy, @_) }
Subroutine copy: not called CC=1 SCAR=6.9
Pod undocumented
225 0 ▶sub move { lazily_require_aio(); _arg_is_two_paths(\&IO::AIO::aio_move, @_) }
Subroutine move: not called CC=1 SCAR=6.9
Pod undocumented
226  
227  
228 0 ▶my sub _arg_is_single_path {
Subroutine _arg_is_single_path: not called CC=1 SCAR=6.9
229 0 my $cb = shift;
230 0 &_drop_self;
231 0 my $first_path = File::Spec->rel2abs(shift);
232 0 my $deferred = deferred();
233 0 ▶ $cb->($first_path, @_, sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
234 0 ▶ $_[0] < 0 ? $deferred->reject(errno()) : $deferred->resolve(@_)
Branch: 1 branch
BranchTrueFalseTotal
$_[0] < 0 ? : 0 0 0
235 0 });
236 0 return $deferred->promise;
237 }
238 0 ▶sub readlink { _arg_is_single_path(\&IO::AIO::aio_readlink, @_) }
Subroutine readlink: not called CC=1 SCAR=6.9
Pod undocumented
239 0 ▶sub realpath { _arg_is_single_path(\&IO::AIO::aio_realpath, @_) }
Subroutine realpath: not called CC=1 SCAR=6.9
Pod undocumented
240 0 ▶sub mkdir { _arg_is_single_path(\&IO::AIO::aio_mkdir, @_) }
Subroutine mkdir: not called CC=1 SCAR=6.9
Pod undocumented
241 0 ▶sub rmdir { _arg_is_single_path(\&IO::AIO::aio_rmdir, @_) }
Subroutine rmdir: not called CC=1 SCAR=6.9
Pod undocumented
242 0 ▶sub rmtree { _arg_is_single_path(\&IO::AIO::aio_rmtree, @_) }
Subroutine rmtree: not called CC=1 SCAR=6.9
Pod undocumented
243  
244 3 ▶sub slurp {
Subroutine slurp: called CC=1 SCAR=0.0
Pod undocumented
245 3 &_drop_self;
246 3 my $file = File::Spec->rel2abs(shift);
247 3 my $deferred = deferred();
248 3 my $buffer = '';
249 3 ▶ IO::AIO::aio_slurp($file, 0, 0, $buffer, sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
250 3 ▶ if ( $_[0] < 0 ) { # will be 0 if the file was empty
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] < 0) 1 2 3
251 1 $deferred->reject(errno());
252 1 return;
253 }
254 2 $deferred->resolve($buffer);
255 3 });
256 3 return $deferred->promise;
257 }
258  
259 88 ▶sub readline {
Subroutine readline: called CC=7 SCAR=19.5
Pod undocumented
260 88 &_drop_self;
261 88 my ($fh, $block_size) = @_;
262 88 ▶ $block_size ||= 8192; #(stat $fh)[11] || 1024;
Condition50%1/2
$block_size ||= 8192
l!l
088
MC/DC0%0/1
$block_size ||= 8192
$block_size
Truth table50%1/2
A
A= $block_size
Aresult
11
00
263  
264 88 my $eol = $/;
265  
266 88 ▶ if ( !$fh ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh) 0 88 88
267 0 return rejected("No filehandle provided to readline()");
268 }
269  
270 88 my $io = *{$fh}{IO};
271  
272 88 ▶ my $buffer = \($per_fh_buffer_cache{$io} //= '');
Condition100%2/2
$per_fh_buffer_cache{$io} //= ""
l!l
862
MC/DC100%1/1
$per_fh_buffer_cache{$io} //= ""
$per_fh_buffer_cache{$io}
Truth table100%2/2
A
A= $per_fh_buffer_cache{$io}
Aresult
11
00
273  
274 88 my $fileno = fileno($fh);
275 88 my $buf_index = length($$buffer);
276 88 ▶ if ( $buf_index ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($buf_index) 85 3 88
277 85 ▶ my $eol_index = $eol ? index($$buffer, $eol, 0) : -1;
Branch: 1 branch
BranchTrueFalseTotal
$eol ? : 85 0 85
278  
279 85 ▶ if ( $eol_index >= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($eol_index >= 0) 85 0 85
280 85 DEBUG and TELL "fd %d: cached EOL", $fileno;
281 # previous read got multiple lines!
282 85 my $line = substr($$buffer, 0, $eol_index + 1, '');
283 85 return resolved($line);
284 }
285 }
286  
287 3 my $deferred = deferred();
288 4 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
289 4 my $do_aio_read = __SUB__;
290 4 my $this_read_buf = '';
291 IO::AIO::aio_read(
292 $fh,
293 undef, # read offset -- undef means from the fd's offset
294 $block_size, # read size
295 $this_read_buf, # buffer to place the read data into
296 0, # offset in the buffer to start writing from
297 4 ▶ sub {
Subroutine __ANON__: called CC=6 SCAR=17.9
298 4 my ($bytes_read) = @_;
299  
300 4 ▶ if ( !$bytes_read ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($bytes_read) 2 2 4
301 # EOF; return what we have so far
302 2 ▶ if ( $$buffer ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$buffer) { } 1 1 2
303 1 DEBUG and TELL "fd %d: EOF, with cached EOL", $fileno;
304 1 $deferred->resolve("$$buffer");
305 1 $$buffer = '';
306 }
307 else {
308 1 DEBUG and TELL "fd %d: EOF", $fileno;
309 # we read nothing, and had nothing buffered. Return undef.
310 1 $deferred->resolve(undef);
311 }
312 2 return;
313 }
314  
315 2 ▶ $$buffer .= $this_read_buf if $bytes_read;
Branch: 1 branch
BranchTrueFalseTotal
if $bytes_read 2 0 2
316  
317 2 ▶ my $eol_index = $eol ? index($$buffer, $eol, $buf_index) : -1;
Branch: 1 branch
BranchTrueFalseTotal
$eol ? : 1 1 2
318  
319 2 ▶ if ( $eol_index >= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($eol_index >= 0) 1 1 2
320 1 DEBUG and TELL "fd %d: EOL", $fileno;
321 1 $buf_index = 0;
322 1 my $found = substr($$buffer, 0, $eol_index + 1, '');
323 1 $deferred->resolve($found);
324 1 return;
325 }
326  
327 1 $buf_index += $bytes_read;
328  
329 # Not EOF, but not EOL, so do another read:
330 1 DEBUG and TELL "fd %d: No EOL or EOF, doing another read", $fileno;
331 1 return $do_aio_read->();
332 },
333 4 );
334 3 }->();
335  
336 3 return $deferred->promise;
337 }
338  
339 2 1;
340 __END__
↑ Summary Promises/Utils.pm »