Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions lib/Test/MockFile/FileHandle.pm
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,10 @@ sub PRINT {
# at the C level after PRINT returns), so this only covers explicit usage.
$output .= $\ if defined $\;

$self->_write_bytes($output);
$self->_update_write_times();
my $bytes = $self->_write_bytes($output);
$self->_update_write_times() if $bytes;

return 1;
return $bytes ? 1 : 0;
}

=head2 PRINTF
Expand All @@ -183,10 +183,10 @@ sub PRINTF {
return;
}

$self->_write_bytes( sprintf( $format, @_ ) );
$self->_update_write_times();
my $bytes = $self->_write_bytes( sprintf( $format, @_ ) );
$self->_update_write_times() if $bytes;

return 1;
return $bytes ? 1 : 0;
}

=head2 WRITE
Expand Down
29 changes: 23 additions & 6 deletions t/filehandle_weakref.t
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,32 @@ subtest 'sysread after mock destruction returns 0' => sub {
close $fh;
};

subtest 'print after mock destruction fails gracefully' => sub {
subtest 'print after mock destruction returns false' => sub {
my $fh = _open_then_destroy_mock('/fake/print', '', '>');

my $ret;
my $warn_msg;
local $SIG{__WARN__} = sub { $warn_msg = shift };

my $ok = lives { $ret = print {$fh} "hello" };
my ($ret, $errno);
my $ok = lives {
$ret = print {$fh} "hello";
$errno = $! + 0;
};
ok($ok, "print does not crash after mock destruction");
ok(!$ret, "print returns false when mock is destroyed");
is($errno, EBADF, "errno is EBADF after print on destroyed mock");

close $fh;
};

subtest 'printf after mock destruction returns false' => sub {
my $fh = _open_then_destroy_mock('/fake/printf', '', '>');

my ($ret, $errno);
my $ok = lives {
$ret = printf {$fh} "%s", "hello";
$errno = $! + 0;
};
ok($ok, "printf does not crash after mock destruction");
ok(!$ret, "printf returns false when mock is destroyed");
is($errno, EBADF, "errno is EBADF after printf on destroyed mock");

close $fh;
};
Expand Down
Loading