| 1 | package Test::Mock::ExternalCommand; | ||||||||||
| 2 | 6 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 6 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 6 | ▶ | use Config; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 6 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 6 | ▶ | use Variable::Expand::AnyLevel qw(expand_variable); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 6 | ▶ | use 5.010; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 6 | our $VERSION = '0.03'; | |||||||||
| 10 | |||||||||||
| 11 | 6 | my $command_registry = {}; | |||||||||
| 12 | 6 | my $command_history = {}; | |||||||||
| 13 | |||||||||||
| 14 | 6 | ▶ | BEGIN { | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.1
| |||||||||||
| 15 | 14 | ▶ | sub _command_and_args { | ||||||||
|
Subroutine
_command_and_args: called CC=1 SCAR=0.0
| |||||||||||
| 16 | 14 | my ( $command, @args ) = @_; | |||||||||
| 17 | 14 | my ( $command_real, @args2 ) = split qr/\s+/, $command; | |||||||||
| 18 | 14 | my @args_real = (@args2, @args); | |||||||||
| 19 | 14 | return ($command_real, @args_real); | |||||||||
| 20 | } | ||||||||||
| 21 | |||||||||||
| 22 | 7 | ▶ | *CORE::GLOBAL::system = sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
| |||||||||||
| 23 | 7 | my ( $command, @args ) = _command_and_args(@_); | |||||||||
| 24 | 7 | ▶ | if ( defined $command_registry->{$command} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 25 | 7 | return $command_registry->{$command}->{system}->(@args); | |||||||||
| 26 | } | ||||||||||
| 27 | 0 | CORE::system(@_); | |||||||||
| 28 | 6 | }; | |||||||||
| 29 | |||||||||||
| 30 | 7 | ▶ | *CORE::GLOBAL::readpipe = sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
| |||||||||||
| 31 | # readpipe receives variable name if variable is used in backquote string ... | ||||||||||
| 32 | # so it is need to expand using Variable::Expand::AnyLevel::expand_variable | ||||||||||
| 33 | 7 | my @new_args = map { expand_variable($_, 1) } @_; | |||||||||
| 34 | 7 | my ( $command, @command_args ) = _command_and_args(@new_args); | |||||||||
| 35 | 7 | ▶ | if ( defined $command_registry->{$command} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 36 | 7 | return $command_registry->{$command}->{readpipe}->(@command_args); | |||||||||
| 37 | } | ||||||||||
| 38 | 0 | CORE::readpipe(@_); | |||||||||
| 39 | 6 | }; | |||||||||
| 40 | } | ||||||||||
| 41 | |||||||||||
| 42 | |||||||||||
| 43 | =head1 NAME | ||||||||||
| 44 | |||||||||||
| 45 | Test::Mock::ExternalCommand - Create mock external-command easily | ||||||||||
| 46 | |||||||||||
| 47 | =head1 SYNOPSIS | ||||||||||
| 48 | |||||||||||
| 49 | use Test::Mock::ExternalCommand; | ||||||||||
| 50 | my $m = Test::Mock::ExternalCommand->new(); | ||||||||||
| 51 | $m->set_command( 'my-command-aaa', 'command-output', 0); | ||||||||||
| 52 | # use 'my-command-aaa' in your test. | ||||||||||
| 53 | |||||||||||
| 54 | =head1 DESCRIPTION | ||||||||||
| 55 | |||||||||||
| 56 | Test::Mock::ExternalCommand enable to make mock-external command in easy way. | ||||||||||
| 57 | |||||||||||
| 58 | =head1 Methods | ||||||||||
| 59 | |||||||||||
| 60 | =cut | ||||||||||
| 61 | |||||||||||
| 62 | =head2 new() | ||||||||||
| 63 | |||||||||||
| 64 | =cut | ||||||||||
| 65 | |||||||||||
| 66 | 9 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 67 | 9 | my ( $class ) = @_; | |||||||||
| 68 | 9 | my $self = { | |||||||||
| 69 | my_commands => {}, | ||||||||||
| 70 | }; | ||||||||||
| 71 | 9 | bless $self, $class; | |||||||||
| 72 | 9 | my $address = $self + 0; | |||||||||
| 73 | 9 | $command_history->{$address} = []; | |||||||||
| 74 | 9 | return $self; | |||||||||
| 75 | } | ||||||||||
| 76 | |||||||||||
| 77 | =head2 set_command( $command_name, $command_output_string, $command_exit_status ) | ||||||||||
| 78 | |||||||||||
| 79 | set mock external command command. | ||||||||||
| 80 | |||||||||||
| 81 | =cut | ||||||||||
| 82 | |||||||||||
| 83 | 12 | ▶ | sub set_command { | ||||||||
|
Subroutine
set_command: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 84 | 12 | my ( $self, $command_name, $command_output, $command_exit_status ) = @_; | |||||||||
| 85 | |||||||||||
| 86 | 12 | ▶ | carp "${command_name}: already defined\n" if ( defined $command_registry->{$command_name} ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 87 | 12 | $self->{my_commands}->{$command_name} = $command_name; | |||||||||
| 88 | |||||||||||
| 89 | 12 | my $address = $self + 0; # address is calculated in this scope avoiding refcount increment | |||||||||
| 90 | |||||||||||
| 91 | 4 | ▶ | $command_registry->{$command_name}->{system} = sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 92 | 4 | my ( @args ) = @_; | |||||||||
| 93 | 4 | push @{ $command_history->{$address} }, [$command_name, @args]; | |||||||||
| 94 | 4 | print $command_output; | |||||||||
| 95 | 4 | return $command_exit_status << 8; | |||||||||
| 96 | 12 | }; | |||||||||
| 97 | |||||||||||
| 98 | 3 | ▶ | $command_registry->{$command_name}->{readpipe} = sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 99 | 3 | my ( @args ) = @_; | |||||||||
| 100 | 3 | push @{ $command_history->{$address} }, [$command_name, @args]; | |||||||||
| 101 | 3 | return $command_output; | |||||||||
| 102 | 12 | }; | |||||||||
| 103 | } | ||||||||||
| 104 | |||||||||||
| 105 | =head2 set_command_by_coderef( $command_name, $command_behavior_subref ) | ||||||||||
| 106 | |||||||||||
| 107 | set mock external command command using subroutine reference(coderef). | ||||||||||
| 108 | |||||||||||
| 109 | =cut | ||||||||||
| 110 | |||||||||||
| 111 | 9 | ▶ | sub set_command_by_coderef { | ||||||||
|
Subroutine
set_command_by_coderef: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 112 | 9 | my ( $self, $command_name, $command_behavior_subref ) = @_; | |||||||||
| 113 | |||||||||||
| 114 | 9 | ▶ | carp "${command_name}: already defined\n" if ( defined $command_registry->{$command_name} ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 115 | 9 | $self->{my_commands}->{$command_name} = $command_name; | |||||||||
| 116 | |||||||||||
| 117 | 9 | my $address = $self + 0; # address is calculated in this scope avoiding refcount increment | |||||||||
| 118 | |||||||||||
| 119 | 3 | ▶ | $command_registry->{$command_name}->{system} = sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 120 | 3 | my ( @args ) = @_; | |||||||||
| 121 | 3 | push @{ $command_history->{$address} }, [$command_name, @args]; | |||||||||
| 122 | 3 | my $ret = $command_behavior_subref->(@args); | |||||||||
| 123 | 3 | return $ret << 8; | |||||||||
| 124 | 9 | }; | |||||||||
| 125 | 4 | ▶ | $command_registry->{$command_name}->{readpipe} = sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 126 | 4 | my ( @args ) = @_; | |||||||||
| 127 | 4 | push @{ $command_history->{$address} }, [$command_name, @args]; | |||||||||
| 128 | 4 | return $command_behavior_subref->(@args); | |||||||||
| 129 | 9 | }; | |||||||||
| 130 | } | ||||||||||
| 131 | |||||||||||
| 132 | =head2 history() | ||||||||||
| 133 | |||||||||||
| 134 | return command history. | ||||||||||
| 135 | |||||||||||
| 136 | =cut | ||||||||||
| 137 | |||||||||||
| 138 | 3 | ▶ | sub history { | ||||||||
|
Subroutine
history: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 139 | 3 | my ( $self ) = @_; | |||||||||
| 140 | 3 | my $address = $self + 0; | |||||||||
| 141 | 3 | return @{ $command_history->{$address} }; | |||||||||
| 142 | } | ||||||||||
| 143 | |||||||||||
| 144 | =head2 reset_history() | ||||||||||
| 145 | |||||||||||
| 146 | reset command history. | ||||||||||
| 147 | |||||||||||
| 148 | =cut | ||||||||||
| 149 | |||||||||||
| 150 | 11 | ▶ | sub reset_history { | ||||||||
|
Subroutine
reset_history: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 151 | 11 | my ( $self ) = @_; | |||||||||
| 152 | 11 | my $address = $self + 0; | |||||||||
| 153 | 11 | $command_history->{$address} = []; | |||||||||
| 154 | } | ||||||||||
| 155 | |||||||||||
| 156 | =head2 commands() | ||||||||||
| 157 | |||||||||||
| 158 | return overridden command names | ||||||||||
| 159 | |||||||||||
| 160 | =cut | ||||||||||
| 161 | |||||||||||
| 162 | 13 | ▶ | sub commands { | ||||||||
|
Subroutine
commands: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 163 | 13 | my ( $self ) = @_; | |||||||||
| 164 | 13 | my @result = sort keys %{ $self->{my_commands} }; | |||||||||
| 165 | 13 | return @result; | |||||||||
| 166 | } | ||||||||||
| 167 | |||||||||||
| 168 | # commands registered in global structure | ||||||||||
| 169 | 4 | ▶ | sub _registered_commands { | ||||||||
|
Subroutine
_registered_commands: called CC=1 SCAR=0.0
| |||||||||||
| 170 | 4 | my @result = sort keys %{ $command_registry }; | |||||||||
| 171 | 4 | return @result; | |||||||||
| 172 | } | ||||||||||
| 173 | |||||||||||
| 174 | 10 | ▶ | sub _unset_all_commands { | ||||||||
|
Subroutine
_unset_all_commands: called CC=2 SCAR=6.9
| |||||||||||
| 175 | 10 | my ( $self ) = @_; | |||||||||
| 176 | 10 | for my $command ( $self->commands() ) { | |||||||||
| 177 | 18 | delete $command_registry->{$command}; | |||||||||
| 178 | } | ||||||||||
| 179 | 10 | $self->{my_commands} = {}; | |||||||||
| 180 | 10 | $self->reset_history(); | |||||||||
| 181 | } | ||||||||||
| 182 | |||||||||||
| 183 | 8 | ▶ | sub DESTROY { | ||||||||
|
Subroutine
DESTROY: called CC=2 SCAR=7.2
| |||||||||||
| 184 | 8 | my ( $self ) = @_; | |||||||||
| 185 | 8 | ▶ | $self->_unset_all_commands() if ( defined $self ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 186 | } | ||||||||||
| 187 | |||||||||||
| 188 | 6 | 1; | |||||||||
| 189 | __END__ | ||||||||||