Skip to content

Commit

Permalink
Add some fixes & tests for contains() with updir stuff (github #43)
Browse files Browse the repository at this point in the history
  • Loading branch information
kenahoo committed Dec 8, 2015
1 parent 927fa74 commit 935eeed
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
20 changes: 16 additions & 4 deletions lib/Path/Class/Dir.pm
Expand Up @@ -267,12 +267,13 @@ sub next {
}

sub subsumes {
Carp::croak "Too many arguments given to subsumes()" if $#_ > 2;
my ($self, $other) = @_;
Carp::croak( "No second entity given to subsumes()" ) unless $other;

$other = $self->new($other) unless eval{$other->isa( "Path::Class::Entity")} ;
$other = $other->dir unless $other->is_dir;

if ($self->is_absolute) {
$other = $other->absolute;
} elsif ($other->is_absolute) {
Expand All @@ -282,14 +283,19 @@ sub subsumes {
$self = $self->cleanup;
$other = $other->cleanup;

if ($self->volume) {
if ($self->volume || $other->volume) {
return 0 unless $other->volume eq $self->volume;
}

# The root dir subsumes everything (but ignore the volume because
# we've already checked that)
return 1 if "@{$self->{dirs}}" eq "@{$self->new('')->{dirs}}";


# The current dir subsumes every relative path (unless starting with updir)
if ($self eq $self->_spec->curdir) {
return $other->{dirs}[0] ne $self->_spec->updir;
}

my $i = 0;
while ($i <= $#{ $self->{dirs} }) {
return 0 if $i > $#{ $other->{dirs} };
Expand All @@ -300,8 +306,14 @@ sub subsumes {
}

sub contains {
Carp::croak "Too many arguments given to contains()" if $#_ > 2;
my ($self, $other) = @_;
return !!(-d $self and (-e $other or -l $other) and $self->subsumes($other));
Carp::croak "No second entity given to contains()" unless $other;
return unless -d $self and (-e $other or -l $other);

$other = $self->new($other) unless eval{$other->isa("Path::Class::Entity")};
$other->resolve;
return $self->subsumes($other);
}

sub tempfile {
Expand Down
20 changes: 18 additions & 2 deletions t/03-filesystem.t
Expand Up @@ -2,8 +2,6 @@ use strict;
use Test::More;
use File::Temp qw(tmpnam tempdir);

plan tests => 103;

use_ok 'Path::Class';


Expand Down Expand Up @@ -228,6 +226,22 @@ SKIP: {
ok $t->contains($foo_bar), "t now contains t/foo/bar";

$t->subdir('foo')->rmtree;

my $cur = dir();
ok $cur->subsumes(dir("foo"));
ok $cur->subsumes(dir("foo", "..", "bar"));
ok !$cur->subsumes("..");
}

{
# Some edge cases with updir
my $c = dir();
ok $c->contains(dir());
ok $c->contains(dir("t"));
ok !$c->contains(dir(".."));
ok !$c->contains(dir("t", "..", "foo"));
ok $c->contains(dir("t", ".."));
ok !$c->contains(dir("t", "..", ".."));
}

{
Expand Down Expand Up @@ -370,3 +384,5 @@ SKIP: {
$file2->remove;
ok( ! -e $_, "$_ should be gone") for ($file1, $file2);
}

done_testing();

1 comment on commit 935eeed

@pghmcfc
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The use of done_testing() requires Test::More version 0.88 or later, but this requirement is not present in the dist metadata (no version requirement is specified).

Please sign in to comment.