Skip to content
Open
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
15 changes: 8 additions & 7 deletions lib/Test/MockFile.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1974,7 +1974,7 @@ sub __sysopen (*$$;$) {
sub __opendir (*$) {

# Upgrade but ignore bareword indicator
( undef, @_ ) = _upgrade_barewords(@_) if defined $_[0] && !ref $_[9];
( undef, @_ ) = _upgrade_barewords(@_) if defined $_[0] && !ref $_[0];

my $mock_dir = _get_file_object( $_[1] );

Expand Down Expand Up @@ -2023,7 +2023,7 @@ sub __opendir (*$) {
sub __readdir (*) {

# Upgrade but ignore bareword indicator
( undef, @_ ) = _upgrade_barewords(@_) if defined $_[0] && !ref $_[9];
( undef, @_ ) = _upgrade_barewords(@_) if defined $_[0] && !ref $_[0];

my $mocked_dir = _get_file_object( $_[0] );

Expand Down Expand Up @@ -2064,7 +2064,7 @@ sub __readdir (*) {
sub __telldir (*) {

# Upgrade but ignore bareword indicator
( undef, @_ ) = _upgrade_barewords(@_) if defined $_[0] && !ref $_[9];
( undef, @_ ) = _upgrade_barewords(@_) if defined $_[0] && !ref $_[0];

my ($fh) = @_;
my $mocked_dir = _get_file_object($fh);
Expand All @@ -2091,7 +2091,7 @@ sub __telldir (*) {
sub __rewinddir (*) {

# Upgrade but ignore bareword indicator
( undef, @_ ) = _upgrade_barewords(@_) if defined $_[0] && !ref $_[9];
( undef, @_ ) = _upgrade_barewords(@_) if defined $_[0] && !ref $_[0];

my ($fh) = @_;
my $mocked_dir = _get_file_object($fh);
Expand Down Expand Up @@ -2119,7 +2119,7 @@ sub __rewinddir (*) {
sub __seekdir (*$) {

# Upgrade but ignore bareword indicator
( undef, @_ ) = _upgrade_barewords(@_) if defined $_[0] && !ref $_[9];
( undef, @_ ) = _upgrade_barewords(@_) if defined $_[0] && !ref $_[0];

my ( $fh, $goto ) = @_;
my $mocked_dir = _get_file_object($fh);
Expand All @@ -2140,13 +2140,14 @@ sub __seekdir (*$) {
confess("seekdir called on a closed dirhandle");
}

return $obj->{'tell'} = $goto;
$obj->{'tell'} = $goto;
return 1;
}

sub __closedir (*) {

# Upgrade but ignore bareword indicator
( undef, @_ ) = _upgrade_barewords(@_) if defined $_[0] && !ref $_[9];
( undef, @_ ) = _upgrade_barewords(@_) if defined $_[0] && !ref $_[0];

my ($fh) = @_;
my $mocked_dir = _get_file_object($fh);
Expand Down
28 changes: 28 additions & 0 deletions t/opendir.t
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,33 @@ is(
closedir $dh;
}

note "-------------- BAREWORD GUARD REGRESSION --------------";
# Regression: the bareword upgrade guard was checking $_[9] (always undef
# for 1-2 arg dir functions) instead of $_[0]. This meant _upgrade_barewords
# ran unconditionally, even for reference filehandles.
# Also: seekdir must return 1 (like CORE::seekdir), not the seek position.
{
my $mock_dir = Test::MockFile->dir('/guardtest');
my $mock_file = Test::MockFile->file( '/guardtest/aaa', 'data' );

is( opendir( my $dh, '/guardtest' ), 1, "opendir with ref filehandle works" );

is( scalar readdir($dh), ".", "readdir with ref fh reads ." );
is( scalar readdir($dh), "..", "readdir with ref fh reads .." );
is( telldir($dh), 2, "telldir with ref fh returns correct position" );
is( scalar readdir($dh), "aaa", "readdir with ref fh reads aaa" );

is( rewinddir($dh), 1, "rewinddir with ref fh returns 1" );
is( telldir($dh), 0, "telldir after rewinddir is 0" );

# seekdir's return value is not reliably testable across Perl versions
# with CORE::GLOBAL overrides — test the effect instead.
seekdir( $dh, 2 );
is( telldir($dh), 2, "telldir is 2 after seekdir(2)" );
is( [ readdir($dh) ], ["aaa"], "readdir after seekdir(2) returns remaining entries" );

is( closedir($dh), 1, "closedir with ref fh returns 1" );
}

done_testing();
exit;