blib/lib/MCP/Run/Bash.pm

filter: Statementstmt 95.3% 61 / 64 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.5% 86 / 96 CC 11 SCAR 20.2
CC
11
Cov
92%
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 21 our $VERSION = '0.106';
3 21 ▶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 21 ▶use IPC::Open3;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 21 ▶use IO::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 21 ▶use Symbol 'gensym';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 21 ▶use POSIX ':sys_wait_h';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 21 has name => 'mcp-run-bash';
14  
15  
16 21 has version => $VERSION;
17  
18  
19 16 ▶sub execute ($self, $command, $working_directory, $timeout) {
Subroutine execute: called CC=11 SCAR=24.1
Pod documented
20 16 my $full_command = $command;
21 16 ▶ if (defined $working_directory && length $working_directory) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $working_directory and length $working_directory) 2 14 16
Condition66%2/3
defined $working_directory and length $working_directory
!ll&&!rl&&r
1402
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
22 2 my $escaped = $working_directory;
23 2 $escaped =~ s/'/'\\''/g;
24 2 $full_command = "cd '$escaped' && $command";
25 }
26  
27 16 my ($stdout, $stderr) = ('', '');
28 16 my ($exit_code, $error);
29  
30 16 eval {
31 16 my $err = gensym;
32 16 my $pid = open3(my $in, my $out, $err, 'bash', '-c', $full_command);
33 16 close $in;
34  
35 16 my $select = IO::Select->new($out, $err);
36 16 my $timed_out = 0;
37  
38 16 ▶ local $SIG{ALRM} = sub { $timed_out = 1; die "alarm\n" };
Subroutine __ANON__: called CC=1 SCAR=0.0
39 16 alarm($timeout);
40  
41 16 eval {
42 16 ▶ while (my @ready = $select->can_read) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@ready) = $select->can_read) 27 15 42
43 27 for my $fh (@ready) {
44 42 my $buf;
45 42 my $bytes = sysread($fh, $buf, 4096);
46 42 ▶ if (!$bytes) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($bytes) 30 12 42
47 30 $select->remove($fh);
48 30 next;
49 }
50 12 ▶ if ($fh == $out) { $stdout .= $buf }
Branch: 1 branch
BranchTrueFalseTotal
if ($fh == $out) { } 11 1 12
51 1 else { $stderr .= $buf }
52 }
53 }
54 };
55  
56 16 alarm(0);
57  
58 16 ▶ if ($timed_out) {
Branch: 1 branch
BranchTrueFalseTotal
if ($timed_out) { } 1 15 16
59 1 kill 'TERM', $pid;
60 1 waitpid($pid, 0);
61 1 $exit_code = 124;
62 1 $error = "Command timed out after ${timeout}s";
63 }
64 else {
65 15 waitpid($pid, 0);
66 15 $exit_code = $? >> 8;
67 }
68  
69 16 close $out;
70 16 close $err;
71 };
72  
73 16 ▶ if ($@ && !defined $exit_code) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ and not defined $exit_code) 0 16 16
Condition33%1/3
$@ and not defined $exit_code
!ll&&!rl&&r
1600
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
74 0 $exit_code = 1;
75 0 $error = "$@";
76 0 chomp $error;
77 }
78  
79 16 chomp $stdout;
80 16 chomp $stderr;
81  
82 16 ▶ return { exit_code => $exit_code, stdout => $stdout, stderr => $stderr, (defined $error ? (error => $error) : ()) };
Branch: 1 branch
BranchTrueFalseTotal
defined $error ? : 1 15 16
83 }
84  
85  
86  
87 21 1;
88  
89 __END__
« blib/lib/MCP/Run.pm ↑ Summary blib/lib/MCP/Run/Compress/Filters.pm »