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
14 changes: 8 additions & 6 deletions lib/Test/MockFile/FileHandle.pm
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ sub _write_bytes {

my $data = $self->{'data'} or do {
$! = EBADF;
return 0;
return undef;
};

my $tell = $self->{'tell'};
Expand Down Expand Up @@ -218,21 +218,21 @@ sub WRITE {

if ( !$self->{'write'} ) {
$! = EBADF;
return 0;
return undef;
}

unless ( $len =~ m/^-?[0-9.]+$/ ) {
CORE::warn(qq{Argument "$len" isn't numeric in syswrite at @{[ join ' line ', (caller)[1,2] ]}.\n});
$! = EINVAL;
return 0;
return undef;
}

$len = int($len); # Perl seems to do this to floats.

if ( $len < 0 ) {
CORE::warn(qq{Negative length at @{[ join ' line ', (caller)[1,2] ]}.\n});
$! = EINVAL;
return 0;
return undef;
}

my $strlen = length($buf);
Expand All @@ -245,7 +245,7 @@ sub WRITE {
if ( $offset < 0 || $offset > $strlen ) {
CORE::warn(qq{Offset outside string at @{[ join ' line ', (caller)[1,2] ]}.\n});
$! = EINVAL;
return 0;
return undef;
}

# Write directly — syswrite must NOT inherit $, or $\ from PRINT.
Expand Down Expand Up @@ -346,6 +346,7 @@ sub READLINE {
if ( !$self->{'read'} ) {
my $path = $self->{'file'} // 'unknown';
CORE::warn("Filehandle $path opened only for output");
$! = EBADF;
return;
}

Expand Down Expand Up @@ -388,6 +389,7 @@ sub GETC {
if ( !$self->{'read'} ) {
my $path = $self->{'file'} // 'unknown';
CORE::warn("Filehandle $path opened only for output");
$! = EBADF;
return undef;
}

Expand Down Expand Up @@ -439,7 +441,7 @@ sub READ {

my $data = $self->{'data'} or do {
$! = EBADF;
return 0;
return undef;
};

my $contents_len = length $data->{'contents'};
Expand Down
2 changes: 1 addition & 1 deletion t/filehandle_cleanup.t
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ note "--- syswrite with negative offset ---";
my @warnings;
local $SIG{__WARN__} = sub { push @warnings, $_[0] };
my $result = syswrite( $fh, $buf, 2, -10 );
is( $result, 0, "syswrite with out-of-bounds negative offset returns 0" );
is( $result, undef, "syswrite with out-of-bounds negative offset returns undef" );
ok( scalar @warnings, "warning emitted for out-of-bounds offset" );
like( $warnings[0], qr/Offset outside string/, "warning mentions offset" );
close($fh);
Expand Down
8 changes: 4 additions & 4 deletions t/filehandle_weakref.t
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ subtest 'getc after mock destruction returns undef' => sub {
close $fh;
};

subtest 'sysread after mock destruction returns 0' => sub {
subtest 'sysread after mock destruction returns undef' => sub {
my $fh = _open_then_destroy_mock('/fake/sysread', "data");

my ($buf, $ret, $errno) = ('');
Expand All @@ -60,7 +60,7 @@ subtest 'sysread after mock destruction returns 0' => sub {
$errno = $! + 0;
};
ok($ok, "sysread does not crash after mock destruction");
is($ret, 0, "sysread returns 0 bytes");
is($ret, undef, "sysread returns undef on error");
is($errno, EBADF, "errno is EBADF after sysread on destroyed mock");

close $fh;
Expand Down Expand Up @@ -96,7 +96,7 @@ subtest 'printf after mock destruction returns false' => sub {
close $fh;
};

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

my ($ret, $errno);
Expand All @@ -105,7 +105,7 @@ subtest 'syswrite after mock destruction returns 0' => sub {
$errno = $! + 0;
};
ok($ok, "syswrite does not crash after mock destruction");
is($ret, 0, "syswrite returns 0 bytes");
is($ret, undef, "syswrite returns undef on error");
is($errno, EBADF, "errno is EBADF after syswrite on destroyed mock");

close $fh;
Expand Down
8 changes: 4 additions & 4 deletions t/portability_errno.t
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ subtest "syswrite with non-numeric length warns" => sub {
local $SIG{__WARN__} = sub { push @warnings, $_[0] };

my $ret = syswrite( $fh, "hello", "abc" );
is( $ret, 0, "syswrite with non-numeric len returns 0" );
is( $ret, undef, "syswrite with non-numeric len returns undef" );
is( $! + 0, EINVAL, "\$! is set to EINVAL" );
ok( scalar @warnings >= 1, "got a warning" );
like( $warnings[0], qr/isn't numeric/, "warning mentions non-numeric argument" ) if @warnings;
Expand All @@ -61,7 +61,7 @@ subtest "syswrite with negative length warns" => sub {
local $SIG{__WARN__} = sub { push @warnings, $_[0] };

my $ret = syswrite( $fh, "hello", -1 );
is( $ret, 0, "syswrite with negative length returns 0" );
is( $ret, undef, "syswrite with negative length returns undef" );
is( $! + 0, EINVAL, "\$! is set to EINVAL" );
ok( scalar @warnings >= 1, "got a warning" );
like( $warnings[0], qr/Negative length/, "warning mentions negative length" ) if @warnings;
Expand All @@ -77,7 +77,7 @@ subtest "syswrite with offset outside string warns" => sub {
local $SIG{__WARN__} = sub { push @warnings, $_[0] };

my $ret = syswrite( $fh, "hello", 2, 100 );
is( $ret, 0, "syswrite with offset beyond string returns 0" );
is( $ret, undef, "syswrite with offset beyond string returns undef" );
is( $! + 0, EINVAL, "\$! is set to EINVAL" );
ok( scalar @warnings >= 1, "got a warning" );
like( $warnings[0], qr/Offset outside string/, "warning mentions offset" ) if @warnings;
Expand All @@ -104,7 +104,7 @@ subtest "syswrite with too-negative offset warns" => sub {
local $SIG{__WARN__} = sub { push @warnings, $_[0] };

my $ret = syswrite( $fh, "hello", 2, -10 );
is( $ret, 0, "syswrite with offset before start of string returns 0" );
is( $ret, undef, "syswrite with offset before start of string returns undef" );
is( $! + 0, EINVAL, "\$! is set to EINVAL" );
ok( scalar @warnings >= 1, "got a warning" );
like( $warnings[0], qr/Offset outside string/, "warning mentions offset" ) if @warnings;
Expand Down
6 changes: 6 additions & 0 deletions t/readline.t
Original file line number Diff line number Diff line change
Expand Up @@ -138,18 +138,22 @@ note "-------------- readline on write-only handle --------------";
{
my $warn_msg;
local $SIG{__WARN__} = sub { $warn_msg = shift };
local $!;
my $line = readline($wfh);
ok( !defined $line, 'readline on write-only handle returns undef' );
like( $warn_msg, qr{opened only for output}, 'readline on write-only handle warns' );
is( $! + 0, EBADF, 'readline on write-only handle sets $! to EBADF' );
}

# List context
{
my $warn_msg;
local $SIG{__WARN__} = sub { $warn_msg = shift };
local $!;
my @lines = <$wfh>;
is( scalar @lines, 0, 'readline in list context on write-only handle returns empty list' );
like( $warn_msg, qr{opened only for output}, 'readline list context on write-only handle warns' );
is( $! + 0, EBADF, 'readline list context on write-only handle sets $! to EBADF' );
}

close $wfh;
Expand All @@ -162,9 +166,11 @@ note "-------------- getc on write-only handle --------------";

my $warn_msg;
local $SIG{__WARN__} = sub { $warn_msg = shift };
local $!;
my $ch = getc($wfh);
ok( !defined $ch, 'getc on write-only handle returns undef' );
like( $warn_msg, qr{opened only for output}, 'getc on write-only handle warns' );
is( $! + 0, EBADF, 'getc on write-only handle sets $! to EBADF' );

close $wfh;
}
Expand Down
8 changes: 4 additions & 4 deletions t/sysreadwrite_edge_cases.t
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ use Test::MockFile qw< nostrict >;
}

{
note "--- syswrite with non-numeric len warns and returns 0 ---";
note "--- syswrite with non-numeric len warns and returns undef ---";

my $mock = Test::MockFile->file('/fake/sw_nonnumeric');
sysopen( my $fh, '/fake/sw_nonnumeric', O_WRONLY | O_CREAT | O_TRUNC ) or die;
Expand All @@ -126,7 +126,7 @@ use Test::MockFile qw< nostrict >;

$! = 0;
my $ret = syswrite( $fh, "data", "abc" );
is( $ret, 0, "syswrite with non-numeric len returns 0" );
is( $ret, undef, "syswrite with non-numeric len returns undef" );
is( $! + 0, EINVAL, "errno is EINVAL for non-numeric len" );
ok( @warns >= 1, "warning emitted for non-numeric len" );
like( $warns[0], qr/isn't numeric/, "warning mentions non-numeric argument" );
Expand All @@ -136,7 +136,7 @@ use Test::MockFile qw< nostrict >;
}

{
note "--- syswrite with negative len warns and returns 0 ---";
note "--- syswrite with negative len warns and returns undef ---";

my $mock = Test::MockFile->file('/fake/sw_neglen');
sysopen( my $fh, '/fake/sw_neglen', O_WRONLY | O_CREAT | O_TRUNC ) or die;
Expand All @@ -146,7 +146,7 @@ use Test::MockFile qw< nostrict >;

$! = 0;
my $ret = syswrite( $fh, "data", -5 );
is( $ret, 0, "syswrite with negative len returns 0" );
is( $ret, undef, "syswrite with negative len returns undef" );
is( $! + 0, EINVAL, "errno is EINVAL for negative len" );
ok( @warns >= 1, "warning emitted for negative len" );
like( $warns[0], qr/Negative length/, "warning mentions negative length" );
Expand Down
6 changes: 3 additions & 3 deletions t/write_tell.t
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ use Test::MockFile qw< nostrict >;

local $!;
my $ret = syswrite( $fh, "nope", 4 );
is( $ret, 0, "syswrite on read-only handle returns 0" );
is( $ret, undef, "syswrite on read-only handle returns undef" );
is( $! + 0, EBADF, "errno is EBADF for syswrite on read-only handle" );

close $fh;
Expand Down Expand Up @@ -354,7 +354,7 @@ use Test::MockFile qw< nostrict >;
my @warns;
local $SIG{__WARN__} = sub { push @warns, $_[0] };
my $ret = syswrite( $fh, "abc", 3, -10 );
is( $ret, 0, "syswrite with offset past buffer start returns 0" );
is( $ret, undef, "syswrite with offset past buffer start returns undef" );
is( $! + 0, EINVAL, "errno is EINVAL for out-of-bounds negative offset" );
ok( grep( /Offset outside string/, @warns ), "warning emitted for out-of-bounds negative offset" );

Expand All @@ -373,7 +373,7 @@ use Test::MockFile qw< nostrict >;
my @warns;
local $SIG{__WARN__} = sub { push @warns, $_[0] };
my $ret = syswrite( $fh, "abc", 3, 10 );
is( $ret, 0, "syswrite with offset past buffer end returns 0" );
is( $ret, undef, "syswrite with offset past buffer end returns undef" );
is( $! + 0, EINVAL, "errno is EINVAL for out-of-bounds positive offset" );
ok( grep( /Offset outside string/, @warns ), "warning emitted for out-of-bounds positive offset" );

Expand Down
Loading