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
|
use strict;
use warnings;
use Test::More;
use File::Spec;
use System::Command;
my @cmd = ( $^X, File::Spec->catfile( t => 'lines.pl' ) );
my $O = 1 + int rand 5;
my $E = 1 + int rand 5;
plan tests => $O + $E + 2;
# basic usage
my ( $o, $e ) = ( 1, 1 );
my $cmd = System::Command->new( @cmd, $O, $E );
$cmd->loop_on(
stdout =>
sub { like( shift, qr/^STDOUT line $o$/, "STDOUT line $o" ); $o++ },
stderr =>
sub { like( shift, qr/^STDERR line $e$/, "STDERR line $e" ); $e++ },
);
# early abort
my $c = 0;
$cmd = System::Command->new( @cmd, 10, 10 );
$cmd->loop_on(
stdout => sub { return !( ++$c == 5 ) },
stderr => ''
);
is( $c, 5, 'Aborted after 5 lines' );
like( $cmd->stdout->getline, qr/^STDOUT line 6/, 'Command still runs' );
|