blib/lib/MCP/Run/Bash.pm

filter: Statementstmt 95.1% 59 / 62 Branchbran 92.8% 13 / 14 Conditioncond 50.0% 3 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 100% 7 / 7 Podpod 100% 1 / 1 totaltotal 89.3% 84 / 94 CC 11 SCAR 20.2
CC
11
Cov
91%
execute
11.2
BEGIN
1.0
BEGIN
1.0
CRAP 7.6
« blib/lib/MCP/Run.pm ↑ Summary blib/lib/MCP/Run/Compress/Filters.pm »
1 package MCP::Run::Bash;
2 3 our $VERSION = '0.103';
3 3 ▶use Mojo::Base 'MCP::Run', -signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 # ABSTRACT: MCP server that executes commands via bash
6  
7  
8 3 ▶use IPC::Open3;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use IO::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Symbol 'gensym';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use POSIX ':sys_wait_h';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 ▶sub execute ($self, $command, $working_directory, $timeout) {
Subroutine execute: called CC=11 SCAR=24.1
Pod documented
14 8 my $full_command = $command;
15 8 ▶ if (defined $working_directory && length $working_directory) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $working_directory and length $working_directory) 2 6 8
Condition66%2/3
defined $working_directory and length $working_directory
!ll&&!rl&&r
602
MC/DC50%1/2
defined $working_directory and length $working_directory
defined $working_directory length $working_directory
Truth table66%2/3
A and B
A= defined $working_directoryB= length $working_directory
ABresult
0X0
100
111
16 2 my $escaped = $working_directory;
17 2 $escaped =~ s/'/'\\''/g;
18 2 $full_command = "cd '$escaped' && $command";
19 }
20  
21 8 my ($stdout, $stderr) = ('', '');
22 8 my ($exit_code, $error);
23  
24 8 eval {
25 8 my $err = gensym;
26 8 my $pid = open3(my $in, my $out, $err, 'bash', '-c', $full_command);
27 8 close $in;
28  
29 8 my $select = IO::Select->new($out, $err);
30 8 my $timed_out = 0;
31  
32 8 ▶ local $SIG{ALRM} = sub { $timed_out = 1; die "alarm\n" };
Subroutine __ANON__: called CC=1 SCAR=0.0
33 8 alarm($timeout);
34  
35 8 eval {
36 8 ▶ while (my @ready = $select->can_read) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@ready) = $select->can_read) 15 7 22
37 15 for my $fh (@ready) {
38 20 my $buf;
39 20 my $bytes = sysread($fh, $buf, 4096);
40 20 ▶ if (!$bytes) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($bytes) 14 6 20
41 14 $select->remove($fh);
42 14 next;
43 }
44 6 ▶ if ($fh == $out) { $stdout .= $buf }
Branch: 1 branch
BranchTrueFalseTotal
if ($fh == $out) { } 5 1 6
45 1 else { $stderr .= $buf }
46 }
47 }
48 };
49  
50 8 alarm(0);
51  
52 8 ▶ if ($timed_out) {
Branch: 1 branch
BranchTrueFalseTotal
if ($timed_out) { } 1 7 8
53 1 kill 'TERM', $pid;
54 1 waitpid($pid, 0);
55 1 $exit_code = 124;
56 1 $error = "Command timed out after ${timeout}s";
57 }
58 else {
59 7 waitpid($pid, 0);
60 7 $exit_code = $? >> 8;
61 }
62  
63 8 close $out;
64 8 close $err;
65 };
66  
67 8 ▶ if ($@ && !defined $exit_code) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ and not defined $exit_code) 0 8 8
Condition33%1/3
$@ and not defined $exit_code
!ll&&!rl&&r
800
MC/DC0%0/2
$@ and not defined $exit_code
$@ not defined $exit_code
Truth table33%1/3
A and not(B)
A= $@B= not defined $exit_code
ABresult
0X0
100
111
68 0 $exit_code = 1;
69 0 $error = "$@";
70 0 chomp $error;
71 }
72  
73 8 chomp $stdout;
74 8 chomp $stderr;
75  
76 8 ▶ return { exit_code => $exit_code, stdout => $stdout, stderr => $stderr, (defined $error ? (error => $error) : ()) };
Branch: 1 branch
BranchTrueFalseTotal
defined $error ? : 1 7 8
77 }
78  
79  
80  
81 3 1;
82  
83 __END__
« blib/lib/MCP/Run.pm ↑ Summary blib/lib/MCP/Run/Compress/Filters.pm »