Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

seek / truncate / tell are not supported #77

Open
atoomic opened this issue Jan 11, 2022 · 2 comments
Open

seek / truncate / tell are not supported #77

atoomic opened this issue Jan 11, 2022 · 2 comments
Labels
bug Something isn't working Feature Request New feature or request Ready for work

Comments

@atoomic
Copy link
Contributor

atoomic commented Jan 11, 2022

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

@xsawyerx xsawyerx added bug Something isn't working Feature Request New feature or request labels Jan 12, 2022
@xsawyerx
Copy link
Contributor

I think the difference between "bug" and "enhancement" is whether this is a described or expected behavior. Since this is not described (though makes sense to be expected), I labeled it as both "bug" and "enhancement."

@atoomic
Copy link
Contributor Author

atoomic commented Jan 12, 2022

I agree:

To solve the bug

  • either crash when using it throwing an error with the current limitation, so acknowledge & document it
  • or povide an implementation

Enhancement:

  • provide an implementation for it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Feature Request New feature or request Ready for work
Development

No branches or pull requests

3 participants