| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
length $stdout and my $sub = $self->{"stdout"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%4/6
not $self->{"redirect"} and length $stderr and my $sub = $self->{"stderr"}
not $self->{"redirect"} and length $stderr
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
$self->{"env"} or $self->{"cwd"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | ▶ | my $guard = File::pushd::pushd($self->{cwd}) if $self->{cwd}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | $code->(); | |||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | }; | |||||||||||||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||
| 84 | 16 | ▶ | if (!$self->{timeout}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | 15 | ▶ | my $result = $wrap_code ? $wrap_code->() : $code->(); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 96 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 97 | 1 | $err = $@; | |||||||||||||||||||||||||||||||||||||||||
| 98 | 1 | alarm 0; | |||||||||||||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||||||||||||
| 100 | 1 | ▶ | if (!$err) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | my @cmd = map { Command::Runner::Quote::quote_win32($_) } $command->@*; | |||||||||||||||||||||||||||||||||||||||||
| 114 | 0 | ▶ | local %ENV = $self->{env}->%* if $self->{env}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 115 | 0 | ▶ | my $guard = File::pushd::pushd($self->{cwd}) if $self->{cwd}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 116 | 0 | $pid = system { $command->[0] } 1, @cmd; | |||||||||||||||||||||||||||||||||||||||||
| 117 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | ▶ | local %ENV = $self->{env}->%* if $self->{env}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 119 | 0 | ▶ | my $guard = File::pushd::pushd($self->{cwd}) if $self->{cwd}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition0%0/3
$timeout and my $signal = $timeout->signal
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
MC/DC0%0/2
$timeout && $timeout->signaled
$timeout
$timeout->signaled
Truth table0%0/3
A && B
A= $timeoutB= $timeout->signaled
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 162 | 23 | ▶ | if ($pid == 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 163 | 7 | close $_ for grep $_, $stdout_read, $stderr_read; | |||||||||||||||||||||||||||||||||||||||||
| 164 | 7 | open STDOUT, ">&", $stdout_write; | |||||||||||||||||||||||||||||||||||||||||
| 165 | 7 | ▶ | if ($self->{redirect}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 166 | 1 | open STDERR, ">&", \*STDOUT; | |||||||||||||||||||||||||||||||||||||||||
| 167 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 168 | 6 | open STDERR, ">&", $stderr_write; | |||||||||||||||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||||||||||||||||
| 170 | 7 | ▶ | if ($Config::Config{d_setpgrp}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 171 | 7 | ▶ | POSIX::setpgid(0, 0) or die "setpgid: $!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||||||||
| 174 | 7 | ▶ | if ($self->{cwd}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 175 | 0 | ▶ | chdir $self->{cwd} or die "chdir $self->{cwd}: $!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||||||||||||||||
| 177 | 7 | ▶ | if ($self->{env}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 178 | 0 | %ENV = $self->{env}->%*; | |||||||||||||||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||||||
| 181 | 7 | ▶ | if (ref $command) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 194 | 16 | my $select = IO::Select->new(grep $_, $stdout_read, $stderr_read); | |||||||||||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||||||||||||
| 196 | 16 | ▶ | while ($select->count) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 197 | 819 | ▶ | if ($INT) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 198 | 0 | kill INT => $signal_pid; | |||||||||||||||||||||||||||||||||||||||||
| 199 | 0 | $INT = 0; | |||||||||||||||||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||||||||||||||||
| 201 | 819 | ▶ | if ($timeout and my $signal = $timeout->signal) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$timeout and my $signal = $timeout->signal
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 206 | 50 | my $len = sysread $ready, my $buf, 64*1024; | |||||||||||||||||||||||||||||||||||||||||
| 207 | 50 | ▶ | if ($len) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 208 | 21 | my $buffer = $self->{_buffer}{$type}; | |||||||||||||||||||||||||||||||||||||||||
| 209 | 21 | $buffer->add($buf); | |||||||||||||||||||||||||||||||||||||||||
| 210 | 21 | ▶ | next unless my @line = $buffer->get; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 211 | 21 | ▶ | next unless my $sub = $self->{$type}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 212 | 21 | $sub->($_) for @line; | |||||||||||||||||||||||||||||||||||||||||
| 213 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 214 | 29 | ▶ | warn "sysread $type pipe failed: $!" unless defined $len; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 222 | 26 | ▶ | my $buffer = $self->{_buffer}{$type} or next; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 223 | 23 | ▶ | my @line = $buffer->get(1) or next; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
$timeout && $timeout->signaled
MC/DC50%1/2
$timeout && $timeout->signaled
$timeout
$timeout->signaled
Truth table66%2/3
A && B
A= $timeoutB= $timeout->signaled
| |||||||||||||||||||||||||||||||||||||||||||
| 234 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 235 | 16 | $self->{_buffer} = +{}; # cleanup | |||||||||||||||||||||||||||||||||||||||||
| 236 | 16 | return $res; | |||||||||||||||||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||||||||
| 239 | 10 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 240 | __END__ | ||||||||||||||||||||||||||||||||||||||||||