Skip to content

Commit

Permalink
Follow links for stat but not lstat
Browse files Browse the repository at this point in the history
  • Loading branch information
toddr committed Dec 20, 2018
1 parent 68c72a0 commit 1c443ae
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
17 changes: 13 additions & 4 deletions lib/Test/MockFile.pm
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,12 @@ sub _mock_stat {
}

my $file = _find_file_or_fh( $file_or_fh, $follow_link );

# If it's a broken link, we don't want to fall back, we want to return an empty array.
if ( $follow_link && !$file && _find_file_or_fh( $file_or_fh, 0 ) ) {
return [];
}

return $file if ref $file eq 'ARRAY'; # Allow an ELOOP to fall through here.

if ( !defined $file or !length $file ) {
Expand All @@ -467,7 +473,7 @@ sub _mock_stat {
}

# File is not present so no stats for you!
return [] if !defined $file_data->{'contents'};
return [] if !$file_data->is_link && !defined $file_data->{'contents'};

# Make sure the file size is correct in the stats before returning its contents.
return [ $file_data->stat ];
Expand All @@ -489,7 +495,7 @@ sub _find_file_or_fh {
my $mock_object = $files_being_mocked{$file_or_fh};

if ( $parent and !$mock_object ) {
die( sprintf( "Mocked file %s points to unmocked file %s", $parent, $file // '??' ) );
return;
}

return $file_or_fh unless $follow_link && $mock_object && $mock_object->is_link;
Expand Down Expand Up @@ -574,7 +580,7 @@ sub contents {
my ( $self, $new_contents ) = @_;
$self or die;

die("checking or setting contents on a symlink is not supported") if $self->is_link;
Carp::confess("checking or setting contents on a symlink is not supported") if $self->is_link;

# If 2nd arg was passed.
if ( scalar @_ == 2 ) {
Expand Down Expand Up @@ -757,6 +763,9 @@ returns the size of the file based on its contents.
sub size {
my ($self) = @_;

# Lstat for a symlink returns 1 for its size.
return 1 if $self->is_link;

# length undef is 0 not undef in perl 5.10
if ( $] < 5.012 ) {
return undef unless $self->exists;
Expand Down Expand Up @@ -788,7 +797,7 @@ Calculates the block count of the file based on its size.
sub blocks {
my ($self) = @_;

my $blocks = $self->size / abs( $self->{'blksize'} || 1 );
my $blocks = int( $self->size / abs( $self->{'blksize'} ) + 1 );
if ( int($blocks) > $blocks ) {
$blocks = int($blocks) + 1;
}
Expand Down
27 changes: 21 additions & 6 deletions t/mock_stat.t
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,7 @@ is( Test::MockFile::_find_file_or_fh('/abc'), '/abc', "_find_file_or_fh('/abc')"
is( Test::MockFile::_find_file_or_fh( '/abc', 1 ), '/foo/bar', "_find_file_or_fh('/abc', 1) - follow" );

push @mocked_files, Test::MockFile->symlink( '/not/a/file', '/broken_link' );
like(
dies { Test::MockFile::_find_file_or_fh( '/broken_link', 1 ) },
qr{^Mocked file /broken_link points to unmocked file \?\? at },
"_find_file_or_fh('/broken_link', 1) dies when /broken_link is mocked."
);
is(Test::MockFile::_find_file_or_fh( '/broken_link', 1 ), undef, "_find_file_or_fh('/broken_link', 1) is undef when /broken_link is mocked.");

push @mocked_files, Test::MockFile->symlink( '/aaa', '/bbb' );
push @mocked_files, Test::MockFile->symlink( '/bbb', '/aaa' );
Expand Down Expand Up @@ -80,7 +76,7 @@ my $basic_stat_return = array {
item match qr/^\d\d\d\d+$/;
item match qr/^\d\d\d\d+$/;
item 4096;
item 0;
item 1;
};

is( Test::MockFile::_mock_stat( 'lstat', '/foo/bar' ), $basic_stat_return, "/foo/bar mock stat" );
Expand All @@ -90,5 +86,24 @@ is( $! + 0, ELOOP, "Throws an ELOOP error" );
push @mocked_files, Test::MockFile->file('/foo/baz'); # Missing file but mocked.
is( Test::MockFile::_mock_stat( 'lstat', '/foo/baz' ), [], "/foo/baz mock stat when missing." );

my $symlink_lstat_return = array {
item 0;
item 0;
item 0127777;
item 0;
item 0;
item 0;
item 0;
item 1;
item match qr/^\d\d\d\d+$/;
item match qr/^\d\d\d\d+$/;
item match qr/^\d\d\d\d+$/;
item 4096;
item 1;
};

is( Test::MockFile::_mock_stat( 'lstat', '/broken_link' ), $symlink_lstat_return, "lstat on /broken_link returns the stat on the symlink itself." );
is( Test::MockFile::_mock_stat( 'stat', '/broken_link' ), [], "stat on /broken_link is an empty array since what it points to doesn't exist." );

done_testing();
exit;

0 comments on commit 1c443ae

Please sign in to comment.