/usr/share/perl5/Test/Command/Simple.pm is in libtest-command-simple-perl 0.05-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 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 | package Test::Command::Simple;
use warnings;
use strict;
=head1 NAME
Test::Command::Simple - Test external commands (nearly) as easily as loaded modules.
=head1 VERSION
Version 0.05
=cut
our $VERSION = '0.05';
use base 'Test::Builder::Module';
use IPC::Open3;
use IO::Select;
use Symbol qw(gensym);
use Scalar::Util qw(looks_like_number);
our @EXPORT = qw(
run
stdout
stderr
rc
run_ok
exit_status
);
=head1 SYNOPSIS
use Test::Command::Simple;
run('echo', 'has this output'); # only tests that the command can be started, not checking rc
is(rc,0,'Returned successfully')
like(stdout,qr/has this output/,'Testing stdout');
is(length stderr, 0,'No stderr');
=head1 PURPOSE
This test module is intended to simplify testing of external commands.
It does so by running the command under L<IPC::Open3>, closing the stdin
immediately, and reading everything from the command's stdout and stderr.
It then makes the output available to be tested.
It is not (yet?) as feature-rich as L<Test::Cmd>, but I think the
interface to this is much simpler. Tests also plug directly into the
L<Test::Builder> framework, which plays nice with L<Test::More>.
As compared to L<Test::Command>, this module is simpler, relying on
the user to feed rc, stdout, and stderr to the appropriate other
tests, presumably in L<Test::More>, but not necessarily. This makes it
possible, for example, to test line 3 of the output:
my (undef, undef, $line) = split /\r?\n/, stdout;
is($line, 'This is the third line', 'Test the third line');
While this is possible to do with Test::Command's stdout_like, some regex's
can get very awkward, and it becomes better to do this in multiple steps.
Also, Test::Command saves stdout and stderr to files. That has an advantage
when you're saving a lot of text. However, this module prefers to slurp
everything in using IPC::Open3, IO::Select, and sysread. Most of the time,
commands being tested do not produce significant amounts of output, so there
becomes no reason to use temporary files and involve the disk at all.
=head1 EXPORTS
=head2 run
Runs the given command. It will return when the command is done.
This will also reinitialise all of the states for stdout, stderr, and rc.
If you need to keep the values of a previous run() after a later one,
you will need to store it. This should be mostly pretty rare.
Counts as one test: whether the IPC::Open3 call to open3 succeeded.
That is not returned in a meaningful way to the user, though. To check
if that's the case for purposes of SKIPping, rc will be set to -1.
=cut
my ($stdout, $stderr, $rc);
sub run {
my $opts = @_ && ref $_[0] eq 'HASH' ? shift : {};
my @cmd = @_;
# initialise everything each run.
$rc = -1;
$stdout = '';
$stderr = '';
my ($wtr, $rdr, $err) = map { gensym() } 1..3;
my $pid = open3($wtr, $rdr, $err, @cmd) or do {
return __PACKAGE__->builder->ok(0, "Can run '@cmd'");
};
__PACKAGE__->builder->ok(1, "Can run '@cmd'");
my $s = IO::Select->new();
if ($opts->{stdin})
{
print $wtr $opts->{stdin};
}
close $wtr;
$s->add($rdr);
$s->add($err);
my %map = (
fileno($rdr) => \$stdout,
fileno($err) => \$stderr,
);
while ($s->count())
{
if (my @ready = $s->can_read())
{
for my $fh (@ready)
{
my $buffer;
my $fileno = fileno($fh);
my $read = sysread($fh, $buffer, 1024);
if ($read && $map{$fileno})
{
${$map{$fileno}} .= $buffer;
}
else
{
# done.
$s->remove($fh);
close $fh;
}
}
}
elsif (my @err = $s->has_exception())
{
warn "Exception on ", fileno($_) for @err;
}
}
waitpid $pid, 0;
$rc = $?;
$rc;
}
=head2 stdout
Returns the last run's stdout
=cut
sub stdout() {
$stdout
}
=head2 stderr
Returns the last run's stderr
=cut
sub stderr() {
$stderr
}
=head2 rc
Returns the last run's full $?, suitable for passing to L<POSIX>'s
:sys_wait_h macros (WIFEXITED, WEXITSTATUS, etc.)
=cut
sub rc() {
$rc
}
=head2 exit_status
Returns the exit status of the last run
=cut
sub exit_status()
{
#WEXITSTATUS($rc);
$rc >> 8;
}
=head2 run_ok
Shortcut for checking that the return from a command is 0. Will
still set stdout and stderr for further testing.
If the first parameter is an integer 0-255, then that is the expected
return code instead. Remember: $? has both a return code (0-255) and a
reason for exit embedded. This function must make the assumption that
you want a "normal" exit only. If any signal is given, this will treat
that as a failure.
Note that this becomes B<three> tests: one that IPC::Open3 could create
the subprocess with the command, the next is the test that the process
exited normally, and the last is the test of the rc.
=cut
sub run_ok
{
my $wanted_rc = 0;
if (looks_like_number($_[0]) &&
0 <= $_[0] && $_[0] <= 255 &&
int($_[0]) == $_[0])
{
$wanted_rc = shift();
}
run(@_);
__PACKAGE__->builder->is_eq(rc & 0xFF, 0, "Process terminated without a signal");
__PACKAGE__->builder->is_eq(exit_status, $wanted_rc, "Check return from '@_' is $wanted_rc");
}
=head1 AUTHOR
Darin McBride, C<< <dmcbride at cpan.org> >>
=head1 BUGS
Please report any bugs or feature requests to C<bug-test-command at rt.cpan.org>, or through
the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Test-Command-Simple>. I will be notified, and then you'll
automatically be notified of progress on your bug as I make changes.
=head1 SUPPORT
You can find documentation for this module with the perldoc command.
perldoc Test::Command::Simple
You can also look for information at:
=over 4
=item * RT: CPAN's request tracker
L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Test-Command-Simple>
=item * AnnoCPAN: Annotated CPAN documentation
L<http://annocpan.org/dist/Test-Command-Simple>
=item * CPAN Ratings
L<http://cpanratings.perl.org/d/Test-Command-Simple>
=item * Search CPAN
L<http://search.cpan.org/dist/Test-Command-Simple/>
=back
=head1 ACKNOWLEDGEMENTS
=head1 LICENSE AND COPYRIGHT
Copyright 2010 Darin McBride.
This program is free software; you can redistribute it and/or modify it
under the terms of either: the GNU General Public License as published
by the Free Software Foundation; or the Artistic License.
See http://dev.perl.org/licenses/ for more information.
=cut
1; # End of Test::Command::Simple
|