DistDir.pm

filter: Statementstmt 99.4% 165 / 166 Branchbran 70.0% 63 / 90 Conditioncond 69.2% 36 / 52 MC/DCmcdc 44.1% 15 / 34 Subroutinesub 100% 21 / 21 Podpod 100% 6 / 6 totaltotal 82.9% 306 / 369 CC 70 SCAR 23.5
CC
70
Cov
86%
copy
19.3
_distribution_for_module
14.4
_copy_directory_contents
11.8
CRAP 10.5
↑ Summary DistDir/CLI.pm »
1 package File::DistDir;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 ▶use Cwd qw(abs_path getcwd);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use English qw(-no_match_vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use File::Basename qw(basename dirname);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use File::Copy ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use File::Path qw(make_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use File::ShareDir qw(dist_dir);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use ExtUtils::Installed;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use Module::Metadata;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 4 our $VERSION = '1.0.1';
17  
18 ########################################################################
19 6 ▶sub new {
Subroutine new: called CC=9 SCAR=24.1
Pod documented
20 ########################################################################
21 6 my ( $class, %args ) = @_;
22  
23 6 my $distribution = $args{distribution};
24 6 my $module = $args{module};
25  
26 6 ▶ die "ERROR: specify either distribution or module, not both\n"
Branch: 1 branch
BranchTrueFalseTotal
if defined $distribution and defined $module 0 6 6
Condition66%2/3
defined $distribution and defined $module
!ll&&!rl&&r
330
MC/DC0%0/2
defined $distribution and defined $module
defined $distribution defined $module
Truth table66%2/3
A and B
A= defined $distributionB= defined $module
ABresult
0X0
100
111
27 if defined $distribution && defined $module;
28  
29 6 ▶ if ( defined $module ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $module) 3 3 6
30 3 ▶ die "ERROR: module is required\n"
Branch: 1 branch
BranchTrueFalseTotal
unless length $module 0 3 3
31 if !length $module;
32  
33 3 $distribution = _distribution_for_module($module);
34 }
35  
36 6 ▶ die "ERROR: distribution or module is required\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $distribution and length $distribution 0 6 6
Condition33%1/3
defined $distribution and length $distribution
!ll&&!rl&&r
006
MC/DC0%0/2
defined $distribution and length $distribution
defined $distribution length $distribution
Truth table33%1/3
A and B
A= defined $distributionB= length $distribution
ABresult
0X0
100
111
37 if !defined $distribution || !length $distribution;
38  
39 6 my $root = abs_path( dist_dir($distribution) );
40  
41 6 ▶ die sprintf "ERROR: distribution share directory not found: %s\n", $distribution
Branch: 1 branch
BranchTrueFalseTotal
unless defined $root and -d $root 0 6 6
Condition33%1/3
defined $root and -d $root
!ll&&!rl&&r
006
MC/DC0%0/2
defined $root and -d $root
defined $root -d $root
Truth table33%1/3
A and B
A= defined $rootB= -d $root
ABresult
0X0
100
111
42 if !defined $root || !-d $root;
43  
44 6 return bless {
45 distribution => $distribution,
46 dir => $root,
47 }, $class;
48 }
49  
50 ########################################################################
51 3 ▶sub _distribution_for_module {
Subroutine _distribution_for_module: called CC=13 SCAR=26.7
52 ########################################################################
53 3 my ($module) = @_;
54  
55 3 ▶ die sprintf "ERROR: invalid module name: %s\n", $module
Branch: 1 branch
BranchTrueFalseTotal
unless $module =~ /\A[A-Za-z_]\w*(?:::[A-Za-z_]\w*)*\z/ 0 3 3
56 if $module !~ /\A[A-Za-z_]\w*(?:::[A-Za-z_]\w*)*\z/;
57  
58 # Most packages share their name with the distribution that contains them.
59 # Try that cheap lookup before inspecting installed .packlist files.
60 3 my $candidate = $module;
61 3 $candidate =~ s/::/-/g;
62  
63 3 my $share_dir = eval { dist_dir($candidate) };
64 3 ▶ return $candidate
Branch: 1 branch
BranchTrueFalseTotal
if defined $share_dir and -d $share_dir 1 2 3
Condition66%2/3
defined $share_dir and -d $share_dir
!ll&&!rl&&r
201
MC/DC50%1/2
defined $share_dir and -d $share_dir
defined $share_dir -d $share_dir
Truth table66%2/3
A and B
A= defined $share_dirB= -d $share_dir
ABresult
0X0
100
111
65 if defined $share_dir && -d $share_dir;
66  
67 2 my $metadata = Module::Metadata->new_from_module($module);
68 2 ▶ die sprintf "ERROR: module not installed: %s\n", $module
Branch: 1 branch
BranchTrueFalseTotal
unless $metadata 0 2 2
69 if !$metadata;
70  
71 2 my $filename = abs_path( $metadata->filename );
72 2 ▶ die sprintf "ERROR: cannot locate installed module file: %s\n", $module
Branch: 1 branch
BranchTrueFalseTotal
unless defined $filename 0 2 2
73 if !defined $filename;
74  
75 2 my $installed = ExtUtils::Installed->new();
76 2 my @owners;
77  
78 2 foreach my $owner ( $installed->modules ) {
79 2 my $packlist = $installed->packlist($owner);
80 2 ▶ next if !$packlist;
Branch: 1 branch
BranchTrueFalseTotal
unless $packlist 0 2 2
81  
82 2 foreach my $file ( keys %{$packlist} ) {
83 2 my $installed_file = abs_path($file);
84 2 ▶ next if !defined $installed_file || $installed_file ne $filename;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $installed_file or $installed_file ne $filename 0 2 2
Condition33%1/3
not defined $installed_file or $installed_file ne $filename
l!l&&r!l&&!r
002
MC/DC0%0/2
not defined $installed_file or $installed_file ne $filename
not defined $installed_file $installed_file ne $filename
Truth table33%1/3
not(A) or B
A= not defined $installed_fileB= $installed_file ne $filename
ABresult
1X1
011
000
85  
86 2 push @owners, $owner;
87 2 last;
88 }
89 }
90  
91 2 ▶ die sprintf "ERROR: cannot determine installed distribution for module %s; use distribution => NAME\n", $module
Branch: 1 branch
BranchTrueFalseTotal
unless @owners 0 2 2
92 if !@owners;
93  
94 2 ▶ die sprintf "ERROR: ambiguous distribution ownership for module %s: %s\n", $module, join( ', ', sort @owners )
Branch: 1 branch
BranchTrueFalseTotal
if @owners > 1 0 2 2
95 if @owners > 1;
96  
97 2 my $distribution = $owners[0];
98 2 $distribution =~ s/::/-/g;
99  
100 2 return $distribution;
101 }
102  
103 ########################################################################
104 111 ▶sub dir {
Subroutine dir: called CC=1 SCAR=0.0
Pod documented
105 ########################################################################
106 111 my ($self) = @_;
107 111 return $self->{dir};
108 }
109  
110 ########################################################################
111 10 ▶sub path {
Subroutine path: called CC=6 SCAR=18.1
Pod documented
112 ########################################################################
113 10 my ( $self, $relative ) = @_;
114  
115 10 ▶ $relative //= q{.};
Condition50%1/2
$relative //= "."
l!l
100
MC/DC0%0/1
$relative //= "."
$relative
Truth table50%1/2
A
A= $relative
Aresult
11
00
116  
117 10 ▶ die "ERROR: source must be relative to the distribution directory\n"
Branch: 1 branch
BranchTrueFalseTotal
if "File::Spec"->file_name_is_absolute($relative) 0 10 10
118 if File::Spec->file_name_is_absolute($relative);
119  
120 10 my $root = $self->dir;
121 10 my $candidate = File::Spec->catfile( $root, $relative );
122 10 my $resolved = abs_path($candidate);
123  
124 10 ▶ die sprintf "ERROR: source not found: %s\n", $relative
Branch: 1 branch
BranchTrueFalseTotal
unless defined $resolved 0 10 10
125 if !defined $resolved;
126  
127 10 ▶ die sprintf "ERROR: source is outside distribution directory: %s\n", $relative
Branch: 1 branch
BranchTrueFalseTotal
if $resolved ne $root and index($resolved, $root . '/') != 0 1 9 10
Condition100%3/3
$resolved ne $root and index($resolved, $root . '/') != 0
!ll&&!rl&&r
451
MC/DC100%2/2
$resolved ne $root and index($resolved, $root . '/') != 0
$resolved ne $root index($resolved, $root . '/') != 0
Truth table100%3/3
A and B
A= $resolved ne $rootB= index($resolved, $root . '/') != 0
ABresult
0X0
100
111
128 if $resolved ne $root && index( $resolved, $root . q{/} ) != 0;
129  
130 9 return $resolved;
131 }
132  
133 ########################################################################
134 9 ▶sub copy {
Subroutine copy: called CC=17 SCAR=29.6
Pod documented
135 ########################################################################
136 9 my ( $self, $source, $destination, %options ) = @_;
137  
138 9 ▶ die "ERROR: copy requires a source\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $source 0 9 9
139 if !defined $source;
140  
141 9 ▶ die "ERROR: destination is required for file copying\n"
Branch: 1 branch
BranchTrueFalseTotal
if not defined $destination and $source ne "." 0 9 9
Condition66%2/3
not defined $destination and $source ne "."
!ll&&!rl&&r
810
MC/DC0%0/2
not defined $destination and $source ne "."
not defined $destination $source ne "."
Truth table66%2/3
not(A) and B
A= not defined $destinationB= $source ne "."
ABresult
0X0
100
111
142 if !defined $destination && $source ne q{.};
143  
144 9 ▶ $destination //= q{.};
Condition100%2/2
$destination //= "."
l!l
81
MC/DC100%1/1
$destination //= "."
$destination
Truth table100%2/2
A
A= $destination
Aresult
11
00
145  
146 9 my $source_path = File::Spec->catfile( $self->dir, $source );
147 9 _reject_symlink_components($source_path);
148  
149 8 my $resolved = $self->path($source);
150 8 _reject_symlink_components($destination);
151  
152 7 ▶ if ( -d $resolved ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $resolved) 6 1 7
153 die "ERROR: copying directories requires recursive => 1\n"
154 6 ▶ if !$options{recursive};
Branch: 1 branch
BranchTrueFalseTotal
unless $options{"recursive"} 1 5 6
155  
156 5 ▶ die sprintf "ERROR: destination is not a directory: %s\n", $destination
Branch: 1 branch
BranchTrueFalseTotal
if -e $destination and not -d $destination 0 5 5
Condition66%2/3
-e $destination and not -d $destination
!ll&&!rl&&r
410
MC/DC0%0/2
-e $destination and not -d $destination
-e $destination not -d $destination
Truth table66%2/3
A and not(B)
A= -e $destinationB= not -d $destination
ABresult
0X0
100
111
157 if -e $destination && !-d $destination;
158  
159 5 my $dest_abs = File::Spec->rel2abs($destination);
160 5 my $dest_existing = abs_path($destination);
161 5 ▶ if ( defined $dest_existing ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $dest_existing) 5 0 5
162 5 $dest_abs = $dest_existing;
163 }
164  
165 5 ▶ die sprintf "ERROR: destination is inside source directory: %s\n", $destination
Branch: 1 branch
BranchTrueFalseTotal
if $dest_abs eq $resolved or index($dest_abs, $resolved . '/') == 0 1 4 5
Condition66%2/3
$dest_abs eq $resolved or index($dest_abs, $resolved . '/') == 0
l!l&&r!l&&!r
014
MC/DC50%1/2
$dest_abs eq $resolved or index($dest_abs, $resolved . '/') == 0
$dest_abs eq $resolved index($dest_abs, $resolved . '/') == 0
Truth table66%2/3
A or B
A= $dest_abs eq $resolvedB= index($dest_abs, $resolved . '/') == 0
ABresult
1X1
011
000
166 if $dest_abs eq $resolved || index( $dest_abs, $resolved . q{/} ) == 0;
167  
168 4 ▶ make_path($destination)
Branch: 1 branch
BranchTrueFalseTotal
unless -d $destination 3 1 4
169 if !-d $destination;
170  
171 4 _copy_directory_contents( $resolved, $destination );
172 3 return 1;
173 }
174  
175 1 ▶ die sprintf "ERROR: source is not a regular file: %s\n", $source
Branch: 1 branch
BranchTrueFalseTotal
unless -f $resolved 0 1 1
176 if !-f $resolved;
177  
178 1 ▶ if ( -d $destination ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $destination) 0 1 1
179 0 $destination = File::Spec->catfile( $destination, basename($source) );
180 }
181  
182 1 ▶ die sprintf "ERROR: destination directory does not exist: %s\n", dirname($destination)
Branch: 1 branch
BranchTrueFalseTotal
unless -d dirname($destination) 0 1 1
183 if !-d dirname($destination);
184  
185 1 ▶ die sprintf "ERROR: unable to copy %s => %s: %s\n", $resolved, $destination, $OS_ERROR
Branch: 1 branch
BranchTrueFalseTotal
unless File::Copy::copy($resolved, $destination) 0 1 1
186 if !File::Copy::copy( $resolved, $destination );
187  
188 1 return 1;
189 }
190  
191 ########################################################################
192 11 ▶sub list {
Subroutine list: called CC=1 SCAR=0.0
Pod documented
193 ########################################################################
194 11 my ($self) = @_;
195  
196 11 my @files;
197 11 _collect_entries( $self->dir, q{}, \@files );
198  
199 11 return @files;
200 }
201  
202 ########################################################################
203 12 ▶sub find {
Subroutine find: called CC=8 SCAR=20.8
Pod documented
204 ########################################################################
205 12 my ( $self, $pattern ) = @_;
206  
207 12 ▶ die "ERROR: find requires a filename pattern\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pattern and length $pattern 2 10 12
Condition66%2/3
defined $pattern and length $pattern
!ll&&!rl&&r
2010
MC/DC50%1/2
defined $pattern and length $pattern
defined $pattern length $pattern
Truth table66%2/3
A and B
A= defined $patternB= length $pattern
ABresult
0X0
100
111
208 if !defined $pattern || !length $pattern;
209  
210 # A pattern containing a slash matches a path relative to the share
211 # directory; otherwise it matches the basename at any depth.
212 10 my $expression = quotemeta($pattern);
213 10 $expression =~ s/\\\*/.*/g;
214 10 $expression =~ s/\\\?/./g;
215 10 my $regex = qr/\A$expression\z/;
216 10 my $match_path = index( $pattern, q{/} ) >= 0;
217  
218 10 my @matches;
219 10 foreach my $relative ( $self->list ) {
220 80 my $full = File::Spec->catfile( $self->dir, $relative );
221 80 ▶ next if !-f $full || -l $full;
Branch: 1 branch
BranchTrueFalseTotal
if not -f $full or -l $full 40 40 80
Condition66%2/3
not -f $full or -l $full
l!l&&r!l&&!r
40040
MC/DC50%1/2
not -f $full or -l $full
not -f $full -l $full
Truth table66%2/3
not(A) or B
A= not -f $fullB= -l $full
ABresult
1X1
011
000
222  
223 40 ▶ my $target = $match_path ? $relative : basename($relative);
Branch: 1 branch
BranchTrueFalseTotal
$match_path ? : 8 32 40
224 40 ▶ push @matches, $relative
Branch: 1 branch
BranchTrueFalseTotal
if $target =~ /$regex/ 14 26 40
225 if $target =~ $regex;
226 }
227  
228 10 return @matches;
229 }
230  
231 ########################################################################
232 42 ▶sub _collect_entries {
Subroutine _collect_entries: called CC=7 SCAR=19.5
233 ########################################################################
234 42 my ( $root, $prefix, $result ) = @_;
235  
236 42 ▶ opendir my $dh, $root
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $root or die sprintf("ERROR: cannot open directory %s: %s\n", $root, $File::DistDir::OS_ERROR) 42 0 42
237 or die sprintf "ERROR: cannot open directory %s: %s\n", $root, $OS_ERROR;
238  
239 42 ▶ my @entries = sort grep { $_ ne q{.} && $_ ne q{..} } readdir $dh;
Condition100%3/3
$_ ne '.' && $_ ne '..'
!ll&&!rl&&r
424283
MC/DC100%2/2
$_ ne '.' && $_ ne '..'
$_ ne '.' $_ ne '..'
Truth table100%3/3
A && B
A= $_ ne '.'B= $_ ne '..'
ABresult
0X0
100
111
240 42 closedir $dh;
241  
242 42 foreach my $entry (@entries) {
243 83 ▶ my $relative = length $prefix ? File::Spec->catfile( $prefix, $entry ) : $entry;
Branch: 1 branch
BranchTrueFalseTotal
length $prefix ? : 31 52 83
244 83 my $full = File::Spec->catfile( $root, $entry );
245  
246 83 push @{$result}, $relative;
247  
248 83 ▶ if ( -d $full && !-l $full ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $full and not -l $full) 31 52 83
Condition100%3/3
-d $full and not -l $full
!ll&&!rl&&r
421031
MC/DC100%2/2
-d $full and not -l $full
-d $full not -l $full
Truth table100%3/3
A and not(B)
A= -d $fullB= not -l $full
ABresult
0X0
100
111
249 31 _collect_entries( $full, $relative, $result );
250 }
251 }
252  
253 42 return;
254 }
255  
256 ########################################################################
257 28 ▶sub _reject_symlink_components {
Subroutine _reject_symlink_components: called CC=6 SCAR=17.9
258 ########################################################################
259 28 my ($path) = @_;
260  
261 # Preserve explicit .. components while inspecting the actual path walked
262 # by the filesystem. rel2abs() would normalize them prematurely.
263 28 ▶ my $absolute
Branch: 1 branch
BranchTrueFalseTotal
'File::Spec'->file_name_is_absolute($path) ? : 24 4 28
264 = File::Spec->file_name_is_absolute($path)
265 ? $path
266 : File::Spec->catfile( getcwd(), $path );
267 28 my $current = File::Spec->rootdir;
268  
269 28 foreach my $part ( File::Spec->splitdir($absolute) ) {
270 139 ▶ next if !length $part || $part eq q{.};
Branch: 1 branch
BranchTrueFalseTotal
if not length $part or $part eq "." 33 106 139
Condition100%3/3
not length $part or $part eq "."
l!l&&r!l&&!r
285106
MC/DC100%2/2
not length $part or $part eq "."
not length $part $part eq "."
Truth table100%3/3
not(A) or B
A= not length $partB= $part eq "."
ABresult
1X1
011
000
271  
272 106 $current = File::Spec->catdir( $current, $part );
273  
274 106 ▶ die sprintf "ERROR: symbolic links are not supported: %s\n", $current
Branch: 1 branch
BranchTrueFalseTotal
if -l $current 2 104 106
275 if -l $current;
276 }
277  
278 26 return;
279 }
280  
281 ########################################################################
282 7 ▶sub _copy_directory_contents {
Subroutine _copy_directory_contents: called CC=11 SCAR=24.6
283 ########################################################################
284 7 my ( $source, $destination ) = @_;
285  
286 7 ▶ opendir my $dh, $source
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $source or die sprintf("ERROR: cannot open directory %s: %s\n", $source, $File::DistDir::OS_ERROR) 7 0 7
287 or die sprintf "ERROR: cannot open directory %s: %s\n", $source, $OS_ERROR;
288  
289 7 ▶ my @entries = grep { $_ ne q{.} && $_ ne q{..} } readdir $dh;
Condition100%3/3
$_ ne '.' && $_ ne '..'
!ll&&!rl&&r
7711
MC/DC100%2/2
$_ ne '.' && $_ ne '..'
$_ ne '.' $_ ne '..'
Truth table100%3/3
A && B
A= $_ ne '.'B= $_ ne '..'
ABresult
0X0
100
111
290 7 closedir $dh;
291  
292 7 foreach my $entry (@entries) {
293 11 my $from = File::Spec->catfile( $source, $entry );
294 11 my $to = File::Spec->catfile( $destination, $entry );
295  
296 11 _reject_symlink_components($to);
297  
298 11 ▶ die sprintf "ERROR: symbolic links are not supported: %s\n", $from
Branch: 1 branch
BranchTrueFalseTotal
if -l $from 1 10 11
299 if -l $from;
300  
301 10 ▶ if ( -d $from ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $from) 3 7 10
302 3 ▶ die sprintf "ERROR: destination is not a directory: %s\n", $to
Branch: 1 branch
BranchTrueFalseTotal
if -e $to and not -d $to 0 3 3
Condition33%1/3
-e $to and not -d $to
!ll&&!rl&&r
300
MC/DC0%0/2
-e $to and not -d $to
-e $to not -d $to
Truth table33%1/3
A and not(B)
A= -e $toB= not -d $to
ABresult
0X0
100
111
303 if -e $to && !-d $to;
304  
305 3 ▶ make_path($to)
Branch: 1 branch
BranchTrueFalseTotal
unless -d $to 3 0 3
306 if !-d $to;
307  
308 3 _copy_directory_contents( $from, $to );
309 3 next;
310 }
311  
312 7 ▶ die sprintf "ERROR: unsupported resource type: %s\n", $from
Branch: 1 branch
BranchTrueFalseTotal
unless -f $from 0 7 7
313 if !-f $from;
314  
315 7 ▶ die sprintf "ERROR: unable to copy %s => %s: %s\n", $from, $to, $OS_ERROR
Branch: 1 branch
BranchTrueFalseTotal
unless File::Copy::copy($from, $to) 0 7 7
316 if !File::Copy::copy( $from, $to );
317 }
318  
319 6 return;
320 }
321  
322 4 1;
323  
324 __END__
↑ Summary DistDir/CLI.pm »