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

symlink is not updating symlinked file control by Test::MockFile #37

Closed
atoomic opened this issue Feb 8, 2019 · 2 comments
Closed

symlink is not updating symlinked file control by Test::MockFile #37

atoomic opened this issue Feb 8, 2019 · 2 comments

Comments

@atoomic
Copy link
Contributor

atoomic commented Feb 8, 2019

When creating a symlink file controlled by Test::MockFile calling symlink later would not update its location as show in the example below. readlink is returning an accurate value

use Test::More;
use Test::MockFile;

{
    # unmocked version

    # cleanup on startup
    unlink('/bin/foo');
    unlink('/not/there');

    # create a symlink
    ok symlink( '/not-there', '/bin/foo' );
    ok -l '/bin/foo';
    is readlink('/bin/foo'), '/not-there';
    ok !-e '/bin/foo';
    ok unlink('/bin/foo');
    ok !-l '/bin/foo';
    ok symlink( '/abcd', '/bin/foo' );
    is readlink('/bin/foo'), '/abcd', 'symlink now points to /abcd';

}

{
    # now testing with MockFile

    # cleanup
    unlink('/bin/foo');
    unlink('/not/there');

    # we want to take control of that file and set it as a symlink
    my $mock_bin_foo = Test::MockFile->symlink( "/not-there", "/bin/foo" );
    ok -l '/bin/foo';
    is readlink('/bin/foo'), '/not-there';
    ok !-e '/bin/foo';
    ok unlink('/bin/foo');
    ok !-l '/bin/foo';
    ok symlink( '/abcd', '/bin/foo' );
    is readlink('/bin/foo'), '/abcd', 'symlink now points to /abcd';
    # ^^ symlink on a Test::MockFile symlinked file is not updating its location
}

done_testing;

output

ok 1
ok 2
ok 3
ok 4
ok 5
ok 6
ok 7
ok 8 - symlink now points to /abcd
ok 9
ok 10
ok 11
ok 12
ok 13
ok 14
not ok 15 - symlink now points to /abcd
#   Failed test 'symlink now points to /abcd'
#   at test.t line X.
#          got: undef
#     expected: '/abcd'
1..15
@drmuey
Copy link

drmuey commented Jan 19, 2021

hacked (and I do mean hack, lol, because this test will never need a real symlink) around it for now:

use Test::MockFile;
BEGIN {
    *CORE::GLOBAL::symlink = sub {
        my ( $target, $symlink ) = @_; 
        $Test::MockFile::files_being_mocked{$symlink}->{readlink} = $target;
    };  
}

may need to do same w/ readlink() ➜ it was correct but it may have been reading from the file system symlink that symlink() created.

@toddr
Copy link
Member

toddr commented Dec 27, 2021

This is redundant to #16

@toddr toddr closed this as completed Dec 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants