Open
Description
As shown with that example truncate
+ tell
are not supported by Test::MockFile.
We should consider adding a die/warning when using it on a mocked file, before providing a correct interface for them
Using this script as a proof of concept
#!perl
use v5.32;
use Test::MockFile;
my $test_file = q[/tmp/my.test.file];
# Note: commenting this file show the broken behavior
## ----
# my $mock_file = Test::MockFile->file( $test_file => '' );
## ---
sub _content {
return <<~EOS;
# line 1
# line 2
# line 3
# line 4
# line 5
# line 6
EOS
}
sub setup_file {
open( my $fh, '>', $test_file ) or die;
print {$fh} _content();
return 1;
}
sub cleanup_file {
my $f = $test_file;
open( my $fh, '+<', $f ) or return;
{
local $/;
my $content = <$fh>;
}
seek( $fh, 0, 0 );
print {$fh} "CLEAR";
truncate( $fh, tell($fh) );
close( $fh );
return;
}
sub _dump_file {
local $/;
open( my $fh, '<', $test_file );
my $content = <$fh>;
say "#"x20;
say $content;
say "#"x20;
return;
}
setup_file();
_dump_file();
cleanup_file();
_dump_file();
When run without mocking the file the output is
╰─> perl test.pl
####################
# line 1
# line 2
# line 3
# line 4
# line 5
# line 6
####################
####################
CLEAR
####################
if we now uncomment the line which mocks the file
# Note: commenting this file show the broken behavior
## ----
my $mock_file = Test::MockFile->file( $test_file => '' );
## ---
The output becomes
╰─> perl test.pl
####################
# line 1
# line 2
# line 3
# line 4
# line 5
# line 6
####################
####################
# line 1
# line 2
# line 3
# line 4
# line 5
# line 6
CLEAR
####################
we can see that the seek + truncate do not work as expected