lib/Rex/Interface/Exec/Local.pm

filter: Statementstmt 92.0% 92 / 100 Branchbran 72.9% 35 / 48 Conditioncond 54.5% 6 / 11 MC/DCmcdc 28.5% 2 / 7 Subroutinesub 100% 15 / 15 Podpod 0.0% 0 / 4 totaltotal 81.0% 150 / 185 CC 29 SCAR 22.1
CC
29
Cov
84%
exec
17.8
_exec
8.4
set_env
4.1
CRAP 9.1
« lib/Rex/Helper/Rexfile/ParamLookup.pm ↑ Summary lib/Rex/Service/NetBSD.pm »
1 #
2 # (c) Jan Gehring <jan.gehring@gmail.com>
3 #
4  
5 package Rex::Interface::Exec::Local;
6  
7 60 ▶use v5.14.4;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 60 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 60 our $VERSION = '1.16.1'; # VERSION
11  
12 60 ▶use Rex::Logger;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 60 ▶use Rex::Commands;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 60 ▶use English qw(-no_match_vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 60 ▶use Symbol 'gensym';
Subroutine BEGIN: called CC=1 SCAR=0.0
17 60 ▶use IPC::Open3;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 60 ▶use IO::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 60 ▶use Rex::Interface::Exec::IOReader;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 # Use 'parent' is recommended, but from Perl 5.10.1 its in core
22 60 ▶use base qw(Rex::Interface::Exec::Base Rex::Interface::Exec::IOReader);
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 2250 ▶sub new {
Subroutine new: called CC=2 SCAR=7.2
Pod undocumented
25 2250 my $that = shift;
26 2250 ▶ my $proto = ref($that) || $that;
Condition33%1/3
ref $that || $that
l!l&&r!l&&!r
022500
MC/DC0%0/2
ref $that || $that
ref $that $that
Truth table33%1/3
A || B
A= ref $thatB= $that
ABresult
1X1
011
000
27 2250 my $self = {@_};
28  
29 2250 bless( $self, $proto );
30  
31 2250 return $self;
32 }
33  
34 49 ▶sub set_env {
Subroutine set_env: called CC=4 SCAR=14.0
Pod undocumented
35 49 my ( $self, $env ) = @_;
36 49 my $cmd = undef;
37  
38 49 ▶ die("Error: env must be a hash")
Branch: 1 branch
BranchTrueFalseTotal
if ref $env ne "HASH" 0 49 49
39 if ( ref $env ne "HASH" );
40  
41 49 ▶ while ( my ( $k, $v ) = each(%$env) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$env) 95 49 144
42 95 ▶ $cmd .= $OSNAME eq 'MSWin32' ? "set $k=$v && " : "export $k='$v'; ";
Branch: 1 branch
BranchTrueFalseTotal
$Rex::Interface::Exec::Local::OSNAME eq 'MSWin32' ? : 0 95 95
43 }
44 49 $self->{env} = $cmd;
45 }
46  
47 1221 ▶sub exec {
Subroutine exec: called CC=16 SCAR=28.8
Pod undocumented
48 1221 my ( $self, $cmd, $path, $option ) = @_;
49  
50 1221 my ( $out, $err, $pid );
51  
52 1221 ▶ if ( exists $option->{cwd} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $option->{"cwd"}) 71 1150 1221
53 71 ▶ my $cd_cmd = $OSNAME eq 'MSWin32' ? 'cd /d' : 'cd';
Branch: 1 branch
BranchTrueFalseTotal
$Rex::Interface::Exec::Local::OSNAME eq 'MSWin32' ? : 0 71 71
54 71 $cmd = "$cd_cmd " . $option->{cwd} . " && $cmd";
55 }
56  
57 1221 ▶ if ( exists $option->{path} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $option->{"path"}) 0 1221 1221
58 0 $path = $option->{path};
59 }
60  
61 1221 ▶ if ( exists $option->{env} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $option->{"env"}) 49 1172 1221
62 49 $self->set_env( $option->{env} );
63 }
64  
65 1221 ▶ if ( exists $option->{format_cmd} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $option->{"format_cmd"}) 0 1221 1221
66 0 $option->{format_cmd} =~ s/\{\{CMD\}\}/$cmd/;
67 0 $cmd = $option->{format_cmd};
68 }
69  
70 1221 Rex::Commands::profiler()->start("exec: $cmd");
71 1221 ▶ if ( $^O !~ m/^MSWin/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $^O =~ /^MSWin/u) { } 1221 0 1221
72 1221 ▶ if ($path) { $path = "PATH=$path" }
Branch: 1 branch
BranchTrueFalseTotal
if ($path) 984 237 1221
73 1221 ▶ $path ||= "";
Condition100%2/2
$path ||= ""
l!l
984237
MC/DC100%1/1
$path ||= ""
$path
Truth table100%2/2
A
A= $path
Aresult
11
00
74  
75 1221 my $new_cmd = "LC_ALL=C $cmd";
76 1221 ▶ if ($path) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path) 984 237 1221
77 984 $new_cmd = "export $path ; $new_cmd";
78 }
79  
80 1221 ▶ if ( $self->{env} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"env"}) 48 1173 1221
81 48 $new_cmd = $self->{env} . " $new_cmd";
82 }
83  
84 1221 ▶ if ( Rex::Config->get_source_global_profile ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("Rex::Config"->get_source_global_profile) 0 1221 1221
85 0 $new_cmd = ". /etc/profile >/dev/null 2>&1; $new_cmd";
86 }
87  
88 1221 $cmd = $new_cmd;
89 }
90 else {
91 0 ▶ if ( $self->{env} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"env"}) 0 0 0
92 0 $cmd = $self->{env} . " $cmd";
93 }
94 }
95  
96 1221 Rex::Logger::debug("Executing: $cmd");
97  
98 1221 ( $out, $err ) = $self->_exec( $cmd, $option );
99  
100 1221 ▶ Rex::Logger::debug($out) if ($out);
Branch: 1 branch
BranchTrueFalseTotal
if $out 604 617 1221
101 1221 ▶ if ($err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 2 1219 1221
102 2 Rex::Logger::debug("========= ERR ============");
103 2 Rex::Logger::debug($err);
104 2 Rex::Logger::debug("========= ERR ============");
105 }
106  
107 1221 Rex::Commands::profiler()->end("exec: $cmd");
108  
109 1221 ▶ if (wantarray) { return ( $out, $err ); }
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 985 236 1221
110  
111 236 return $out;
112 }
113  
114 556 ▶sub can_run {
Subroutine can_run: called CC=3 SCAR=12.5
Pod undocumented
115 556 my ( $self, $commands_to_check, $check_with_command ) = @_;
116  
117 556 ▶ $check_with_command ||= $^O =~ /^MSWin/i ? 'where' : 'command -v';
Branch: 1 branch
BranchTrueFalseTotal
$^O =~ /^MSWin/iu ? : 0 556 556
Condition33%1/3
$check_with_command ||= $^O =~ /^MSWin/iu ? "where" : "command -v"
l!l&&r!l&&!r
05560
MC/DC0%0/2
$check_with_command ||= $^O =~ /^MSWin/iu ? "where" : "command -v"
$check_with_command $^O =~ /^MSWin/iu ? "where" : "command -v"
Truth table33%1/3
A ||= B
A= $check_with_commandB= $^O =~ /^MSWin/iu ? "where" : "command -v"
ABresult
1X1
011
000
118  
119 556 return $self->SUPER::can_run( $commands_to_check, $check_with_command );
120 }
121  
122 1237 ▶sub _exec {
Subroutine _exec: called CC=8 SCAR=21.3
123 1237 my ( $self, $cmd, $option ) = @_;
124  
125 1237 my ( $pid, $writer, $reader, $error, $out, $err );
126 1237 $error = gensym;
127  
128 1237 ▶ if ( $^O !~ m/^MSWin/ && Rex::Config->get_no_tty ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $^O =~ /^MSWin/u and 'Rex::Config'->get_no_tty) { } 2 1235 1237
Condition66%2/3
not $^O =~ /^MSWin/u and "Rex::Config"->get_no_tty
!ll&&!rl&&r
012352
MC/DC50%1/2
not $^O =~ /^MSWin/u and "Rex::Config"->get_no_tty
not $^O =~ /^MSWin/u "Rex::Config"->get_no_tty
Truth table66%2/3
not(A) and B
A= not $^O =~ /^MSWin/uB= "Rex::Config"->get_no_tty
ABresult
0X0
100
111
129 2 $pid = open3( $writer, $reader, $error, $cmd );
130  
131 2 ( $out, $err ) = $self->io_read( $reader, $error, $pid, $option );
132  
133 2 ▶ waitpid( $pid, 0 ) or die($!);
Branch: 1 branch
BranchTrueFalseTotal
waitpid $pid, 0 or die $! 2 0 2
134 }
135 else {
136 1235 ▶ $pid = open( my $fh, "-|", "$cmd 2>&1" ) or die($!);
Branch: 1 branch
BranchTrueFalseTotal
$pid = open(my $fh, "-|", "$cmd 2>&1") or die $! 1233 2 1235
137 1233 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 5229 1233 6462
138 5229 $out .= $_;
139 5229 chomp;
140 $self->execute_line_based_operation( $_, $option )
141 0 ▶ && do { kill( 'KILL', $pid ); last };
Branch: 1 branch
BranchTrueFalseTotal
$self->execute_line_based_operation($_, $option) and do { kill "KILL", $pid; last } 0 5229 5229
142 }
143 1233 ▶ waitpid( $pid, 0 ) or die($!);
Branch: 1 branch
BranchTrueFalseTotal
waitpid $pid, 0 or die $! 1233 0 1233
144  
145 }
146  
147 # we need to bitshift $? so that $? contains the right (and for all
148 # connection methods the same) exit code after a run()/i_run() call.
149 # this is for the user, so that he can query $? in his task.
150 1235 $? >>= 8;
151  
152 1235 return ( $out, $err );
153 }
154  
155 60 1;
« lib/Rex/Helper/Rexfile/ParamLookup.pm ↑ Summary lib/Rex/Service/NetBSD.pm »