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
|
use strict;
use warnings;
package App::Cmd::Tester::CaptureExternal 0.340;
use parent 'App::Cmd::Tester';
use Capture::Tiny 0.13 qw/capture/;
# ABSTRACT: Extends App::Cmd::Tester to capture from external subprograms
#pod =head1 SYNOPSIS
#pod
#pod use Test::More tests => 4;
#pod use App::Cmd::Tester::CaptureExternal;
#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 ok($result->output, "STDOUT concatenated with STDERR");
#pod
#pod =head1 DESCRIPTION
#pod
#pod L<App::Cmd::Tester> provides a useful scaffold for testing applications, but it
#pod is unable to capture output generated from any external subprograms that are
#pod invoked from the application.
#pod
#pod This subclass uses an alternate mechanism for capturing output
#pod (L<Capture::Tiny>) that does capture from external programs, with one
#pod major limitation.
#pod
#pod It is not possible to capture externally from both STDOUT and STDERR while
#pod also having appropriately interleaved combined output. Therefore, the
#pod C<output> from this subclass simply concatenates the two.
#pod
#pod You can still use C<output> for testing if there is any output at all or for
#pod testing if something appeared in either output stream, but you can't rely on
#pod the ordering being correct between lines to STDOUT and lines to STDERR.
#pod
#pod =cut
sub _run_with_capture {
my ($class, $app, $argv) = @_;
my $run_rv;
my ($stdout, $stderr, $ok) = capture {
eval {
local $App::Cmd::Tester::TEST_IN_PROGRESS = 1;
local @ARGV = @$argv;
$run_rv = $app->run;
1;
};
};
my $error = $ok ? undef : $@;
return {
stdout => $stdout,
stderr => $stderr,
output => $stdout . $stderr,
error => $error,
run_rv => $run_rv,
};
}
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
App::Cmd::Tester::CaptureExternal - Extends App::Cmd::Tester to capture from external subprograms
=head1 VERSION
version 0.340
=head1 SYNOPSIS
use Test::More tests => 4;
use App::Cmd::Tester::CaptureExternal;
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');
ok($result->output, "STDOUT concatenated with STDERR");
=head1 DESCRIPTION
L<App::Cmd::Tester> provides a useful scaffold for testing applications, but it
is unable to capture output generated from any external subprograms that are
invoked from the application.
This subclass uses an alternate mechanism for capturing output
(L<Capture::Tiny>) that does capture from external programs, with one
major limitation.
It is not possible to capture externally from both STDOUT and STDERR while
also having appropriately interleaved combined output. Therefore, the
C<output> from this subclass simply concatenates the two.
You can still use C<output> for testing if there is any output at all or for
testing if something appeared in either output stream, but you can't rely on
the ordering being correct between lines to STDOUT and lines to STDERR.
=head1 PERL VERSION
This library should run on perls released even a long time ago. It should
work on any version of perl released in the last five years.
Although it may work on older versions of perl, no guarantee is made that the
minimum required version will not be increased. The version may be increased
for any reason, and there is no promise that patches will be accepted to
lower the minimum required perl.
=head1 AUTHOR
Ricardo Signes <cpan@semiotic.systems>
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2026 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
|