From a5eee8ae8df9082ff08939ae7f82d58db10507bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C5=8Dan?= Date: Tue, 10 Feb 2026 20:31:38 -0700 Subject: [PATCH 1/2] fix: use keys when grepping %files_being_mocked in dir() grep on a hash iterates both keys AND values. Since values are weakrefs to blessed hashrefs, their stringified form could accidentally match the path regex. Using 'keys' ensures only file paths are matched. Co-Authored-By: Claude Opus 4.6 --- lib/Test/MockFile.pm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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, From 5a908ccbd6156bec99793f3ea3d004f7a2e4f3fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C5=8Dan?= Date: Wed, 11 Feb 2026 12:03:10 -0700 Subject: [PATCH 2/2] rebase: apply review feedback on #192 --- t/opendir.t | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) 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;