Runner.pm

filter: Statementstmt 75.5% 139 / 184 Branchbran 53.1% 51 / 96 Conditioncond 45.8% 11 / 24 MC/DCmcdc 26.6% 4 / 15 Subroutinesub 81.4% 22 / 27 Podpod 100% 2 / 2 totaltotal 65.8% 229 / 348 CC 66 SCAR 38.3
CC
66
Cov
66%
_system_win32
182.0
_exec
45.8
_run_code
12.2
CRAP 46.0
↑ Summary Runner/LineBuffer.pm »
1 package Command::Runner v1.0.0;
2 10 ▶use v5.24;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use experimental qw(lexical_subs signatures);
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 10 our $TRIAL = 0;
7  
8 10 ▶use Capture::Tiny ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 10 ▶use Command::Runner::LineBuffer;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 10 ▶use Command::Runner::Quote ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 10 ▶use Command::Runner::Timeout;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 10 ▶use Config ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 10 ▶use File::pushd ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 10 ▶use IO::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 10 ▶use POSIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 10 ▶use Time::HiRes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 10 ▶use constant WIN32 => $^O eq 'MSWin32';
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 10 our $TICK = 0.02;
21  
22 39 ▶sub new ($class, %argv) {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
23 39 bless { keep => 1, _buffer => {}, %argv }, $class;
24 }
25  
26 10 for my $attr (qw(command cwd redirect timeout keep stdout stderr env)) {
27 10 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
28 106 ▶ *$attr = sub ($self, $value) {
Subroutine __ANON__: called CC=1 SCAR=0.0
29 106 $self->{$attr} = $value;
30 106 $self;
31 80 };
32 }
33  
34 39 ▶sub run ($self) {
Subroutine run: called CC=2 SCAR=7.0
Pod documented
35 39 my $command = $self->{command};
36 39 ▶ if (ref $command eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $command eq 'CODE') { } 16 23 39
37 16 ▶ $self->_wrap(sub (@) { $self->_run_code($command) });
Subroutine __ANON__: called CC=1 SCAR=0.0
38 } elsif (WIN32) {
39 0 ▶ $self->_wrap(sub (@) { $self->_system_win32($command) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
40 } else {
41 23 $self->_exec($command);
42 }
43 }
44  
45 16 ▶sub _wrap ($self, $code) {
Subroutine _wrap: called CC=10 SCAR=23.4
46  
47 16 my ($stdout, $stderr, $res);
48 16 ▶ if ($self->{redirect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'redirect'}) { } 5 11 16
49 5 ($stdout, $res) = &Capture::Tiny::capture_merged($code);
50 } else {
51 11 ($stdout, $stderr, $res) = &Capture::Tiny::capture($code);
52 }
53  
54 16 ▶ if (length $stdout and my $sub = $self->{stdout}) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $stdout and my $sub = $self->{"stdout"}) 16 0 16
Condition33%1/3
length $stdout and my $sub = $self->{"stdout"}
!ll&&!rl&&r
0016
MC/DC0%0/2
length $stdout and my $sub = $self->{"stdout"}
length $stdout my $sub = $self->{"stdout"}
Truth table33%1/3
A and B
A= length $stdoutB= my $sub = $self->{"stdout"}
ABresult
0X0
100
111
55 16 my $buffer = Command::Runner::LineBuffer->new(buffer => $stdout);
56 16 my @line = $buffer->get(1);
57 16 $sub->($_) for @line;
58 }
59 16 ▶ if (!$self->{redirect} and length $stderr and my $sub = $self->{stderr}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"redirect"} and length $stderr and my $sub = $self->{"stderr"}) 11 5 16
Condition66%4/6
not $self->{"redirect"} and length $stderr and my $sub = $self->{"stderr"}
!ll&&!rl&&r
5011
not $self->{"redirect"} and length $stderr
!ll&&!rl&&r
5011
MC/DC33%1/3
not $self->{"redirect"} and length $stderr and my $sub = $self->{"stderr"}
not $self->{"redirect"} length $stderr my $sub = $self->{"stderr"}
Truth table50%2/4
not(A) and B and C
A= not $self->{"redirect"}B= length $stderrC= my $sub = $self->{"stderr"}
ABCresult
0XX0
10X0
1100
1111
60 11 my $buffer = Command::Runner::LineBuffer->new(buffer => $stderr);
61 11 my @line = $buffer->get(1);
62 11 $sub->($_) for @line;
63 }
64  
65 16 ▶ if ($self->{keep}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"keep"}) 13 3 16
66 13 $res->{stdout} = $stdout;
67 13 $res->{stderr} = $stderr;
68 }
69  
70 16 return $res;
71 }
72  
73 16 ▶sub _run_code ($self, $code) {
Subroutine _run_code: called CC=8 SCAR=25.1
74  
75 16 my $wrap_code;
76 16 ▶ if ($self->{env} || $self->{cwd}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"env"} or $self->{"cwd"}) 0 16 16
Condition33%1/3
$self->{"env"} or $self->{"cwd"}
l!l&&r!l&&!r
0016
MC/DC0%0/2
$self->{"env"} or $self->{"cwd"}
$self->{"env"} $self->{"cwd"}
Truth table33%1/3
A or B
A= $self->{"env"}B= $self->{"cwd"}
ABresult
1X1
011
000
77 0 ▶ $wrap_code = sub (@) {
Subroutine __ANON__: not called CC=3 SCAR=24.8
78 0 ▶ local %ENV = $self->{env}->%* if $self->{env};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"env"} 0 0 0
79 0 ▶ my $guard = File::pushd::pushd($self->{cwd}) if $self->{cwd};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"cwd"} 0 0 0
80 0 $code->();
81 0 };
82 }
83  
84 16 ▶ if (!$self->{timeout}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"timeout"}) 15 1 16
85 15 ▶ my $result = $wrap_code ? $wrap_code->() : $code->();
Branch: 1 branch
BranchTrueFalseTotal
$wrap_code ? : 0 15 15
86 15 return { pid => $$, result => $result };
87 }
88  
89 1 my ($result, $err);
90 {
91 1 local $SIG{__DIE__} = 'DEFAULT';
92 1 ▶ local $SIG{ALRM} = sub (@) { die "__TIMEOUT__\n" };
Subroutine __ANON__: called CC=1 SCAR=0.0
93 1 eval {
94 1 alarm $self->{timeout};
95 1 ▶ $result = $wrap_code ? $wrap_code->() : $code->();
Branch: 1 branch
BranchTrueFalseTotal
$wrap_code ? : 0 1 1
96 };
97 1 $err = $@;
98 1 alarm 0;
99 }
100 1 ▶ if (!$err) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $err) { } 0 1 1
elsif ($err eq "__TIMEOUT__\n") { } 1 0 1
101 0 return { pid => $$, result => $result, };
102 } elsif ($err eq "__TIMEOUT__\n") {
103 1 return { pid => $$, result => $result, timeout => 1 };
104 } else {
105 0 die $err;
106 }
107 }
108  
109 0 ▶sub _system_win32 ($self, $command) {
Subroutine _system_win32: not called CC=13 SCAR=52.0
110  
111 0 my $pid;
112 0 ▶ if (ref $command) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $command) { } 0 0 0
113 0 my @cmd = map { Command::Runner::Quote::quote_win32($_) } $command->@*;
114 0 ▶ local %ENV = $self->{env}->%* if $self->{env};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"env"} 0 0 0
115 0 ▶ my $guard = File::pushd::pushd($self->{cwd}) if $self->{cwd};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"cwd"} 0 0 0
116 0 $pid = system { $command->[0] } 1, @cmd;
117 } else {
118 0 ▶ local %ENV = $self->{env}->%* if $self->{env};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"env"} 0 0 0
119 0 ▶ my $guard = File::pushd::pushd($self->{cwd}) if $self->{cwd};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"cwd"} 0 0 0
120 0 $pid = system 1, $command;
121 }
122  
123 0 ▶ my $timeout = $self->{timeout} ? Command::Runner::Timeout->new($self->{timeout}, 1) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'timeout'} ? : 0 0 0
124 0 ▶ my $INT; local $SIG{INT} = sub (@) { $INT++ };
Subroutine __ANON__: not called CC=1 SCAR=6.9
125 0 my $result;
126 0 while (1) {
127 0 ▶ if ($INT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($INT) 0 0 0
128 0 kill INT => $pid;
129 0 $INT = 0;
130 }
131  
132 0 my $res = waitpid $pid, POSIX::WNOHANG();
133 0 ▶ if ($res == -1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($res == -1) { } 0 0 0
elsif ($res > 0) { } 0 0 0
134 0 warn "waitpid($pid, POSIX::WNOHANG()) returns unexpectedly -1";
135 0 last;
136 } elsif ($res > 0) {
137 0 $result = $?;
138 0 last;
139 } else {
140 0 ▶ if ($timeout and my $signal = $timeout->signal) {
Branch: 1 branch
BranchTrueFalseTotal
if ($timeout and my $signal = $timeout->signal) 0 0 0
Condition0%0/3
$timeout and my $signal = $timeout->signal
!ll&&!rl&&r
000
MC/DC0%0/2
$timeout and my $signal = $timeout->signal
$timeout my $signal = $timeout->signal
Truth table0%0/3
A and B
A= $timeoutB= my $signal = $timeout->signal
ABresult
0X0
100
111
141 0 kill $signal => $pid;
142 }
143 0 Time::HiRes::sleep($TICK);
144 }
145 }
146 0 ▶ return { pid => $pid, result => $result, timeout => $timeout && $timeout->signaled };
Condition0%0/3
$timeout && $timeout->signaled
!ll&&!rl&&r
000
MC/DC0%0/2
$timeout && $timeout->signaled
$timeout $timeout->signaled
Truth table0%0/3
A && B
A= $timeoutB= $timeout->signaled
ABresult
0X0
100
111
147 }
148  
149 23 ▶sub _exec ($self, $command) {
Subroutine _exec: called CC=35 SCAR=38.3
150  
151 23 pipe my $stdout_read, my $stdout_write;
152 23 $self->{_buffer}{stdout} = Command::Runner::LineBuffer->new(keep => $self->{keep});
153  
154 23 my ($stderr_read, $stderr_write);
155 23 ▶ if (!$self->{redirect}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"redirect"}) 19 4 23
156 19 pipe $stderr_read, $stderr_write;
157 19 $self->{_buffer}{stderr} = Command::Runner::LineBuffer->new(keep => $self->{keep});
158 }
159  
160 23 my $pid = fork;
161 23 ▶ die "fork: $!" unless defined $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pid 0 23 23
162 23 ▶ if ($pid == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid == 0) 7 16 23
163 7 close $_ for grep $_, $stdout_read, $stderr_read;
164 7 open STDOUT, ">&", $stdout_write;
165 7 ▶ if ($self->{redirect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'redirect'}) { } 1 6 7
166 1 open STDERR, ">&", \*STDOUT;
167 } else {
168 6 open STDERR, ">&", $stderr_write;
169 }
170 7 ▶ if ($Config::Config{d_setpgrp}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Config::Config{"d_setpgrp"}) 7 0 7
171 7 ▶ POSIX::setpgid(0, 0) or die "setpgid: $!";
Branch: 1 branch
BranchTrueFalseTotal
POSIX::setpgid(0, 0) or die "setpgid: $!" 7 0 7
172 }
173  
174 7 ▶ if ($self->{cwd}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"cwd"}) 0 7 7
175 0 ▶ chdir $self->{cwd} or die "chdir $self->{cwd}: $!";
Branch: 1 branch
BranchTrueFalseTotal
chdir $self->{"cwd"} or die qq[chdir $self->{"cwd"}: $!] 0 0 0
176 }
177 7 ▶ if ($self->{env}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"env"}) 0 7 7
178 0 %ENV = $self->{env}->%*;
179 }
180  
181 7 ▶ if (ref $command) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $command) { } 7 0 7
182 7 exec { $command->[0] } $command->@*;
183 } else {
184 0 exec $command;
185 }
186 0 exit 127;
187 }
188 16 close $_ for grep $_, $stdout_write, $stderr_write;
189  
190 16 ▶ my $signal_pid = $Config::Config{d_setpgrp} ? -$pid : $pid;
Branch: 1 branch
BranchTrueFalseTotal
$Config::Config{'d_setpgrp'} ? : 16 0 16
191  
192 0 ▶ my $INT; local $SIG{INT} = sub (@) { $INT++ };
Subroutine __ANON__: not called CC=1 SCAR=0.4
193 16 ▶ my $timeout = $self->{timeout} ? Command::Runner::Timeout->new($self->{timeout}, 1) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'timeout'} ? : 4 12 16
194 16 my $select = IO::Select->new(grep $_, $stdout_read, $stderr_read);
195  
196 16 ▶ while ($select->count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($select->count) 819 16 835
197 819 ▶ if ($INT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($INT) 0 819 819
198 0 kill INT => $signal_pid;
199 0 $INT = 0;
200 }
201 819 ▶ if ($timeout and my $signal = $timeout->signal) {
Branch: 1 branch
BranchTrueFalseTotal
if ($timeout and my $signal = $timeout->signal) 4 815 819
Condition100%3/3
$timeout and my $signal = $timeout->signal
!ll&&!rl&&r
6911244
MC/DC100%2/2
$timeout and my $signal = $timeout->signal
$timeout my $signal = $timeout->signal
Truth table100%3/3
A and B
A= $timeoutB= my $signal = $timeout->signal
ABresult
0X0
100
111
202 4 kill $signal => $signal_pid;
203 }
204 819 for my $ready ($select->can_read($TICK)) {
205 50 ▶ my $type = $ready == $stdout_read ? "stdout" : "stderr";
Branch: 1 branch
BranchTrueFalseTotal
$ready == $stdout_read ? : 28 22 50
206 50 my $len = sysread $ready, my $buf, 64*1024;
207 50 ▶ if ($len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len) { } 21 29 50
208 21 my $buffer = $self->{_buffer}{$type};
209 21 $buffer->add($buf);
210 21 ▶ next unless my @line = $buffer->get;
Branch: 1 branch
BranchTrueFalseTotal
unless my(@line) = $buffer->get 0 21 21
211 21 ▶ next unless my $sub = $self->{$type};
Branch: 1 branch
BranchTrueFalseTotal
unless my $sub = $self->{$type} 0 21 21
212 21 $sub->($_) for @line;
213 } else {
214 29 ▶ warn "sysread $type pipe failed: $!" unless defined $len;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $len 0 29 29
215 29 $select->remove($ready);
216 29 close $ready;
217 }
218 }
219 }
220 16 for my $type (qw(stdout stderr)) {
221 32 ▶ next unless my $sub = $self->{$type};
Branch: 1 branch
BranchTrueFalseTotal
unless my $sub = $self->{$type} 6 26 32
222 26 ▶ my $buffer = $self->{_buffer}{$type} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $buffer = $self->{"_buffer"}{$type} or next 23 3 26
223 23 ▶ my @line = $buffer->get(1) or next;
Branch: 1 branch
BranchTrueFalseTotal
my(@line) = $buffer->get(1) or next 0 23 23
224 0 $sub->($_) for @line;
225 }
226 16 close $_ for $select->handles;
227 16 waitpid $pid, 0;
228 my $res = {
229 pid => $pid,
230 result => $?,
231 timeout => $timeout && $timeout->signaled,
232 stdout => $self->{_buffer}{stdout} ? $self->{_buffer}{stdout}->raw : "",
233 16 ▶ stderr => $self->{_buffer}{stderr} ? $self->{_buffer}{stderr}->raw : "",
Branch: 2 branches
BranchTrueFalseTotal
$self->{'_buffer'}{'stdout'} ? : 16 0 16
$self->{'_buffer'}{'stderr'} ? : 13 3 16
Condition66%2/3
$timeout && $timeout->signaled
!ll&&!rl&&r
1204
MC/DC50%1/2
$timeout && $timeout->signaled
$timeout $timeout->signaled
Truth table66%2/3
A && B
A= $timeoutB= $timeout->signaled
ABresult
0X0
100
111
234 };
235 16 $self->{_buffer} = +{}; # cleanup
236 16 return $res;
237 }
238  
239 10 1;
240 __END__
↑ Summary Runner/LineBuffer.pm »