Skip to content
Merged
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
2 changes: 1 addition & 1 deletion lib/Test/MockFile.pm
Original file line number Diff line number Diff line change
Expand Up @@ -846,7 +846,7 @@ sub dir {
# TODO: Add stat information

# FIXME: Quick and dirty: provide a helper method?
my $has_content = grep m{^\Q$path/\E}xms, %files_being_mocked;
my $has_content = grep m{^\Q$path/\E}xms, keys %files_being_mocked;
return $class->new(
{
'path' => $path,
Expand Down
16 changes: 16 additions & 0 deletions t/opendir.t
Original file line number Diff line number Diff line change
Expand Up @@ -99,5 +99,21 @@ is(
is( \@content, [qw< . .. bar >], 'Did not get confused by internal files' );
}

# Regression: dir() must use "keys" when grepping %files_being_mocked.
# Without "keys", grep iterates over both keys (paths) and values (weakrefs
# to blessed hashrefs). The stringified mock objects could accidentally match
# the path regex, inflating has_content or causing uninitialized-value warnings
# when weakrefs are cleared during global destruction.
{
my $mock_file = Test::MockFile->file( '/regdir/somefile', 'data' );
my $mock_dir = Test::MockFile->dir('/regdir');

is( $mock_dir->contents(), [qw< . .. somefile >], 'dir() detects mocked child file via keys %files_being_mocked' );

opendir my $dh, '/regdir' or die "opendir /regdir: $!";
is( [ readdir($dh) ], [qw< . .. somefile >], 'readdir returns correct entries for dir with mocked children' );
closedir $dh;
}

done_testing();
exit;