Cmd/Tester.pm

filter: Statementstmt 100% 54 / 54 Branchbran 92.8% 13 / 14 Conditioncond 81.8% 9 / 11 MC/DCmcdc 71.4% 5 / 7 Subroutinesub 100% 12 / 12 Podpod 50.0% 1 / 2 totaltotal 94.0% 94 / 100 CC 12 SCAR 14.8
CC
12
Cov
96%
test_app
9.0
throw
2.0
__ANON__
2.0
CRAP 4.4
« Cmd/Command.pm ↑ Summary Cmd/Setup.pm »
1 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package App::Cmd::Tester 0.340;
4  
5 # ABSTRACT: for capturing the result of running an app
6  
7 #pod =head1 SYNOPSIS
8 #pod
9 #pod use Test::More tests => 4;
10 #pod use App::Cmd::Tester;
11 #pod
12 #pod use YourApp;
13 #pod
14 #pod my $result = test_app(YourApp => [ qw(command --opt value) ]);
15 #pod
16 #pod like($result->stdout, qr/expected output/, 'printed what we expected');
17 #pod
18 #pod is($result->stderr, '', 'nothing sent to sderr');
19 #pod
20 #pod is($result->error, undef, 'threw no exceptions');
21 #pod
22 #pod my $result = test_app(YourApp => [ qw(command --opt value --quiet) ]);
23 #pod
24 #pod is($result->output, '', 'absolutely no output with --quiet');
25 #pod
26 #pod =head1 DESCRIPTION
27 #pod
28 #pod One of the reasons that user-executed programs are so often poorly tested is
29 #pod that they are hard to test. App::Cmd::Tester is one of the tools App-Cmd
30 #pod provides to help make it easy to test App::Cmd-based programs.
31 #pod
32 #pod It provides one routine: test_app.
33 #pod
34 #pod =method test_app
35 #pod
36 #pod B<Note>: while C<test_app> is a method, it is by default exported as a
37 #pod subroutine into the namespace that uses App::Cmd::Tester. In other words: you
38 #pod probably don't need to think about this as a method unless you want to subclass
39 #pod App::Cmd::Tester.
40 #pod
41 #pod my $result = test_app($app_class => \@argv_contents);
42 #pod
43 #pod This will locally set C<@ARGV> to simulate command line arguments, and will
44 #pod then call the C<run> method on the given application class (or application).
45 #pod Output to the standard output and standard error filehandles will be captured.
46 #pod
47 #pod C<$result> is an App::Cmd::Tester::Result object, which has methods to access
48 #pod the following data:
49 #pod
50 #pod stdout - the output sent to stdout
51 #pod stderr - the output sent to stderr
52 #pod output - the combined output of stdout and stderr
53 #pod error - the exception thrown by running the application, or undef
54 #pod run_rv - the return value of the run method (generally irrelevant)
55 #pod exit_code - the numeric exit code that would've been issued (0 is 'okay')
56 #pod
57 #pod The output is captured using L<IO::TieCombine>, which I<can> ensure that the
58 #pod ordering is preserved in the combined output, but I<can't> capture the output
59 #pod of external programs. You can reverse these tradeoffs by using
60 #pod L<App::Cmd::Tester::CaptureExternal> instead.
61 #pod
62 #pod =cut
63  
64 13 ▶use Sub::Exporter::Util qw(curry_method);
Subroutine BEGIN: called CC=1 SCAR=0.0
65 13 ▶use Sub::Exporter -setup => {
Subroutine BEGIN: called CC=1 SCAR=0.0
66 exports => { test_app => curry_method },
67 groups => { default => [ qw(test_app) ] },
68 13 };
69  
70 13 our $TEST_IN_PROGRESS;
71 13 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
72 3 ▶ *CORE::GLOBAL::exit = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
73 3 my ($rc) = @_;
74 3 ▶ return CORE::exit($rc) unless $TEST_IN_PROGRESS;
Branch: 1 branch
BranchTrueFalseTotal
unless $App::Cmd::Tester::TEST_IN_PROGRESS 2 1 3
75 1 App::Cmd::Tester::Exited->throw($rc);
76 13 };
77 }
78  
79 #pod =for Pod::Coverage result_class
80 #pod
81 #pod =cut
82  
83 17 ▶sub result_class { 'App::Cmd::Tester::Result' }
Subroutine result_class: called CC=1 SCAR=0.0
Pod undocumented
84  
85 17 ▶sub test_app {
Subroutine test_app: called CC=9 SCAR=22.0
Pod documented
86 17 my ($class, $app, $argv) = @_;
87  
88 17 local $App::Cmd::_bad = 0;
89  
90 17 ▶ $app = $app->new unless ref($app) or $app->isa('App::Cmd::Simple');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $app or $app->isa("App::Cmd::Simple") 12 5 17
Condition66%2/3
ref $app or $app->isa("App::Cmd::Simple")
l!l&&r!l&&!r
0512
MC/DC50%1/2
ref $app or $app->isa("App::Cmd::Simple")
ref $app $app->isa("App::Cmd::Simple")
Truth table66%2/3
A or B
A= ref $appB= $app->isa("App::Cmd::Simple")
ABresult
1X1
011
000
91  
92 17 my $result = $class->_run_with_capture($app, $argv);
93  
94 17 my $error = $result->{error};
95  
96 17 ▶ my $exit_code = defined $error ? ((0+$!)||-1) : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $error ? : 3 14 17
Condition100%2/2
0 + $! or -1
l!l
12
MC/DC100%1/1
0 + $! or -1
0 + $!
Truth table100%2/2
A
A= 0 + $!
Aresult
11
00
97  
98 17 ▶ if ($error and eval { $error->isa('App::Cmd::Tester::Exited') }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error and eval { do { $error->isa("App::Cmd::Tester::Exited") } }) 1 16 17
Condition100%3/3
$error and eval { do { $error->isa("App::Cmd::Tester::Exited") } }
!ll&&!rl&&r
1421
MC/DC100%2/2
$error and eval { do { $error->isa("App::Cmd::Tester::Exited") } }
$error eval { do { $error->isa("App::Cmd::Tester::Exited") } }
Truth table100%3/3
A and B
A= $errorB= eval { do { $error->isa("App::Cmd::Tester::Exited") } }
ABresult
0X0
100
111
99 1 $exit_code = $$error;
100 }
101  
102 17 ▶ $exit_code =1 if $App::Cmd::_bad && ! $exit_code;
Branch: 1 branch
BranchTrueFalseTotal
if $App::Cmd::_bad and not $exit_code 1 16 17
Condition66%2/3
$App::Cmd::_bad and not $exit_code
!ll&&!rl&&r
1601
MC/DC50%1/2
$App::Cmd::_bad and not $exit_code
$App::Cmd::_bad not $exit_code
Truth table66%2/3
A and not(B)
A= $App::Cmd::_badB= not $exit_code
ABresult
0X0
100
111
103  
104 17 $class->result_class->new({
105 app => $app,
106 exit_code => $exit_code,
107 %$result,
108 });
109 }
110  
111 18 ▶sub _run_with_capture {
Subroutine _run_with_capture: called CC=2 SCAR=6.9
112 18 my ($class, $app, $argv) = @_;
113  
114 18 require IO::TieCombine;
115 18 my $hub = IO::TieCombine->new;
116  
117 18 my $stdout = tie local *STDOUT, $hub, 'stdout';
118 18 my $stderr = tie local *STDERR, $hub, 'stderr';
119  
120 18 my $run_rv;
121  
122 18 my $ok = eval {
123 18 local $TEST_IN_PROGRESS = 1;
124 18 local @ARGV = @$argv;
125 18 $run_rv = $app->run;
126 15 1;
127 };
128  
129 18 ▶ my $error = $ok ? undef : $@;
Branch: 1 branch
BranchTrueFalseTotal
$ok ? : 15 3 18
130  
131 return {
132 18 stdout => $hub->slot_contents('stdout'),
133 stderr => $hub->slot_contents('stderr'),
134 output => $hub->combined_contents,
135 error => $error,
136 run_rv => $run_rv,
137 };
138 }
139  
140 {
141 package App::Cmd::Tester::Result 0.340;
142  
143 17 ▶ sub new {
Subroutine new: called CC=1 SCAR=0.0
144 17 my ($class, $arg) = @_;
145 17 bless $arg => $class;
146 }
147  
148 13 for my $attr (qw(app stdout stderr output error run_rv exit_code)) {
149 Sub::Install::install_sub({
150 20 ▶ code => sub { $_[0]->{$attr} },
Subroutine __ANON__: called CC=1 SCAR=0.0
151 91 as => $attr,
152 });
153 }
154 }
155  
156 {
157 13 package App::Cmd::Tester::Exited 0.340;
158  
159 1 ▶ sub throw {
Subroutine throw: called CC=2 SCAR=7.0
160 1 my ($class, $code) = @_;
161 1 ▶ $code = 0 unless defined $code;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $code 0 1 1
162 1 my $self = (bless \$code => $class);
163 1 die $self;
164 }
165 }
166  
167 13 1;
168  
169 __END__
« Cmd/Command.pm ↑ Summary Cmd/Setup.pm »