diff --git a/lib/Test/MockFile.pm b/lib/Test/MockFile.pm index 57b972f..c16f30c 100644 --- a/lib/Test/MockFile.pm +++ b/lib/Test/MockFile.pm @@ -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, diff --git a/t/opendir.t b/t/opendir.t index 6370d1d..af0e5f9 100644 --- a/t/opendir.t +++ b/t/opendir.t @@ -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;