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

Convert relative paths on opendir/open #108

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

Convert relative paths on opendir/open #108

atoomic opened this issue Jan 26, 2022 · 2 comments
Labels
Feature Request New feature or request

Comments

@atoomic
Copy link
Contributor

atoomic commented Jan 26, 2022

When mocking a directory and a file in the same directory we can then see . and ..
but unfortunately we have no way to mock them and they should be also provided/mocked for free for us by the directory / file

use Test::MockFile qw/strict/; 

my $d = Test::MockFile->dir("/there"); 
my $f = Test::MockFile->file("/there/xyz" => q[...]); 

opendir( my $dh, "/there") or return;
foreach my $f ( readdir($dh) ) { 
      say $f
} 

output

.
..
xyz
use Test::MockFile qw/strict/; 

my $d = Test::MockFile->dir("/there"); 
my $f = Test::MockFile->file("/there/xyz" => q[...]); 

opendir( my $dh, "/there") or return;
foreach my $f ( readdir($dh) ) { 
      -e "/there/$f"
} 

output

$>perl foo.pl 
Use of stat to access unmocked file or directory '/there/.' in strict mode at foo.pl line 8 at /usr/local/cpanel/3rdparty/perl/532/lib/perl5/cpanel_lib/Test/MockFile.pm line 238.
	Test::MockFile::_strict_mode_violation("stat", ARRAY(0xd4a360)) called at /usr/local/cpanel/3rdparty/perl/532/lib/perl5/cpanel_lib/Test/MockFile.pm line 1150
	Test::MockFile::_real_file_access_hook("stat", ARRAY(0xd4a360)) called at /usr/local/cpanel/3rdparty/perl/532/lib/perl5/cpanel_lib/Test/MockFile.pm line 612
	Test::MockFile::_mock_stat("stat", "/there/.") called at /usr/local/cpanel/3rdparty/perl/532/lib/perl5/cpanel_lib/x86_64-linux-64int/Overload/FileCheck.pm line 295
	Overload::FileCheck::_check_from_stat("e", "/there/.", CODE(0xc2a408)) called at /usr/local/cpanel/3rdparty/perl/532/lib/perl5/cpanel_lib/x86_64-linux-64int/Overload/FileCheck.pm line 265
	Overload::FileCheck::__ANON__("e", "/there/.") called at /usr/local/cpanel/3rdparty/perl/532/lib/perl5/cpanel_lib/x86_64-linux-64int/Overload/FileCheck.pm line 210
	Overload::FileCheck::__ANON__("/there/.") called at /usr/local/cpanel/3rdparty/perl/532/lib/perl5/cpanel_lib/x86_64-linux-64int/Overload/FileCheck.pm line 586
	Overload::FileCheck::_check(275, "/there/.") called at foo.pl line 8
@xsawyerx
Copy link
Contributor

This can open a can of complexity.

opendir my $dh, '/there' or die $!;
foreach my $f ( readdir $dh ) {
    $f == '..' or next;
    opendir my $dh2, $f or die $!;
    foreach my $f2 ( readdir $dh2 ) {...}
}

How far should this go? What behavior do we expect?

@atoomic
Copy link
Contributor Author

atoomic commented Jan 27, 2022

This is great example.

As I think we mock .. for free, it should exist and we should be able to open it.
If you try to open .. again then same behavior it should be mocked again and again... probably in a dynamic way.

Note that for example the following script show the same issue unmocked, you can keep looping forever on .. if you do not have the correct guards in place

#!perl

use v5.32;

sub readit {
    my $where = shift or return;

    opendir my $dh, $where or die $!;
    foreach my $f ( readdir $dh ) {
        $f == '..' or next;
        say $f;
        readit( "$where/$f" );
    }    

    return;
}

readit( '.' );

When run you would eventually get:

.
.
.
.
.
.
.
Too many open files at test.pl line 8.

@toddr toddr changed the title Mocked directories should hold . and .. Convert relative paths on opendir/open Jan 28, 2022
@toddr toddr added the Feature Request New feature or request label Jan 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature Request New feature or request
Development

No branches or pull requests

3 participants