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
2 changes: 2 additions & 0 deletions lib/Test/MockFile.pm
Original file line number Diff line number Diff line change
Expand Up @@ -4101,11 +4101,13 @@ sub __rename ($$) {

delete $files_being_mocked{$key};
$files_being_mocked{$new_key} = $child;
Scalar::Util::weaken( $files_being_mocked{$new_key} );
$child->{'path'} = $new_key;

# Update autovivify tracking for child directories
if ( $_autovivify_dirs{$key} ) {
$_autovivify_dirs{$new_key} = delete $_autovivify_dirs{$key};
Scalar::Util::weaken( $_autovivify_dirs{$new_key} );
}
}
}
Expand Down
31 changes: 31 additions & 0 deletions t/rename.t
Original file line number Diff line number Diff line change
Expand Up @@ -239,4 +239,35 @@ note "-------------- rename: preserves inode and nlink --------------";
is( $st[3], 3, 'nlink preserved after rename' );
}

note "-------------- rename: child file mock weakref on re-key --------------";
{
my $parent = Test::MockFile->new_dir('/mock/wkparent');
my $child = Test::MockFile->file( '/mock/wkparent/data', 'hello' );
my $dest = Test::MockFile->new_dir('/mock/wknew');

ok( rename( '/mock/wkparent', '/mock/wknew' ), 'rename parent dir with child file' );
is( $child->contents, 'hello', 'child contents intact after rename' );

undef $child;

ok( !-e '/mock/wknew/data', 'child file mock GC\'d after undef (weakref preserved)' );
}

note "-------------- rename: autovivify child dir weakref on re-key --------------";
{
my $parent = Test::MockFile->new_dir('/mock/avparent');
my $child = Test::MockFile->new_dir( '/mock/avparent/sub', { 'autovivify' => 1 } );
my $dest = Test::MockFile->new_dir('/mock/avnew');

ok( rename( '/mock/avparent', '/mock/avnew' ), 'rename parent dir with autovivify child' );

ok( open( my $fh, '>', '/mock/avnew/sub/test.txt' ), 'autovivify works under renamed child' );
close $fh;
ok( -e '/mock/avnew/sub/test.txt', 'autovivified file exists' );

undef $child;

ok( !-d '/mock/avnew/sub', 'child dir mock GC\'d after undef (autovivify weakref preserved)' );
}

done_testing();
Loading