lib/Badger/Filesystem/Virtual.pm

filter: Statementstmt 92.8% 78 / 84 Branchbran 72.7% 32 / 44 Conditioncond 52.0% 13 / 25 MC/DCmcdc 7.1% 1 / 14 Subroutinesub 87.5% 7 / 8 Podpod 100% 6 / 6 totaltotal 75.6% 137 / 181 CC 33 SCAR 25.1
CC
33
Cov
80%
roots
19.7
read_directory
9.9
definitive_paths
6.0
CRAP 12.3
« lib/Badger/Period.pm ↑ Summary lib/Badger/Test/Manager.pm »
1 #========================================================================
2 #
3 # Badger::Filesystem::Virtual
4 #
5 # DESCRIPTION
6 # Subclass of Badger::Filesystem which implements a virtual filesystem
7 # composed from several source directories, conceptually layered on
8 # top of each other.
9 #
10 # AUTHOR
11 # Andy Wardley <abw@wardley.org>
12 #
13 #========================================================================
14  
15 package Badger::Filesystem::Virtual;
16  
17 7 ▶use Badger::Debug ':dump';
Subroutine BEGIN: called CC=1 SCAR=0.0
18 use Badger::Class
19 7 ▶ version => 0.01,
Subroutine BEGIN: called CC=1 SCAR=0.0
20 debug => 0,
21 base => 'Badger::Filesystem',
22 # accessors => 'root', # interferes
23 utils => 'blessed',
24 constants => 'ARRAY CODE',
25 constant => {
26 VFS => __PACKAGE__,
27 virtual => __PACKAGE__,
28 PATH_METHOD => 'path',
29 PATHS_METHOD => 'paths',
30 ROOTS_METHOD => 'roots',
31 },
32 exports => {
33 any => 'VFS',
34 },
35 messages => {
36 bad_root => 'Invalid root directory: %s',
37 max_roots => 'The number of virtual filesystem roots exceeds the max_roots limit of %s',
38 7 };
39  
40 7 *definitive = \&definitive_write;
41  
42 7 ▶our $MAX_ROOTS = 32 unless defined $MAX_ROOTS;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Badger::Filesystem::Virtual::MAX_ROOTS 7 0 7
43  
44 23 ▶sub init {
Subroutine init: called CC=5 SCAR=16.3
Pod documented
45 23 my ($self, $config) = @_;
46  
47 # let the base class have a go first, so it can set rootdir et al
48 23 $self->SUPER::init($config);
49  
50 # root can be a single item or list ref
51 23 ▶ my $root = $config->{ root } || $self->{ rootdir };
Condition66%2/3
$config->{'root'} || $self->{'rootdir'}
l!l&&r!l&&!r
2210
MC/DC0%0/2
$config->{'root'} || $self->{'rootdir'}
$config->{'root'} $self->{'rootdir'}
Truth table66%2/3
A || B
A= $config->{'root'}B= $self->{'rootdir'}
ABresult
1X1
011
000
52 23 ▶ $root = [$root] unless ref $root eq ARRAY;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $root eq "ARRAY" 20 3 23
53 23 $self->{ root } = $root;
54  
55 # the dynamic flag indicates that the list of roots can change so we must
56 # recompute them each time we use them. max_roots sets a limit on the
57 # expansion to prevent runaways
58 23 $self->{ dynamic } = $config->{ dynamic };
59 $self->{ max_roots } =
60 defined $config->{ max_roots }
61 ? $config->{ max_roots }
62 23 ▶ : $MAX_ROOTS;
Branch: 1 branch
BranchTrueFalseTotal
defined $config->{'max_roots'} ? : 0 23 23
63  
64 # we must set cwd to / so that the relative -> absolute path translation
65 # works as expected. The concept of having a current working directory
66 # in a VFS is just a bit too weird to contemplate anyway.
67 23 $self->{ cwd } = $self->{ rootdir };
68  
69 23 ▶ $self->debug("Virtual root: ", join(', ', @$root), "\n" ) if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Badger::Filesystem::Virtual::DEBUG 0 23 23
70  
71 23 return $self;
72 }
73  
74 249 ▶sub roots {
Subroutine roots: called CC=18 SCAR=29.8
Pod documented
75 249 my $self = shift;
76  
77 249 ▶ if (my $roots = $self->{ roots }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $roots = $self->{"roots"}) 224 25 249
78 return wantarray
79 224 ▶ ? @$roots
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 216 8 224
80 : $roots;
81 }
82  
83 25 my $max = $self->{ max_roots };
84 25 my @paths = @{ $self->{ root } };
85 25 my (@roots, $type, $paths, $dir, $code);
86  
87 # If a positive max_roots is defined then we'll limit the number of
88 # roots we resolve. If it's zero or negative then it will pre-decrement
89 # before being tested so will always be true
90 25 ▶ while (@paths && --$max) {
Branch: 1 branch
BranchTrueFalseTotal
if (@paths and --$max) 91 25 116
Condition66%2/3
@paths and --$max
!ll&&!rl&&r
25091
MC/DC50%1/2
@paths and --$max
@paths --$max
Truth table66%2/3
A and B
A= @pathsB= --$max
ABresult
0X0
100
111
91 91 ▶ $dir = shift @paths || next;
Condition50%1/2
shift @paths || (next)
l!l
910
MC/DC0%0/1
shift @paths || (next)
shift @paths
Truth table50%1/2
A
A= shift @paths
Aresult
11
00
92  
93 91 ▶ $type = ref $dir || do {
Condition100%3/3
ref $dir || do { '???'; push @roots, $dir; next }
l!l&&r!l&&!r
50365
MC/DC0%0/2
ref $dir || do { '???'; push @roots, $dir; next }
ref $dir do { '???'; push @roots, $dir; next }
Truth table33%1/3
A || B
A= ref $dirB= do { '???'; push @roots, $dir; next }
ABresult
1X1
011
000
94 # non-reference paths get added as they are
95 41 $self->debug("discovered root directory: $dir\n") if DEBUG;
96 41 push(@roots, $dir);
97 41 next;
98 };
99  
100 # anything else can expand out to one or more paths, each of which
101 # can expand recursively, so we push all new paths back onto the
102 # candidate list and test each in turn.
103  
104 50 ▶ if ($type eq CODE) {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'CODE') { } 8 42 50
elsif ($type eq 'ARRAY') { } 3 39 42
elsif (builtin::blessed($dir)) { } 39 0 39
105 # call code ref
106 8 $paths = $dir->();
107 8 $self->debug(
108 "discovered root directories from code ref: ",
109 $self->dump_data_inline($paths), "\n"
110 ) if DEBUG;
111 8 ▶ unshift(@paths, ref $paths eq ARRAY ? @$paths : $paths);
Branch: 1 branch
BranchTrueFalseTotal
ref $paths eq 'ARRAY' ? : 8 0 8
112 8 next;
113 }
114 elsif ($type eq ARRAY) {
115 # expand list ref
116 3 $self->debug(
117 "discovered root directories from code ref: ",
118 join(', ', @$dir), "\n"
119 ) if DEBUG;
120 3 unshift(@paths, @$dir);
121 3 next;
122 }
123 elsif (blessed $dir) {
124 # see if object has a path(), paths() or roots() method
125 # TODO: this is broken - we don't want to recompute paths
126 # each time just because we're using an object that has a
127 # path
128 39 ▶ if ($code = $dir->can(PATH_METHOD)
Branch: 1 branch
BranchTrueFalseTotal
if ($code = $dir->can("path") || $dir->can("paths") || $dir->can("roots")) 39 0 39
Condition33%2/6
$dir->can('path') || $dir->can('paths') || $dir->can('roots')
l!l&&r!l&&!r
3900
$dir->can("path") or $dir->can("paths")
l!l&&r!l&&!r
3900
MC/DC0%0/3
$dir->can('path') || $dir->can('paths') || $dir->can('roots')
$dir->can("path") $dir->can("paths") $dir->can('roots')
Truth table25%1/4
A or B || C
A= $dir->can("path")B= $dir->can("paths")C= $dir->can('roots')
ABCresult
1XX1
01X1
0011
0000
129 || $dir->can(PATHS_METHOD)
130 || $dir->can(ROOTS_METHOD) ) {
131 39 $paths = $code->($dir);
132 39 $self->debug(
133 "discovered root directories from $type object: $paths / ",
134 $self->dump_data_inline($paths), "\n"
135 ) if DEBUG;
136 39 ▶ unshift(@paths, ref $paths eq ARRAY ? @$paths : $paths);
Branch: 1 branch
BranchTrueFalseTotal
ref $paths eq 'ARRAY' ? : 0 39 39
137 39 next;
138 }
139 }
140  
141 0 $self->error( bad_root => $dir );
142 }
143  
144 # anything left in @paths means we must have blown the max_roots limit
145 return $self->error_msg( max_roots => $self->{ max_roots } )
146 25 ▶ if @paths;
Branch: 1 branch
BranchTrueFalseTotal
if @paths 0 25 25
147  
148 # we can cache roots if all are static and the dynamic flag isn't set
149 $self->{ roots } = \@roots
150 25 ▶ unless $self->{ dynamic };
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"dynamic"} 22 3 25
151  
152 25 $self->debug("resolved roots: [\n ", join("\n ", @roots), "\n]\n") if DEBUG;
153  
154 return wantarray
155 ? @roots
156 25 ▶ : \@roots;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 24 1 25
157 }
158  
159 0 ▶sub definitive_paths {
Subroutine definitive_paths: not called CC=2 SCAR=17.9
Pod documented
160 0 my $self = shift;
161 0 my $path = $self->absolute(@_);
162 0 my @paths = map { $self->merge_paths($_, $path) } $self->roots;
163 return wantarray
164 ? @paths
165 0 ▶ : \@paths;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
166 }
167  
168 9 ▶sub definitive_write {
Subroutine definitive_write: called CC=1 SCAR=0.0
Pod documented
169 9 my $self = shift;
170 9 $self->debug("definitive_write(", join(', ', @_), ")\n") if DEBUG;
171 9 my $path = $self->absolute(@_);
172 9 return $self->join_directory($self->roots->[0], $path);
173 }
174  
175 188 ▶sub definitive_read {
Subroutine definitive_read: called CC=3 SCAR=11.0
Pod documented
176 188 my $self = shift;
177 188 my $path = $self->absolute(@_);
178 188 my ($base, $full);
179  
180 188 $self->debug("definitive_read($path)") if DEBUG;
181  
182 188 foreach $base ($self->roots) {
183 221 $full = $self->merge_paths($base, $path);
184 221 $self->debug("looking for [$base] + [$path] => $full\n") if DEBUG;
185 221 ▶ return $full if -e $full;
Branch: 1 branch
BranchTrueFalseTotal
if -e $full 183 38 221
186 38 $self->debug("not found\n") if DEBUG;
187 }
188 5 return undef;
189 }
190  
191 52 ▶sub read_directory {
Subroutine read_directory: called CC=9 SCAR=23.0
Pod documented
192 52 my $self = shift;
193 52 my $path = $self->absolute(shift);
194 52 my $all = shift;
195 52 my ($base, $full, $dirh, $item, @items, %seen);
196  
197 52 require IO::Dir;
198  
199 52 foreach $base ($self->roots) {
200 58 $full = $self->join_directory($base, $path);
201 58 $self->debug("Opening directory: $full\n") if DEBUG;
202 58 ▶ $dirh = IO::Dir->new($full)
Condition50%1/2
'IO::Dir'->new($full) || (next)
l!l
580
MC/DC0%0/1
'IO::Dir'->new($full) || (next)
'IO::Dir'->new($full)
Truth table50%1/2
A
A= 'IO::Dir'->new($full)
Aresult
11
00
203 || next;
204 # Some directory may not exist, so ignore them
205 # || $self->error_msg( open_failed => directory => $full => $! );
206 58 ▶ while (defined ($item = $dirh->read)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($item = $dirh->read)) 219 58 277
207 219 if (DEBUG) {
208 if ($seen{ $item }) {
209 $self->debug("skipping $item (already seen)");
210 }
211 else {
212 $self->debug("adding $item");
213 }
214 }
215 219 ▶ push(@items, $item) unless $seen{ $item }++;
Branch: 1 branch
BranchTrueFalseTotal
unless $seen{$item}++ 201 18 219
216 }
217 58 $dirh->close;
218 }
219 @items = $self->FILESPEC->no_upwards(@items)
220 52 ▶ unless $all || ref $self && $self->{ all_entries };
Branch: 1 branch
BranchTrueFalseTotal
unless $all or ref $self and $self->{"all_entries"} 52 0 52
Condition33%2/6
$all or ref $self and $self->{"all_entries"}
l!l&&r!l&&!r
0052
ref $self and $self->{"all_entries"}
!ll&&!rl&&r
0520
MC/DC0%0/3
$all or ref $self and $self->{"all_entries"}
$all ref $self $self->{"all_entries"}
Truth table25%1/4
A or B and C
A= $allB= ref $selfC= $self->{"all_entries"}
ABCresult
1XX1
0111
00X0
0100
221  
222 52 ▶ return wantarray ? @items : \@items;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 52 0 52
223 }
224  
225  
226 7 1;
227  
228 __END__
« lib/Badger/Period.pm ↑ Summary lib/Badger/Test/Manager.pm »