Cmd/Tester.pm

filter: Statementstmt 89.0% 81 / 91 Branchbran 73.6% 28 / 38 Conditioncond 52.9% 9 / 17 MC/DCmcdc 22.2% 2 / 9 Subroutinesub 84.2% 16 / 19 Podpod 100% 3 / 3 totaltotal 78.5% 139 / 177 CC 26 SCAR 19.5
CC
26
Cov
81%
__ANON__
14.1
test_cmd_ok
7.1
__ANON__
6.0
CRAP 7.0
↑ Summary Cmd/Role.pm »
1 package MooX::Cmd::Tester;
2  
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 6 our $VERSION = "0.016_001";
7  
8 6 require Exporter;
9 6 ▶use Test::More import => ['!pass'];
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use Package::Stash;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use Capture::Tiny qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 6 ▶use parent qw(Test::Builder::Module Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 6 our @EXPORT = qw(test_cmd test_cmd_ok);
16 6 our @EXPORT_OK = qw(test_cmd test_cmd_ok);
17  
18 6 our $TEST_IN_PROGRESS;
19 6 my $CLASS = __PACKAGE__;
20  
21 BEGIN
22 {
23 *CORE::GLOBAL::exit = sub {
24 0 ▶ return CORE::exit(@_) unless $TEST_IN_PROGRESS;
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
unless $MooX::Cmd::Tester::TEST_IN_PROGRESS 0 0 0
25 0 MooX::Cmd::Tester::Exited->throw($_[0]);
26 6 ▶ };
Subroutine BEGIN: called CC=1 SCAR=0.0
27 }
28  
29 28 ▶sub result_class { 'MooX::Cmd::Tester::Result' }
Subroutine result_class: called CC=1 SCAR=0.0
Pod documented
30  
31 sub test_cmd
32 {
33 28 ▶ my ($app, $argv) = @_;
Subroutine test_cmd: called CC=5 SCAR=16.8
Pod documented
34  
35 28 my $result = _run_with_capture($app, $argv);
36 28 ▶ my $exit_code = defined $result->{error} ? ((0 + $!) || -1) : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $result->{'error'} ? : 3 25 28
Condition50%1/2
0 + $! or -1
l!l
03
MC/DC0%0/1
0 + $! or -1
0 + $!
Truth table50%1/2
A
A= 0 + $!
Aresult
11
00
37  
38 $result->{error}
39 3 and eval { $result->{error}->isa('MooX::Cmd::Tester::Exited') }
40 28 ▶ and $exit_code = ${$result->{error}};
Branch: 1 branch
BranchTrueFalseTotal
$result->{"error"} and eval { do { $result->{"error"}->isa("MooX::Cmd::Tester::Exited") } } and $exit_code = ${$result->{"error"};} 0 28 28
Condition66%2/3
$result->{"error"} and eval { do { $result->{"error"}->isa("MooX::Cmd::Tester::Exited") } }
!ll&&!rl&&r
2530
MC/DC0%0/2
$result->{"error"} and eval { do { $result->{"error"}->isa("MooX::Cmd::Tester::Exited") } }
$result->{"error"} eval { do { $result->{"error"}->isa("MooX::Cmd::Tester::Exited") } }
Truth table66%2/3
A and B
A= $result->{"error"}B= eval { do { $result->{"error"}->isa("MooX::Cmd::Tester::Exited") } }
ABresult
0X0
100
111
41  
42 28 result_class->new(
43 {
44 exit_code => $exit_code,
45 %$result,
46 }
47 );
48 }
49  
50 sub test_cmd_ok
51 {
52 16 ▶ my $rv = test_cmd(@_);
Subroutine test_cmd_ok: called CC=6 SCAR=19.6
Pod documented
53  
54 16 my $test_ident = $rv->app . " => [ " . join(" ", @{$_[1]}) . " ]";
55 16 ▶ ok(!$rv->error, "Everythink ok running cmd $test_ident") or diag($rv->error);
Branch: 1 branch
BranchTrueFalseTotal
&ok(!$rv->error, "Everythink ok running cmd $test_ident") or diag($rv->error) 0 16 16
56 # no error and cmd means, we're reasonable successful so far
57 $rv
58 and not $rv->error
59 and $rv->cmd
60 and $rv->cmd->command_name
61 16 ▶ and ok($rv->cmd->command_commands->{$rv->cmd->command_name}, "found command at $test_ident");
Branch: 1 branch
BranchTrueFalseTotal
$rv and not $rv->error and $rv->cmd and $rv->cmd->command_name and &ok($rv->cmd->command_commands->{$rv->cmd->command_name}, "found command at $test_ident") 10 6 16
Condition44%4/9
$rv and not $rv->error and $rv->cmd and $rv->cmd->command_name
!ll&&!rl&&r
0610
$rv and not $rv->error and $rv->cmd
!ll&&!rl&&r
0016
$rv and not $rv->error
!ll&&!rl&&r
0016
MC/DC25%1/4
$rv and not $rv->error and $rv->cmd and $rv->cmd->command_name
$rv not $rv->error $rv->cmd $rv->cmd->command_name
Truth table40%2/5
A and not(B) and C and D
A= $rvB= not $rv->errorC= $rv->cmdD= $rv->cmd->command_name
ABCDresult
0XXX0
10XX0
110X0
11100
11111
62  
63 16 $rv;
64 }
65  
66 ## no critic qw(ProhibitSubroutinePrototypes)
67 sub _capture_merged(&)
68 {
69 28 ▶ my $code = shift;
Subroutine _capture_merged: called CC=2 SCAR=7.6
70 28 my ($stdout, $stderr, $merged, $ok);
71 28 ▶ if ($^O eq 'MSWin32')
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq 'MSWin32') { } 0 28 28
72 {
73 0 ▶ ($stdout, $stderr, $ok) = tee { $code->(); };
Subroutine __ANON__: not called CC=1 SCAR=6.9
74 0 $merged = $stdout . $stderr;
75 }
76 else
77 {
78 ($merged) = tee_merged
79 {
80 28 ▶ ($stdout, $stderr, $ok) = tee { $code->() };
Subroutine __ANON__: called CC=1 SCAR=0.0
81 28 };
82 }
83 28 ($stdout, $stderr, $merged, $ok);
84 }
85  
86 sub _run_with_capture
87 {
88 28 ▶ my ($app, $argv) = @_;
Subroutine _run_with_capture: called CC=1 SCAR=0.0
89  
90 28 my ($execute_rv, $cmd, $cmd_name, $error);
91  
92 my ($stdout, $stderr, $merged, $ok) = _capture_merged
93 {
94 28 ▶ eval {
Subroutine __ANON__: called CC=14 SCAR=26.5
Branch: 1 branch
BranchTrueFalseTotal
eval { do { local $MooX::Cmd::Tester::TEST_IN_PROGRESS = 1; local(@ARGV) = @$argv; my $tb = $CLASS->builder; $cmd = ref $app ? $app : $app->new_with_cmd; ref $app and $app = ref $app; my $test_ident = "$app => [ " . join(" ", @$argv) . ' ]'; &ok(scalar $cmd->isa($app), "got a '${app}' from new_with_cmd"); @$argv and defined($cmd_name = $cmd->command_name) and &ok(scalar grep({index($cmd_name, $_) != -1;} @$argv), "proper cmd name from $test_ident"); &ok(scalar @{$cmd->command_chain;} <= 1 + scalar(@$argv), "\$#argv vs. command chain length testing $test_ident"); @$argv and &ok($cmd->command_chain_end == $cmd->command_chain->[-1], "command_chain_end ok"); unless ($execute_rv = $cmd->execute_return) { my($command_execute_from_new, $command_execute_method_name); my $cce = $cmd->can("command_chain_end"); $cce and $cce = &$cce($cmd); $cce and $command_execute_from_new = $cce->can("command_execute_from_new"); $command_execute_from_new and $command_execute_from_new = &$command_execute_from_new($cce); $command_execute_from_new or $command_execute_method_name = $cce->can("command_execute_method_name"); $command_execute_method_name and $execute_rv = [$cce->can(&$command_execute_method_name($cce))->($cce)]; } ; 1 } } or $error = 1 3 25 28
95 28 local $TEST_IN_PROGRESS = 1;
96 28 local @ARGV = @$argv;
97  
98 28 my $tb = $CLASS->builder();
99  
100 28 ▶ $cmd = ref $app ? $app : $app->new_with_cmd;
Branch: 1 branch
BranchTrueFalseTotal
ref $app ? : 8 20 28
101 25 ▶ ref $app and $app = ref $app;
Branch: 1 branch
BranchTrueFalseTotal
ref $app and $app = ref $app 8 17 25
102 25 my $test_ident = "$app => [ " . join(" ", @$argv) . " ]";
103 25 ok($cmd->isa($app), "got a '$app' from new_with_cmd");
104 @$argv
105 and defined($cmd_name = $cmd->command_name)
106 25 ▶ and ok((grep { index($cmd_name, $_) != -1 } @$argv), "proper cmd name from $test_ident");
Branch: 1 branch
BranchTrueFalseTotal
@$argv and defined($cmd_name = $cmd->command_name) and &ok(scalar grep({index($cmd_name, $_) != -1;} @$argv), "proper cmd name from $test_ident") 16 9 25
Condition66%2/3
@$argv and defined($cmd_name = $cmd->command_name)
!ll&&!rl&&r
9016
MC/DC50%1/2
@$argv and defined($cmd_name = $cmd->command_name)
@$argv defined($cmd_name = $cmd->command_name)
Truth table66%2/3
A and B
A= @$argvB= defined($cmd_name = $cmd->command_name)
ABresult
0X0
100
111
107 25 ok(scalar @{$cmd->command_chain} <= 1 + scalar @$argv, "\$#argv vs. command chain length testing $test_ident");
108 25 ▶ @$argv and ok($cmd->command_chain_end == $cmd->command_chain->[-1], "command_chain_end ok");
Branch: 1 branch
BranchTrueFalseTotal
@$argv and &ok($cmd->command_chain_end == $cmd->command_chain->[-1], "command_chain_end ok") 16 9 25
109  
110 25 ▶ unless ($execute_rv = $cmd->execute_return)
Branch: 1 branch
BranchTrueFalseTotal
unless ($execute_rv = $cmd->execute_return) 9 16 25
111 {
112 9 my ($command_execute_from_new, $command_execute_method_name);
113 9 my $cce = $cmd->can("command_chain_end");
114 9 ▶ $cce and $cce = $cce->($cmd);
Branch: 1 branch
BranchTrueFalseTotal
$cce and $cce = &$cce($cmd) 9 0 9
115 9 ▶ $cce and $command_execute_from_new = $cce->can("command_execute_from_new");
Branch: 1 branch
BranchTrueFalseTotal
$cce and $command_execute_from_new = $cce->can("command_execute_from_new") 9 0 9
116 9 ▶ $command_execute_from_new and $command_execute_from_new = $command_execute_from_new->($cce);
Branch: 1 branch
BranchTrueFalseTotal
$command_execute_from_new and $command_execute_from_new = &$command_execute_from_new($cce) 6 3 9
117 9 ▶ $command_execute_from_new or $command_execute_method_name = $cce->can('command_execute_method_name');
Branch: 1 branch
BranchTrueFalseTotal
$command_execute_from_new or $command_execute_method_name = $cce->can("command_execute_method_name") 9 0 9
118 9 ▶ $command_execute_method_name
Branch: 1 branch
BranchTrueFalseTotal
$command_execute_method_name and $execute_rv = [$cce->can(&$command_execute_method_name($cce))->($cce)] 6 3 9
119 and $execute_rv = [$cce->can($command_execute_method_name->($cce))->($cce)];
120 }
121 25 1;
122 } or $error = 1;
123 28 ▶ $@ and $error = $@;
Branch: 1 branch
BranchTrueFalseTotal
$@ and $error = $@ 3 25 28
124 28 };
125  
126 return {
127 28 app => $app,
128 cmd => $cmd,
129 stdout => $stdout,
130 stderr => $stderr,
131 output => $merged,
132 error => $error,
133 execute_rv => $execute_rv,
134 };
135 }
136  
137 {
138 ## no critic qw(ProhibitMultiplePackages)
139 package # no-index
140 MooX::Cmd::Tester::Result;
141  
142 sub new
143 {
144 28 ▶ my ($class, $arg) = @_;
Subroutine new: called CC=1 SCAR=0.0
145 28 bless $arg => $class;
146 }
147 }
148  
149 6 my $res = Package::Stash->new("MooX::Cmd::Tester::Result");
150 6 for my $attr (qw(app cmd stdout stderr output error execute_rv exit_code))
151 {
152 158 ▶ $res->add_symbol('&' . $attr, sub { $_[0]->{$attr} });
Subroutine __ANON__: called CC=1 SCAR=0.0
153 }
154  
155 {
156 ## no critic qw(ProhibitMultiplePackages)
157 package # no-index
158 MooX::Cmd::Tester::Exited;
159  
160 sub throw
161 {
162 0 ▶ my ($class, $code) = @_;
Subroutine throw: not called CC=2 SCAR=17.9
163 0 ▶ defined $code or $code = 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $code or $code = 0 0 0 0
164 0 my $self = (bless \$code => $class);
165 ## no critic qw(RequireCarping)
166 0 die $self;
167 }
168 }
169  
170 =head1 NAME
171  
172 MooX::Cmd::Tester - MooX cli app commands tester
173  
174 =head1 SYNOPSIS
175  
176 use MooX::Cmd::Tester;
177 use Test::More;
178  
179 use MyFoo;
180  
181 # basic tests as instance check, initialization check etc. is done there
182 my $rv = test_cmd( MyFoo => [ command(s) option(s) ] );
183  
184 like( $rv->stdout, qr/operation successful/, "Command performed" );
185 like( $rv->stderr, qr/patient dead/, "Deal with expected command error" );
186  
187 is_deeply( $rv->execute_rv, \@expected_return_values, "got what I deserve?" );
188  
189 cmp_ok( $rv->exit_code, "==", 0, "Command successful" );
190  
191 =head1 DESCRIPTION
192  
193 The test coverage of most CLI apps is somewhere between poor and wretched.
194 With the same approach as L<App::Cmd::Tester> comes MooX::Cmd::Tester to
195 ease writing tests for CLI apps.
196  
197 =head1 FUNCTIONS
198  
199 =head2 test_cmd
200  
201 my $rv = test_cmd( MyApp => \@argv );
202  
203 test_cmd invokes the app with given argv as if would be invoked from
204 command line and captures the output, the return values and exit code.
205  
206 Some minor tests are done to prove whether class matches, execute succeeds,
207 command_name and command_chain are not totally scrambled.
208  
209 It returns an object with following attributes/accessors:
210  
211 =head3 app
212  
213 Name of package of App
214  
215 =head3 cmd
216  
217 Name of executed (1st level) command
218  
219 =head3 stdout
220  
221 Content of stdout
222  
223 =head3 stderr
224  
225 Content of stderr
226  
227 =head3 output
228  
229 Content of merged stdout and stderr
230  
231 =head3 error
232  
233 the exception thrown by running the application (if any)
234  
235 =head3 execute_rv
236  
237 return values from execute
238  
239 =head3 exit_code
240  
241 0 on success, $! when error occurred and $! available, -1 otherwise
242  
243 =head2 test_cmd_ok
244  
245 my $rv = test_cmd_ok( MyApp => \@argv );
246  
247 Runs C<test_cmd> and expects it being successful - command_name must be in
248 command_commands, etc.
249  
250 Returns the same object C<test_cmd> returns.
251  
252 If an error occurred, no additional test is done (behavior as C<test_cmd>).
253  
254 =head2 result_class
255  
256 Builder for result class to use. Returns C<MooX::Cmd::Tester::Result> by
257 default.
258  
259 =head1 ACKNOWLEDGEMENTS
260  
261 MooX::Cmd::Tester is I<inspired> by L<App::Cmd::Tester> from Ricardo Signes.
262 In fact, I reused the entire design and adopt it to the requirements of
263 MooX::Cmd.
264  
265 =head1 LICENSE AND COPYRIGHT
266  
267 Copyright 2013-2017 Jens Rehsack.
268  
269 This program is free software; you can redistribute it and/or modify it
270 under the terms of either: the GNU General Public License as published
271 by the Free Software Foundation; or the Artistic License.
272  
273 See L<http://dev.perl.org/licenses/> for more information.
274  
275 =cut
276  
277 6 1;
↑ Summary Cmd/Role.pm »