Callback/CommandRunner.pm untested

filter: Statementstmt 0.0% 0 / 67 Branchbran 0.0% 0 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 104 CC 7 SCAR 17.3
CC
7
Cov
0%
_build_callback
12.0
run_or_die
12.0
_condense
6.0
CRAP 5.6
1 use strict;
2 use warnings;
3  
4 package IPC::Open3::Callback::CommandRunner;
5 $IPC::Open3::Callback::CommandRunner::VERSION = '1.18';
6 # ABSTRACT: A utility class that wraps IPC::Open3::Callback with available output buffers and an option to die on failure instead of returning exit code.
7  
8 use Hash::Util qw(lock_keys);
9 use IPC::Open3::Callback;
10 use IPC::Open3::Callback::CommandFailedException;
11  
12 sub new {
13 return bless( {}, shift )->_init(@_);
14 }
15  
16 sub _build_callback {
17 my ( $self, $out_or_err, $options ) = @_;
18  
19 if ( defined( $options->{ $out_or_err . '_callback' } ) ) {
20 return $options->{ $out_or_err . '_callback' };
21 }
22 elsif ( $options->{ $out_or_err . '_buffer' } ) {
23 $self->{ $out_or_err . '_buffer' } = ();
24 return sub {
25 push( @{ $self->{ $out_or_err . '_buffer' } }, shift );
26 };
27 }
28 return;
29 }
30  
31 sub _clear_buffers {
32 my ($self) = @_;
33 delete( $self->{out_buffer} );
34 delete( $self->{err_buffer} );
35 }
36  
37 sub _condense {
38 my ( $self, $buffer ) = @_;
39  
40 if ( $self->{$buffer} ) {
41 $self->{$buffer} = [ join( '', @{ $self->{$buffer} } ) ];
42 return $self->{$buffer}[0];
43 }
44  
45 return;
46 }
47  
48 sub get_err_buffer {
49 return $_[0]->_condense('err_buffer');
50 }
51  
52 sub _init {
53 my ( $self, @options ) = @_;
54  
55 $self->{command_runner} = IPC::Open3::Callback->new(@options);
56  
57 lock_keys( %{$self}, keys( %{$self} ), 'out_buffer', 'err_buffer' );
58  
59 return $self;
60 }
61  
62 sub _options {
63 my ( $self, %options ) = @_;
64  
65 $options{out_callback} = $self->_build_callback( 'out', \%options );
66 $options{err_callback} = $self->_build_callback( 'err', \%options );
67  
68 return %options;
69 }
70  
71 sub get_out_buffer {
72 return $_[0]->_condense('out_buffer');
73 }
74  
75 sub run {
76 my ( $self, @command ) = @_;
77 my %options = ();
78  
79 # if last arg is hashref, its command options not arg...
80 if ( ref( $command[-1] ) eq 'HASH' ) {
81 %options = $self->_options( %{ pop(@command) } );
82 }
83  
84 $self->_clear_buffers();
85  
86 return $self->{command_runner}->run_command( @command, \%options );
87 }
88  
89 sub run_or_die {
90 my ( $self, @command ) = @_;
91 my %options = ();
92  
93 # if last arg is hashref, its command options not arg...
94 if ( ref( $command[-1] ) eq 'HASH' ) {
95 %options = $self->_options( %{ pop(@command) } );
96 }
97  
98 $self->_clear_buffers();
99  
100 my $exit_code = $self->{command_runner}->run_command( @command, \%options );
101 if ($exit_code) {
102 my $exception = IPC::Open3::Callback::CommandFailedException->new(
103 \@command, $exit_code,
104 scalar( $self->get_out_buffer() ),
105 scalar( $self->get_err_buffer() )
106 );
107 die($exception);
108 }
109  
110 return $self->get_out_buffer();
111 }
112  
113 1;
114  
115 __END__