blib/lib/Zuzu/Module/Proc.pm

filter: Statementstmt 46.3% 193 / 416 Branchbran 23.5% 57 / 242 Conditioncond 15.0% 8 / 53 MC/DCmcdc 0.0% 0 / 31 Subroutinesub 62.0% 31 / 50 Podpod 0.0% 0 / 1 totaltotal 36.4% 289 / 793 CC 153 SCAR 57.0
CC
153
Cov
36%
_run_pipeline_true_async
1332.0
_run_command
244.0
_result_from_wait_status
110.0
CRAP 300.0
« blib/lib/Zuzu/Module/String/Encode.pm ↑ Summary blib/lib/Zuzu/Module/Marshal.pm »
1 package Zuzu::Module::Proc;
2  
3 6 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 6 our $VERSION = '0.007002';
6  
7 6 ▶use File::Temp qw( tempfile );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use IPC::Run qw( harness run timeout );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use Time::HiRes qw( sleep time );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use Cwd qw( getcwd );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 6 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 6 ▶use Zuzu::Util::NativeHelpers qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
15 native_class
16 native_function
17 perl_to_zuzu
18 zuzu_bool
19 zuzu_to_perl
20 6 );
21  
22 6 my %SIGNAL_PREV;
23 my %SIGNAL_CALLBACKS;
24 6 my %SIGNAL_CALLBACK_INSTALLED;
25  
26 6 my $TEE_CODE = <<'PERL';
27 my $file = shift;
28 open my $fh, '>', $file or die "Could not open tee output: $!";
29 binmode STDIN;
30 binmode STDOUT;
31 binmode $fh;
32 while (1) {
33 my $read = sysread STDIN, my $buffer, 8192;
34 die "Could not read pipeline input: $!" if !defined $read;
35 last if !$read;
36 print {$fh} $buffer;
37 print STDOUT $buffer;
38 }
39 PERL
40  
41 2 ▶sub _normalize_signal_name {
Subroutine _normalize_signal_name: called CC=2 SCAR=7.0
42 2 my ( $name ) = @_;
43  
44 2 ▶ my $signal = defined $name ? "$name" : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $name ? : 2 0 2
45 2 $signal =~ s/\A\s+//;
46 2 $signal =~ s/\s+\z//;
47 2 $signal =~ s/\ASIG//i;
48 2 $signal = uc $signal;
49  
50 2 return $signal;
51 }
52  
53 4 ▶sub _as_command_array {
Subroutine _as_command_array: called CC=6 SCAR=21.4
54 4 my ( $command, $argv ) = @_;
55  
56 4 my @cmd;
57 4 ▶ if ( ref($command) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $command eq 'ARRAY') { } 0 4 4
58 0 ▶ @cmd = map { defined $_ ? "$_" : '' } @{ $command };
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 0 0 0
59 }
60 else {
61 4 ▶ push @cmd, defined $command ? "$command" : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $command ? : 4 0 4
62 4 ▶ if ( ref($argv) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $argv eq "ARRAY") 4 0 4
63 4 ▶ push @cmd, map { defined $_ ? "$_" : '' } @{ $argv };
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 8 0 8
64 }
65 }
66  
67 4 return \@cmd;
68 }
69  
70 1 ▶sub _install_onsignal_handler {
Subroutine _install_onsignal_handler: called CC=2 SCAR=7.3
71 1 my ( $runtime, $signal ) = @_;
72  
73 1 ▶ return if $SIGNAL_CALLBACK_INSTALLED{$signal};
Branch: 1 branch
BranchTrueFalseTotal
if $SIGNAL_CALLBACK_INSTALLED{$signal} 0 1 1
74  
75 1 $SIGNAL_PREV{$signal} = $SIG{$signal};
76 1 ▶ $SIG{$signal} = sub {
Subroutine __ANON__: called CC=6 SCAR=19.9
77 1 ▶ my $callbacks = $SIGNAL_CALLBACKS{$signal} // [];
Condition50%1/2
$SIGNAL_CALLBACKS{$signal} // []
l!l
10
MC/DC0%0/1
$SIGNAL_CALLBACKS{$signal} // []
$SIGNAL_CALLBACKS{$signal}
Truth table50%1/2
A
A= $SIGNAL_CALLBACKS{$signal}
Aresult
11
00
78 1 for my $callback ( @{ $callbacks } ) {
79 1 ▶ next if not blessed($callback)
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($callback) and $callback->isa("Zuzu::Value::Function") 0 1 1
Condition33%1/3
builtin::blessed($callback) and $callback->isa("Zuzu::Value::Function")
!ll&&!rl&&r
001
MC/DC0%0/2
builtin::blessed($callback) and $callback->isa("Zuzu::Value::Function")
builtin::blessed($callback) $callback->isa("Zuzu::Value::Function")
Truth table33%1/3
A and B
A= builtin::blessed($callback)B= $callback->isa("Zuzu::Value::Function")
ABresult
0X0
100
111
80 or not $callback->isa( 'Zuzu::Value::Function' );
81 eval {
82 1 $runtime->_call_function( $callback, [], '<std/proc>', 0 );
83 1 1;
84 1 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $runtime->_call_function($callback, [], "<std/proc>", 0); 1 } } or do { return } 1 0 1
85 0 return;
86 };
87 }
88 1 return;
89 1 };
90 1 $SIGNAL_CALLBACK_INSTALLED{$signal} = 1;
91  
92 1 return;
93 }
94  
95 2 ▶sub _result_ok {
Subroutine _result_ok: called CC=8 SCAR=24.5
96 2 my ( $result ) = @_;
97  
98 2 ▶ return 0 if ref($result) ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $result ne "HASH" 0 2 2
99 2 ▶ return 0 if defined $result->{error} and $result->{error} ne '';
Branch: 1 branch
BranchTrueFalseTotal
if defined $result->{"error"} and $result->{"error"} ne "" 0 2 2
Condition33%1/3
defined $result->{"error"} and $result->{"error"} ne ""
!ll&&!rl&&r
200
MC/DC0%0/2
defined $result->{"error"} and $result->{"error"} ne ""
defined $result->{"error"} $result->{"error"} ne ""
Truth table33%1/3
A and B
A= defined $result->{"error"}B= $result->{"error"} ne ""
ABresult
0X0
100
111
100 2 ▶ return 0 if ( $result->{signal} // 0 ) != 0;
Branch: 1 branch
BranchTrueFalseTotal
if ($result->{"signal"} // 0) != 0 0 2 2
Condition50%1/2
$result->{"signal"} // 0
l!l
20
MC/DC0%0/1
$result->{"signal"} // 0
$result->{"signal"}
Truth table50%1/2
A
A= $result->{"signal"}
Aresult
11
00
101 2 ▶ return 0 if ( $result->{exit_code} // 0 ) != 0;
Branch: 1 branch
BranchTrueFalseTotal
if ($result->{"exit_code"} // 0) != 0 0 2 2
Condition50%1/2
$result->{"exit_code"} // 0
l!l
20
MC/DC0%0/1
$result->{"exit_code"} // 0
$result->{"exit_code"}
Truth table50%1/2
A
A= $result->{"exit_code"}
Aresult
11
00
102  
103 2 return 1;
104 }
105  
106 9 ▶sub _platform_supports_true_pipeline_async {
Subroutine _platform_supports_true_pipeline_async: called CC=2 SCAR=7.2
107 9 ▶ return 0 if $^O =~ /^(?:MSWin32|dos|os2|VMS)\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $^O =~ /^(?:MSWin32|dos|os2|VMS)\z/ 0 9 9
108 9 return 1;
109 }
110  
111 0 ▶sub _result_from_wait_status {
Subroutine _result_from_wait_status: not called CC=10 SCAR=47.0
112 0 my ( $cmd, $opts, $status, $stdout, $stderr, $error, $timed_out ) = @_;
113  
114 0 my $capture_stdout = zuzu_bool( $opts->{capture_stdout}, 1 );
115 0 my $capture_stderr = zuzu_bool( $opts->{capture_stderr}, 1 );
116 0 my $merge_stderr = zuzu_bool( $opts->{merge_stderr}, 0 );
117 0 my $exit_code = $status >> 8;
118 0 my $signal = $status & 127;
119 0 ▶ my $core_dump = ( $status & 128 ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$status & 128 ? : 0 0 0
120 0 ▶ if ( $timed_out ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($timed_out) 0 0 0
121 0 ▶ $signal = 14 if $signal == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $signal == 0 0 0 0
122 0 ▶ $error = 'timeout after ' . ( 0 + ( $opts->{timeout} // 0 ) ) . 's';
Condition0%0/2
$opts->{"timeout"} // 0
l!l
00
MC/DC0%0/1
$opts->{"timeout"} // 0
$opts->{"timeout"}
Truth table0%0/2
A
A= $opts->{"timeout"}
Aresult
11
00
123 }
124  
125 0 ▶ my $result = {
Branch: 4 branches
BranchTrueFalseTotal
$capture_stdout || $merge_stderr ? : 0 0 0
$capture_stderr ? : 0 0 0
$merge_stderr ? : 0 0 0
$timed_out ? : 0 0 0
Condition0%0/3
$capture_stdout or $merge_stderr
l!l&&r!l&&!r
000
MC/DC0%0/2
$capture_stdout or $merge_stderr
$capture_stdout $merge_stderr
Truth table0%0/3
A or B
A= $capture_stdoutB= $merge_stderr
ABresult
1X1
011
000
126 command => $cmd,
127 exit_code => $exit_code,
128 signal => $signal,
129 core_dump => $core_dump,
130 ok => 0,
131 stdout => $capture_stdout || $merge_stderr ? $stdout : undef,
132 stderr => $capture_stderr ? ( $merge_stderr ? '' : $stderr ) : undef,
133 error => $error,
134 timed_out => $timed_out ? 1 : 0,
135 };
136 0 $result->{ok} = _result_ok($result);
137  
138 0 return $result;
139 }
140  
141 0 ▶sub _timeout_result {
Subroutine _timeout_result: not called CC=2 SCAR=17.9
142 0 my ( $cmd, $opts ) = @_;
143  
144 return _result_from_wait_status(
145 $cmd,
146 $opts,
147 14,
148 '',
149 '',
150 0 ▶ 'timeout after ' . ( 0 + ( $opts->{timeout} // 0 ) ) . 's',
Condition0%0/2
$opts->{"timeout"} // 0
l!l
00
MC/DC0%0/1
$opts->{"timeout"} // 0
$opts->{"timeout"}
Truth table0%0/2
A
A= $opts->{"timeout"}
Aresult
11
00
151 1,
152 );
153 }
154  
155 0 ▶sub _pipeline_result {
Subroutine _pipeline_result: not called CC=4 SCAR=30.0
156 0 my ( $steps ) = @_;
157  
158 0 my $failed_steps = scalar grep { !$_->{ok} } @{$steps};
159 0 ▶ my $ok = @{$steps}
Branch: 2 branches
BranchTrueFalseTotal
@{$steps;} ? : 0 0 0
$failed_steps ? : 0 0 0
160 ? ( $failed_steps ? 0 : 1 )
161 : 1;
162 0 ▶ my $last = @{$steps} ? $steps->[-1] : {
Branch: 1 branch
BranchTrueFalseTotal
@{$steps;} ? : 0 0 0
163 command => [],
164 exit_code => 0,
165 signal => 0,
166 core_dump => 0,
167 ok => 1,
168 stdout => '',
169 stderr => '',
170 error => undef,
171 timed_out => 0,
172 };
173  
174 return {
175 ok => $ok,
176 stdout => $last->{stdout},
177 stderr => $last->{stderr},
178 error => $last->{error},
179 exit_code => $last->{exit_code},
180 signal => $last->{signal},
181 core_dump => $last->{core_dump},
182 timed_out => $last->{timed_out},
183 0 steps => $steps,
184 };
185 }
186  
187 2 ▶sub _run_command {
Subroutine _run_command: called CC=40 SCAR=55.0
188 2 my ( $cmd, $opts ) = @_;
189  
190 my $stdin = exists $opts->{stdin}
191 2 ▶ ? ( defined $opts->{stdin} ? "$opts->{stdin}" : '' )
Branch: 2 branches
BranchTrueFalseTotal
exists $opts->{'stdin'} ? : 1 1 2
defined $opts->{'stdin'} ? : 1 0 1
192 : '';
193 2 my $capture_stdout = zuzu_bool( $opts->{capture_stdout}, 1 );
194 2 my $capture_stderr = zuzu_bool( $opts->{capture_stderr}, 1 );
195 2 my $merge_stderr = zuzu_bool( $opts->{merge_stderr}, 0 );
196 2 ▶ my $timeout = defined $opts->{timeout} ? 0 + $opts->{timeout} : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $opts->{'timeout'} ? : 0 2 2
197  
198 2 my $stdout = '';
199 2 my $stderr = '';
200 2 ▶ my $out_ref = $capture_stdout ? \$stdout : undef;
Branch: 1 branch
BranchTrueFalseTotal
$capture_stdout ? : 2 0 2
201 2 my $err_ref;
202 2 ▶ if ( $merge_stderr ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($merge_stderr) { } 0 2 2
203 0 $err_ref = $out_ref;
204 }
205 else {
206 2 ▶ $err_ref = $capture_stderr ? \$stderr : undef;
Branch: 1 branch
BranchTrueFalseTotal
$capture_stderr ? : 2 0 2
207 }
208  
209 2 my $error;
210 2 my $timed_out = 0;
211 2 my $ran_process = 0;
212 2 my %old_env;
213 my @unset_keys;
214 2 my $had_env = 0;
215 2 my $old_cwd;
216 2 my $had_cwd = 0;
217  
218 2 ▶ my $env = exists $opts->{env} ? $opts->{env} : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $opts->{'env'} ? : 0 2 2
219 2 ▶ if ( ref($env) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $env eq "HASH") 0 2 2
220 0 $had_env = 1;
221 0 for my $key ( sort CORE::keys %{ $env } ) {
222 0 ▶ if ( exists $ENV{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $ENV{$key}) { } 0 0 0
223 0 $old_env{$key} = $ENV{$key};
224 }
225 else {
226 0 push @unset_keys, $key;
227 }
228 0 ▶ if ( defined $env->{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $env->{$key}) { } 0 0 0
229 0 $ENV{$key} = "$env->{$key}";
230 }
231 else {
232 0 delete $ENV{$key};
233 }
234 }
235 }
236  
237 2 ▶ if ( exists $opts->{cwd} and defined $opts->{cwd} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts->{"cwd"} and defined $opts->{"cwd"}) 0 2 2
Condition33%1/3
exists $opts->{"cwd"} and defined $opts->{"cwd"}
!ll&&!rl&&r
200
MC/DC0%0/2
exists $opts->{"cwd"} and defined $opts->{"cwd"}
exists $opts->{"cwd"} defined $opts->{"cwd"}
Truth table33%1/3
A and B
A= exists $opts->{"cwd"}B= defined $opts->{"cwd"}
ABresult
0X0
100
111
238 0 $had_cwd = 1;
239 0 $old_cwd = getcwd();
240 0 my $cwd = "$opts->{cwd}";
241 0 ▶ if ( !chdir $cwd ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (chdir $cwd) 0 0 0
242 0 $error = "could not change cwd to $cwd: $!";
243 }
244 }
245  
246 eval {
247 2 ▶ if ( !defined $error ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $error) 2 0 2
248 0 ▶ local $SIG{ALRM} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
249 0 $timed_out = 1;
250 0 die "run timeout\n";
251 2 };
252 2 ▶ alarm $timeout if $timeout > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $timeout > 0 0 2 2
253 2 $ran_process = 1;
254 2 my @run_spec = ( $cmd, '<', \$stdin );
255 2 ▶ push @run_spec, '>', $out_ref if $capture_stdout;
Branch: 1 branch
BranchTrueFalseTotal
if $capture_stdout 2 0 2
256 2 ▶ if ( $merge_stderr ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($merge_stderr) { } 0 2 2
elsif ($capture_stderr) { } 2 0 2
257 0 push @run_spec, '2>&1';
258 }
259 elsif ( $capture_stderr ) {
260 2 push @run_spec, '2>', $err_ref;
261 }
262 2 run(@run_spec);
263 2 ▶ alarm 0 if $timeout > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $timeout > 0 0 2 2
264 }
265 2 1;
266 2 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { unless (defined $error) { local $SIG{"ALRM"} = sub { $timed_out = 1; die "run timeout\n"; } ; alarm $timeout if $timeout > 0; $ran_process = 1; my(@run_spec) = ($cmd, "<", \$stdin); push @run_spec, ">", $out_ref if $capture_stdout; if ($merge_stderr) { push @run_spec, "2>&1"; } elsif ($capture_stderr) { push @run_spec, "2>", $err_ref; } ; run(@run_spec); alarm 0 if $timeout > 0; } ; 1 } } or do { $error = "$@"; alarm 0 if $timeout > 0 } 2 0 2
267 0 $error = "$@";
268 0 ▶ alarm 0 if $timeout > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $timeout > 0 0 0 0
269 };
270  
271 2 ▶ if ( $had_cwd ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($had_cwd) 0 2 2
272 chdir $old_cwd
273 0 ▶ or do {
Branch: 1 branch
BranchTrueFalseTotal
chdir $old_cwd or do { my $restore_error = "could not restore cwd to ${old_cwd}: $!"; $error = defined $error && $error ne "" ? "$error; $restore_error" : $restore_error } 0 0 0
274 0 my $restore_error =
275 "could not restore cwd to $old_cwd: $!";
276 0 ▶ $error = defined $error && $error ne ''
Branch: 1 branch
BranchTrueFalseTotal
defined $error && $error ne '' ? : 0 0 0
Condition0%0/3
defined $error and $error ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $error and $error ne ""
defined $error $error ne ""
Truth table0%0/3
A and B
A= defined $errorB= $error ne ""
ABresult
0X0
100
111
277 ? "$error; $restore_error"
278 : $restore_error;
279 };
280 }
281  
282 2 ▶ if ( $had_env ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($had_env) 0 2 2
283 0 for my $key ( sort CORE::keys %old_env ) {
284 0 $ENV{$key} = $old_env{$key};
285 }
286 0 for my $key ( @unset_keys ) {
287 0 delete $ENV{$key};
288 }
289 }
290  
291 2 my $status = $?;
292 2 ▶ $status = 255 << 8
Branch: 1 branch
BranchTrueFalseTotal
if not $ran_process and defined $error and $error ne "" 0 2 2
Condition33%2/6
not $ran_process and defined $error and $error ne ""
!ll&&!rl&&r
200
not $ran_process and defined $error
!ll&&!rl&&r
200
MC/DC0%0/3
not $ran_process and defined $error and $error ne ""
not $ran_process defined $error $error ne ""
Truth table25%1/4
not(A) and B and C
A= not $ran_processB= defined $errorC= $error ne ""
ABCresult
0XX0
10X0
1100
1111
293 if !$ran_process and defined $error and $error ne '';
294 2 my $exit_code = $status >> 8;
295 2 my $signal = $status & 127;
296 2 ▶ my $core_dump = ( $status & 128 ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$status & 128 ? : 0 2 2
297 2 ▶ if ( $timed_out ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($timed_out) 0 2 2
298 0 ▶ $signal = 14 if $signal == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $signal == 0 0 0 0
299 0 $error = "timeout after ${timeout}s";
300 }
301  
302 2 ▶ my $result = {
Branch: 3 branches
BranchTrueFalseTotal
$capture_stdout ? : 2 0 2
$capture_stderr ? : 2 0 2
$timed_out ? : 0 2 2
303 command => $cmd,
304 exit_code => $exit_code,
305 signal => $signal,
306 core_dump => $core_dump,
307 ok => 0,
308 stdout => $capture_stdout ? $stdout : undef,
309 stderr => $capture_stderr ? $stderr : undef,
310 error => $error,
311 timed_out => $timed_out ? 1 : 0,
312 };
313 2 $result->{ok} = _result_ok($result);
314 _emit_run_diag( $result, $stdout, $stderr )
315 2 ▶ if $ENV{ZUZU_PROC_RUN_DIAG};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"ZUZU_PROC_RUN_DIAG"} 0 2 2
316  
317 2 return $result;
318 }
319  
320 0 ▶sub _emit_run_diag {
Subroutine _emit_run_diag: not called CC=9 SCAR=45.0
321 0 my ( $result, $stdout, $stderr ) = @_;
322  
323 return
324 if $result->{ok}
325 0 ▶ and defined $stdout
Branch: 1 branch
BranchTrueFalseTotal
if $result->{"ok"} and defined $stdout and $stdout ne "" 0 0 0
Condition0%0/6
$result->{"ok"} and defined $stdout and $stdout ne ""
!ll&&!rl&&r
000
$result->{"ok"} and defined $stdout
!ll&&!rl&&r
000
MC/DC0%0/3
$result->{"ok"} and defined $stdout and $stdout ne ""
$result->{"ok"} defined $stdout $stdout ne ""
Truth table0%0/4
A and B and C
A= $result->{"ok"}B= defined $stdoutC= $stdout ne ""
ABCresult
0XX0
10X0
1100
1111
326 and $stdout ne '';
327  
328 0 ▶ my $command = join ' ', map { defined $_ ? $_ : '<undef>' }
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 0 0 0
329 0 ▶ @{ $result->{command} // [] };
Condition0%0/2
$result->{"command"} // []
l!l
00
MC/DC0%0/1
$result->{"command"} // []
$result->{"command"}
Truth table0%0/2
A
A= $result->{"command"}
Aresult
11
00
330 0 warn "[ZUZU_PROC_RUN_DIAG] command=$command\n";
331 warn "[ZUZU_PROC_RUN_DIAG] exit=$result->{exit_code} "
332 . "signal=$result->{signal} error="
333 0 ▶ . ( defined $result->{error} ? $result->{error} : '' ) . "\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $result->{'error'} ? : 0 0 0
334 0 ▶ warn "[ZUZU_PROC_RUN_DIAG] stdout="
Branch: 1 branch
BranchTrueFalseTotal
defined $stdout ? : 0 0 0
335 . _diag_snippet( defined $stdout ? $stdout : '' ) . "\n";
336 0 ▶ warn "[ZUZU_PROC_RUN_DIAG] stderr="
Branch: 1 branch
BranchTrueFalseTotal
defined $stderr ? : 0 0 0
337 . _diag_snippet( defined $stderr ? $stderr : '' ) . "\n";
338 }
339  
340 0 ▶sub _diag_snippet {
Subroutine _diag_snippet: not called CC=3 SCAR=24.8
341 0 my ( $text ) = @_;
342  
343 0 $text =~ s/\s+\z//;
344 0 ▶ $text = substr( $text, 0, 4000 ) . "\n...[truncated]"
Branch: 1 branch
BranchTrueFalseTotal
if length $text > 4000 0 0 0
345 if length($text) > 4000;
346 0 ▶ return $text eq '' ? '<empty>' : $text;
Branch: 1 branch
BranchTrueFalseTotal
$text eq '' ? : 0 0 0
347 }
348  
349 0 ▶sub _run_pipeline_legacy {
Subroutine _run_pipeline_legacy: not called CC=5 SCAR=34.0
350 0 my ( $items, $opts ) = @_;
351  
352 0 my @steps;
353 0 ▶ my $stdin = exists $opts->{stdin} ? $opts->{stdin} : '';
Branch: 1 branch
BranchTrueFalseTotal
exists $opts->{'stdin'} ? : 0 0 0
354 0 for my $command ( @{ $items } ) {
355 0 my $cmd = _as_command_array( zuzu_to_perl($command), [] );
356 0 my %step_opts = %{ $opts };
357 0 $step_opts{stdin} = $stdin;
358 0 $step_opts{capture_stdout} = 1;
359 0 my $result = _run_command( $cmd, \%step_opts );
360 0 push @steps, $result;
361 0 ▶ $stdin = defined $result->{stdout} ? $result->{stdout} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $result->{'stdout'} ? : 0 0 0
362 0 ▶ last if not $result->{ok};
Branch: 1 branch
BranchTrueFalseTotal
unless $result->{"ok"} 0 0 0
363 }
364  
365 0 return _pipeline_result( \@steps );
366 }
367  
368 0 ▶sub _run_pipeline_true_async {
Subroutine _run_pipeline_true_async: not called CC=36 SCAR=71.9
369 0 my ( $items, $opts ) = @_;
370  
371 0 my @cmds = map { _as_command_array( zuzu_to_perl($_), [] ) } @{ $items };
372 0 ▶ return _pipeline_result( [] ) if !@cmds;
Branch: 1 branch
BranchTrueFalseTotal
unless @cmds 0 0 0
373  
374 0 my $capture_stderr = zuzu_bool( $opts->{capture_stderr}, 1 );
375 0 my $merge_stderr = zuzu_bool( $opts->{merge_stderr}, 0 );
376 my $stdin = exists $opts->{stdin}
377 0 ▶ ? ( defined $opts->{stdin} ? "$opts->{stdin}" : '' )
Branch: 2 branches
BranchTrueFalseTotal
exists $opts->{'stdin'} ? : 0 0 0
defined $opts->{'stdin'} ? : 0 0 0
378 : '';
379 0 ▶ my $timeout_seconds = defined $opts->{timeout} ? 0 + $opts->{timeout} : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $opts->{'timeout'} ? : 0 0 0
380 0 my @stderr = map { '' } @cmds;
381 0 my $final_stdout = '';
382 0 my @tee_files;
383 my @original_process_ix;
384 0 my @spec;
385 0 my $process_ix = 0;
386  
387 0 for my $ix ( 0 .. $#cmds ) {
388 0 push @spec, $cmds[$ix];
389 0 push @original_process_ix, $process_ix++;
390 0 ▶ if ( $ix == 0 and length $stdin ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ix == 0 and length $stdin) 0 0 0
Condition0%0/3
$ix == 0 and length $stdin
!ll&&!rl&&r
000
MC/DC0%0/2
$ix == 0 and length $stdin
$ix == 0 length $stdin
Truth table0%0/3
A and B
A= $ix == 0B= length $stdin
ABresult
0X0
100
111
391 0 push @spec, '<', \$stdin;
392 }
393 0 ▶ if ( $merge_stderr ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($merge_stderr) { } 0 0 0
elsif ($capture_stderr) { } 0 0 0
394 0 push @spec, '2>&1';
395 }
396 elsif ( $capture_stderr ) {
397 0 push @spec, '2>', \$stderr[$ix];
398 }
399 0 ▶ if ( $ix < $#cmds ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ix < $#cmds) 0 0 0
400 0 my ( $fh, $path ) = tempfile(
401 'zuzu-proc-pipeline-XXXXXX',
402 TMPDIR => 1,
403 UNLINK => 0,
404 );
405 0 close $fh;
406 0 push @tee_files, $path;
407 0 push @spec, '|', [ $^X, '-e', $TEE_CODE, $path ], '2>', \my $tee_err;
408 0 $process_ix++;
409 0 push @spec, '|';
410 }
411 }
412 0 push @spec, '>', \$final_stdout;
413 0 ▶ push @spec, timeout( $timeout_seconds, exception => 'pipeline timeout' )
Branch: 1 branch
BranchTrueFalseTotal
if $timeout_seconds > 0 0 0 0
414 if $timeout_seconds > 0;
415  
416 0 my $h = harness(@spec);
417 0 my %old_env;
418 my @unset_keys;
419 0 my $had_env = 0;
420 0 my $old_cwd;
421 0 my $had_cwd = 0;
422 0 my $error;
423 0 ▶ my $env = exists $opts->{env} ? $opts->{env} : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $opts->{'env'} ? : 0 0 0
424 0 ▶ if ( ref($env) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $env eq "HASH") 0 0 0
425 0 $had_env = 1;
426 0 for my $key ( sort CORE::keys %{ $env } ) {
427 0 ▶ if ( exists $ENV{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $ENV{$key}) { } 0 0 0
428 0 $old_env{$key} = $ENV{$key};
429 }
430 else {
431 0 push @unset_keys, $key;
432 }
433 0 ▶ if ( defined $env->{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $env->{$key}) { } 0 0 0
434 0 $ENV{$key} = "$env->{$key}";
435 }
436 else {
437 0 delete $ENV{$key};
438 }
439 }
440 }
441  
442 0 ▶ if ( exists $opts->{cwd} and defined $opts->{cwd} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts->{"cwd"} and defined $opts->{"cwd"}) 0 0 0
Condition0%0/3
exists $opts->{"cwd"} and defined $opts->{"cwd"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $opts->{"cwd"} and defined $opts->{"cwd"}
exists $opts->{"cwd"} defined $opts->{"cwd"}
Truth table0%0/3
A and B
A= exists $opts->{"cwd"}B= defined $opts->{"cwd"}
ABresult
0X0
100
111
443 0 $had_cwd = 1;
444 0 $old_cwd = getcwd();
445 0 my $cwd = "$opts->{cwd}";
446 0 ▶ if ( !chdir $cwd ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (chdir $cwd) 0 0 0
447 0 $error = "could not change cwd to $cwd: $!";
448 }
449 }
450  
451 0 my $timed_out = 0;
452 0 my $ok = eval {
453 0 ▶ $h->run if !defined $error;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $error 0 0 0
454 0 1;
455 };
456 0 ▶ if ( $had_cwd ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($had_cwd) 0 0 0
457 chdir $old_cwd
458 0 ▶ or do {
Branch: 1 branch
BranchTrueFalseTotal
chdir $old_cwd or do { my $restore_error = "could not restore cwd to ${old_cwd}: $!"; $error = defined $error && $error ne "" ? "$error; $restore_error" : $restore_error; $ok = 0 } 0 0 0
459 0 my $restore_error =
460 "could not restore cwd to $old_cwd: $!";
461 0 ▶ $error = defined $error && $error ne ''
Branch: 1 branch
BranchTrueFalseTotal
defined $error && $error ne '' ? : 0 0 0
Condition0%0/3
defined $error and $error ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $error and $error ne ""
defined $error $error ne ""
Truth table0%0/3
A and B
A= defined $errorB= $error ne ""
ABresult
0X0
100
111
462 ? "$error; $restore_error"
463 : $restore_error;
464 0 $ok = 0;
465 };
466 }
467 0 ▶ if ( $had_env ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($had_env) 0 0 0
468 0 for my $key ( sort CORE::keys %old_env ) {
469 0 $ENV{$key} = $old_env{$key};
470 }
471 0 for my $key ( @unset_keys ) {
472 0 delete $ENV{$key};
473 }
474 }
475 0 ▶ if ( !$ok ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 0 0
476 0 my $eval_error = "$@";
477 0 ▶ $error = $eval_error if $eval_error ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $eval_error ne "" 0 0 0
478 0 ▶ $timed_out = $error =~ /pipeline timeout|timed out/i ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$error =~ /pipeline timeout|timed out/i ? : 0 0 0
479 0 eval { $h->kill_kill; 1 };
480 }
481  
482 0 my @stdout = map { '' } @cmds;
483 0 for my $ix ( 0 .. $#tee_files ) {
484 0 ▶ if ( open my $fh, '<', $tee_files[$ix] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, "<", $tee_files[$ix]) 0 0 0
485 0 local $/;
486 0 $stdout[$ix] = <$fh>;
487 0 close $fh;
488 }
489 0 unlink $tee_files[$ix];
490 }
491 0 $stdout[-1] = $final_stdout;
492  
493 0 my @steps;
494 0 for my $ix ( 0 .. $#cmds ) {
495 0 ▶ if ( $timed_out ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($timed_out) 0 0 0
496 0 push @steps, _timeout_result( $cmds[$ix], $opts );
497 0 next;
498 }
499 0 my $status = eval {
500 0 $h->full_result( $original_process_ix[$ix] );
501 };
502 0 ▶ $status = 1 << 8 if !defined $status;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $status 0 0 0
503 push @steps, _result_from_wait_status(
504 $cmds[$ix],
505 {
506 0 %{ $opts },
507 capture_stdout => 1,
508 },
509 $status,
510 $stdout[$ix],
511 $stderr[$ix],
512 $error,
513 0,
514 );
515 }
516  
517 0 return _pipeline_result( \@steps );
518 }
519  
520 3 ▶sub _async_task {
Subroutine _async_task: called CC=1 SCAR=0.0
521 3 my ( $runtime, $name, $work, $wrap ) = @_;
522  
523 3 my $worker = $runtime->_new_task(
524 name => "$name.worker",
525 start => 1,
526 schedule => 1,
527 thunk => $work,
528 );
529 return $runtime->_new_task(
530 name => $name,
531 schedule => 1,
532 3 ▶ thunk => sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
533 3 my $value = $worker->await;
534 3 ▶ return defined $wrap ? $wrap->($value) : $value;
Branch: 1 branch
BranchTrueFalseTotal
defined $wrap ? : 3 0 3
535 },
536 3 );
537 }
538  
539 2 ▶sub _warn_blocking_operation {
Subroutine _warn_blocking_operation: called CC=2 SCAR=7.1
540 2 my ( $runtime, $operation ) = @_;
541  
542 2 ▶ $runtime->_warn_blocking_operation($operation)
Branch: 1 branch
BranchTrueFalseTotal
if $runtime->can("_warn_blocking_operation") 2 0 2
543 if $runtime->can('_warn_blocking_operation');
544  
545 2 return;
546 }
547  
548 0 ▶sub _pipeline_async_legacy_native {
Subroutine _pipeline_async_legacy_native: not called CC=1 SCAR=6.9
549 0 my ( $runtime ) = @_;
550  
551 return native_function(
552 name => 'pipeline_async',
553 0 ▶ native => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
554 0 my ( $self, $commands, $options ) = @_;
555 0 my $items = zuzu_to_perl( $commands );
556 0 ▶ $items = [] if ref($items) ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $items ne "ARRAY" 0 0 0
557 0 my $opts = zuzu_to_perl( $options );
558 0 ▶ $opts = {} if ref($opts) ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $opts ne "HASH" 0 0 0
559  
560 return _async_task(
561 $runtime,
562 'proc.pipeline_async',
563 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
564 0 return _run_pipeline_legacy( $items, $opts );
565 },
566 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
567 0 my ( $result ) = @_;
568 0 return perl_to_zuzu($result);
569 },
570 0 );
571 },
572 0 );
573 }
574  
575 9 ▶sub _pipeline_async_true_native {
Subroutine _pipeline_async_true_native: called CC=1 SCAR=0.1
576 9 my ( $runtime ) = @_;
577  
578 return native_function(
579 name => 'pipeline_async',
580 1 ▶ native => sub {
Subroutine __ANON__: called CC=3 SCAR=11.3
581 1 my ( $self, $commands, $options ) = @_;
582 1 my $items = zuzu_to_perl( $commands );
583 1 ▶ $items = [] if ref($items) ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $items ne "ARRAY" 0 1 1
584 1 my $opts = zuzu_to_perl( $options );
585 1 ▶ $opts = {} if ref($opts) ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $opts ne "HASH" 0 1 1
586  
587 return _async_task(
588 $runtime,
589 'proc.pipeline_async',
590 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
591 0 return _run_pipeline_true_async( $items, $opts );
592 },
593 1 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
594 1 my ( $result ) = @_;
595 1 return perl_to_zuzu($result);
596 },
597 1 );
598 },
599 9 );
600 }
601  
602 9 ▶sub IMPORT {
Subroutine IMPORT: called CC=2 SCAR=8.7
Pod undocumented
603 9 my ( $class, $runtime ) = @_;
604  
605 9 my $proc_class = native_class(
606 name => 'Proc',
607 );
608  
609 9 my $env_class = native_class(
610 name => 'Env',
611 );
612  
613 $proc_class->static_methods->{pid} = native_function(
614 name => 'pid',
615 2 ▶ native => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
616 2 return 0 + $$;
617 },
618 9 );
619  
620  
621 $env_class->static_methods->{get} = native_function(
622 name => 'get',
623 1 ▶ native => sub {
Subroutine __ANON__: called CC=3 SCAR=12.5
624 1 my ( $self, $name, $default ) = @_;
625 1 ▶ my $key = defined $name ? "$name" : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $name ? : 1 0 1
626 1 ▶ return $ENV{$key} if exists $ENV{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $ENV{$key} 1 0 1
627 0 return $default;
628 },
629 9 );
630  
631 $env_class->static_methods->{set} = native_function(
632 name => 'set',
633 1 ▶ native => sub {
Subroutine __ANON__: called CC=3 SCAR=11.4
634 1 my ( $self, $name, $value ) = @_;
635 1 ▶ my $key = defined $name ? "$name" : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $name ? : 1 0 1
636 1 ▶ $ENV{$key} = defined $value ? "$value" : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 1 0 1
637 1 return $ENV{$key};
638 },
639 9 );
640  
641 $env_class->static_methods->{remove} = native_function(
642 name => 'remove',
643 2 ▶ native => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
644 2 my ( $self, $name ) = @_;
645 2 ▶ my $key = defined $name ? "$name" : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $name ? : 2 0 2
646 2 delete $ENV{$key};
647 2 return undef;
648 },
649 9 );
650  
651 $proc_class->static_methods->{run} = native_function(
652 name => 'run',
653 2 ▶ native => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
654 2 my ( $self, $command, $argv, $options ) = @_;
655 2 _warn_blocking_operation( $runtime, 'std/proc Proc.run' );
656  
657 2 my $args = zuzu_to_perl( $argv );
658 2 ▶ $args = [] if ref($args) ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $args ne "ARRAY" 0 2 2
659  
660 2 my $opts = zuzu_to_perl( $options );
661 2 ▶ $opts = {} if ref($opts) ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $opts ne "HASH" 1 1 2
662  
663 2 my $cmd = _as_command_array( zuzu_to_perl($command), $args );
664 2 my $result = _run_command( $cmd, $opts );
665 2 return perl_to_zuzu($result);
666 },
667 9 );
668  
669 $proc_class->static_methods->{run_async} = native_function(
670 name => 'run_async',
671 2 ▶ native => sub {
Subroutine __ANON__: called CC=3 SCAR=11.3
672 2 my ( $self, $command, $argv, $options ) = @_;
673  
674 2 my $args = zuzu_to_perl( $argv );
675 2 ▶ $args = [] if ref($args) ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $args ne "ARRAY" 0 2 2
676  
677 2 my $opts = zuzu_to_perl( $options );
678 2 ▶ $opts = {} if ref($opts) ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $opts ne "HASH" 2 0 2
679  
680 2 my $cmd = _as_command_array( zuzu_to_perl($command), $args );
681 return _async_task(
682 $runtime,
683 'proc.run_async',
684 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
685 0 return _run_command( $cmd, $opts );
686 },
687 2 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
688 2 my ( $result ) = @_;
689 2 return perl_to_zuzu($result);
690 },
691 2 );
692 },
693 9 );
694  
695 $proc_class->static_methods->{pipeline} = native_function(
696 name => 'pipeline',
697 0 ▶ native => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
698 0 my ( $self, $commands, $options ) = @_;
699 0 _warn_blocking_operation( $runtime, 'std/proc Proc.pipeline' );
700 0 my $items = zuzu_to_perl( $commands );
701 0 ▶ $items = [] if ref($items) ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $items ne "ARRAY" 0 0 0
702 0 my $opts = zuzu_to_perl( $options );
703 0 ▶ $opts = {} if ref($opts) ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $opts ne "HASH" 0 0 0
704  
705 0 return perl_to_zuzu( _run_pipeline_legacy( $items, $opts ) );
706 },
707 9 );
708  
709 $proc_class->static_methods->{pipeline_async} =
710 9 ▶ _platform_supports_true_pipeline_async()
Branch: 1 branch
BranchTrueFalseTotal
_platform_supports_true_pipeline_async() ? : 9 0 9
711 ? _pipeline_async_true_native($runtime)
712 : _pipeline_async_legacy_native($runtime);
713  
714 $proc_class->static_methods->{is_success} = native_function(
715 name => 'is_success',
716 0 ▶ native => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
717 0 my ( $self, $result ) = @_;
718 0 my $perl = zuzu_to_perl($result);
719 0 ▶ return _result_ok($perl) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
_result_ok($perl) ? : 0 0 0
720 },
721 9 );
722  
723 $proc_class->static_methods->{status_text} = native_function(
724 name => 'status_text',
725 0 ▶ native => sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
726 0 my ( $self, $result ) = @_;
727 0 my $perl = zuzu_to_perl($result);
728 0 ▶ return 'invalid result' if ref($perl) ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $perl ne "HASH" 0 0 0
729 0 ▶ if ( defined $perl->{error} and $perl->{error} ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $perl->{"error"} and $perl->{"error"} ne "") 0 0 0
Condition0%0/3
defined $perl->{"error"} and $perl->{"error"} ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $perl->{"error"} and $perl->{"error"} ne ""
defined $perl->{"error"} $perl->{"error"} ne ""
Truth table0%0/3
A and B
A= defined $perl->{"error"}B= $perl->{"error"} ne ""
ABresult
0X0
100
111
730 0 return "error: $perl->{error}";
731 }
732 0 ▶ if ( ( $perl->{signal} // 0 ) != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (($perl->{"signal"} // 0) != 0) 0 0 0
Condition0%0/2
$perl->{"signal"} // 0
l!l
00
MC/DC0%0/1
$perl->{"signal"} // 0
$perl->{"signal"}
Truth table0%0/2
A
A= $perl->{"signal"}
Aresult
11
00
733 0 return "signal $perl->{signal}";
734 }
735 0 return "exit $perl->{exit_code}";
736 },
737 9 );
738  
739 $proc_class->static_methods->{kill} = native_function(
740 name => 'kill',
741 1 ▶ native => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
742 1 my ( $self, $signal_name, $pid ) = @_;
743 1 my $signal = _normalize_signal_name( $signal_name );
744 1 ▶ my $target = defined $pid ? 0 + $pid : 0 + $$;
Branch: 1 branch
BranchTrueFalseTotal
defined $pid ? : 1 0 1
745 1 my $count = CORE::kill( $signal, $target );
746 1 return $count;
747 },
748 9 );
749  
750 $proc_class->static_methods->{onsignal} = native_function(
751 name => 'onsignal',
752 1 ▶ native => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
753 1 my ( $self, $signal_name, $callback ) = @_;
754 1 my $signal = _normalize_signal_name( $signal_name );
755 $SIGNAL_CALLBACKS{$signal} = []
756 1 ▶ if not exists $SIGNAL_CALLBACKS{$signal};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $SIGNAL_CALLBACKS{$signal} 1 0 1
757 1 push @{ $SIGNAL_CALLBACKS{$signal} }, $callback;
758 1 _install_onsignal_handler( $runtime, $signal );
759 1 return scalar @{ $SIGNAL_CALLBACKS{$signal} };
760 },
761 9 );
762  
763 $proc_class->static_methods->{exit} = native_function(
764 name => 'exit',
765 1 ▶ native => sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
766 1 my ( $self, $code ) = @_;
767 1 ▶ my $exit_code = defined $code ? 0 + $code : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $code ? : 1 0 1
768 1 CORE::exit( $exit_code );
769 },
770 9 );
771  
772 my $sleep_fn = native_function(
773 name => 'sleep',
774 0 ▶ native => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
775 0 my ( $seconds ) = @_;
776 0 _warn_blocking_operation( $runtime, 'std/proc sleep' );
777 0 ▶ my $duration = defined $seconds ? 0 + $seconds : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $seconds ? : 0 0 0
778 0 ▶ $duration = 0 if $duration < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $duration < 0 0 0 0
779 0 sleep($duration);
780 0 return undef;
781 },
782 9 );
783  
784 my $sleep_async_fn = native_function(
785 name => 'sleep_async',
786 0 ▶ native => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
787 0 my ( $seconds ) = @_;
788 0 ▶ my $duration = defined $seconds ? 0 + $seconds : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $seconds ? : 0 0 0
789 0 ▶ $duration = 0 if $duration < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $duration < 0 0 0 0
790 0 return $runtime->_new_task(
791 name => 'proc.sleep_async',
792 status => 'sleeping',
793 ready_at => time + $duration,
794 );
795 },
796 9 );
797  
798 return {
799 9 Proc => $proc_class,
800 Env => $env_class,
801 sleep => $sleep_fn,
802 sleep_async => $sleep_async_fn,
803 };
804 }
805  
806 6 1;
807  
808 =pod
809  
810 =head1 NAME
811  
812 Zuzu::Module::Proc - std/proc bindings for ZuzuScript.
813  
814 =head1 DESCRIPTION
815  
816 Implements C<std/proc>, exporting C<Proc>, C<Env>, and C<sleep>.
817  
818 =head1 CLASSES
819  
820 =head2 Env
821  
822 Static helpers for reading and mutating process environment
823 variables.
824  
825 =over
826  
827 =item * C<get(name, default?)>
828  
829 Reads an environment variable. Returns C<default> (or null) when
830 not set.
831  
832 =item * C<set(name, value)>
833  
834 Sets an environment variable and returns the new value.
835  
836 =item * C<remove(name)>
837  
838 Unsets an environment variable.
839  
840 =back
841  
842 =head2 Proc
843  
844 Static helpers for interacting with operating-system processes.
845  
846 =over
847  
848 =item * C<pid>
849  
850 Returns the current process ID.
851  
852  
853 =item * C<run(command, argv?, options?)>
854  
855 Runs an external process with C<IPC::Run>. Returns a Dict with
856 C<command>, C<exit_code>, C<signal>, C<core_dump>, C<ok>,
857 C<stdout>, C<stderr>, and C<error>.
858  
859 C<command> may be a string or an array-style command. C<argv>
860 provides additional arguments when C<command> is a string.
861  
862 Options currently supported:
863  
864 =over
865  
866 =item * C<stdin>
867  
868 =item * C<capture_stdout> (default true)
869  
870 =item * C<capture_stderr> (default true)
871  
872 =item * C<merge_stderr> (default false)
873  
874 =item * C<cwd>
875  
876 =back
877  
878 =item * C<kill(signal, pid?)>
879  
880 Sends a signal. PID defaults to the current process.
881  
882 =item * C<onsignal(signal, callback)>
883  
884 Registers a callback function to run when that signal is received.
885  
886 =item * C<exit(code)>
887  
888 Immediately exits the current process with the provided status code.
889  
890 =back
891  
892 =head2 sleep
893  
894 Function:
895  
896 =over
897  
898 =item * C<sleep(seconds)>
899  
900 Blocks the current process for the requested number of seconds. Fractional
901 seconds are supported.
902  
903 =back
904  
905 =head1 COPYRIGHT AND LICENCE
906  
907 B<< Zuzu::Module::Proc >> is copyright Toby Inkster.
908  
909 It is free software; you may redistribute it and/or modify it under
910 the terms of either the Artistic License 1.0 or the GNU General Public
911 License version 2.
912  
913 =cut
« blib/lib/Zuzu/Module/String/Encode.pm ↑ Summary blib/lib/Zuzu/Module/Marshal.pm »