Capture-Tiny

 view release on metacpan or  search on metacpan

t/18-custom-capture.t  view on Meta::CPAN


is( scalar do {local (@ARGV,$/) = $temp_out; <>} , "foo\n",
  "captured STDOUT to custom handle (GLOB)"
);
is( scalar do {local (@ARGV,$/) = $temp_err; <>} , "bar\n",
  "captured STDERR to custom handle (GLOB)"
);

unlink $_ for $temp_out, $temp_err;

#--------------------------------------------------------------------------#
# append to custom IO::File
#--------------------------------------------------------------------------#

$temp_out = tmpnam();
$temp_err = tmpnam();

ok( !-e $temp_out, "Temp out '$temp_out' doesn't exist" );
ok( !-e $temp_err, "Temp out '$temp_err' doesn't exist" );

$out_fh = IO::File->new($temp_out, "w+");
$err_fh = IO::File->new($temp_err, "w+");

$out_fh->autoflush(1);
$err_fh->autoflush(1);

print $out_fh "Shouldn't see this in capture\n";
print $err_fh "Shouldn't see this in capture\n";

my ($got_out, $got_err) = capture {
  print STDOUT "foo\n";
  print STDERR "bar\n";
} stdout => $out_fh, stderr => $err_fh;

$out_fh->close;
$err_fh->close;

is( $got_out, "foo\n",
  "captured appended STDOUT to custom handle"
);
is( $got_err, "bar\n",
  "captured appended STDERR to custom handle"
);

unlink $_ for $temp_out, $temp_err;

#--------------------------------------------------------------------------#
# repeated append to custom IO::File with no output
#--------------------------------------------------------------------------#

$temp_out = tmpnam();
$temp_err = tmpnam();

ok( !-e $temp_out, "Temp out '$temp_out' doesn't exist" );
ok( !-e $temp_err, "Temp out '$temp_err' doesn't exist" );

$out_fh = IO::File->new($temp_out, "a+");
$err_fh = IO::File->new($temp_err, "a+");

($got_out, $got_err) = capture {
  my $i = 0; $i++ for 1 .. 10; # no output, just busywork
} stdout => $out_fh, stderr => $err_fh;

is( $got_out, "",
  "Try 1: captured empty appended STDOUT to custom handle"
);
is( $got_err, "",
  "Try 1: captured empty appended STDERR to custom handle"
);

($got_out, $got_err) = capture {
  my $i = 0; $i++ for 1 .. 10; # no output, just busywork
} stdout => $out_fh, stderr => $err_fh;

is( $got_out, "",
  "Try 2: captured empty appended STDOUT to custom handle"
);
is( $got_err, "",
  "Try 2: captured empty appended STDERR to custom handle"
);

unlink $_ for $temp_out, $temp_err;

#--------------------------------------------------------------------------#
# finish
#--------------------------------------------------------------------------#

close ARGV; # opened by reading from <>
is( next_fd, $fd, "no file descriptors leaked" );

exit 0;



( run in 4.134 seconds using v1.01-cache-2.11-cpan-97f6503c9c8 )