/usr/share/perl5/App/Cmd/Tester.pm is in libapp-cmd-perl 0.331-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 | use strict;
use warnings;
package App::Cmd::Tester;
$App::Cmd::Tester::VERSION = '0.331';
# ABSTRACT: for capturing the result of running an app
#pod =head1 SYNOPSIS
#pod
#pod use Test::More tests => 4;
#pod use App::Cmd::Tester;
#pod
#pod use YourApp;
#pod
#pod my $result = test_app(YourApp => [ qw(command --opt value) ]);
#pod
#pod like($result->stdout, qr/expected output/, 'printed what we expected');
#pod
#pod is($result->stderr, '', 'nothing sent to sderr');
#pod
#pod is($result->error, undef, 'threw no exceptions');
#pod
#pod my $result = test_app(YourApp => [ qw(command --opt value --quiet) ]);
#pod
#pod is($result->output, '', 'absolutely no output with --quiet');
#pod
#pod =head1 DESCRIPTION
#pod
#pod One of the reasons that user-executed programs are so often poorly tested is
#pod that they are hard to test. App::Cmd::Tester is one of the tools App-Cmd
#pod provides to help make it easy to test App::Cmd-based programs.
#pod
#pod It provides one routine: test_app.
#pod
#pod =method test_app
#pod
#pod B<Note>: while C<test_app> is a method, it is by default exported as a
#pod subroutine into the namespace that uses App::Cmd::Tester. In other words: you
#pod probably don't need to think about this as a method unless you want to subclass
#pod App::Cmd::Tester.
#pod
#pod my $result = test_app($app_class => \@argv_contents);
#pod
#pod This will locally set C<@ARGV> to simulate command line arguments, and will
#pod then call the C<run> method on the given application class (or application).
#pod Output to the standard output and standard error filehandles will be captured.
#pod
#pod C<$result> is an App::Cmd::Tester::Result object, which has methods to access
#pod the following data:
#pod
#pod stdout - the output sent to stdout
#pod stderr - the output sent to stderr
#pod output - the combined output of stdout and stderr
#pod error - the exception thrown by running the application, or undef
#pod run_rv - the return value of the run method (generally irrelevant)
#pod exit_code - the numeric exit code that would've been issued (0 is 'okay')
#pod
#pod The output is captured using L<IO::TieCombine>, which I<can> ensure that the
#pod ordering is preserved in the combined output, but I<can't> capture the output
#pod of external programs. You can reverse these tradeoffs by using
#pod L<App::Cmd::Tester::CaptureExternal> instead.
#pod
#pod =cut
use Sub::Exporter::Util qw(curry_method);
use Sub::Exporter -setup => {
exports => { test_app => curry_method },
groups => { default => [ qw(test_app) ] },
};
our $TEST_IN_PROGRESS;
BEGIN {
*CORE::GLOBAL::exit = sub {
my ($rc) = @_;
return CORE::exit($rc) unless $TEST_IN_PROGRESS;
App::Cmd::Tester::Exited->throw($rc);
};
}
#pod =for Pod::Coverage result_class
#pod
#pod =cut
sub result_class { 'App::Cmd::Tester::Result' }
sub test_app {
my ($class, $app, $argv) = @_;
local $App::Cmd::_bad = 0;
$app = $app->new unless ref($app) or $app->isa('App::Cmd::Simple');
my $result = $class->_run_with_capture($app, $argv);
my $error = $result->{error};
my $exit_code = defined $error ? ((0+$!)||-1) : 0;
if ($error and eval { $error->isa('App::Cmd::Tester::Exited') }) {
$exit_code = $$error;
}
$exit_code =1 if $App::Cmd::_bad && ! $exit_code;
$class->result_class->new({
app => $app,
exit_code => $exit_code,
%$result,
});
}
sub _run_with_capture {
my ($class, $app, $argv) = @_;
require IO::TieCombine;
my $hub = IO::TieCombine->new;
my $stdout = tie local *STDOUT, $hub, 'stdout';
my $stderr = tie local *STDERR, $hub, 'stderr';
my $run_rv;
my $ok = eval {
local $TEST_IN_PROGRESS = 1;
local @ARGV = @$argv;
$run_rv = $app->run;
1;
};
my $error = $ok ? undef : $@;
return {
stdout => $hub->slot_contents('stdout'),
stderr => $hub->slot_contents('stderr'),
output => $hub->combined_contents,
error => $error,
run_rv => $run_rv,
};
}
{
package App::Cmd::Tester::Result;
$App::Cmd::Tester::Result::VERSION = '0.331';
sub new {
my ($class, $arg) = @_;
bless $arg => $class;
}
for my $attr (qw(app stdout stderr output error run_rv exit_code)) {
Sub::Install::install_sub({
code => sub { $_[0]->{$attr} },
as => $attr,
});
}
}
{
package App::Cmd::Tester::Exited;
$App::Cmd::Tester::Exited::VERSION = '0.331';
sub throw {
my ($class, $code) = @_;
$code = 0 unless defined $code;
my $self = (bless \$code => $class);
die $self;
}
}
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
App::Cmd::Tester - for capturing the result of running an app
=head1 VERSION
version 0.331
=head1 SYNOPSIS
use Test::More tests => 4;
use App::Cmd::Tester;
use YourApp;
my $result = test_app(YourApp => [ qw(command --opt value) ]);
like($result->stdout, qr/expected output/, 'printed what we expected');
is($result->stderr, '', 'nothing sent to sderr');
is($result->error, undef, 'threw no exceptions');
my $result = test_app(YourApp => [ qw(command --opt value --quiet) ]);
is($result->output, '', 'absolutely no output with --quiet');
=head1 DESCRIPTION
One of the reasons that user-executed programs are so often poorly tested is
that they are hard to test. App::Cmd::Tester is one of the tools App-Cmd
provides to help make it easy to test App::Cmd-based programs.
It provides one routine: test_app.
=head1 METHODS
=head2 test_app
B<Note>: while C<test_app> is a method, it is by default exported as a
subroutine into the namespace that uses App::Cmd::Tester. In other words: you
probably don't need to think about this as a method unless you want to subclass
App::Cmd::Tester.
my $result = test_app($app_class => \@argv_contents);
This will locally set C<@ARGV> to simulate command line arguments, and will
then call the C<run> method on the given application class (or application).
Output to the standard output and standard error filehandles will be captured.
C<$result> is an App::Cmd::Tester::Result object, which has methods to access
the following data:
stdout - the output sent to stdout
stderr - the output sent to stderr
output - the combined output of stdout and stderr
error - the exception thrown by running the application, or undef
run_rv - the return value of the run method (generally irrelevant)
exit_code - the numeric exit code that would've been issued (0 is 'okay')
The output is captured using L<IO::TieCombine>, which I<can> ensure that the
ordering is preserved in the combined output, but I<can't> capture the output
of external programs. You can reverse these tradeoffs by using
L<App::Cmd::Tester::CaptureExternal> instead.
=for Pod::Coverage result_class
=head1 AUTHOR
Ricardo Signes <rjbs@cpan.org>
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2016 by Ricardo Signes.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut
|