Class/Dir.pm

filter: Statementstmt 97.5% 196 / 201 Branchbran 87.2% 89 / 102 Conditioncond 72.9% 27 / 37 MC/DCmcdc 56.5% 13 / 23 Subroutinesub 95.0% 38 / 40 Podpod 100% 25 / 25 totaltotal 90.6% 388 / 428 CC 66 SCAR 18.4
CC
66
Cov
92%
subsumes
15.3
new
11.2
children
8.5
CRAP 6.3
« Class/File.pm ↑ Summary Class/Entity.pm »
1 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package Path::Class::Dir;
4 {
5 7 $Path::Class::Dir::VERSION = '0.37';
6 }
7  
8 7 ▶use Path::Class::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use Carp();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use parent qw(Path::Class::Entity);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 7 ▶use IO::Dir ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 7 ▶use File::Path ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 7 ▶use File::Temp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 7 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 # updir & curdir on the local machine, for screening them out in
18 # children(). Note that they don't respect 'foreign' semantics.
19 7 my $Updir = __PACKAGE__->_spec->updir;
20 7 my $Curdir = __PACKAGE__->_spec->curdir;
21  
22 498 ▶sub new {
Subroutine new: called CC=11 SCAR=24.2
Pod documented
23 498 my $self = shift->SUPER::new();
24  
25 # If the only arg is undef, it's probably a mistake. Without this
26 # special case here, we'd return the root directory, which is a
27 # lousy thing to do to someone when they made a mistake. Return
28 # undef instead.
29 498 ▶ return if @_==1 && !defined($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 and not defined $_[0] 1 497 498
Condition100%3/3
@_ == 1 and not defined $_[0]
!ll&&!rl&&r
1053921
MC/DC100%2/2
@_ == 1 and not defined $_[0]
@_ == 1 not defined $_[0]
Truth table100%3/3
A and not(B)
A= @_ == 1B= not defined $_[0]
ABresult
0X0
100
111
30  
31 497 my $s = $self->_spec;
32
33 497 ▶ my $first = (@_ == 0 ? $s->curdir :
Branch: 2 branches
BranchTrueFalseTotal
@_ == 0 ? : 8 489 497
!ref($_[0]) && $_[0] eq '' ? : 29 460 489
Condition100%3/3
not ref $_[0] and $_[0] eq ""
!ll&&!rl&&r
23722329
MC/DC100%2/2
not ref $_[0] and $_[0] eq ""
not ref $_[0] $_[0] eq ""
Truth table100%3/3
not(A) and B
A= not ref $_[0]B= $_[0] eq ""
ABresult
0X0
100
111
34 !ref($_[0]) && $_[0] eq '' ? (shift, $s->rootdir) :
35 shift()
36 );
37
38 497 $self->{dirs} = [];
39 497 ▶ if ( Scalar::Util::blessed($first) && $first->isa("Path::Class::Dir") ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($first) and $first->isa('Path::Class::Dir')) { } 237 260 497
Condition66%2/3
builtin::blessed($first) and $first->isa("Path::Class::Dir")
!ll&&!rl&&r
2600237
MC/DC50%1/2
builtin::blessed($first) and $first->isa("Path::Class::Dir")
builtin::blessed($first) $first->isa("Path::Class::Dir")
Truth table66%2/3
A and B
A= builtin::blessed($first)B= $first->isa("Path::Class::Dir")
ABresult
0X0
100
111
40 237 $self->{volume} = $first->{volume};
41 237 push @{$self->{dirs}}, @{$first->{dirs}};
42 }
43 else {
44 260 ($self->{volume}, my $dirs) = $s->splitpath( $s->canonpath("$first") , 1);
45 260 ▶ push @{$self->{dirs}}, $dirs eq $s->rootdir ? "" : $s->splitdir($dirs);
Branch: 1 branch
BranchTrueFalseTotal
$dirs eq $s->rootdir ? : 43 217 260
46 }
47  
48 497 push @{$self->{dirs}}, map {
49 497 Scalar::Util::blessed($_) && $_->isa("Path::Class::Dir")
50 0 ▶ ? @{$_->{dirs}}
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($_) && $_->isa('Path::Class::Dir') ? : 0 127 127
Condition33%1/3
builtin::blessed($_) and $_->isa("Path::Class::Dir")
!ll&&!rl&&r
12700
MC/DC0%0/2
builtin::blessed($_) and $_->isa("Path::Class::Dir")
builtin::blessed($_) $_->isa("Path::Class::Dir")
Truth table33%1/3
A and B
A= builtin::blessed($_)B= $_->isa("Path::Class::Dir")
ABresult
0X0
100
111
51 : $s->splitdir( $s->canonpath($_) )
52 } @_;
53  
54  
55 497 return $self;
56 }
57  
58 112 ▶sub file_class { "Path::Class::File" }
Subroutine file_class: called CC=1 SCAR=0.0
Pod documented
59  
60 61 ▶sub is_dir { 1 }
Subroutine is_dir: called CC=1 SCAR=0.0
Pod documented
61  
62 41 ▶sub as_foreign {
Subroutine as_foreign: called CC=2 SCAR=6.9
Pod documented
63 41 my ($self, $type) = @_;
64  
65 41 my $foreign = do {
66 41 local $self->{file_spec_class} = $self->_spec_class($type);
67 41 $self->SUPER::new;
68 };
69
70 # Clone internal structure
71 41 $foreign->{volume} = $self->{volume};
72 41 my ($u, $fu) = ($self->_spec->updir, $foreign->_spec->updir);
73 41 ▶ $foreign->{dirs} = [ map {$_ eq $u ? $fu : $_} @{$self->{dirs}}];
Branch: 1 branch
BranchTrueFalseTotal
$_ eq $u ? : 1 98 99
74 41 return $foreign;
75 }
76  
77 853 ▶sub stringify {
Subroutine stringify: called CC=1 SCAR=0.0
Pod documented
78 853 my $self = shift;
79 853 my $s = $self->_spec;
80 return $s->catpath($self->{volume},
81 853 $s->catdir(@{$self->{dirs}}),
82 '');
83 }
84  
85 53 ▶sub volume { shift()->{volume} }
Subroutine volume: called CC=1 SCAR=0.0
Pod documented
86  
87 113 ▶sub file {
Subroutine file: called CC=2 SCAR=6.9
Pod documented
88 113 ▶ local $Path::Class::Foreign = $_[0]->{file_spec_class} if $_[0]->{file_spec_class};
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]{"file_spec_class"} 1 112 113
89 113 return $_[0]->file_class->new(@_);
90 }
91  
92 11 ▶sub basename { shift()->{dirs}[-1] }
Subroutine basename: called CC=1 SCAR=0.0
Pod documented
93  
94 35 ▶sub dir_list {
Subroutine dir_list: called CC=6 SCAR=17.9
Pod documented
95 35 my $self = shift;
96 35 my $d = $self->{dirs};
97 35 ▶ return @$d unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 26 9 35
98
99 9 my $offset = shift;
100 9 ▶ if ($offset < 0) { $offset = $#$d + $offset + 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($offset < 0) 5 4 9
101
102 9 ▶ return wantarray ? @$d[$offset .. $#$d] : $d->[$offset] unless @_;
Branch: 2 branches
BranchTrueFalseTotal
unless @_ 4 5 9
wantarray ? : 2 2 4
103
104 5 my $length = shift;
105 5 ▶ if ($length < 0) { $length = $#$d + $length + 1 - $offset }
Branch: 1 branch
BranchTrueFalseTotal
if ($length < 0) 2 3 5
106 5 return @$d[$offset .. $length + $offset - 1];
107 }
108  
109 24 ▶sub components {
Subroutine components: called CC=1 SCAR=0.0
Pod documented
110 24 my $self = shift;
111 24 return $self->dir_list(@_);
112 }
113  
114 74 ▶sub subdir {
Subroutine subdir: called CC=1 SCAR=0.0
Pod documented
115 74 my $self = shift;
116 74 return $self->new($self, @_);
117 }
118  
119 31 ▶sub parent {
Subroutine parent: called CC=6 SCAR=17.9
Pod documented
120 31 my $self = shift;
121 31 my $dirs = $self->{dirs};
122 31 my ($curdir, $updir) = ($self->_spec->curdir, $self->_spec->updir);
123  
124 31 ▶ if ($self->is_absolute) {
Branch: 4 branches
BranchTrueFalseTotal
if ($self->is_absolute) { } 11 20 31
elsif ($self eq $curdir) { } 2 18 20
elsif (not grep({$_ ne $updir;} @$dirs)) { } 1 17 18
elsif (@$dirs == 1) { } 4 13 17
125 11 my $parent = $self->new($self);
126 11 ▶ pop @{$parent->{dirs}} if @$dirs > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @$dirs > 1 10 1 11
127 11 return $parent;
128  
129 } elsif ($self eq $curdir) {
130 2 return $self->new($updir);
131  
132 38 } elsif (!grep {$_ ne $updir} @$dirs) { # All updirs
133 1 return $self->new($self, $updir); # Add one more
134  
135 } elsif (@$dirs == 1) {
136 4 return $self->new($curdir);
137  
138 } else {
139 13 my $parent = $self->new($self);
140 13 pop @{$parent->{dirs}};
141 13 return $parent;
142 }
143 }
144  
145 36 ▶sub relative {
Subroutine relative: called CC=2 SCAR=7.1
Pod documented
146 # File::Spec->abs2rel before version 3.13 returned the empty string
147 # when the two paths were equal - work around it here.
148 36 my $self = shift;
149 36 my $rel = $self->_spec->abs2rel($self->stringify, @_);
150 36 ▶ return $self->new( length $rel ? $rel : $self->_spec->curdir );
Branch: 1 branch
BranchTrueFalseTotal
length $rel ? : 36 0 36
151 }
152  
153 54 ▶sub open { IO::Dir->new(@_) }
Subroutine open: called CC=1 SCAR=0.0
Pod documented
154 15 ▶sub mkpath { File::Path::mkpath(shift()->stringify, @_) }
Subroutine mkpath: called CC=1 SCAR=0.0
Pod documented
155 8 ▶sub rmtree { File::Path::rmtree(shift()->stringify, @_) }
Subroutine rmtree: called CC=1 SCAR=0.0
Pod documented
156  
157 0 ▶sub remove {
Subroutine remove: not called CC=1 SCAR=6.9
Pod documented
158 0 rmdir( shift() );
159 }
160  
161 15 ▶sub traverse {
Subroutine traverse: called CC=1 SCAR=0.0
Pod documented
162 15 my $self = shift;
163 15 my ($callback, @args) = @_;
164 15 my @children = $self->children;
165 return $self->$callback(
166 15 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
167 15 my @inner_args = @_;
168 15 return map { $_->traverse($callback, @inner_args) } @children;
169 },
170 @args
171 15 );
172 }
173  
174 9 ▶sub traverse_if {
Subroutine traverse_if: called CC=1 SCAR=0.0
Pod documented
175 9 my $self = shift;
176 9 my ($callback, $condition, @args) = @_;
177 9 my @children = grep { $condition->($_) } $self->children;
178 return $self->$callback(
179 9 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
180 9 my @inner_args = @_;
181 9 return map { $_->traverse_if($callback, $condition, @inner_args) } @children;
182 },
183 @args
184 9 );
185 }
186  
187 6 ▶sub recurse {
Subroutine recurse: called CC=6 SCAR=17.9
Pod documented
188 6 my $self = shift;
189 6 my %opts = (preorder => 1, depthfirst => 0, @_);
190
191 my $callback = $opts{callback}
192 6 ▶ or Carp::croak( "Must provide a 'callback' parameter to recurse()" );
Branch: 1 branch
BranchTrueFalseTotal
my $callback = $opts{"callback"} or Carp::croak("Must provide a 'callback' parameter to recurse()") 6 0 6
193
194 6 my @queue = ($self);
195
196 6 my $visit_entry;
197 my $visit_dir =
198 $opts{depthfirst} && $opts{preorder}
199 5 ▶ ? sub {
Subroutine __ANON__: called CC=3 SCAR=11.4
200 5 my $dir = shift;
201 5 my $ret = $callback->($dir);
202 5 ▶ unless( ($ret||'') eq $self->PRUNE ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (($ret || "") eq $self->PRUNE) 5 0 5
Condition50%1/2
$ret || ''
l!l
50
MC/DC0%0/1
$ret || ''
$ret
Truth table50%1/2
A
A= $ret
Aresult
11
00
203 5 unshift @queue, $dir->children;
204 }
205 }
206 : $opts{preorder}
207 18 ▶ ? sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
208 18 my $dir = shift;
209 18 my $ret = $callback->($dir);
210 18 ▶ unless( ($ret||'') eq $self->PRUNE ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (($ret || "") eq $self->PRUNE) 16 2 18
Condition100%2/2
$ret || ''
l!l
126
MC/DC100%1/1
$ret || ''
$ret
Truth table100%2/2
A
A= $ret
Aresult
11
00
211 16 push @queue, $dir->children;
212 }
213 }
214 5 ▶ : sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
215 5 my $dir = shift;
216 5 $visit_entry->($_) foreach $dir->children;
217 5 $callback->($dir);
218 6 ▶ };
Branch: 2 branches
BranchTrueFalseTotal
$opts{'depthfirst'} && $opts{'preorder'} ? : 1 5 6
$opts{'preorder'} ? : 4 1 5
Condition100%3/3
$opts{"depthfirst"} and $opts{"preorder"}
!ll&&!rl&&r
411
MC/DC100%2/2
$opts{"depthfirst"} and $opts{"preorder"}
$opts{"depthfirst"} $opts{"preorder"}
Truth table100%3/3
A and B
A= $opts{"depthfirst"}B= $opts{"preorder"}
ABresult
0X0
100
111
219
220 48 ▶ $visit_entry = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
221 48 my $entry = shift;
222 48 ▶ if ($entry->is_dir) { $visit_dir->($entry) } # Will call $callback
Branch: 1 branch
BranchTrueFalseTotal
if ($entry->is_dir) { } 28 20 48
223 20 else { $callback->($entry) }
224 6 };
225
226 6 ▶ while (@queue) {
Branch: 1 branch
BranchTrueFalseTotal
if (@queue) 40 6 46
227 40 $visit_entry->( shift @queue );
228 }
229 }
230  
231 51 ▶sub children {
Subroutine children: called CC=8 SCAR=21.4
Pod documented
232 51 my ($self, %opts) = @_;
233
234 51 ▶ my $dh = $self->open or Carp::croak( "Can't open directory $self: $!" );
Branch: 1 branch
BranchTrueFalseTotal
my $dh = $self->open or Carp::croak("Can't open directory ${self}: $!") 51 0 51
235
236 51 my @out;
237 51 ▶ while (defined(my $entry = $dh->read)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $entry = $dh->read)) 182 51 233
238 182 ▶ next if !$opts{all} && $self->_is_local_dot_dir($entry);
Branch: 1 branch
BranchTrueFalseTotal
if not $opts{"all"} and $self->_is_local_dot_dir($entry) 102 80 182
Condition66%2/3
not $opts{"all"} and $self->_is_local_dot_dir($entry)
!ll&&!rl&&r
080102
MC/DC50%1/2
not $opts{"all"} and $self->_is_local_dot_dir($entry)
not $opts{"all"} $self->_is_local_dot_dir($entry)
Truth table66%2/3
not(A) and B
A= not $opts{"all"}B= $self->_is_local_dot_dir($entry)
ABresult
0X0
100
111
239 80 ▶ next if ($opts{no_hidden} && $entry =~ /^\./);
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"no_hidden"} and $entry =~ /^\./ 0 80 80
Condition33%1/3
$opts{"no_hidden"} and $entry =~ /^\./
!ll&&!rl&&r
8000
MC/DC0%0/2
$opts{"no_hidden"} and $entry =~ /^\./
$opts{"no_hidden"} $entry =~ /^\./
Truth table33%1/3
A and B
A= $opts{"no_hidden"}B= $entry =~ /^\./
ABresult
0X0
100
111
240 80 push @out, $self->file($entry);
241 80 ▶ $out[-1] = $self->subdir($entry) if -d $out[-1];
Branch: 1 branch
BranchTrueFalseTotal
if -d $out[-1] 45 35 80
242 }
243 51 return @out;
244 }
245  
246 182 ▶sub _is_local_dot_dir {
Subroutine _is_local_dot_dir: called CC=2 SCAR=6.9
247 182 my $self = shift;
248 182 my $dir = shift;
249  
250 182 ▶ return ($dir eq $Updir or $dir eq $Curdir);
Condition100%3/3
$dir eq $Updir || $dir eq $Curdir
l!l&&r!l&&!r
515180
MC/DC100%2/2
$dir eq $Updir || $dir eq $Curdir
$dir eq $Updir $dir eq $Curdir
Truth table100%3/3
A || B
A= $dir eq $UpdirB= $dir eq $Curdir
ABresult
1X1
011
000
251 }
252  
253 10 ▶sub next {
Subroutine next: called CC=5 SCAR=16.1
Pod documented
254 10 my $self = shift;
255 10 ▶ unless ($self->{dh}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"dh"}) 2 8 10
256 2 ▶ $self->{dh} = $self->open or Carp::croak( "Can't open directory $self: $!" );
Branch: 1 branch
BranchTrueFalseTotal
$self->{"dh"} = $self->open or Carp::croak("Can't open directory ${self}: $!") 2 0 2
257 }
258
259 10 my $next = $self->{dh}->read;
260 10 ▶ unless (defined $next) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $next) 2 8 10
261 2 delete $self->{dh};
262 ## no critic
263 2 return undef;
264 }
265
266 # Figure out whether it's a file or directory
267 8 my $file = $self->file($next);
268 8 ▶ $file = $self->subdir($next) if -d $file;
Branch: 1 branch
BranchTrueFalseTotal
if -d $file 6 2 8
269 8 return $file;
270 }
271  
272 26 ▶sub subsumes {
Subroutine subsumes: called CC=15 SCAR=27.3
Pod documented
273 26 ▶ Carp::croak "Too many arguments given to subsumes()" if $#_ > 2;
Branch: 1 branch
BranchTrueFalseTotal
if $#_ > 2 0 26 26
274 26 my ($self, $other) = @_;
275 26 ▶ Carp::croak( "No second entity given to subsumes()" ) unless defined $other;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $other 0 26 26
276  
277 26 ▶ $other = $self->new($other) unless eval{$other->isa( "Path::Class::Entity")};
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { $other->isa("Path::Class::Entity") } } 8 18 26
278 26 ▶ $other = $other->dir unless $other->is_dir;
Branch: 1 branch
BranchTrueFalseTotal
unless $other->is_dir 2 24 26
279  
280 26 ▶ if ($self->is_absolute) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->is_absolute) { } 8 18 26
elsif ($other->is_absolute) { } 0 18 18
281 8 $other = $other->absolute;
282 } elsif ($other->is_absolute) {
283 0 $self = $self->absolute;
284 }
285  
286 26 $self = $self->cleanup;
287 26 $other = $other->cleanup;
288  
289 26 ▶ if ($self->volume || $other->volume) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->volume or $other->volume) 1 25 26
Condition66%2/3
$self->volume or $other->volume
l!l&&r!l&&!r
1025
MC/DC50%1/2
$self->volume or $other->volume
$self->volume $other->volume
Truth table66%2/3
A or B
A= $self->volumeB= $other->volume
ABresult
1X1
011
000
290 1 ▶ return 0 unless $other->volume eq $self->volume;
Branch: 1 branch
BranchTrueFalseTotal
unless $other->volume eq $self->volume 0 1 1
291 }
292  
293 # The root dir subsumes everything (but ignore the volume because
294 # we've already checked that)
295 26 ▶ return 1 if "@{$self->{dirs}}" eq "@{$self->new('')->{dirs}}";
Branch: 1 branch
BranchTrueFalseTotal
if join($", @{$self->{"dirs"};}) eq join($", @{$self->new("")->{"dirs"};}) 4 22 26
296  
297 # The current dir subsumes every relative path (unless starting with updir)
298 22 ▶ if ($self eq $self->_spec->curdir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self eq $self->_spec->curdir) 12 10 22
299 12 return $other->{dirs}[0] ne $self->_spec->updir;
300 }
301  
302 10 my $i = 0;
303 10 ▶ while ($i <= $#{ $self->{dirs} }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#{$$self{"dirs"};}) 17 6 23
304 17 ▶ return 0 if $i > $#{ $other->{dirs} };
Branch: 1 branch
BranchTrueFalseTotal
if $i > $#{$$other{"dirs"};} 1 16 17
305 16 ▶ return 0 if $self->{dirs}[$i] ne $other->{dirs}[$i];
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"dirs"}[$i] ne $other->{"dirs"}[$i] 3 13 16
306 13 $i++;
307 }
308 6 return 1;
309 }
310  
311 12 ▶sub contains {
Subroutine contains: called CC=6 SCAR=18.5
Pod documented
312 12 ▶ Carp::croak "Too many arguments given to contains()" if $#_ > 2;
Branch: 1 branch
BranchTrueFalseTotal
if $#_ > 2 0 12 12
313 12 my ($self, $other) = @_;
314 12 ▶ Carp::croak "No second entity given to contains()" unless defined $other;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $other 0 12 12
315 12 ▶ return unless -d $self and (-e $other or -l $other);
Branch: 1 branch
BranchTrueFalseTotal
unless -d $self and -e $other || -l $other 2 10 12
Condition66%4/6
-d $self and -e $other || -l $other
!ll&&!rl&&r
0210
-e $other or -l $other
l!l&&r!l&&!r
1002
MC/DC33%1/3
-d $self and -e $other || -l $other
-d $self -e $other -l $other
Truth table50%2/4
A and B or C
A= -d $selfB= -e $otherC= -l $other
ABCresult
0XX0
1000
11X1
1011
316  
317 # We're going to resolve the path, and don't want side effects on the objects
318 # so clone them. This also handles strings passed as $other.
319 10 $self= $self->new($self)->resolve;
320 10 $other= $self->new($other)->resolve;
321
322 10 return $self->subsumes($other);
323 }
324  
325 0 ▶sub tempfile {
Subroutine tempfile: not called CC=1 SCAR=6.9
Pod documented
326 0 my $self = shift;
327 0 return File::Temp::tempfile(@_, DIR => $self->stringify);
328 }
329  
330 7 1;
331 __END__
« Class/File.pm ↑ Summary Class/Entity.pm »